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

Source Code for Module caldavclientlibrary.protocol.http.session

  1  ## 
  2  # Copyright (c) 2006-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 httplib import InvalidURL 
 18  import httplib 
 19   
20 -class Session(object):
21 22 STATE_OPEN = 0 23 STATE_CLOSED = 1 24
25 - def __init__(self, server, port=None, ssl=False, log=None):
26 27 self.server = server 28 self.port = port 29 self.ssl = ssl 30 if not self.port: 31 self.port = httplib.HTTPS_PORT if ssl else httplib.HTTP_PORT 32 self.authorization = None 33 self.connection_state = Session.STATE_CLOSED 34 self.log = log
35
36 - def hasAuthorization(self):
37 return self.authorization != None
38
39 - def getAuthorization(self):
40 return self.authorization
41
42 - def addHeaders(self, hdrs, request):
43 if self.hasAuthorization(): 44 self.getAuthorization().addHeaders(hdrs, request)
45
46 - def setServer(self, server, port=None):
47 48 if port is None: 49 i = server.rfind(':') 50 j = server.rfind(']') 51 if i > j: 52 try: 53 port = int(server[i+1:]) 54 except ValueError: 55 raise InvalidURL("nonnumeric port: '%s'" % server[i+1:]) 56 server = server[:i] 57 else: 58 port = httplib.HTTPS_PORT if self.ssl else httplib.HTTP_PORT 59 if server and server[0] == '[' and server[-1] == ']': 60 server = server[1:-1] 61 62 if self.server != server: 63 self.server = server 64 self.port = port 65 66 # Always clear out authorization when host changes 67 self.authorization = None
68
69 - def sendRequest(self, request):
70 try: 71 72 # First need a connection 73 self.needConnection(); 74 75 # Now do the connection 76 self.doRequest(request); 77 78 # Check the final connection state and close if that's what the server wants 79 if request.getConnectionClose(): 80 self.closeConnection() 81 82 except Exception: 83 84 # log.err(e) 85 self.connection_state = Session.STATE_CLOSED 86 raise
87
88 - def handleHTTPError(self, request):
89 raise NotImplementedError
90
91 - def displayHTTPError(self, request):
92 raise NotImplementedError
93
94 - def isConnectionOpen(self):
95 return self.connection_state == Session.STATE_OPEN
96
97 - def needConnection(self):
100
101 - def openConnection(self):
102 if not self.isConnectionOpen(): 103 self.openSession() 104 self.connection_state = Session.STATE_OPEN
105
106 - def closeConnection(self):
107 if self.isConnectionOpen(): 108 self.closeSession(); 109 self.connection_state = Session.STATE_CLOSED
110
111 - def openSession(self):
112 raise NotImplementedError
113
114 - def closeSession(self):
115 raise NotImplementedError
116
117 - def runSession(self, request):
118 raise NotImplementedError
119
120 - def doRequest(self, request):
121 raise NotImplementedError
122