| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | """HTTP/1.1 client library
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <intro stuff goes here> | 
					
						
							|  |  |  | <other stuff, too> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-30 00:27:34 +00:00
										 |  |  | HTTPConnection goes through a number of "states", which define when a client | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | may legally make another request or fetch the response for a particular | 
					
						
							|  |  |  | request. This diagram details these state transitions: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     (null) | 
					
						
							|  |  |  |       | | 
					
						
							|  |  |  |       | HTTPConnection() | 
					
						
							|  |  |  |       v | 
					
						
							|  |  |  |     Idle | 
					
						
							|  |  |  |       | | 
					
						
							|  |  |  |       | putrequest() | 
					
						
							|  |  |  |       v | 
					
						
							|  |  |  |     Request-started | 
					
						
							|  |  |  |       | | 
					
						
							|  |  |  |       | ( putheader() )*  endheaders() | 
					
						
							|  |  |  |       v | 
					
						
							|  |  |  |     Request-sent | 
					
						
							|  |  |  |       | | 
					
						
							|  |  |  |       | response = getresponse() | 
					
						
							|  |  |  |       v | 
					
						
							|  |  |  |     Unread-response   [Response-headers-read] | 
					
						
							|  |  |  |       |\____________________ | 
					
						
							| 
									
										
										
										
											2001-01-13 19:16:21 +00:00
										 |  |  |       |                     | | 
					
						
							|  |  |  |       | response.read()     | putrequest() | 
					
						
							|  |  |  |       v                     v | 
					
						
							|  |  |  |     Idle                  Req-started-unread-response | 
					
						
							|  |  |  |                      ______/| | 
					
						
							|  |  |  |                    /        | | 
					
						
							|  |  |  |    response.read() |        | ( putheader() )*  endheaders() | 
					
						
							|  |  |  |                    v        v | 
					
						
							|  |  |  |        Request-started    Req-sent-unread-response | 
					
						
							|  |  |  |                             | | 
					
						
							|  |  |  |                             | response.read() | 
					
						
							|  |  |  |                             v | 
					
						
							|  |  |  |                           Request-sent | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | This diagram presents the following rules: | 
					
						
							|  |  |  |   -- a second request may not be started until {response-headers-read} | 
					
						
							|  |  |  |   -- a response [object] cannot be retrieved until {request-sent} | 
					
						
							|  |  |  |   -- there is no differentiation between an unread response body and a | 
					
						
							|  |  |  |      partially read response body | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note: this enforcement is applied by the HTTPConnection class. The | 
					
						
							|  |  |  |       HTTPResponse class does not enforce this state machine, which | 
					
						
							|  |  |  |       implies sophisticated clients may accelerate the request/response | 
					
						
							|  |  |  |       pipeline. Caution should be taken, though: accelerating the states | 
					
						
							|  |  |  |       beyond the above pattern may imply knowledge of the server's | 
					
						
							|  |  |  |       connection-close behavior for certain requests. For example, it | 
					
						
							|  |  |  |       is impossible to tell whether the server will close the connection | 
					
						
							|  |  |  |       UNTIL the response headers have been read; this means that further | 
					
						
							|  |  |  |       requests cannot be placed into the pipeline until it is known that | 
					
						
							|  |  |  |       the server will NOT be closing the connection. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Logical State                  __state            __response | 
					
						
							|  |  |  | -------------                  -------            ---------- | 
					
						
							|  |  |  | Idle                           _CS_IDLE           None | 
					
						
							|  |  |  | Request-started                _CS_REQ_STARTED    None | 
					
						
							|  |  |  | Request-sent                   _CS_REQ_SENT       None | 
					
						
							|  |  |  | Unread-response                _CS_IDLE           <response_class> | 
					
						
							|  |  |  | Req-started-unread-response    _CS_REQ_STARTED    <response_class> | 
					
						
							|  |  |  | Req-sent-unread-response       _CS_REQ_SENT       <response_class> | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											1994-02-21 16:36:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-11 17:47:22 +00:00
										 |  |  | import errno | 
					
						
							| 
									
										
										
										
											1995-08-07 20:13:02 +00:00
										 |  |  | import mimetools | 
					
						
							| 
									
										
										
										
											2001-10-11 17:47:22 +00:00
										 |  |  | import socket | 
					
						
							| 
									
										
										
										
											2002-03-08 19:35:51 +00:00
										 |  |  | from urlparse import urlsplit | 
					
						
							| 
									
										
										
										
											2007-08-31 19:02:23 +00:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											1994-02-21 16:36:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-07 21:37:17 +00:00
										 |  |  | try: | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     from cStringIO import StringIO | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | except ImportError: | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     from StringIO import StringIO | 
					
						
							| 
									
										
										
										
											1999-12-07 21:37:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 21:54:39 +00:00
										 |  |  | __all__ = ["HTTP", "HTTPResponse", "HTTPConnection", | 
					
						
							| 
									
										
										
										
											2001-06-01 16:25:38 +00:00
										 |  |  |            "HTTPException", "NotConnected", "UnknownProtocol", | 
					
						
							| 
									
										
										
										
											2002-06-28 23:38:14 +00:00
										 |  |  |            "UnknownTransferEncoding", "UnimplementedFileMode", | 
					
						
							|  |  |  |            "IncompleteRead", "InvalidURL", "ImproperConnectionState", | 
					
						
							|  |  |  |            "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", | 
					
						
							| 
									
										
										
										
											2006-02-17 19:17:25 +00:00
										 |  |  |            "BadStatusLine", "error", "responses"] | 
					
						
							| 
									
										
										
										
											2001-01-23 15:35:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-02-21 16:36:04 +00:00
										 |  |  | HTTP_PORT = 80 | 
					
						
							| 
									
										
										
										
											1999-12-07 21:37:17 +00:00
										 |  |  | HTTPS_PORT = 443 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | _UNKNOWN = 'UNKNOWN' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # connection states | 
					
						
							|  |  |  | _CS_IDLE = 'Idle' | 
					
						
							|  |  |  | _CS_REQ_STARTED = 'Request-started' | 
					
						
							|  |  |  | _CS_REQ_SENT = 'Request-sent' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-18 09:03:49 +00:00
										 |  |  | # status codes | 
					
						
							|  |  |  | # informational | 
					
						
							|  |  |  | CONTINUE = 100 | 
					
						
							|  |  |  | SWITCHING_PROTOCOLS = 101 | 
					
						
							|  |  |  | PROCESSING = 102 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # successful | 
					
						
							|  |  |  | OK = 200 | 
					
						
							|  |  |  | CREATED = 201 | 
					
						
							|  |  |  | ACCEPTED = 202 | 
					
						
							|  |  |  | NON_AUTHORITATIVE_INFORMATION = 203 | 
					
						
							|  |  |  | NO_CONTENT = 204 | 
					
						
							|  |  |  | RESET_CONTENT = 205 | 
					
						
							|  |  |  | PARTIAL_CONTENT = 206 | 
					
						
							|  |  |  | MULTI_STATUS = 207 | 
					
						
							|  |  |  | IM_USED = 226 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # redirection | 
					
						
							|  |  |  | MULTIPLE_CHOICES = 300 | 
					
						
							|  |  |  | MOVED_PERMANENTLY = 301 | 
					
						
							|  |  |  | FOUND = 302 | 
					
						
							|  |  |  | SEE_OTHER = 303 | 
					
						
							|  |  |  | NOT_MODIFIED = 304 | 
					
						
							|  |  |  | USE_PROXY = 305 | 
					
						
							|  |  |  | TEMPORARY_REDIRECT = 307 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # client error | 
					
						
							|  |  |  | BAD_REQUEST = 400 | 
					
						
							|  |  |  | UNAUTHORIZED = 401 | 
					
						
							|  |  |  | PAYMENT_REQUIRED = 402 | 
					
						
							|  |  |  | FORBIDDEN = 403 | 
					
						
							|  |  |  | NOT_FOUND = 404 | 
					
						
							|  |  |  | METHOD_NOT_ALLOWED = 405 | 
					
						
							|  |  |  | NOT_ACCEPTABLE = 406 | 
					
						
							|  |  |  | PROXY_AUTHENTICATION_REQUIRED = 407 | 
					
						
							|  |  |  | REQUEST_TIMEOUT = 408 | 
					
						
							|  |  |  | CONFLICT = 409 | 
					
						
							|  |  |  | GONE = 410 | 
					
						
							|  |  |  | LENGTH_REQUIRED = 411 | 
					
						
							|  |  |  | PRECONDITION_FAILED = 412 | 
					
						
							|  |  |  | REQUEST_ENTITY_TOO_LARGE = 413 | 
					
						
							|  |  |  | REQUEST_URI_TOO_LONG = 414 | 
					
						
							|  |  |  | UNSUPPORTED_MEDIA_TYPE = 415 | 
					
						
							|  |  |  | REQUESTED_RANGE_NOT_SATISFIABLE = 416 | 
					
						
							|  |  |  | EXPECTATION_FAILED = 417 | 
					
						
							|  |  |  | UNPROCESSABLE_ENTITY = 422 | 
					
						
							|  |  |  | LOCKED = 423 | 
					
						
							|  |  |  | FAILED_DEPENDENCY = 424 | 
					
						
							|  |  |  | UPGRADE_REQUIRED = 426 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # server error | 
					
						
							|  |  |  | INTERNAL_SERVER_ERROR = 500 | 
					
						
							|  |  |  | NOT_IMPLEMENTED = 501 | 
					
						
							|  |  |  | BAD_GATEWAY = 502 | 
					
						
							|  |  |  | SERVICE_UNAVAILABLE = 503 | 
					
						
							|  |  |  | GATEWAY_TIMEOUT = 504 | 
					
						
							|  |  |  | HTTP_VERSION_NOT_SUPPORTED = 505 | 
					
						
							|  |  |  | INSUFFICIENT_STORAGE = 507 | 
					
						
							|  |  |  | NOT_EXTENDED = 510 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-17 19:17:25 +00:00
										 |  |  | # Mapping status codes to official W3C names | 
					
						
							|  |  |  | responses = { | 
					
						
							|  |  |  |     100: 'Continue', | 
					
						
							|  |  |  |     101: 'Switching Protocols', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     200: 'OK', | 
					
						
							|  |  |  |     201: 'Created', | 
					
						
							|  |  |  |     202: 'Accepted', | 
					
						
							|  |  |  |     203: 'Non-Authoritative Information', | 
					
						
							|  |  |  |     204: 'No Content', | 
					
						
							|  |  |  |     205: 'Reset Content', | 
					
						
							|  |  |  |     206: 'Partial Content', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     300: 'Multiple Choices', | 
					
						
							|  |  |  |     301: 'Moved Permanently', | 
					
						
							|  |  |  |     302: 'Found', | 
					
						
							|  |  |  |     303: 'See Other', | 
					
						
							|  |  |  |     304: 'Not Modified', | 
					
						
							|  |  |  |     305: 'Use Proxy', | 
					
						
							|  |  |  |     306: '(Unused)', | 
					
						
							|  |  |  |     307: 'Temporary Redirect', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     400: 'Bad Request', | 
					
						
							|  |  |  |     401: 'Unauthorized', | 
					
						
							|  |  |  |     402: 'Payment Required', | 
					
						
							|  |  |  |     403: 'Forbidden', | 
					
						
							|  |  |  |     404: 'Not Found', | 
					
						
							|  |  |  |     405: 'Method Not Allowed', | 
					
						
							|  |  |  |     406: 'Not Acceptable', | 
					
						
							|  |  |  |     407: 'Proxy Authentication Required', | 
					
						
							|  |  |  |     408: 'Request Timeout', | 
					
						
							|  |  |  |     409: 'Conflict', | 
					
						
							|  |  |  |     410: 'Gone', | 
					
						
							|  |  |  |     411: 'Length Required', | 
					
						
							|  |  |  |     412: 'Precondition Failed', | 
					
						
							|  |  |  |     413: 'Request Entity Too Large', | 
					
						
							|  |  |  |     414: 'Request-URI Too Long', | 
					
						
							|  |  |  |     415: 'Unsupported Media Type', | 
					
						
							|  |  |  |     416: 'Requested Range Not Satisfiable', | 
					
						
							|  |  |  |     417: 'Expectation Failed', | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     500: 'Internal Server Error', | 
					
						
							|  |  |  |     501: 'Not Implemented', | 
					
						
							|  |  |  |     502: 'Bad Gateway', | 
					
						
							|  |  |  |     503: 'Service Unavailable', | 
					
						
							|  |  |  |     504: 'Gateway Timeout', | 
					
						
							|  |  |  |     505: 'HTTP Version Not Supported', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-29 20:16:07 +00:00
										 |  |  | # maximal amount of data to read at one time in _safe_read | 
					
						
							|  |  |  | MAXAMOUNT = 1048576 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  | class HTTPMessage(mimetools.Message): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def addheader(self, key, value): | 
					
						
							|  |  |  |         """Add header for field key handling repeats.""" | 
					
						
							|  |  |  |         prev = self.dict.get(key) | 
					
						
							|  |  |  |         if prev is None: | 
					
						
							|  |  |  |             self.dict[key] = value | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             combined = ", ".join((prev, value)) | 
					
						
							|  |  |  |             self.dict[key] = combined | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def addcontinue(self, key, more): | 
					
						
							|  |  |  |         """Add more field data from a continuation line.""" | 
					
						
							|  |  |  |         prev = self.dict[key] | 
					
						
							|  |  |  |         self.dict[key] = prev + "\n " + more | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def readheaders(self): | 
					
						
							|  |  |  |         """Read header lines.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Read header lines up to the entirely blank line that terminates them. | 
					
						
							|  |  |  |         The (normally blank) line that ends the headers is skipped, but not | 
					
						
							|  |  |  |         included in the returned list.  If a non-header line ends the headers, | 
					
						
							|  |  |  |         (which is an error), an attempt is made to backspace over it; it is | 
					
						
							|  |  |  |         never included in the returned list. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The variable self.status is set to the empty string if all went well, | 
					
						
							|  |  |  |         otherwise it is an error message.  The variable self.headers is a | 
					
						
							|  |  |  |         completely uninterpreted list of lines contained in the header (so | 
					
						
							|  |  |  |         printing them will reproduce the header exactly as it appears in the | 
					
						
							|  |  |  |         file). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If multiple header fields with the same name occur, they are combined | 
					
						
							|  |  |  |         according to the rules in RFC 2616 sec 4.2: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Appending each subsequent field-value to the first, each separated | 
					
						
							|  |  |  |         by a comma. The order in which header fields with the same field-name | 
					
						
							|  |  |  |         are received is significant to the interpretation of the combined | 
					
						
							|  |  |  |         field value. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         # XXX The implementation overrides the readheaders() method of | 
					
						
							|  |  |  |         # rfc822.Message.  The base class design isn't amenable to | 
					
						
							|  |  |  |         # customized behavior here so the method here is a copy of the | 
					
						
							|  |  |  |         # base class code with a few small changes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.dict = {} | 
					
						
							|  |  |  |         self.unixfrom = '' | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         self.headers = hlist = [] | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |         self.status = '' | 
					
						
							|  |  |  |         headerseen = "" | 
					
						
							|  |  |  |         firstline = 1 | 
					
						
							|  |  |  |         startofline = unread = tell = None | 
					
						
							|  |  |  |         if hasattr(self.fp, 'unread'): | 
					
						
							|  |  |  |             unread = self.fp.unread | 
					
						
							|  |  |  |         elif self.seekable: | 
					
						
							|  |  |  |             tell = self.fp.tell | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         while True: | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |             if tell: | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     startofline = tell() | 
					
						
							|  |  |  |                 except IOError: | 
					
						
							|  |  |  |                     startofline = tell = None | 
					
						
							|  |  |  |                     self.seekable = 0 | 
					
						
							|  |  |  |             line = self.fp.readline() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 self.status = 'EOF in headers' | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             # Skip unix From name time lines | 
					
						
							|  |  |  |             if firstline and line.startswith('From '): | 
					
						
							|  |  |  |                 self.unixfrom = self.unixfrom + line | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             firstline = 0 | 
					
						
							|  |  |  |             if headerseen and line[0] in ' \t': | 
					
						
							|  |  |  |                 # XXX Not sure if continuation lines are handled properly | 
					
						
							|  |  |  |                 # for http and/or for repeating headers | 
					
						
							|  |  |  |                 # It's a continuation line. | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |                 hlist.append(line) | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |                 self.addcontinue(headerseen, line.strip()) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             elif self.iscomment(line): | 
					
						
							|  |  |  |                 # It's a comment.  Ignore it. | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             elif self.islast(line): | 
					
						
							|  |  |  |                 # Note! No pushback here!  The delimiter line gets eaten. | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             headerseen = self.isheader(line) | 
					
						
							|  |  |  |             if headerseen: | 
					
						
							|  |  |  |                 # It's a legal header line, save it. | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |                 hlist.append(line) | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |                 self.addheader(headerseen, line[len(headerseen)+1:].strip()) | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 # It's not a header line; throw it back and stop here. | 
					
						
							|  |  |  |                 if not self.dict: | 
					
						
							|  |  |  |                     self.status = 'No headers' | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self.status = 'Non-header line where header expected' | 
					
						
							|  |  |  |                 # Try to undo the read. | 
					
						
							|  |  |  |                 if unread: | 
					
						
							|  |  |  |                     unread(line) | 
					
						
							|  |  |  |                 elif tell: | 
					
						
							|  |  |  |                     self.fp.seek(startofline) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self.status = self.status + '; bad seek' | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class HTTPResponse: | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # strict: If true, raise BadStatusLine if the status line can't be | 
					
						
							|  |  |  |     # parsed as a valid HTTP/1.0 or 1.1 status line.  By default it is | 
					
						
							| 
									
										
										
										
											2002-07-25 16:10:38 +00:00
										 |  |  |     # false because it prevents clients from talking to HTTP/0.9 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |     # servers.  Note that a response with a sufficiently corrupted | 
					
						
							|  |  |  |     # status line will look like an HTTP/0.9 response. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |     def __init__(self, sock, debuglevel=0, strict=0, method=None): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.fp = sock.makefile('rb', 0) | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         self.debuglevel = debuglevel | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         self.strict = strict | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |         self._method = method | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.msg = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # from the Status-Line of the response | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |         self.version = _UNKNOWN # HTTP-Version | 
					
						
							|  |  |  |         self.status = _UNKNOWN  # Status-Code | 
					
						
							|  |  |  |         self.reason = _UNKNOWN  # Reason-Phrase | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |         self.chunked = _UNKNOWN         # is "chunked" being used? | 
					
						
							|  |  |  |         self.chunk_left = _UNKNOWN      # bytes left to read in current chunk | 
					
						
							|  |  |  |         self.length = _UNKNOWN          # number of bytes left in response | 
					
						
							|  |  |  |         self.will_close = _UNKNOWN      # conn will close at end of response | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |     def _read_status(self): | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         # Initialize with Simple-Response defaults | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  |         line = self.fp.readline() | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         if self.debuglevel > 0: | 
					
						
							|  |  |  |             print "reply:", repr(line) | 
					
						
							| 
									
										
										
										
											2003-06-29 17:55:05 +00:00
										 |  |  |         if not line: | 
					
						
							|  |  |  |             # Presumably, the server closed the connection before | 
					
						
							|  |  |  |             # sending a valid response. | 
					
						
							|  |  |  |             raise BadStatusLine(line) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2000-12-15 15:09:42 +00:00
										 |  |  |             [version, status, reason] = line.split(None, 2) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         except ValueError: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2000-12-15 15:09:42 +00:00
										 |  |  |                 [version, status] = line.split(None, 1) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |                 reason = "" | 
					
						
							|  |  |  |             except ValueError: | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |                 # empty version will cause next test to fail and status | 
					
						
							|  |  |  |                 # will be treated as 0.9 response. | 
					
						
							|  |  |  |                 version = "" | 
					
						
							|  |  |  |         if not version.startswith('HTTP/'): | 
					
						
							|  |  |  |             if self.strict: | 
					
						
							|  |  |  |                 self.close() | 
					
						
							|  |  |  |                 raise BadStatusLine(line) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 # assume it's a Simple-Response from an 0.9 server | 
					
						
							|  |  |  |                 self.fp = LineAndFileWrapper(line, self.fp) | 
					
						
							|  |  |  |                 return "HTTP/0.9", 200, "" | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-13 14:57:08 +00:00
										 |  |  |         # The status code is a three-digit number | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |             status = int(status) | 
					
						
							| 
									
										
										
										
											2001-04-13 14:57:08 +00:00
										 |  |  |             if status < 100 or status > 999: | 
					
						
							|  |  |  |                 raise BadStatusLine(line) | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             raise BadStatusLine(line) | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |         return version, status, reason | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-12 14:04:09 +00:00
										 |  |  |     def begin(self): | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |         if self.msg is not None: | 
					
						
							|  |  |  |             # we've already started reading the response | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |         # read until we get a non-100 response | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         while True: | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |             version, status, reason = self._read_status() | 
					
						
							| 
									
										
										
										
											2004-09-18 09:03:49 +00:00
										 |  |  |             if status != CONTINUE: | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |                 break | 
					
						
							|  |  |  |             # skip the header from the 100 response | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |             while True: | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |                 skip = self.fp.readline().strip() | 
					
						
							|  |  |  |                 if not skip: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |                 if self.debuglevel > 0: | 
					
						
							|  |  |  |                     print "header:", skip | 
					
						
							| 
									
										
										
										
											2002-07-16 21:35:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |         self.status = status | 
					
						
							|  |  |  |         self.reason = reason.strip() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         if version == 'HTTP/1.0': | 
					
						
							|  |  |  |             self.version = 10 | 
					
						
							| 
									
										
										
										
											2000-10-12 19:58:36 +00:00
										 |  |  |         elif version.startswith('HTTP/1.'): | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |             self.version = 11   # use HTTP/1.1 code for HTTP/1.x where x>=1 | 
					
						
							| 
									
										
										
										
											2000-10-12 19:58:36 +00:00
										 |  |  |         elif version == 'HTTP/0.9': | 
					
						
							|  |  |  |             self.version = 9 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise UnknownProtocol(version) | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-12 19:58:36 +00:00
										 |  |  |         if self.version == 9: | 
					
						
							| 
									
										
										
										
											2005-06-26 22:06:54 +00:00
										 |  |  |             self.length = None | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |             self.chunked = 0 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |             self.will_close = 1 | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |             self.msg = HTTPMessage(StringIO()) | 
					
						
							| 
									
										
										
										
											2000-10-12 19:58:36 +00:00
										 |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-07 16:51:37 +00:00
										 |  |  |         self.msg = HTTPMessage(self.fp, 0) | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         if self.debuglevel > 0: | 
					
						
							|  |  |  |             for hdr in self.msg.headers: | 
					
						
							|  |  |  |                 print "header:", hdr, | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # don't let the msg keep an fp | 
					
						
							|  |  |  |         self.msg.fp = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # are we using the chunked-style of transfer encoding? | 
					
						
							|  |  |  |         tr_enc = self.msg.getheader('transfer-encoding') | 
					
						
							| 
									
										
										
										
											2002-09-03 19:24:24 +00:00
										 |  |  |         if tr_enc and tr_enc.lower() == "chunked": | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             self.chunked = 1 | 
					
						
							|  |  |  |             self.chunk_left = None | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.chunked = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # will the connection close at the end of the response? | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |         self.will_close = self._check_close() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # do we have a Content-Length? | 
					
						
							|  |  |  |         # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" | 
					
						
							|  |  |  |         length = self.msg.getheader('content-length') | 
					
						
							|  |  |  |         if length and not self.chunked: | 
					
						
							| 
									
										
										
										
											2000-09-14 20:34:27 +00:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 self.length = int(length) | 
					
						
							|  |  |  |             except ValueError: | 
					
						
							|  |  |  |                 self.length = None | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.length = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # does the body have a fixed length? (of zero) | 
					
						
							| 
									
										
										
										
											2004-09-18 09:03:49 +00:00
										 |  |  |         if (status == NO_CONTENT or status == NOT_MODIFIED or | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |             100 <= status < 200 or      # 1xx codes | 
					
						
							|  |  |  |             self._method == 'HEAD'): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             self.length = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # if the connection remains open, and we aren't using chunked, and | 
					
						
							|  |  |  |         # a content-length was not provided, then assume that the connection | 
					
						
							|  |  |  |         # WILL close. | 
					
						
							|  |  |  |         if not self.will_close and \ | 
					
						
							|  |  |  |            not self.chunked and \ | 
					
						
							|  |  |  |            self.length is None: | 
					
						
							|  |  |  |             self.will_close = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |     def _check_close(self): | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |         conn = self.msg.getheader('connection') | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |         if self.version == 11: | 
					
						
							|  |  |  |             # An HTTP/1.1 proxy is assumed to stay open unless | 
					
						
							|  |  |  |             # explicitly closed. | 
					
						
							|  |  |  |             conn = self.msg.getheader('connection') | 
					
						
							| 
									
										
										
										
											2004-05-04 09:21:43 +00:00
										 |  |  |             if conn and "close" in conn.lower(): | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |                 return True | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |         # Some HTTP/1.0 implementations have support for persistent | 
					
						
							|  |  |  |         # connections, using rules different than HTTP/1.1. | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-08 10:56:39 +00:00
										 |  |  |         # For older HTTP, Keep-Alive indicates persistent connection. | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |         if self.msg.getheader('keep-alive'): | 
					
						
							|  |  |  |             return False | 
					
						
							| 
									
										
										
										
											2002-11-24 02:35:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |         # At least Akamai returns a "Connection: Keep-Alive" header, | 
					
						
							|  |  |  |         # which was supposed to be sent by the client. | 
					
						
							|  |  |  |         if conn and "keep-alive" in conn.lower(): | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |         # Proxy-Connection is a netscape hack. | 
					
						
							|  |  |  |         pconn = self.msg.getheader('proxy-connection') | 
					
						
							| 
									
										
										
										
											2004-05-04 09:21:43 +00:00
										 |  |  |         if pconn and "keep-alive" in pconn.lower(): | 
					
						
							| 
									
										
										
										
											2002-11-13 17:27:43 +00:00
										 |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # otherwise, assume it will close | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def close(self): | 
					
						
							|  |  |  |         if self.fp: | 
					
						
							|  |  |  |             self.fp.close() | 
					
						
							|  |  |  |             self.fp = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def isclosed(self): | 
					
						
							|  |  |  |         # NOTE: it is possible that we will not ever call self.close(). This | 
					
						
							|  |  |  |         #       case occurs when will_close is TRUE, length is None, and we | 
					
						
							|  |  |  |         #       read up to the last byte, but NOT past it. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be | 
					
						
							|  |  |  |         #          called, meaning self.isclosed() is meaningful. | 
					
						
							|  |  |  |         return self.fp is None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |     # XXX It would be nice to have readline and __iter__ for this, too. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def read(self, amt=None): | 
					
						
							|  |  |  |         if self.fp is None: | 
					
						
							|  |  |  |             return '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.chunked: | 
					
						
							| 
									
										
										
										
											2002-09-03 20:49:06 +00:00
										 |  |  |             return self._read_chunked(amt) | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-03 20:49:06 +00:00
										 |  |  |         if amt is None: | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             # unbounded read | 
					
						
							| 
									
										
										
										
											2004-11-07 16:13:49 +00:00
										 |  |  |             if self.length is None: | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |                 s = self.fp.read() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 s = self._safe_read(self.length) | 
					
						
							| 
									
										
										
										
											2004-11-07 16:13:49 +00:00
										 |  |  |                 self.length = 0 | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |             self.close()        # we read everything | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             return s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.length is not None: | 
					
						
							|  |  |  |             if amt > self.length: | 
					
						
							|  |  |  |                 # clip the read to the "end of response" | 
					
						
							|  |  |  |                 amt = self.length | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # we do not use _safe_read() here because this may be a .will_close | 
					
						
							|  |  |  |         # connection, and the user is reading more bytes than will be provided | 
					
						
							|  |  |  |         # (for example, reading in 1k chunks) | 
					
						
							|  |  |  |         s = self.fp.read(amt) | 
					
						
							| 
									
										
										
										
											2004-11-07 16:13:49 +00:00
										 |  |  |         if self.length is not None: | 
					
						
							|  |  |  |             self.length -= len(s) | 
					
						
							| 
									
										
										
										
											2007-10-18 03:16:03 +00:00
										 |  |  |             if not self.length: | 
					
						
							|  |  |  |                 self.close() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         return s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-03 20:49:06 +00:00
										 |  |  |     def _read_chunked(self, amt): | 
					
						
							|  |  |  |         assert self.chunked != _UNKNOWN | 
					
						
							|  |  |  |         chunk_left = self.chunk_left | 
					
						
							|  |  |  |         value = '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # XXX This accumulates chunks by repeated string concatenation, | 
					
						
							|  |  |  |         # which is not efficient as the number or size of chunks gets big. | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         while True: | 
					
						
							| 
									
										
										
										
											2002-09-03 20:49:06 +00:00
										 |  |  |             if chunk_left is None: | 
					
						
							|  |  |  |                 line = self.fp.readline() | 
					
						
							|  |  |  |                 i = line.find(';') | 
					
						
							|  |  |  |                 if i >= 0: | 
					
						
							|  |  |  |                     line = line[:i] # strip chunk-extensions | 
					
						
							|  |  |  |                 chunk_left = int(line, 16) | 
					
						
							|  |  |  |                 if chunk_left == 0: | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  |             if amt is None: | 
					
						
							|  |  |  |                 value += self._safe_read(chunk_left) | 
					
						
							|  |  |  |             elif amt < chunk_left: | 
					
						
							|  |  |  |                 value += self._safe_read(amt) | 
					
						
							|  |  |  |                 self.chunk_left = chunk_left - amt | 
					
						
							|  |  |  |                 return value | 
					
						
							|  |  |  |             elif amt == chunk_left: | 
					
						
							|  |  |  |                 value += self._safe_read(amt) | 
					
						
							|  |  |  |                 self._safe_read(2)  # toss the CRLF at the end of the chunk | 
					
						
							|  |  |  |                 self.chunk_left = None | 
					
						
							|  |  |  |                 return value | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 value += self._safe_read(chunk_left) | 
					
						
							|  |  |  |                 amt -= chunk_left | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # we read the whole chunk, get another | 
					
						
							|  |  |  |             self._safe_read(2)      # toss the CRLF at the end of the chunk | 
					
						
							|  |  |  |             chunk_left = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # read and discard trailer up to the CRLF terminator | 
					
						
							|  |  |  |         ### note: we shouldn't have any trailers! | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         while True: | 
					
						
							| 
									
										
										
										
											2002-09-03 20:49:06 +00:00
										 |  |  |             line = self.fp.readline() | 
					
						
							|  |  |  |             if line == '\r\n': | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # we read everything; close the "file" | 
					
						
							|  |  |  |         self.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return value | 
					
						
							| 
									
										
										
										
											2002-11-09 05:08:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def _safe_read(self, amt): | 
					
						
							|  |  |  |         """Read the number of bytes requested, compensating for partial reads.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Normally, we have a blocking socket, but a read() can be interrupted | 
					
						
							|  |  |  |         by a signal (resulting in a partial read). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Note that we cannot distinguish between EOF and an interrupt when zero | 
					
						
							|  |  |  |         bytes have been read. IncompleteRead() will be raised in this | 
					
						
							|  |  |  |         situation. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This function should be used when <amt> bytes "should" be present for | 
					
						
							|  |  |  |         reading. If the bytes are truly not available (due to EOF), then the | 
					
						
							|  |  |  |         IncompleteRead exception can be used to detect the problem. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2005-09-29 20:16:07 +00:00
										 |  |  |         s = [] | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         while amt > 0: | 
					
						
							| 
									
										
										
										
											2005-09-29 20:16:07 +00:00
										 |  |  |             chunk = self.fp.read(min(amt, MAXAMOUNT)) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             if not chunk: | 
					
						
							|  |  |  |                 raise IncompleteRead(s) | 
					
						
							| 
									
										
										
										
											2005-09-29 20:16:07 +00:00
										 |  |  |             s.append(chunk) | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |             amt -= len(chunk) | 
					
						
							| 
									
										
										
										
											2005-09-29 20:16:07 +00:00
										 |  |  |         return ''.join(s) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getheader(self, name, default=None): | 
					
						
							|  |  |  |         if self.msg is None: | 
					
						
							|  |  |  |             raise ResponseNotReady() | 
					
						
							|  |  |  |         return self.msg.getheader(name, default) | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-18 12:46:26 +00:00
										 |  |  |     def getheaders(self): | 
					
						
							|  |  |  |         """Return list of (header, value) tuples.""" | 
					
						
							|  |  |  |         if self.msg is None: | 
					
						
							|  |  |  |             raise ResponseNotReady() | 
					
						
							|  |  |  |         return self.msg.items() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class HTTPConnection: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     _http_vsn = 11 | 
					
						
							|  |  |  |     _http_vsn_str = 'HTTP/1.1' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     response_class = HTTPResponse | 
					
						
							|  |  |  |     default_port = HTTP_PORT | 
					
						
							|  |  |  |     auto_open = 1 | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |     debuglevel = 0 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |     strict = 0 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-23 18:54:07 +00:00
										 |  |  |     def __init__(self, host, port=None, strict=None, timeout=None): | 
					
						
							|  |  |  |         self.timeout = timeout | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.sock = None | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         self._buffer = [] | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.__response = None | 
					
						
							|  |  |  |         self.__state = _CS_IDLE | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |         self._method = None | 
					
						
							| 
									
										
										
										
											2002-07-16 21:35:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self._set_hostport(host, port) | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         if strict is not None: | 
					
						
							|  |  |  |             self.strict = strict | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _set_hostport(self, host, port): | 
					
						
							|  |  |  |         if port is None: | 
					
						
							| 
									
										
										
										
											2004-09-14 16:32:02 +00:00
										 |  |  |             i = host.rfind(':') | 
					
						
							| 
									
										
										
										
											2004-09-14 17:55:21 +00:00
										 |  |  |             j = host.rfind(']')         # ipv6 addresses have [...] | 
					
						
							|  |  |  |             if i > j: | 
					
						
							| 
									
										
										
										
											2002-03-24 16:53:50 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     port = int(host[i+1:]) | 
					
						
							|  |  |  |                 except ValueError: | 
					
						
							| 
									
										
										
										
											2002-07-02 20:19:08 +00:00
										 |  |  |                     raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |                 host = host[:i] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 port = self.default_port | 
					
						
							| 
									
										
										
										
											2004-10-14 15:23:38 +00:00
										 |  |  |             if host and host[0] == '[' and host[-1] == ']': | 
					
						
							| 
									
										
										
										
											2004-09-15 23:26:23 +00:00
										 |  |  |                 host = host[1:-1] | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.host = host | 
					
						
							|  |  |  |         self.port = port | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |     def set_debuglevel(self, level): | 
					
						
							|  |  |  |         self.debuglevel = level | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def connect(self): | 
					
						
							|  |  |  |         """Connect to the host and port specified in __init__.""" | 
					
						
							| 
									
										
										
										
											2007-03-26 20:28:28 +00:00
										 |  |  |         self.sock = socket.create_connection((self.host,self.port), | 
					
						
							|  |  |  |                                              self.timeout) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							|  |  |  |         """Close the connection to the HTTP server.""" | 
					
						
							|  |  |  |         if self.sock: | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |             self.sock.close()   # close it manually... there may be other refs | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             self.sock = None | 
					
						
							|  |  |  |         if self.__response: | 
					
						
							|  |  |  |             self.__response.close() | 
					
						
							|  |  |  |             self.__response = None | 
					
						
							|  |  |  |         self.__state = _CS_IDLE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def send(self, str): | 
					
						
							|  |  |  |         """Send `str' to the server.""" | 
					
						
							|  |  |  |         if self.sock is None: | 
					
						
							|  |  |  |             if self.auto_open: | 
					
						
							|  |  |  |                 self.connect() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise NotConnected() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:41:43 +00:00
										 |  |  |         # send the data to the server. if we get a broken pipe, then close | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         # the socket. we want to reconnect when somebody tries to send again. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # NOTE: we DO propagate the error, though, because we cannot simply | 
					
						
							|  |  |  |         #       ignore the error... the caller will know if they can retry. | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         if self.debuglevel > 0: | 
					
						
							|  |  |  |             print "send:", repr(str) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2006-11-12 10:32:47 +00:00
										 |  |  |             blocksize=8192 | 
					
						
							|  |  |  |             if hasattr(str,'read') : | 
					
						
							|  |  |  |                 if self.debuglevel > 0: print "sendIng a read()able" | 
					
						
							|  |  |  |                 data=str.read(blocksize) | 
					
						
							|  |  |  |                 while data: | 
					
						
							|  |  |  |                     self.sock.sendall(data) | 
					
						
							|  |  |  |                     data=str.read(blocksize) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.sock.sendall(str) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         except socket.error, v: | 
					
						
							| 
									
										
										
										
											2001-01-14 23:47:14 +00:00
										 |  |  |             if v[0] == 32:      # Broken pipe | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |                 self.close() | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |     def _output(self, s): | 
					
						
							|  |  |  |         """Add a line of output to the current request buffer.
 | 
					
						
							| 
									
										
										
										
											2002-08-08 20:19:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:41:43 +00:00
										 |  |  |         Assumes that the line does *not* end with \\r\\n. | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self._buffer.append(s) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _send_output(self): | 
					
						
							|  |  |  |         """Send the currently buffered request and clear the buffer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:41:43 +00:00
										 |  |  |         Appends an extra \\r\\n to the buffer. | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         self._buffer.extend(("", "")) | 
					
						
							|  |  |  |         msg = "\r\n".join(self._buffer) | 
					
						
							|  |  |  |         del self._buffer[:] | 
					
						
							|  |  |  |         self.send(msg) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-11-19 19:51:55 +00:00
										 |  |  |     def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         """Send a request to the server.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         `method' specifies an HTTP request method, e.g. 'GET'. | 
					
						
							|  |  |  |         `url' specifies the object being requested, e.g. '/index.html'. | 
					
						
							| 
									
										
										
										
											2003-11-19 19:51:55 +00:00
										 |  |  |         `skip_host' if True does not add automatically a 'Host:' header | 
					
						
							|  |  |  |         `skip_accept_encoding' if True does not add automatically an | 
					
						
							|  |  |  |            'Accept-Encoding:' header | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-24 06:35:19 +00:00
										 |  |  |         # if a prior response has been completed, then forget about it. | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         if self.__response and self.__response.isclosed(): | 
					
						
							|  |  |  |             self.__response = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-01-18 20:29:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         # in certain cases, we cannot issue another request on this connection. | 
					
						
							|  |  |  |         # this occurs when: | 
					
						
							|  |  |  |         #   1) we are in the process of sending a request.   (_CS_REQ_STARTED) | 
					
						
							|  |  |  |         #   2) a response to a previous request has signalled that it is going | 
					
						
							|  |  |  |         #      to close the connection upon completion. | 
					
						
							|  |  |  |         #   3) the headers for the previous response have not been read, thus | 
					
						
							|  |  |  |         #      we cannot determine whether point (2) is true.   (_CS_REQ_SENT) | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # if there is no prior response, then we can request at will. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # if point (2) is true, then we will have passed the socket to the | 
					
						
							|  |  |  |         # response (effectively meaning, "there is no prior response"), and | 
					
						
							|  |  |  |         # will open a new one when a new request is made. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # Note: if a prior response exists, then we *can* start a new request. | 
					
						
							|  |  |  |         #       We are not allowed to begin fetching the response to this new | 
					
						
							|  |  |  |         #       request, however, until that prior response is complete. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         if self.__state == _CS_IDLE: | 
					
						
							|  |  |  |             self.__state = _CS_REQ_STARTED | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise CannotSendRequest() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |         # Save the method we use, we need it later in the response phase | 
					
						
							|  |  |  |         self._method = method | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         if not url: | 
					
						
							|  |  |  |             url = '/' | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         str = '%s %s %s' % (method, url, self._http_vsn_str) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         self._output(str) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if self._http_vsn == 11: | 
					
						
							|  |  |  |             # Issue some standard headers for better HTTP/1.1 compliance | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-09 06:07:23 +00:00
										 |  |  |             if not skip_host: | 
					
						
							|  |  |  |                 # this header is issued *only* for HTTP/1.1 | 
					
						
							|  |  |  |                 # connections. more specifically, this means it is | 
					
						
							|  |  |  |                 # only issued when the client uses the new | 
					
						
							|  |  |  |                 # HTTPConnection() class. backwards-compat clients | 
					
						
							|  |  |  |                 # will be using HTTP/1.0 and those clients may be | 
					
						
							|  |  |  |                 # issuing this header themselves. we should NOT issue | 
					
						
							|  |  |  |                 # it twice; some web servers (such as Apache) barf | 
					
						
							|  |  |  |                 # when they see two Host: headers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # If we need a non-standard port,include it in the | 
					
						
							|  |  |  |                 # header.  If the request is going through a proxy, | 
					
						
							|  |  |  |                 # but the host of the actual URL, not the host of the | 
					
						
							|  |  |  |                 # proxy. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 netloc = '' | 
					
						
							|  |  |  |                 if url.startswith('http'): | 
					
						
							|  |  |  |                     nil, netloc, nil, nil, nil = urlsplit(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if netloc: | 
					
						
							| 
									
										
										
										
											2006-05-03 18:03:22 +00:00
										 |  |  |                     try: | 
					
						
							|  |  |  |                         netloc_enc = netloc.encode("ascii") | 
					
						
							|  |  |  |                     except UnicodeEncodeError: | 
					
						
							|  |  |  |                         netloc_enc = netloc.encode("idna") | 
					
						
							|  |  |  |                     self.putheader('Host', netloc_enc) | 
					
						
							| 
									
										
										
										
											2002-03-09 06:07:23 +00:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2006-05-03 18:03:22 +00:00
										 |  |  |                     try: | 
					
						
							|  |  |  |                         host_enc = self.host.encode("ascii") | 
					
						
							|  |  |  |                     except UnicodeEncodeError: | 
					
						
							|  |  |  |                         host_enc = self.host.encode("idna") | 
					
						
							|  |  |  |                     if self.port == HTTP_PORT: | 
					
						
							|  |  |  |                         self.putheader('Host', host_enc) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         self.putheader('Host', "%s:%s" % (host_enc, self.port)) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # note: we are assuming that clients will not attempt to set these | 
					
						
							|  |  |  |             #       headers since *this* library must deal with the | 
					
						
							|  |  |  |             #       consequences. this also means that when the supporting | 
					
						
							|  |  |  |             #       libraries are updated to recognize other forms, then this | 
					
						
							|  |  |  |             #       code should be changed (removed or updated). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # we only want a Content-Encoding of "identity" since we don't | 
					
						
							|  |  |  |             # support encodings such as x-gzip or x-deflate. | 
					
						
							| 
									
										
										
										
											2003-11-19 19:51:55 +00:00
										 |  |  |             if not skip_accept_encoding: | 
					
						
							|  |  |  |                 self.putheader('Accept-Encoding', 'identity') | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # we can accept "chunked" Transfer-Encodings, but no others | 
					
						
							|  |  |  |             # NOTE: no TE header implies *only* "chunked" | 
					
						
							|  |  |  |             #self.putheader('TE', 'chunked') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # if TE is supplied in the header, then it must appear in a | 
					
						
							|  |  |  |             # Connection header. | 
					
						
							|  |  |  |             #self.putheader('Connection', 'TE') | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             # For HTTP/1.0, the server will assume "not chunked" | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def putheader(self, header, value): | 
					
						
							|  |  |  |         """Send a request header line to the server.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         For example: h.putheader('Accept', 'text/html') | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if self.__state != _CS_REQ_STARTED: | 
					
						
							|  |  |  |             raise CannotSendHeader() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         str = '%s: %s' % (header, value) | 
					
						
							|  |  |  |         self._output(str) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def endheaders(self): | 
					
						
							|  |  |  |         """Indicate that the last header line has been sent to the server.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.__state == _CS_REQ_STARTED: | 
					
						
							|  |  |  |             self.__state = _CS_REQ_SENT | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise CannotSendHeader() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |         self._send_output() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def request(self, method, url, body=None, headers={}): | 
					
						
							|  |  |  |         """Send a complete request to the server.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self._send_request(method, url, body, headers) | 
					
						
							|  |  |  |         except socket.error, v: | 
					
						
							|  |  |  |             # trap 'Broken pipe' if we're allowed to automatically reconnect | 
					
						
							|  |  |  |             if v[0] != 32 or not self.auto_open: | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |             # try one more time | 
					
						
							|  |  |  |             self._send_request(method, url, body, headers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _send_request(self, method, url, body, headers): | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |         # honour explicitly requested Host: and Accept-Encoding headers | 
					
						
							|  |  |  |         header_names = dict.fromkeys([k.lower() for k in headers]) | 
					
						
							|  |  |  |         skips = {} | 
					
						
							|  |  |  |         if 'host' in header_names: | 
					
						
							|  |  |  |             skips['skip_host'] = 1 | 
					
						
							|  |  |  |         if 'accept-encoding' in header_names: | 
					
						
							|  |  |  |             skips['skip_accept_encoding'] = 1 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-07 16:28:14 +00:00
										 |  |  |         self.putrequest(method, url, **skips) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if body and ('content-length' not in header_names): | 
					
						
							| 
									
										
										
										
											2006-11-12 10:32:47 +00:00
										 |  |  |             thelen=None | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 thelen=str(len(body)) | 
					
						
							|  |  |  |             except TypeError, te: | 
					
						
							|  |  |  |                 # If this is a file-like object, try to | 
					
						
							|  |  |  |                 # fstat its file descriptor | 
					
						
							|  |  |  |                 import os | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     thelen = str(os.fstat(body.fileno()).st_size) | 
					
						
							|  |  |  |                 except (AttributeError, OSError): | 
					
						
							|  |  |  |                     # Don't send a length if this failed | 
					
						
							|  |  |  |                     if self.debuglevel > 0: print "Cannot stat!!" | 
					
						
							| 
									
										
										
										
											2007-01-30 03:03:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-11-12 10:32:47 +00:00
										 |  |  |             if thelen is not None: | 
					
						
							|  |  |  |                 self.putheader('Content-Length',thelen) | 
					
						
							| 
									
										
										
										
											2003-02-26 22:45:18 +00:00
										 |  |  |         for hdr, value in headers.iteritems(): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             self.putheader(hdr, value) | 
					
						
							|  |  |  |         self.endheaders() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if body: | 
					
						
							|  |  |  |             self.send(body) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def getresponse(self): | 
					
						
							|  |  |  |         "Get the response from the server." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-24 06:35:19 +00:00
										 |  |  |         # if a prior response has been completed, then forget about it. | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         if self.__response and self.__response.isclosed(): | 
					
						
							|  |  |  |             self.__response = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # if a prior response exists, then it must be completed (otherwise, we | 
					
						
							|  |  |  |         # cannot read this response's header to determine the connection-close | 
					
						
							|  |  |  |         # behavior) | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # note: if a prior response existed, but was connection-close, then the | 
					
						
							|  |  |  |         # socket and response were made independent of this HTTPConnection | 
					
						
							|  |  |  |         # object since a new request requires that we open a whole new | 
					
						
							|  |  |  |         # connection | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # this means the prior response had one of two states: | 
					
						
							|  |  |  |         #   1) will_close: this connection was reset and the prior socket and | 
					
						
							|  |  |  |         #                  response operate independently | 
					
						
							|  |  |  |         #   2) persistent: the response was retained and we await its | 
					
						
							|  |  |  |         #                  isclosed() status to become true. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         if self.__state != _CS_REQ_SENT or self.__response: | 
					
						
							|  |  |  |             raise ResponseNotReady() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         if self.debuglevel > 0: | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |             response = self.response_class(self.sock, self.debuglevel, | 
					
						
							| 
									
										
										
										
											2003-05-12 20:19:37 +00:00
										 |  |  |                                            strict=self.strict, | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |                                            method=self._method) | 
					
						
							| 
									
										
										
										
											2000-09-18 22:50:38 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2003-05-05 16:13:58 +00:00
										 |  |  |             response = self.response_class(self.sock, strict=self.strict, | 
					
						
							|  |  |  |                                            method=self._method) | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-12 14:04:09 +00:00
										 |  |  |         response.begin() | 
					
						
							| 
									
										
										
										
											2002-06-28 22:38:01 +00:00
										 |  |  |         assert response.will_close != _UNKNOWN | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.__state = _CS_IDLE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if response.will_close: | 
					
						
							| 
									
										
										
										
											2007-03-23 13:27:15 +00:00
										 |  |  |             # this effectively passes the connection to the response | 
					
						
							|  |  |  |             self.close() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             # remember this, so we can tell when it is complete | 
					
						
							|  |  |  |             self.__response = response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  | class HTTP: | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     "Compatibility class with httplib.py from 1.5." | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     _http_vsn = 10 | 
					
						
							|  |  |  |     _http_vsn_str = 'HTTP/1.0' | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     debuglevel = 0 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |     _connection_class = HTTPConnection | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |     def __init__(self, host='', port=None, strict=None): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         "Provide a default host, since the superclass requires one." | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         # some joker passed 0 explicitly, meaning default port | 
					
						
							|  |  |  |         if port == 0: | 
					
						
							|  |  |  |             port = None | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         # Note that we may pass an empty string as the host; this will throw | 
					
						
							|  |  |  |         # an error when we attempt to connect. Presumably, the client code | 
					
						
							|  |  |  |         # will call connect before then, with a proper host. | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         self._setup(self._connection_class(host, port, strict)) | 
					
						
							| 
									
										
										
										
											2001-08-18 09:20:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _setup(self, conn): | 
					
						
							|  |  |  |         self._conn = conn | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |         # set up delegation to flesh out interface | 
					
						
							| 
									
										
										
										
											2001-08-18 09:20:23 +00:00
										 |  |  |         self.send = conn.send | 
					
						
							|  |  |  |         self.putrequest = conn.putrequest | 
					
						
							|  |  |  |         self.endheaders = conn.endheaders | 
					
						
							|  |  |  |         self.set_debuglevel = conn.set_debuglevel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         conn._http_vsn = self._http_vsn | 
					
						
							|  |  |  |         conn._http_vsn_str = self._http_vsn_str | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.file = None | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def connect(self, host=None, port=None): | 
					
						
							|  |  |  |         "Accept arguments to set the host/port, since the superclass doesn't." | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         if host is not None: | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |             self._conn._set_hostport(host, port) | 
					
						
							|  |  |  |         self._conn.connect() | 
					
						
							| 
									
										
										
										
											1999-12-07 21:37:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def getfile(self): | 
					
						
							|  |  |  |         "Provide a getfile, since the superclass' does not use this concept." | 
					
						
							|  |  |  |         return self.file | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def putheader(self, header, *values): | 
					
						
							|  |  |  |         "The superclass allows only one value argument." | 
					
						
							| 
									
										
										
										
											2000-12-15 15:09:42 +00:00
										 |  |  |         self._conn.putheader(header, '\r\n\t'.join(values)) | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def getreply(self): | 
					
						
							|  |  |  |         """Compat definition since superclass does not define it.
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         Returns a tuple consisting of: | 
					
						
							|  |  |  |         - server status code (e.g. '200' if all goes well) | 
					
						
							|  |  |  |         - server "reason" corresponding to status code | 
					
						
							|  |  |  |         - any RFC822 headers in the response from the server | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |             response = self._conn.getresponse() | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         except BadStatusLine, e: | 
					
						
							|  |  |  |             ### hmm. if getresponse() ever closes the socket on a bad request, | 
					
						
							|  |  |  |             ### then we are going to have problems with self.sock | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             ### should we keep this behavior? do people use it? | 
					
						
							|  |  |  |             # keep the socket open (as a file), and return it | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |             self.file = self._conn.sock.makefile('rb', 0) | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             # close our socket -- we want to restart after any protocol error | 
					
						
							|  |  |  |             self.close() | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |             self.headers = None | 
					
						
							|  |  |  |             return -1, e.line, None | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.headers = response.msg | 
					
						
							|  |  |  |         self.file = response.fp | 
					
						
							|  |  |  |         return response.status, response.reason, response.msg | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def close(self): | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |         self._conn.close() | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         # note that self.file == response.fp, which gets closed by the | 
					
						
							|  |  |  |         # superclass. just clear the object ref here. | 
					
						
							|  |  |  |         ### hmm. messy. if status==-1, then self.file is owned by us. | 
					
						
							|  |  |  |         ### well... we aren't explicitly closing, but losing this ref will | 
					
						
							|  |  |  |         ### do it | 
					
						
							|  |  |  |         self.file = None | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  | try: | 
					
						
							|  |  |  |     import ssl | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     class HTTPSConnection(HTTPConnection): | 
					
						
							|  |  |  |         "This class allows communication via SSL." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         default_port = HTTPS_PORT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def __init__(self, host, port=None, key_file=None, cert_file=None, | 
					
						
							|  |  |  |                      strict=None, timeout=None): | 
					
						
							|  |  |  |             HTTPConnection.__init__(self, host, port, strict, timeout) | 
					
						
							|  |  |  |             self.key_file = key_file | 
					
						
							|  |  |  |             self.cert_file = cert_file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def connect(self): | 
					
						
							|  |  |  |             "Connect to a host on a given (SSL) port." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             sock = socket.create_connection((self.host, self.port), self.timeout) | 
					
						
							| 
									
										
										
										
											2007-09-10 21:51:02 +00:00
										 |  |  |             self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file) | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-30 21:54:39 +00:00
										 |  |  |     __all__.append("HTTPSConnection") | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |     class HTTPS(HTTP): | 
					
						
							|  |  |  |         """Compatibility with 1.5 httplib interface
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Python 1.5.2 did not have an HTTPS class, but it defined an | 
					
						
							|  |  |  |         interface for sending http requests that is also useful for | 
					
						
							| 
									
										
										
										
											2001-01-13 19:16:21 +00:00
										 |  |  |         https. | 
					
						
							| 
									
										
										
										
											2000-08-01 17:33:32 +00:00
										 |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-21 22:09:47 +00:00
										 |  |  |         _connection_class = HTTPSConnection | 
					
						
							| 
									
										
										
										
											2001-01-13 19:16:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         def __init__(self, host='', port=None, key_file=None, cert_file=None, | 
					
						
							|  |  |  |                      strict=None): | 
					
						
							| 
									
										
										
										
											2001-08-18 09:20:23 +00:00
										 |  |  |             # provide a default host, pass the X509 cert info | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # urf. compensate for bad input. | 
					
						
							|  |  |  |             if port == 0: | 
					
						
							|  |  |  |                 port = None | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |             self._setup(self._connection_class(host, port, key_file, | 
					
						
							|  |  |  |                                                cert_file, strict)) | 
					
						
							| 
									
										
										
										
											2001-08-18 09:20:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # we never actually use these for anything, but we keep them | 
					
						
							|  |  |  |             # here for compatibility with post-1.5.2 CVS. | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |             self.key_file = key_file | 
					
						
							|  |  |  |             self.cert_file = cert_file | 
					
						
							| 
									
										
										
										
											2001-08-18 09:20:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  |     def FakeSocket (sock, sslobj): | 
					
						
							| 
									
										
										
										
											2007-08-31 19:02:23 +00:00
										 |  |  |         warnings.warn("FakeSocket is deprecated, and won't be in 3.x.  " + | 
					
						
							| 
									
										
										
										
											2007-09-10 21:51:02 +00:00
										 |  |  |                       "Use the result of ssl.wrap_socket() directly instead.", | 
					
						
							| 
									
										
										
										
											2007-08-31 19:02:23 +00:00
										 |  |  |                       DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  |         return sslobj | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | class HTTPException(Exception): | 
					
						
							| 
									
										
										
										
											2002-07-06 18:55:01 +00:00
										 |  |  |     # Subclasses that define an __init__ must call Exception.__init__ | 
					
						
							|  |  |  |     # or define self.args.  Otherwise, str() will fail. | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class NotConnected(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-24 16:53:50 +00:00
										 |  |  | class InvalidURL(HTTPException): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | class UnknownProtocol(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def __init__(self, version): | 
					
						
							| 
									
										
										
										
											2002-07-06 18:55:01 +00:00
										 |  |  |         self.args = version, | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.version = version | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UnknownTransferEncoding(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UnimplementedFileMode(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class IncompleteRead(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def __init__(self, partial): | 
					
						
							| 
									
										
										
										
											2002-07-06 18:55:01 +00:00
										 |  |  |         self.args = partial, | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.partial = partial | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ImproperConnectionState(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CannotSendRequest(ImproperConnectionState): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class CannotSendHeader(ImproperConnectionState): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ResponseNotReady(ImproperConnectionState): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     pass | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BadStatusLine(HTTPException): | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |     def __init__(self, line): | 
					
						
							| 
									
										
										
										
											2002-07-06 18:55:01 +00:00
										 |  |  |         self.args = line, | 
					
						
							| 
									
										
										
										
											2000-07-18 09:09:48 +00:00
										 |  |  |         self.line = line | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # for backwards compatibility | 
					
						
							|  |  |  | error = HTTPException | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  | class LineAndFileWrapper: | 
					
						
							|  |  |  |     """A limited file-like object for HTTP/0.9 responses.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # The status-line parsing code calls readline(), which normally | 
					
						
							|  |  |  |     # get the HTTP status line.  For a 0.9 response, however, this is | 
					
						
							|  |  |  |     # actually the first line of the body!  Clients need to get a | 
					
						
							|  |  |  |     # readable file object that contains that line. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, line, file): | 
					
						
							|  |  |  |         self._line = line | 
					
						
							|  |  |  |         self._file = file | 
					
						
							|  |  |  |         self._line_consumed = 0 | 
					
						
							|  |  |  |         self._line_offset = 0 | 
					
						
							|  |  |  |         self._line_left = len(line) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getattr__(self, attr): | 
					
						
							|  |  |  |         return getattr(self._file, attr) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _done(self): | 
					
						
							|  |  |  |         # called when the last byte is read from the line.  After the | 
					
						
							|  |  |  |         # call, all read methods are delegated to the underlying file | 
					
						
							| 
									
										
										
										
											2003-02-25 17:48:15 +00:00
										 |  |  |         # object. | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         self._line_consumed = 1 | 
					
						
							|  |  |  |         self.read = self._file.read | 
					
						
							|  |  |  |         self.readline = self._file.readline | 
					
						
							|  |  |  |         self.readlines = self._file.readlines | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def read(self, amt=None): | 
					
						
							| 
									
										
										
										
											2004-06-05 13:30:56 +00:00
										 |  |  |         if self._line_consumed: | 
					
						
							|  |  |  |             return self._file.read(amt) | 
					
						
							|  |  |  |         assert self._line_left | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         if amt is None or amt > self._line_left: | 
					
						
							|  |  |  |             s = self._line[self._line_offset:] | 
					
						
							|  |  |  |             self._done() | 
					
						
							|  |  |  |             if amt is None: | 
					
						
							|  |  |  |                 return s + self._file.read() | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2002-07-16 21:35:23 +00:00
										 |  |  |                 return s + self._file.read(amt - len(s)) | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             assert amt <= self._line_left | 
					
						
							|  |  |  |             i = self._line_offset | 
					
						
							|  |  |  |             j = i + amt | 
					
						
							|  |  |  |             s = self._line[i:j] | 
					
						
							|  |  |  |             self._line_offset = j | 
					
						
							|  |  |  |             self._line_left -= amt | 
					
						
							|  |  |  |             if self._line_left == 0: | 
					
						
							|  |  |  |                 self._done() | 
					
						
							|  |  |  |             return s | 
					
						
							| 
									
										
										
										
											2002-07-16 21:35:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |     def readline(self): | 
					
						
							| 
									
										
										
										
											2004-06-05 13:30:56 +00:00
										 |  |  |         if self._line_consumed: | 
					
						
							|  |  |  |             return self._file.readline() | 
					
						
							|  |  |  |         assert self._line_left | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         s = self._line[self._line_offset:] | 
					
						
							|  |  |  |         self._done() | 
					
						
							|  |  |  |         return s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def readlines(self, size=None): | 
					
						
							| 
									
										
										
										
											2004-06-05 13:30:56 +00:00
										 |  |  |         if self._line_consumed: | 
					
						
							|  |  |  |             return self._file.readlines(size) | 
					
						
							|  |  |  |         assert self._line_left | 
					
						
							| 
									
										
										
										
											2002-07-06 18:48:07 +00:00
										 |  |  |         L = [self._line[self._line_offset:]] | 
					
						
							|  |  |  |         self._done() | 
					
						
							|  |  |  |         if size is None: | 
					
						
							|  |  |  |             return L + self._file.readlines() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return L + self._file.readlines(size) | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-02-21 16:36:04 +00:00
										 |  |  | def test(): | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     """Test this module.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-09 21:22:36 +00:00
										 |  |  |     A hodge podge of tests collected here, because they have too many | 
					
						
							|  |  |  |     external dependencies for the regular test suite. | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     import sys | 
					
						
							|  |  |  |     import getopt | 
					
						
							|  |  |  |     opts, args = getopt.getopt(sys.argv[1:], 'd') | 
					
						
							|  |  |  |     dl = 0 | 
					
						
							|  |  |  |     for o, a in opts: | 
					
						
							|  |  |  |         if o == '-d': dl = dl + 1 | 
					
						
							|  |  |  |     host = 'www.python.org' | 
					
						
							|  |  |  |     selector = '/' | 
					
						
							|  |  |  |     if args[0:]: host = args[0] | 
					
						
							|  |  |  |     if args[1:]: selector = args[1] | 
					
						
							|  |  |  |     h = HTTP() | 
					
						
							|  |  |  |     h.set_debuglevel(dl) | 
					
						
							|  |  |  |     h.connect(host) | 
					
						
							|  |  |  |     h.putrequest('GET', selector) | 
					
						
							|  |  |  |     h.endheaders() | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  |     status, reason, headers = h.getreply() | 
					
						
							|  |  |  |     print 'status =', status | 
					
						
							|  |  |  |     print 'reason =', reason | 
					
						
							| 
									
										
										
										
											2002-07-09 21:22:36 +00:00
										 |  |  |     print "read", len(h.getfile().read()) | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     print | 
					
						
							|  |  |  |     if headers: | 
					
						
							| 
									
										
										
										
											2000-12-15 15:09:42 +00:00
										 |  |  |         for header in headers.headers: print header.strip() | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     print | 
					
						
							| 
									
										
										
										
											2000-06-26 08:28:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-08 19:35:51 +00:00
										 |  |  |     # minimal test that code to extract host from url works | 
					
						
							|  |  |  |     class HTTP11(HTTP): | 
					
						
							|  |  |  |         _http_vsn = 11 | 
					
						
							|  |  |  |         _http_vsn_str = 'HTTP/1.1' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     h = HTTP11('www.python.org') | 
					
						
							|  |  |  |     h.putrequest('GET', 'http://www.python.org/~jeremy/') | 
					
						
							|  |  |  |     h.endheaders() | 
					
						
							|  |  |  |     h.getreply() | 
					
						
							|  |  |  |     h.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-29 22:35:05 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         import ssl | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2002-07-16 21:35:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-09 21:22:36 +00:00
										 |  |  |         for host, selector in (('sourceforge.net', '/projects/python'), | 
					
						
							|  |  |  |                                ): | 
					
						
							|  |  |  |             print "https://%s%s" % (host, selector) | 
					
						
							|  |  |  |             hs = HTTPS() | 
					
						
							| 
									
										
										
										
											2002-07-16 21:21:11 +00:00
										 |  |  |             hs.set_debuglevel(dl) | 
					
						
							| 
									
										
										
										
											2002-07-09 21:22:36 +00:00
										 |  |  |             hs.connect(host) | 
					
						
							|  |  |  |             hs.putrequest('GET', selector) | 
					
						
							|  |  |  |             hs.endheaders() | 
					
						
							|  |  |  |             status, reason, headers = hs.getreply() | 
					
						
							|  |  |  |             print 'status =', status | 
					
						
							|  |  |  |             print 'reason =', reason | 
					
						
							|  |  |  |             print "read", len(hs.getfile().read()) | 
					
						
							|  |  |  |             print | 
					
						
							|  |  |  |             if headers: | 
					
						
							|  |  |  |                 for header in headers.headers: print header.strip() | 
					
						
							|  |  |  |             print | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-02-21 16:36:04 +00:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											1997-12-09 00:12:23 +00:00
										 |  |  |     test() |