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