Package caldavclientlibrary :: Package protocol :: Package webdav :: Module session
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.protocol.webdav.session

  1  ## 
  2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  # http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  ## 
 16   
 17  from caldavclientlibrary.protocol.http.session import Session as HTTPSession 
 18  from caldavclientlibrary.protocol.http.requestresponse import RequestResponse 
 19  from caldavclientlibrary.protocol.http.data.string import ResponseDataString 
 20  from caldavclientlibrary.protocol.http.definitions import methods 
 21  from caldavclientlibrary.protocol.http.definitions import statuscodes 
 22  from caldavclientlibrary.protocol.webdav.definitions import headers 
 23   
24 -class Session(HTTPSession):
25
26 - def __init__(self, server, port=None, ssl=False, log=None):
27 super(Session, self).__init__(server, port, ssl, log) 28 self.initialised = False 29 self.version = ()
30
31 - def initialise(self, host, base_uri):
32 # Set host change 33 self.setServer(host) 34 35 # Loop repeating until we can do it or get a fatal error 36 first_time = True 37 while True: 38 39 # Create OPTIONS request for the base_uri 40 request = RequestResponse(self, methods.OPTIONS, base_uri) 41 request.setSession(self) 42 sout = ResponseDataString() 43 request.setData(None, sout) 44 45 # Add request and process it 46 self.sendRequest(request) 47 48 # Check response 49 if request.getStatusCode() == statuscodes.Unauthorized: 50 51 # If we had authorization before, then chances are auth details are wrong - so delete and try again with new auth 52 if self.hasAuthorization(): 53 54 self.authorization = None 55 56 # Display error so user knows why the prompt occurs again 57 self.displayHTTPError(request) 58 59 # Get authorization object (prompt the user) and redo the request 60 self.authorization, cancelled = self.getAuthorizor(first_time, request.getResponseHeaders(headers.WWWAuthenticate)) 61 62 # Check for auth cancellation 63 if cancelled: 64 65 self.authorization = None 66 return False 67 68 first_time = False 69 70 # Repeat the request loop with new authorization 71 continue 72 73 # Look for success and exit loop for further processing 74 if request.getStatusCode() in (statuscodes.OK, statuscodes.NoContent): 75 76 # Grab the server string 77 if request.hasResponseHeader(headers.Server): 78 self.setServerDescriptor = self.setServerDescriptor(request.getResponseHeader(headers.Server)) 79 80 # Now check the response headers for a DAV version (may be more than one) 81 self.version = () 82 for dav_version in request.getResponseHeaders(headers.DAV): 83 84 # Tokenize on commas 85 for token in dav_version.split(","): 86 87 token = token.strip() 88 self.addVersion(token) 89 90 self.setServerType(self.version) 91 92 # Put other strings into capability 93 capa = "" 94 for name, value in request.getResponseHeaders().iteritems(): 95 96 if (not name.lower().startswith(headers.Server) and 97 not name.lower().startswith(headers.Date) and 98 name.lower().startswith("Content-")): 99 100 capa += "%s: %s\n" % (name, value,) 101 102 self.setServerCapability(capa) 103 104 # Just assume any version is fine for now 105 break 106 107 # If we get here we had some kind of fatal error 108 self.handleHTTPError(request) 109 return False 110 111 self.initialised = True 112 113 return True
114
115 - def addVersion(self, token):
116 self.version += (token,)
117
118 - def hasDAVVersion(self, version):
119 return version in self.version
120
121 - def hasDAV(self):
123
124 - def hasDAVLocking(self):
126
127 - def hasDAVACL(self):
129
130 - def getAuthorizor(self, first_time, www_authenticate):
131 raise NotImplementedError
132
133 - def setServerType(self, type):
134 raise NotImplementedError
135
136 - def setServerDescriptor(self, txt):
137 raise NotImplementedError
138
139 - def setServerCapability(self, txt):
140 raise NotImplementedError
141