3131import urllib
3232import io
3333import sys
34+ import Cookie
3435
3536from datetime import datetime
3637from functools import wraps
@@ -225,7 +226,7 @@ def f():
225226 @wraps (request_fun )
226227 def wrapper (self , * args , ** kwargs ):
227228 if self .token is _NoAuthenticationToken and \
228- self .cookie is _NoAuthenticationToken :
229+ len ( self .cookies ) < 1 :
229230 # Not yet logged in.
230231 if self .autologin and self .username and self .password :
231232 # This will throw an uncaught
@@ -428,9 +429,13 @@ def __init__(self, handler=None, **kwargs):
428429 self .username = kwargs .get ("username" , "" )
429430 self .password = kwargs .get ("password" , "" )
430431 self .autologin = kwargs .get ("autologin" , False )
431- self .cookie = kwargs .get ("cookie" , _NoAuthenticationToken )
432- if self .cookie is None : # In case someone explicitly passes cookie=None
433- self .cookie = _NoAuthenticationToken
432+
433+ # By default, there are no cookies
434+ self .cookies = {}
435+ if kwargs .has_key ("cookie" ) and kwargs ['cookie' ] not in [None , _NoAuthenticationToken ]:
436+ parsed_cookie = Cookie .SimpleCookie (kwargs .get (("cookie" )))
437+ for cookie in parsed_cookie .values ():
438+ self .cookies [cookie .key ] = cookie .coded_value
434439
435440 # Shared per-context request headers
436441 @property
@@ -443,8 +448,8 @@ def _auth_headers(self):
443448
444449 :returns: A list of 2-tuples containing key and value
445450 """
446- if self .cookie is not _NoAuthenticationToken :
447- return [("cookie" , self .cookie )]
451+ if len ( self .cookies ) > 0 :
452+ return [("cookie" , "%s=%s" % cookie ) for cookie in self .cookies . items ( )]
448453 elif self .token is _NoAuthenticationToken :
449454 return []
450455 else :
@@ -761,10 +766,10 @@ def login(self):
761766 c = binding.Context(...).login()
762767 # Then issue requests...
763768 """
764- # If self.cookie and self.token only, use the cookie
765- if self .cookie is not _NoAuthenticationToken and \
769+ # If self.cookies and self.token only, use the cookie
770+ if len ( self .cookies ) > 0 and \
766771 (not self .username and not self .password ):
767- # If we were passed a session cookie, but no username or
772+ # If we were passed session cookie(s) , but no username or
768773 # password, then login is a nop, since we're automatically
769774 # logged in.
770775 return
@@ -785,10 +790,12 @@ def login(self):
785790 cookie = "1" ) # In Splunk 6.2+, passing "cookie=1" will return the "set-cookie" header
786791
787792 # Store the cookie
793+
788794 for key , value in response .headers :
789795 if key .lower () == "set-cookie" :
790- self .cookie = value
791- break
796+ parsed_cookies = Cookie .SimpleCookie (value )
797+ for cookie in parsed_cookies .values ():
798+ self .cookies [cookie .key ] = cookie .coded_value
792799
793800 body = response .body .read ()
794801 session = XML (body ).findtext ("./sessionKey" )
@@ -801,8 +808,9 @@ def login(self):
801808 raise
802809
803810 def logout (self ):
804- """Forgets the current session token."""
811+ """Forgets the current session token, and cookies ."""
805812 self .token = _NoAuthenticationToken
813+ self .cookies = {}
806814 return self
807815
808816 def _abspath (self , path_segment ,
@@ -1030,6 +1038,8 @@ class HttpLib(object):
10301038 """
10311039 def __init__ (self , custom_handler = None ):
10321040 self .handler = handler () if custom_handler is None else custom_handler
1041+ if not hasattr (self , 'cookies' ):
1042+ self .cookies = {}
10331043
10341044 def delete (self , url , headers = None , ** kwargs ):
10351045 """Sends a DELETE request to a URL.
@@ -1143,8 +1153,9 @@ def request(self, url, message, **kwargs):
11431153 # Update the cookie with any HTTP request
11441154 for key , value in response .headers :
11451155 if key .lower () == "set-cookie" :
1146- self .cookie = value
1147- break
1156+ parsed_cookie = Cookie .SimpleCookie (value )
1157+ for cookie in parsed_cookie .values ():
1158+ self .cookies [cookie .key ] = cookie .coded_value
11481159 return response
11491160
11501161
0 commit comments