12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445 |
- r"""HTTP/1.1 client library
- <intro stuff goes here>
- <other stuff, too>
- HTTPConnection goes through a number of "states", which define when a client
- 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]
- |\____________________
- | |
- | 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
- 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>
- """
- from array import array
- import os
- import re
- import socket
- from sys import py3kwarning
- from urlparse import urlsplit
- import warnings
- with warnings.catch_warnings():
- if py3kwarning:
- warnings.filterwarnings("ignore", ".*mimetools has been removed",
- DeprecationWarning)
- import mimetools
- try:
- from cStringIO import StringIO
- except ImportError:
- from StringIO import StringIO
- __all__ = ["HTTP", "HTTPResponse", "HTTPConnection",
- "HTTPException", "NotConnected", "UnknownProtocol",
- "UnknownTransferEncoding", "UnimplementedFileMode",
- "IncompleteRead", "InvalidURL", "ImproperConnectionState",
- "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
- "BadStatusLine", "error", "responses"]
- HTTP_PORT = 80
- HTTPS_PORT = 443
- _UNKNOWN = 'UNKNOWN'
- _CS_IDLE = 'Idle'
- _CS_REQ_STARTED = 'Request-started'
- _CS_REQ_SENT = 'Request-sent'
- CONTINUE = 100
- SWITCHING_PROTOCOLS = 101
- PROCESSING = 102
- 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
- MULTIPLE_CHOICES = 300
- MOVED_PERMANENTLY = 301
- FOUND = 302
- SEE_OTHER = 303
- NOT_MODIFIED = 304
- USE_PROXY = 305
- TEMPORARY_REDIRECT = 307
- 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
- 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
- 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',
- }
- MAXAMOUNT = 1048576
- _MAXLINE = 65536
- _MAXHEADERS = 100
- _is_legal_header_name = re.compile(r'\A[^:\s][^:\r\n]*\Z').match
- _is_illegal_header_value = re.compile(r'\n(?![ \t])|\r(?![ \t\n])').search
- _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
- 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.
- """
-
-
-
-
- self.dict = {}
- self.unixfrom = ''
- self.headers = hlist = []
- 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
- while True:
- if len(hlist) > _MAXHEADERS:
- raise HTTPException("got more than %d headers" % _MAXHEADERS)
- if tell:
- try:
- startofline = tell()
- except IOError:
- startofline = tell = None
- self.seekable = 0
- line = self.fp.readline(_MAXLINE + 1)
- if len(line) > _MAXLINE:
- raise LineTooLong("header line")
- if not line:
- self.status = 'EOF in headers'
- break
-
- if firstline and line.startswith('From '):
- self.unixfrom = self.unixfrom + line
- continue
- firstline = 0
- if headerseen and line[0] in ' \t':
-
-
-
- hlist.append(line)
- self.addcontinue(headerseen, line.strip())
- continue
- elif self.iscomment(line):
-
- continue
- elif self.islast(line):
-
- break
- headerseen = self.isheader(line)
- if headerseen:
-
- hlist.append(line)
- self.addheader(headerseen, line[len(headerseen)+1:].strip())
- continue
- elif headerseen is not None:
-
-
-
- continue
- else:
-
- if not self.dict:
- self.status = 'No headers'
- else:
- self.status = 'Non-header line where header expected'
-
- if unread:
- unread(line)
- elif tell:
- self.fp.seek(startofline)
- else:
- self.status = self.status + '; bad seek'
- break
- class HTTPResponse:
-
-
-
-
-
-
- def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False):
- if buffering:
-
-
- self.fp = sock.makefile('rb')
- else:
-
-
-
-
-
- self.fp = sock.makefile('rb', 0)
- self.debuglevel = debuglevel
- self.strict = strict
- self._method = method
- self.msg = None
-
- self.version = _UNKNOWN
- self.status = _UNKNOWN
- self.reason = _UNKNOWN
- self.chunked = _UNKNOWN
- self.chunk_left = _UNKNOWN
- self.length = _UNKNOWN
- self.will_close = _UNKNOWN
- def _read_status(self):
-
- line = self.fp.readline(_MAXLINE + 1)
- if len(line) > _MAXLINE:
- raise LineTooLong("header line")
- if self.debuglevel > 0:
- print "reply:", repr(line)
- if not line:
-
-
- raise BadStatusLine(line)
- try:
- [version, status, reason] = line.split(None, 2)
- except ValueError:
- try:
- [version, status] = line.split(None, 1)
- reason = ""
- except ValueError:
-
-
- version = ""
- if not version.startswith('HTTP/'):
- if self.strict:
- self.close()
- raise BadStatusLine(line)
- else:
-
- self.fp = LineAndFileWrapper(line, self.fp)
- return "HTTP/0.9", 200, ""
-
- try:
- status = int(status)
- if status < 100 or status > 999:
- raise BadStatusLine(line)
- except ValueError:
- raise BadStatusLine(line)
- return version, status, reason
- def begin(self):
- if self.msg is not None:
-
- return
-
- while True:
- version, status, reason = self._read_status()
- if status != CONTINUE:
- break
-
- while True:
- skip = self.fp.readline(_MAXLINE + 1)
- if len(skip) > _MAXLINE:
- raise LineTooLong("header line")
- skip = skip.strip()
- if not skip:
- break
- if self.debuglevel > 0:
- print "header:", skip
- self.status = status
- self.reason = reason.strip()
- if version == 'HTTP/1.0':
- self.version = 10
- elif version.startswith('HTTP/1.'):
- self.version = 11
- elif version == 'HTTP/0.9':
- self.version = 9
- else:
- raise UnknownProtocol(version)
- if self.version == 9:
- self.length = None
- self.chunked = 0
- self.will_close = 1
- self.msg = HTTPMessage(StringIO())
- return
- self.msg = HTTPMessage(self.fp, 0)
- if self.debuglevel > 0:
- for hdr in self.msg.headers:
- print "header:", hdr,
-
- self.msg.fp = None
-
- tr_enc = self.msg.getheader('transfer-encoding')
- if tr_enc and tr_enc.lower() == "chunked":
- self.chunked = 1
- self.chunk_left = None
- else:
- self.chunked = 0
-
- self.will_close = self._check_close()
-
-
- length = self.msg.getheader('content-length')
- if length and not self.chunked:
- try:
- self.length = int(length)
- except ValueError:
- self.length = None
- else:
- if self.length < 0:
- self.length = None
- else:
- self.length = None
-
- if (status == NO_CONTENT or status == NOT_MODIFIED or
- 100 <= status < 200 or
- self._method == 'HEAD'):
- self.length = 0
-
-
-
- if not self.will_close and \
- not self.chunked and \
- self.length is None:
- self.will_close = 1
- def _check_close(self):
- conn = self.msg.getheader('connection')
- if self.version == 11:
-
-
- conn = self.msg.getheader('connection')
- if conn and "close" in conn.lower():
- return True
- return False
-
-
-
- if self.msg.getheader('keep-alive'):
- return False
-
-
- if conn and "keep-alive" in conn.lower():
- return False
-
- pconn = self.msg.getheader('proxy-connection')
- if pconn and "keep-alive" in pconn.lower():
- return False
-
- return True
- def close(self):
- fp = self.fp
- if fp:
- self.fp = None
- fp.close()
- def isclosed(self):
-
-
-
-
-
-
- return self.fp is None
-
- def read(self, amt=None):
- if self.fp is None:
- return ''
- if self._method == 'HEAD':
- self.close()
- return ''
- if self.chunked:
- return self._read_chunked(amt)
- if amt is None:
-
- if self.length is None:
- s = self.fp.read()
- else:
- try:
- s = self._safe_read(self.length)
- except IncompleteRead:
- self.close()
- raise
- self.length = 0
- self.close()
- return s
- if self.length is not None:
- if amt > self.length:
-
- amt = self.length
-
-
-
- s = self.fp.read(amt)
- if not s and amt:
-
-
- self.close()
- if self.length is not None:
- self.length -= len(s)
- if not self.length:
- self.close()
- return s
- def _read_chunked(self, amt):
- assert self.chunked != _UNKNOWN
- chunk_left = self.chunk_left
- value = []
- while True:
- if chunk_left is None:
- line = self.fp.readline(_MAXLINE + 1)
- if len(line) > _MAXLINE:
- raise LineTooLong("chunk size")
- i = line.find(';')
- if i >= 0:
- line = line[:i]
- try:
- chunk_left = int(line, 16)
- except ValueError:
-
-
- self.close()
- raise IncompleteRead(''.join(value))
- if chunk_left == 0:
- break
- if amt is None:
- value.append(self._safe_read(chunk_left))
- elif amt < chunk_left:
- value.append(self._safe_read(amt))
- self.chunk_left = chunk_left - amt
- return ''.join(value)
- elif amt == chunk_left:
- value.append(self._safe_read(amt))
- self._safe_read(2)
- self.chunk_left = None
- return ''.join(value)
- else:
- value.append(self._safe_read(chunk_left))
- amt -= chunk_left
-
- self._safe_read(2)
- chunk_left = None
-
-
- while True:
- line = self.fp.readline(_MAXLINE + 1)
- if len(line) > _MAXLINE:
- raise LineTooLong("trailer line")
- if not line:
-
-
- break
- if line == '\r\n':
- break
-
- self.close()
- return ''.join(value)
- 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.
- """
-
-
-
-
-
- s = []
- while amt > 0:
- chunk = self.fp.read(min(amt, MAXAMOUNT))
- if not chunk:
- raise IncompleteRead(''.join(s), amt)
- s.append(chunk)
- amt -= len(chunk)
- return ''.join(s)
- def fileno(self):
- return self.fp.fileno()
- def getheader(self, name, default=None):
- if self.msg is None:
- raise ResponseNotReady()
- return self.msg.getheader(name, default)
- def getheaders(self):
- """Return list of (header, value) tuples."""
- if self.msg is None:
- raise ResponseNotReady()
- return self.msg.items()
- class HTTPConnection:
- _http_vsn = 11
- _http_vsn_str = 'HTTP/1.1'
- response_class = HTTPResponse
- default_port = HTTP_PORT
- auto_open = 1
- debuglevel = 0
- strict = 0
- def __init__(self, host, port=None, strict=None,
- timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
- self.timeout = timeout
- self.source_address = source_address
- self.sock = None
- self._buffer = []
- self.__response = None
- self.__state = _CS_IDLE
- self._method = None
- self._tunnel_host = None
- self._tunnel_port = None
- self._tunnel_headers = {}
- if strict is not None:
- self.strict = strict
- (self.host, self.port) = self._get_hostport(host, port)
-
-
- self._create_connection = socket.create_connection
- def set_tunnel(self, host, port=None, headers=None):
- """ Set up host and port for HTTP CONNECT tunnelling.
- In a connection that uses HTTP Connect tunneling, the host passed to the
- constructor is used as proxy server that relays all communication to the
- endpoint passed to set_tunnel. This is done by sending a HTTP CONNECT
- request to the proxy server when the connection is established.
- This method must be called before the HTTP connection has been
- established.
- The headers argument should be a mapping of extra HTTP headers
- to send with the CONNECT request.
- """
-
- if self.sock:
- raise RuntimeError("Can't setup tunnel for established connection.")
- self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)
- if headers:
- self._tunnel_headers = headers
- else:
- self._tunnel_headers.clear()
- def _get_hostport(self, host, port):
- if port is None:
- i = host.rfind(':')
- j = host.rfind(']')
- if i > j:
- try:
- port = int(host[i+1:])
- except ValueError:
- if host[i+1:] == "":
- port = self.default_port
- else:
- raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
- host = host[:i]
- else:
- port = self.default_port
- if host and host[0] == '[' and host[-1] == ']':
- host = host[1:-1]
- return (host, port)
- def set_debuglevel(self, level):
- self.debuglevel = level
- def _tunnel(self):
- self.send("CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,
- self._tunnel_port))
- for header, value in self._tunnel_headers.iteritems():
- self.send("%s: %s\r\n" % (header, value))
- self.send("\r\n")
- response = self.response_class(self.sock, strict = self.strict,
- method = self._method)
- (version, code, message) = response._read_status()
- if version == "HTTP/0.9":
-
-
- self.close()
- raise socket.error("Invalid response from tunnel request")
- if code != 200:
- self.close()
- raise socket.error("Tunnel connection failed: %d %s" % (code,
- message.strip()))
- while True:
- line = response.fp.readline(_MAXLINE + 1)
- if len(line) > _MAXLINE:
- raise LineTooLong("header line")
- if not line:
-
- break
- if line == '\r\n':
- break
- def connect(self):
- """Connect to the host and port specified in __init__."""
- self.sock = self._create_connection((self.host,self.port),
- self.timeout, self.source_address)
- if self._tunnel_host:
- self._tunnel()
- def close(self):
- """Close the connection to the HTTP server."""
- self.__state = _CS_IDLE
- try:
- sock = self.sock
- if sock:
- self.sock = None
- sock.close()
- finally:
- response = self.__response
- if response:
- self.__response = None
- response.close()
- def send(self, data):
- """Send `data' to the server."""
- if self.sock is None:
- if self.auto_open:
- self.connect()
- else:
- raise NotConnected()
- if self.debuglevel > 0:
- print "send:", repr(data)
- blocksize = 8192
- if hasattr(data,'read') and not isinstance(data, array):
- if self.debuglevel > 0: print "sendIng a read()able"
- datablock = data.read(blocksize)
- while datablock:
- self.sock.sendall(datablock)
- datablock = data.read(blocksize)
- else:
- self.sock.sendall(data)
- def _output(self, s):
- """Add a line of output to the current request buffer.
- Assumes that the line does *not* end with \\r\\n.
- """
- self._buffer.append(s)
- def _send_output(self, message_body=None):
- """Send the currently buffered request and clear the buffer.
- Appends an extra \\r\\n to the buffer.
- A message_body may be specified, to be appended to the request.
- """
- self._buffer.extend(("", ""))
- msg = "\r\n".join(self._buffer)
- del self._buffer[:]
-
-
-
- if isinstance(message_body, str):
- msg += message_body
- message_body = None
- self.send(msg)
- if message_body is not None:
-
-
- self.send(message_body)
- def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):
- """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'.
- `skip_host' if True does not add automatically a 'Host:' header
- `skip_accept_encoding' if True does not add automatically an
- 'Accept-Encoding:' header
- """
-
- if self.__response and self.__response.isclosed():
- self.__response = None
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if self.__state == _CS_IDLE:
- self.__state = _CS_REQ_STARTED
- else:
- raise CannotSendRequest()
-
- self._method = method
- if not url:
- url = '/'
- hdr = '%s %s %s' % (method, url, self._http_vsn_str)
- self._output(hdr)
- if self._http_vsn == 11:
-
- if not skip_host:
-
-
-
-
-
-
-
-
-
-
-
-
- netloc = ''
- if url.startswith('http'):
- nil, netloc, nil, nil, nil = urlsplit(url)
- if netloc:
- try:
- netloc_enc = netloc.encode("ascii")
- except UnicodeEncodeError:
- netloc_enc = netloc.encode("idna")
- self.putheader('Host', netloc_enc)
- else:
- if self._tunnel_host:
- host = self._tunnel_host
- port = self._tunnel_port
- else:
- host = self.host
- port = self.port
- try:
- host_enc = host.encode("ascii")
- except UnicodeEncodeError:
- host_enc = host.encode("idna")
-
- if host_enc.find(':') >= 0:
- host_enc = "[" + host_enc + "]"
- if port == self.default_port:
- self.putheader('Host', host_enc)
- else:
- self.putheader('Host', "%s:%s" % (host_enc, port))
-
-
-
-
-
-
-
- if not skip_accept_encoding:
- self.putheader('Accept-Encoding', 'identity')
-
-
-
-
-
-
- else:
-
- pass
- def putheader(self, header, *values):
- """Send a request header line to the server.
- For example: h.putheader('Accept', 'text/html')
- """
- if self.__state != _CS_REQ_STARTED:
- raise CannotSendHeader()
- header = '%s' % header
- if not _is_legal_header_name(header):
- raise ValueError('Invalid header name %r' % (header,))
- values = [str(v) for v in values]
- for one_value in values:
- if _is_illegal_header_value(one_value):
- raise ValueError('Invalid header value %r' % (one_value,))
- hdr = '%s: %s' % (header, '\r\n\t'.join(values))
- self._output(hdr)
- def endheaders(self, message_body=None):
- """Indicate that the last header line has been sent to the server.
- This method sends the request to the server. The optional
- message_body argument can be used to pass a message body
- associated with the request. The message body will be sent in
- the same packet as the message headers if it is string, otherwise it is
- sent as a separate packet.
- """
- if self.__state == _CS_REQ_STARTED:
- self.__state = _CS_REQ_SENT
- else:
- raise CannotSendHeader()
- self._send_output(message_body)
- def request(self, method, url, body=None, headers={}):
- """Send a complete request to the server."""
- self._send_request(method, url, body, headers)
- def _set_content_length(self, body, method):
-
-
-
-
- thelen = None
- if body is None and method.upper() in _METHODS_EXPECTING_BODY:
- thelen = '0'
- elif body is not None:
- try:
- thelen = str(len(body))
- except (TypeError, AttributeError):
-
-
- try:
- thelen = str(os.fstat(body.fileno()).st_size)
- except (AttributeError, OSError):
-
- if self.debuglevel > 0: print "Cannot stat!!"
- if thelen is not None:
- self.putheader('Content-Length', thelen)
- def _send_request(self, method, url, body, 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
- self.putrequest(method, url, **skips)
- if 'content-length' not in header_names:
- self._set_content_length(body, method)
- for hdr, value in headers.iteritems():
- self.putheader(hdr, value)
- self.endheaders(body)
- def getresponse(self, buffering=False):
- "Get the response from the server."
-
- if self.__response and self.__response.isclosed():
- self.__response = None
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if self.__state != _CS_REQ_SENT or self.__response:
- raise ResponseNotReady()
- args = (self.sock,)
- kwds = {"strict":self.strict, "method":self._method}
- if self.debuglevel > 0:
- args += (self.debuglevel,)
- if buffering:
-
-
- kwds["buffering"] = True;
- response = self.response_class(*args, **kwds)
- try:
- response.begin()
- assert response.will_close != _UNKNOWN
- self.__state = _CS_IDLE
- if response.will_close:
-
- self.close()
- else:
-
- self.__response = response
- return response
- except:
- response.close()
- raise
- class HTTP:
- "Compatibility class with httplib.py from 1.5."
- _http_vsn = 10
- _http_vsn_str = 'HTTP/1.0'
- debuglevel = 0
- _connection_class = HTTPConnection
- def __init__(self, host='', port=None, strict=None):
- "Provide a default host, since the superclass requires one."
-
- if port == 0:
- port = None
-
-
-
- self._setup(self._connection_class(host, port, strict))
- def _setup(self, conn):
- self._conn = conn
-
- self.send = conn.send
- self.putrequest = conn.putrequest
- self.putheader = conn.putheader
- self.endheaders = conn.endheaders
- self.set_debuglevel = conn.set_debuglevel
- conn._http_vsn = self._http_vsn
- conn._http_vsn_str = self._http_vsn_str
- self.file = None
- def connect(self, host=None, port=None):
- "Accept arguments to set the host/port, since the superclass doesn't."
- if host is not None:
- (self._conn.host, self._conn.port) = self._conn._get_hostport(host, port)
- self._conn.connect()
- def getfile(self):
- "Provide a getfile, since the superclass' does not use this concept."
- return self.file
- def getreply(self, buffering=False):
- """Compat definition since superclass does not define it.
- 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:
- if not buffering:
- response = self._conn.getresponse()
- else:
-
-
- response = self._conn.getresponse(buffering)
- except BadStatusLine, e:
-
-
-
-
- self.file = self._conn.sock.makefile('rb', 0)
-
- self.close()
- self.headers = None
- return -1, e.line, None
- self.headers = response.msg
- self.file = response.fp
- return response.status, response.reason, response.msg
- def close(self):
- self._conn.close()
-
-
-
-
-
- self.file = None
- 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=socket._GLOBAL_DEFAULT_TIMEOUT,
- source_address=None, context=None):
- HTTPConnection.__init__(self, host, port, strict, timeout,
- source_address)
- self.key_file = key_file
- self.cert_file = cert_file
- if context is None:
- context = ssl._create_default_https_context()
- if key_file or cert_file:
- context.load_cert_chain(cert_file, key_file)
- self._context = context
- def connect(self):
- "Connect to a host on a given (SSL) port."
- HTTPConnection.connect(self)
- if self._tunnel_host:
- server_hostname = self._tunnel_host
- else:
- server_hostname = self.host
- self.sock = self._context.wrap_socket(self.sock,
- server_hostname=server_hostname)
- __all__.append("HTTPSConnection")
- 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
- https.
- """
- _connection_class = HTTPSConnection
- def __init__(self, host='', port=None, key_file=None, cert_file=None,
- strict=None, context=None):
-
-
- if port == 0:
- port = None
- self._setup(self._connection_class(host, port, key_file,
- cert_file, strict,
- context=context))
-
-
- self.key_file = key_file
- self.cert_file = cert_file
- def FakeSocket (sock, sslobj):
- warnings.warn("FakeSocket is deprecated, and won't be in 3.x. " +
- "Use the result of ssl.wrap_socket() directly instead.",
- DeprecationWarning, stacklevel=2)
- return sslobj
- class HTTPException(Exception):
-
-
- pass
- class NotConnected(HTTPException):
- pass
- class InvalidURL(HTTPException):
- pass
- class UnknownProtocol(HTTPException):
- def __init__(self, version):
- self.args = version,
- self.version = version
- class UnknownTransferEncoding(HTTPException):
- pass
- class UnimplementedFileMode(HTTPException):
- pass
- class IncompleteRead(HTTPException):
- def __init__(self, partial, expected=None):
- self.args = partial,
- self.partial = partial
- self.expected = expected
- def __repr__(self):
- if self.expected is not None:
- e = ', %i more expected' % self.expected
- else:
- e = ''
- return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e)
- def __str__(self):
- return repr(self)
- class ImproperConnectionState(HTTPException):
- pass
- class CannotSendRequest(ImproperConnectionState):
- pass
- class CannotSendHeader(ImproperConnectionState):
- pass
- class ResponseNotReady(ImproperConnectionState):
- pass
- class BadStatusLine(HTTPException):
- def __init__(self, line):
- if not line:
- line = repr(line)
- self.args = line,
- self.line = line
- class LineTooLong(HTTPException):
- def __init__(self, line_type):
- HTTPException.__init__(self, "got more than %d bytes when reading %s"
- % (_MAXLINE, line_type))
- error = HTTPException
- class LineAndFileWrapper:
- """A limited file-like object for HTTP/0.9 responses."""
-
-
-
-
- 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):
-
-
-
- self._line_consumed = 1
- self.read = self._file.read
- self.readline = self._file.readline
- self.readlines = self._file.readlines
- def read(self, amt=None):
- if self._line_consumed:
- return self._file.read(amt)
- assert self._line_left
- 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:
- return s + self._file.read(amt - len(s))
- 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
- def readline(self):
- if self._line_consumed:
- return self._file.readline()
- assert self._line_left
- s = self._line[self._line_offset:]
- self._done()
- return s
- def readlines(self, size=None):
- if self._line_consumed:
- return self._file.readlines(size)
- assert self._line_left
- L = [self._line[self._line_offset:]]
- self._done()
- if size is None:
- return L + self._file.readlines()
- else:
- return L + self._file.readlines(size)
|