]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """HTTP/1.1 client library\r |
2 | \r | |
3 | <intro stuff goes here>\r | |
4 | <other stuff, too>\r | |
5 | \r | |
6 | HTTPConnection goes through a number of "states", which define when a client\r | |
7 | may legally make another request or fetch the response for a particular\r | |
8 | request. This diagram details these state transitions:\r | |
9 | \r | |
10 | (null)\r | |
11 | |\r | |
12 | | HTTPConnection()\r | |
13 | v\r | |
14 | Idle\r | |
15 | |\r | |
16 | | putrequest()\r | |
17 | v\r | |
18 | Request-started\r | |
19 | |\r | |
20 | | ( putheader() )* endheaders()\r | |
21 | v\r | |
22 | Request-sent\r | |
23 | |\r | |
24 | | response = getresponse()\r | |
25 | v\r | |
26 | Unread-response [Response-headers-read]\r | |
27 | |\____________________\r | |
28 | | |\r | |
29 | | response.read() | putrequest()\r | |
30 | v v\r | |
31 | Idle Req-started-unread-response\r | |
32 | ______/|\r | |
33 | / |\r | |
34 | response.read() | | ( putheader() )* endheaders()\r | |
35 | v v\r | |
36 | Request-started Req-sent-unread-response\r | |
37 | |\r | |
38 | | response.read()\r | |
39 | v\r | |
40 | Request-sent\r | |
41 | \r | |
42 | This diagram presents the following rules:\r | |
43 | -- a second request may not be started until {response-headers-read}\r | |
44 | -- a response [object] cannot be retrieved until {request-sent}\r | |
45 | -- there is no differentiation between an unread response body and a\r | |
46 | partially read response body\r | |
47 | \r | |
48 | Note: this enforcement is applied by the HTTPConnection class. The\r | |
49 | HTTPResponse class does not enforce this state machine, which\r | |
50 | implies sophisticated clients may accelerate the request/response\r | |
51 | pipeline. Caution should be taken, though: accelerating the states\r | |
52 | beyond the above pattern may imply knowledge of the server's\r | |
53 | connection-close behavior for certain requests. For example, it\r | |
54 | is impossible to tell whether the server will close the connection\r | |
55 | UNTIL the response headers have been read; this means that further\r | |
56 | requests cannot be placed into the pipeline until it is known that\r | |
57 | the server will NOT be closing the connection.\r | |
58 | \r | |
59 | Logical State __state __response\r | |
60 | ------------- ------- ----------\r | |
61 | Idle _CS_IDLE None\r | |
62 | Request-started _CS_REQ_STARTED None\r | |
63 | Request-sent _CS_REQ_SENT None\r | |
64 | Unread-response _CS_IDLE <response_class>\r | |
65 | Req-started-unread-response _CS_REQ_STARTED <response_class>\r | |
66 | Req-sent-unread-response _CS_REQ_SENT <response_class>\r | |
67 | """\r | |
68 | \r | |
69 | from array import array\r | |
70 | import os\r | |
71 | import socket\r | |
72 | from sys import py3kwarning\r | |
73 | from urlparse import urlsplit\r | |
74 | import warnings\r | |
75 | with warnings.catch_warnings():\r | |
76 | if py3kwarning:\r | |
77 | warnings.filterwarnings("ignore", ".*mimetools has been removed",\r | |
78 | DeprecationWarning)\r | |
79 | import mimetools\r | |
80 | \r | |
81 | try:\r | |
82 | from cStringIO import StringIO\r | |
83 | except ImportError:\r | |
84 | from StringIO import StringIO\r | |
85 | \r | |
86 | __all__ = ["HTTP", "HTTPResponse", "HTTPConnection",\r | |
87 | "HTTPException", "NotConnected", "UnknownProtocol",\r | |
88 | "UnknownTransferEncoding", "UnimplementedFileMode",\r | |
89 | "IncompleteRead", "InvalidURL", "ImproperConnectionState",\r | |
90 | "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",\r | |
91 | "BadStatusLine", "error", "responses"]\r | |
92 | \r | |
93 | HTTP_PORT = 80\r | |
94 | HTTPS_PORT = 443\r | |
95 | \r | |
96 | _UNKNOWN = 'UNKNOWN'\r | |
97 | \r | |
98 | # connection states\r | |
99 | _CS_IDLE = 'Idle'\r | |
100 | _CS_REQ_STARTED = 'Request-started'\r | |
101 | _CS_REQ_SENT = 'Request-sent'\r | |
102 | \r | |
103 | # status codes\r | |
104 | # informational\r | |
105 | CONTINUE = 100\r | |
106 | SWITCHING_PROTOCOLS = 101\r | |
107 | PROCESSING = 102\r | |
108 | \r | |
109 | # successful\r | |
110 | OK = 200\r | |
111 | CREATED = 201\r | |
112 | ACCEPTED = 202\r | |
113 | NON_AUTHORITATIVE_INFORMATION = 203\r | |
114 | NO_CONTENT = 204\r | |
115 | RESET_CONTENT = 205\r | |
116 | PARTIAL_CONTENT = 206\r | |
117 | MULTI_STATUS = 207\r | |
118 | IM_USED = 226\r | |
119 | \r | |
120 | # redirection\r | |
121 | MULTIPLE_CHOICES = 300\r | |
122 | MOVED_PERMANENTLY = 301\r | |
123 | FOUND = 302\r | |
124 | SEE_OTHER = 303\r | |
125 | NOT_MODIFIED = 304\r | |
126 | USE_PROXY = 305\r | |
127 | TEMPORARY_REDIRECT = 307\r | |
128 | \r | |
129 | # client error\r | |
130 | BAD_REQUEST = 400\r | |
131 | UNAUTHORIZED = 401\r | |
132 | PAYMENT_REQUIRED = 402\r | |
133 | FORBIDDEN = 403\r | |
134 | NOT_FOUND = 404\r | |
135 | METHOD_NOT_ALLOWED = 405\r | |
136 | NOT_ACCEPTABLE = 406\r | |
137 | PROXY_AUTHENTICATION_REQUIRED = 407\r | |
138 | REQUEST_TIMEOUT = 408\r | |
139 | CONFLICT = 409\r | |
140 | GONE = 410\r | |
141 | LENGTH_REQUIRED = 411\r | |
142 | PRECONDITION_FAILED = 412\r | |
143 | REQUEST_ENTITY_TOO_LARGE = 413\r | |
144 | REQUEST_URI_TOO_LONG = 414\r | |
145 | UNSUPPORTED_MEDIA_TYPE = 415\r | |
146 | REQUESTED_RANGE_NOT_SATISFIABLE = 416\r | |
147 | EXPECTATION_FAILED = 417\r | |
148 | UNPROCESSABLE_ENTITY = 422\r | |
149 | LOCKED = 423\r | |
150 | FAILED_DEPENDENCY = 424\r | |
151 | UPGRADE_REQUIRED = 426\r | |
152 | \r | |
153 | # server error\r | |
154 | INTERNAL_SERVER_ERROR = 500\r | |
155 | NOT_IMPLEMENTED = 501\r | |
156 | BAD_GATEWAY = 502\r | |
157 | SERVICE_UNAVAILABLE = 503\r | |
158 | GATEWAY_TIMEOUT = 504\r | |
159 | HTTP_VERSION_NOT_SUPPORTED = 505\r | |
160 | INSUFFICIENT_STORAGE = 507\r | |
161 | NOT_EXTENDED = 510\r | |
162 | \r | |
163 | # Mapping status codes to official W3C names\r | |
164 | responses = {\r | |
165 | 100: 'Continue',\r | |
166 | 101: 'Switching Protocols',\r | |
167 | \r | |
168 | 200: 'OK',\r | |
169 | 201: 'Created',\r | |
170 | 202: 'Accepted',\r | |
171 | 203: 'Non-Authoritative Information',\r | |
172 | 204: 'No Content',\r | |
173 | 205: 'Reset Content',\r | |
174 | 206: 'Partial Content',\r | |
175 | \r | |
176 | 300: 'Multiple Choices',\r | |
177 | 301: 'Moved Permanently',\r | |
178 | 302: 'Found',\r | |
179 | 303: 'See Other',\r | |
180 | 304: 'Not Modified',\r | |
181 | 305: 'Use Proxy',\r | |
182 | 306: '(Unused)',\r | |
183 | 307: 'Temporary Redirect',\r | |
184 | \r | |
185 | 400: 'Bad Request',\r | |
186 | 401: 'Unauthorized',\r | |
187 | 402: 'Payment Required',\r | |
188 | 403: 'Forbidden',\r | |
189 | 404: 'Not Found',\r | |
190 | 405: 'Method Not Allowed',\r | |
191 | 406: 'Not Acceptable',\r | |
192 | 407: 'Proxy Authentication Required',\r | |
193 | 408: 'Request Timeout',\r | |
194 | 409: 'Conflict',\r | |
195 | 410: 'Gone',\r | |
196 | 411: 'Length Required',\r | |
197 | 412: 'Precondition Failed',\r | |
198 | 413: 'Request Entity Too Large',\r | |
199 | 414: 'Request-URI Too Long',\r | |
200 | 415: 'Unsupported Media Type',\r | |
201 | 416: 'Requested Range Not Satisfiable',\r | |
202 | 417: 'Expectation Failed',\r | |
203 | \r | |
204 | 500: 'Internal Server Error',\r | |
205 | 501: 'Not Implemented',\r | |
206 | 502: 'Bad Gateway',\r | |
207 | 503: 'Service Unavailable',\r | |
208 | 504: 'Gateway Timeout',\r | |
209 | 505: 'HTTP Version Not Supported',\r | |
210 | }\r | |
211 | \r | |
212 | # maximal amount of data to read at one time in _safe_read\r | |
213 | MAXAMOUNT = 1048576\r | |
214 | \r | |
215 | # maximal line length when calling readline().\r | |
216 | _MAXLINE = 65536\r | |
217 | \r | |
218 | class HTTPMessage(mimetools.Message):\r | |
219 | \r | |
220 | def addheader(self, key, value):\r | |
221 | """Add header for field key handling repeats."""\r | |
222 | prev = self.dict.get(key)\r | |
223 | if prev is None:\r | |
224 | self.dict[key] = value\r | |
225 | else:\r | |
226 | combined = ", ".join((prev, value))\r | |
227 | self.dict[key] = combined\r | |
228 | \r | |
229 | def addcontinue(self, key, more):\r | |
230 | """Add more field data from a continuation line."""\r | |
231 | prev = self.dict[key]\r | |
232 | self.dict[key] = prev + "\n " + more\r | |
233 | \r | |
234 | def readheaders(self):\r | |
235 | """Read header lines.\r | |
236 | \r | |
237 | Read header lines up to the entirely blank line that terminates them.\r | |
238 | The (normally blank) line that ends the headers is skipped, but not\r | |
239 | included in the returned list. If a non-header line ends the headers,\r | |
240 | (which is an error), an attempt is made to backspace over it; it is\r | |
241 | never included in the returned list.\r | |
242 | \r | |
243 | The variable self.status is set to the empty string if all went well,\r | |
244 | otherwise it is an error message. The variable self.headers is a\r | |
245 | completely uninterpreted list of lines contained in the header (so\r | |
246 | printing them will reproduce the header exactly as it appears in the\r | |
247 | file).\r | |
248 | \r | |
249 | If multiple header fields with the same name occur, they are combined\r | |
250 | according to the rules in RFC 2616 sec 4.2:\r | |
251 | \r | |
252 | Appending each subsequent field-value to the first, each separated\r | |
253 | by a comma. The order in which header fields with the same field-name\r | |
254 | are received is significant to the interpretation of the combined\r | |
255 | field value.\r | |
256 | """\r | |
257 | # XXX The implementation overrides the readheaders() method of\r | |
258 | # rfc822.Message. The base class design isn't amenable to\r | |
259 | # customized behavior here so the method here is a copy of the\r | |
260 | # base class code with a few small changes.\r | |
261 | \r | |
262 | self.dict = {}\r | |
263 | self.unixfrom = ''\r | |
264 | self.headers = hlist = []\r | |
265 | self.status = ''\r | |
266 | headerseen = ""\r | |
267 | firstline = 1\r | |
268 | startofline = unread = tell = None\r | |
269 | if hasattr(self.fp, 'unread'):\r | |
270 | unread = self.fp.unread\r | |
271 | elif self.seekable:\r | |
272 | tell = self.fp.tell\r | |
273 | while True:\r | |
274 | if tell:\r | |
275 | try:\r | |
276 | startofline = tell()\r | |
277 | except IOError:\r | |
278 | startofline = tell = None\r | |
279 | self.seekable = 0\r | |
280 | line = self.fp.readline(_MAXLINE + 1)\r | |
281 | if len(line) > _MAXLINE:\r | |
282 | raise LineTooLong("header line")\r | |
283 | if not line:\r | |
284 | self.status = 'EOF in headers'\r | |
285 | break\r | |
286 | # Skip unix From name time lines\r | |
287 | if firstline and line.startswith('From '):\r | |
288 | self.unixfrom = self.unixfrom + line\r | |
289 | continue\r | |
290 | firstline = 0\r | |
291 | if headerseen and line[0] in ' \t':\r | |
292 | # XXX Not sure if continuation lines are handled properly\r | |
293 | # for http and/or for repeating headers\r | |
294 | # It's a continuation line.\r | |
295 | hlist.append(line)\r | |
296 | self.addcontinue(headerseen, line.strip())\r | |
297 | continue\r | |
298 | elif self.iscomment(line):\r | |
299 | # It's a comment. Ignore it.\r | |
300 | continue\r | |
301 | elif self.islast(line):\r | |
302 | # Note! No pushback here! The delimiter line gets eaten.\r | |
303 | break\r | |
304 | headerseen = self.isheader(line)\r | |
305 | if headerseen:\r | |
306 | # It's a legal header line, save it.\r | |
307 | hlist.append(line)\r | |
308 | self.addheader(headerseen, line[len(headerseen)+1:].strip())\r | |
309 | continue\r | |
310 | else:\r | |
311 | # It's not a header line; throw it back and stop here.\r | |
312 | if not self.dict:\r | |
313 | self.status = 'No headers'\r | |
314 | else:\r | |
315 | self.status = 'Non-header line where header expected'\r | |
316 | # Try to undo the read.\r | |
317 | if unread:\r | |
318 | unread(line)\r | |
319 | elif tell:\r | |
320 | self.fp.seek(startofline)\r | |
321 | else:\r | |
322 | self.status = self.status + '; bad seek'\r | |
323 | break\r | |
324 | \r | |
325 | class HTTPResponse:\r | |
326 | \r | |
327 | # strict: If true, raise BadStatusLine if the status line can't be\r | |
328 | # parsed as a valid HTTP/1.0 or 1.1 status line. By default it is\r | |
329 | # false because it prevents clients from talking to HTTP/0.9\r | |
330 | # servers. Note that a response with a sufficiently corrupted\r | |
331 | # status line will look like an HTTP/0.9 response.\r | |
332 | \r | |
333 | # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.\r | |
334 | \r | |
335 | def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False):\r | |
336 | if buffering:\r | |
337 | # The caller won't be using any sock.recv() calls, so buffering\r | |
338 | # is fine and recommended for performance.\r | |
339 | self.fp = sock.makefile('rb')\r | |
340 | else:\r | |
341 | # The buffer size is specified as zero, because the headers of\r | |
342 | # the response are read with readline(). If the reads were\r | |
343 | # buffered the readline() calls could consume some of the\r | |
344 | # response, which make be read via a recv() on the underlying\r | |
345 | # socket.\r | |
346 | self.fp = sock.makefile('rb', 0)\r | |
347 | self.debuglevel = debuglevel\r | |
348 | self.strict = strict\r | |
349 | self._method = method\r | |
350 | \r | |
351 | self.msg = None\r | |
352 | \r | |
353 | # from the Status-Line of the response\r | |
354 | self.version = _UNKNOWN # HTTP-Version\r | |
355 | self.status = _UNKNOWN # Status-Code\r | |
356 | self.reason = _UNKNOWN # Reason-Phrase\r | |
357 | \r | |
358 | self.chunked = _UNKNOWN # is "chunked" being used?\r | |
359 | self.chunk_left = _UNKNOWN # bytes left to read in current chunk\r | |
360 | self.length = _UNKNOWN # number of bytes left in response\r | |
361 | self.will_close = _UNKNOWN # conn will close at end of response\r | |
362 | \r | |
363 | def _read_status(self):\r | |
364 | # Initialize with Simple-Response defaults\r | |
365 | line = self.fp.readline()\r | |
366 | if self.debuglevel > 0:\r | |
367 | print "reply:", repr(line)\r | |
368 | if not line:\r | |
369 | # Presumably, the server closed the connection before\r | |
370 | # sending a valid response.\r | |
371 | raise BadStatusLine(line)\r | |
372 | try:\r | |
373 | [version, status, reason] = line.split(None, 2)\r | |
374 | except ValueError:\r | |
375 | try:\r | |
376 | [version, status] = line.split(None, 1)\r | |
377 | reason = ""\r | |
378 | except ValueError:\r | |
379 | # empty version will cause next test to fail and status\r | |
380 | # will be treated as 0.9 response.\r | |
381 | version = ""\r | |
382 | if not version.startswith('HTTP/'):\r | |
383 | if self.strict:\r | |
384 | self.close()\r | |
385 | raise BadStatusLine(line)\r | |
386 | else:\r | |
387 | # assume it's a Simple-Response from an 0.9 server\r | |
388 | self.fp = LineAndFileWrapper(line, self.fp)\r | |
389 | return "HTTP/0.9", 200, ""\r | |
390 | \r | |
391 | # The status code is a three-digit number\r | |
392 | try:\r | |
393 | status = int(status)\r | |
394 | if status < 100 or status > 999:\r | |
395 | raise BadStatusLine(line)\r | |
396 | except ValueError:\r | |
397 | raise BadStatusLine(line)\r | |
398 | return version, status, reason\r | |
399 | \r | |
400 | def begin(self):\r | |
401 | if self.msg is not None:\r | |
402 | # we've already started reading the response\r | |
403 | return\r | |
404 | \r | |
405 | # read until we get a non-100 response\r | |
406 | while True:\r | |
407 | version, status, reason = self._read_status()\r | |
408 | if status != CONTINUE:\r | |
409 | break\r | |
410 | # skip the header from the 100 response\r | |
411 | while True:\r | |
412 | skip = self.fp.readline(_MAXLINE + 1)\r | |
413 | if len(skip) > _MAXLINE:\r | |
414 | raise LineTooLong("header line")\r | |
415 | skip = skip.strip()\r | |
416 | if not skip:\r | |
417 | break\r | |
418 | if self.debuglevel > 0:\r | |
419 | print "header:", skip\r | |
420 | \r | |
421 | self.status = status\r | |
422 | self.reason = reason.strip()\r | |
423 | if version == 'HTTP/1.0':\r | |
424 | self.version = 10\r | |
425 | elif version.startswith('HTTP/1.'):\r | |
426 | self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1\r | |
427 | elif version == 'HTTP/0.9':\r | |
428 | self.version = 9\r | |
429 | else:\r | |
430 | raise UnknownProtocol(version)\r | |
431 | \r | |
432 | if self.version == 9:\r | |
433 | self.length = None\r | |
434 | self.chunked = 0\r | |
435 | self.will_close = 1\r | |
436 | self.msg = HTTPMessage(StringIO())\r | |
437 | return\r | |
438 | \r | |
439 | self.msg = HTTPMessage(self.fp, 0)\r | |
440 | if self.debuglevel > 0:\r | |
441 | for hdr in self.msg.headers:\r | |
442 | print "header:", hdr,\r | |
443 | \r | |
444 | # don't let the msg keep an fp\r | |
445 | self.msg.fp = None\r | |
446 | \r | |
447 | # are we using the chunked-style of transfer encoding?\r | |
448 | tr_enc = self.msg.getheader('transfer-encoding')\r | |
449 | if tr_enc and tr_enc.lower() == "chunked":\r | |
450 | self.chunked = 1\r | |
451 | self.chunk_left = None\r | |
452 | else:\r | |
453 | self.chunked = 0\r | |
454 | \r | |
455 | # will the connection close at the end of the response?\r | |
456 | self.will_close = self._check_close()\r | |
457 | \r | |
458 | # do we have a Content-Length?\r | |
459 | # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"\r | |
460 | length = self.msg.getheader('content-length')\r | |
461 | if length and not self.chunked:\r | |
462 | try:\r | |
463 | self.length = int(length)\r | |
464 | except ValueError:\r | |
465 | self.length = None\r | |
466 | else:\r | |
467 | if self.length < 0: # ignore nonsensical negative lengths\r | |
468 | self.length = None\r | |
469 | else:\r | |
470 | self.length = None\r | |
471 | \r | |
472 | # does the body have a fixed length? (of zero)\r | |
473 | if (status == NO_CONTENT or status == NOT_MODIFIED or\r | |
474 | 100 <= status < 200 or # 1xx codes\r | |
475 | self._method == 'HEAD'):\r | |
476 | self.length = 0\r | |
477 | \r | |
478 | # if the connection remains open, and we aren't using chunked, and\r | |
479 | # a content-length was not provided, then assume that the connection\r | |
480 | # WILL close.\r | |
481 | if not self.will_close and \\r | |
482 | not self.chunked and \\r | |
483 | self.length is None:\r | |
484 | self.will_close = 1\r | |
485 | \r | |
486 | def _check_close(self):\r | |
487 | conn = self.msg.getheader('connection')\r | |
488 | if self.version == 11:\r | |
489 | # An HTTP/1.1 proxy is assumed to stay open unless\r | |
490 | # explicitly closed.\r | |
491 | conn = self.msg.getheader('connection')\r | |
492 | if conn and "close" in conn.lower():\r | |
493 | return True\r | |
494 | return False\r | |
495 | \r | |
496 | # Some HTTP/1.0 implementations have support for persistent\r | |
497 | # connections, using rules different than HTTP/1.1.\r | |
498 | \r | |
499 | # For older HTTP, Keep-Alive indicates persistent connection.\r | |
500 | if self.msg.getheader('keep-alive'):\r | |
501 | return False\r | |
502 | \r | |
503 | # At least Akamai returns a "Connection: Keep-Alive" header,\r | |
504 | # which was supposed to be sent by the client.\r | |
505 | if conn and "keep-alive" in conn.lower():\r | |
506 | return False\r | |
507 | \r | |
508 | # Proxy-Connection is a netscape hack.\r | |
509 | pconn = self.msg.getheader('proxy-connection')\r | |
510 | if pconn and "keep-alive" in pconn.lower():\r | |
511 | return False\r | |
512 | \r | |
513 | # otherwise, assume it will close\r | |
514 | return True\r | |
515 | \r | |
516 | def close(self):\r | |
517 | if self.fp:\r | |
518 | self.fp.close()\r | |
519 | self.fp = None\r | |
520 | \r | |
521 | def isclosed(self):\r | |
522 | # NOTE: it is possible that we will not ever call self.close(). This\r | |
523 | # case occurs when will_close is TRUE, length is None, and we\r | |
524 | # read up to the last byte, but NOT past it.\r | |
525 | #\r | |
526 | # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be\r | |
527 | # called, meaning self.isclosed() is meaningful.\r | |
528 | return self.fp is None\r | |
529 | \r | |
530 | # XXX It would be nice to have readline and __iter__ for this, too.\r | |
531 | \r | |
532 | def read(self, amt=None):\r | |
533 | if self.fp is None:\r | |
534 | return ''\r | |
535 | \r | |
536 | if self._method == 'HEAD':\r | |
537 | self.close()\r | |
538 | return ''\r | |
539 | \r | |
540 | if self.chunked:\r | |
541 | return self._read_chunked(amt)\r | |
542 | \r | |
543 | if amt is None:\r | |
544 | # unbounded read\r | |
545 | if self.length is None:\r | |
546 | s = self.fp.read()\r | |
547 | else:\r | |
548 | s = self._safe_read(self.length)\r | |
549 | self.length = 0\r | |
550 | self.close() # we read everything\r | |
551 | return s\r | |
552 | \r | |
553 | if self.length is not None:\r | |
554 | if amt > self.length:\r | |
555 | # clip the read to the "end of response"\r | |
556 | amt = self.length\r | |
557 | \r | |
558 | # we do not use _safe_read() here because this may be a .will_close\r | |
559 | # connection, and the user is reading more bytes than will be provided\r | |
560 | # (for example, reading in 1k chunks)\r | |
561 | s = self.fp.read(amt)\r | |
562 | if self.length is not None:\r | |
563 | self.length -= len(s)\r | |
564 | if not self.length:\r | |
565 | self.close()\r | |
566 | return s\r | |
567 | \r | |
568 | def _read_chunked(self, amt):\r | |
569 | assert self.chunked != _UNKNOWN\r | |
570 | chunk_left = self.chunk_left\r | |
571 | value = []\r | |
572 | while True:\r | |
573 | if chunk_left is None:\r | |
574 | line = self.fp.readline(_MAXLINE + 1)\r | |
575 | if len(line) > _MAXLINE:\r | |
576 | raise LineTooLong("chunk size")\r | |
577 | i = line.find(';')\r | |
578 | if i >= 0:\r | |
579 | line = line[:i] # strip chunk-extensions\r | |
580 | try:\r | |
581 | chunk_left = int(line, 16)\r | |
582 | except ValueError:\r | |
583 | # close the connection as protocol synchronisation is\r | |
584 | # probably lost\r | |
585 | self.close()\r | |
586 | raise IncompleteRead(''.join(value))\r | |
587 | if chunk_left == 0:\r | |
588 | break\r | |
589 | if amt is None:\r | |
590 | value.append(self._safe_read(chunk_left))\r | |
591 | elif amt < chunk_left:\r | |
592 | value.append(self._safe_read(amt))\r | |
593 | self.chunk_left = chunk_left - amt\r | |
594 | return ''.join(value)\r | |
595 | elif amt == chunk_left:\r | |
596 | value.append(self._safe_read(amt))\r | |
597 | self._safe_read(2) # toss the CRLF at the end of the chunk\r | |
598 | self.chunk_left = None\r | |
599 | return ''.join(value)\r | |
600 | else:\r | |
601 | value.append(self._safe_read(chunk_left))\r | |
602 | amt -= chunk_left\r | |
603 | \r | |
604 | # we read the whole chunk, get another\r | |
605 | self._safe_read(2) # toss the CRLF at the end of the chunk\r | |
606 | chunk_left = None\r | |
607 | \r | |
608 | # read and discard trailer up to the CRLF terminator\r | |
609 | ### note: we shouldn't have any trailers!\r | |
610 | while True:\r | |
611 | line = self.fp.readline(_MAXLINE + 1)\r | |
612 | if len(line) > _MAXLINE:\r | |
613 | raise LineTooLong("trailer line")\r | |
614 | if not line:\r | |
615 | # a vanishingly small number of sites EOF without\r | |
616 | # sending the trailer\r | |
617 | break\r | |
618 | if line == '\r\n':\r | |
619 | break\r | |
620 | \r | |
621 | # we read everything; close the "file"\r | |
622 | self.close()\r | |
623 | \r | |
624 | return ''.join(value)\r | |
625 | \r | |
626 | def _safe_read(self, amt):\r | |
627 | """Read the number of bytes requested, compensating for partial reads.\r | |
628 | \r | |
629 | Normally, we have a blocking socket, but a read() can be interrupted\r | |
630 | by a signal (resulting in a partial read).\r | |
631 | \r | |
632 | Note that we cannot distinguish between EOF and an interrupt when zero\r | |
633 | bytes have been read. IncompleteRead() will be raised in this\r | |
634 | situation.\r | |
635 | \r | |
636 | This function should be used when <amt> bytes "should" be present for\r | |
637 | reading. If the bytes are truly not available (due to EOF), then the\r | |
638 | IncompleteRead exception can be used to detect the problem.\r | |
639 | """\r | |
640 | # NOTE(gps): As of svn r74426 socket._fileobject.read(x) will never\r | |
641 | # return less than x bytes unless EOF is encountered. It now handles\r | |
642 | # signal interruptions (socket.error EINTR) internally. This code\r | |
643 | # never caught that exception anyways. It seems largely pointless.\r | |
644 | # self.fp.read(amt) will work fine.\r | |
645 | s = []\r | |
646 | while amt > 0:\r | |
647 | chunk = self.fp.read(min(amt, MAXAMOUNT))\r | |
648 | if not chunk:\r | |
649 | raise IncompleteRead(''.join(s), amt)\r | |
650 | s.append(chunk)\r | |
651 | amt -= len(chunk)\r | |
652 | return ''.join(s)\r | |
653 | \r | |
654 | def fileno(self):\r | |
655 | return self.fp.fileno()\r | |
656 | \r | |
657 | def getheader(self, name, default=None):\r | |
658 | if self.msg is None:\r | |
659 | raise ResponseNotReady()\r | |
660 | return self.msg.getheader(name, default)\r | |
661 | \r | |
662 | def getheaders(self):\r | |
663 | """Return list of (header, value) tuples."""\r | |
664 | if self.msg is None:\r | |
665 | raise ResponseNotReady()\r | |
666 | return self.msg.items()\r | |
667 | \r | |
668 | \r | |
669 | class HTTPConnection:\r | |
670 | \r | |
671 | _http_vsn = 11\r | |
672 | _http_vsn_str = 'HTTP/1.1'\r | |
673 | \r | |
674 | response_class = HTTPResponse\r | |
675 | default_port = HTTP_PORT\r | |
676 | auto_open = 1\r | |
677 | debuglevel = 0\r | |
678 | strict = 0\r | |
679 | \r | |
680 | def __init__(self, host, port=None, strict=None,\r | |
681 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):\r | |
682 | self.timeout = timeout\r | |
683 | self.source_address = source_address\r | |
684 | self.sock = None\r | |
685 | self._buffer = []\r | |
686 | self.__response = None\r | |
687 | self.__state = _CS_IDLE\r | |
688 | self._method = None\r | |
689 | self._tunnel_host = None\r | |
690 | self._tunnel_port = None\r | |
691 | self._tunnel_headers = {}\r | |
692 | \r | |
693 | self._set_hostport(host, port)\r | |
694 | if strict is not None:\r | |
695 | self.strict = strict\r | |
696 | \r | |
697 | def set_tunnel(self, host, port=None, headers=None):\r | |
698 | """ Sets up the host and the port for the HTTP CONNECT Tunnelling.\r | |
699 | \r | |
700 | The headers argument should be a mapping of extra HTTP headers\r | |
701 | to send with the CONNECT request.\r | |
702 | """\r | |
703 | self._tunnel_host = host\r | |
704 | self._tunnel_port = port\r | |
705 | if headers:\r | |
706 | self._tunnel_headers = headers\r | |
707 | else:\r | |
708 | self._tunnel_headers.clear()\r | |
709 | \r | |
710 | def _set_hostport(self, host, port):\r | |
711 | if port is None:\r | |
712 | i = host.rfind(':')\r | |
713 | j = host.rfind(']') # ipv6 addresses have [...]\r | |
714 | if i > j:\r | |
715 | try:\r | |
716 | port = int(host[i+1:])\r | |
717 | except ValueError:\r | |
718 | raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])\r | |
719 | host = host[:i]\r | |
720 | else:\r | |
721 | port = self.default_port\r | |
722 | if host and host[0] == '[' and host[-1] == ']':\r | |
723 | host = host[1:-1]\r | |
724 | self.host = host\r | |
725 | self.port = port\r | |
726 | \r | |
727 | def set_debuglevel(self, level):\r | |
728 | self.debuglevel = level\r | |
729 | \r | |
730 | def _tunnel(self):\r | |
731 | self._set_hostport(self._tunnel_host, self._tunnel_port)\r | |
732 | self.send("CONNECT %s:%d HTTP/1.0\r\n" % (self.host, self.port))\r | |
733 | for header, value in self._tunnel_headers.iteritems():\r | |
734 | self.send("%s: %s\r\n" % (header, value))\r | |
735 | self.send("\r\n")\r | |
736 | response = self.response_class(self.sock, strict = self.strict,\r | |
737 | method = self._method)\r | |
738 | (version, code, message) = response._read_status()\r | |
739 | \r | |
740 | if code != 200:\r | |
741 | self.close()\r | |
742 | raise socket.error("Tunnel connection failed: %d %s" % (code,\r | |
743 | message.strip()))\r | |
744 | while True:\r | |
745 | line = response.fp.readline(_MAXLINE + 1)\r | |
746 | if len(line) > _MAXLINE:\r | |
747 | raise LineTooLong("header line")\r | |
748 | if line == '\r\n': break\r | |
749 | \r | |
750 | \r | |
751 | def connect(self):\r | |
752 | """Connect to the host and port specified in __init__."""\r | |
753 | self.sock = socket.create_connection((self.host,self.port),\r | |
754 | self.timeout, self.source_address)\r | |
755 | \r | |
756 | if self._tunnel_host:\r | |
757 | self._tunnel()\r | |
758 | \r | |
759 | def close(self):\r | |
760 | """Close the connection to the HTTP server."""\r | |
761 | if self.sock:\r | |
762 | self.sock.close() # close it manually... there may be other refs\r | |
763 | self.sock = None\r | |
764 | if self.__response:\r | |
765 | self.__response.close()\r | |
766 | self.__response = None\r | |
767 | self.__state = _CS_IDLE\r | |
768 | \r | |
769 | def send(self, data):\r | |
770 | """Send `data' to the server."""\r | |
771 | if self.sock is None:\r | |
772 | if self.auto_open:\r | |
773 | self.connect()\r | |
774 | else:\r | |
775 | raise NotConnected()\r | |
776 | \r | |
777 | if self.debuglevel > 0:\r | |
778 | print "send:", repr(data)\r | |
779 | blocksize = 8192\r | |
780 | if hasattr(data,'read') and not isinstance(data, array):\r | |
781 | if self.debuglevel > 0: print "sendIng a read()able"\r | |
782 | datablock = data.read(blocksize)\r | |
783 | while datablock:\r | |
784 | self.sock.sendall(datablock)\r | |
785 | datablock = data.read(blocksize)\r | |
786 | else:\r | |
787 | self.sock.sendall(data)\r | |
788 | \r | |
789 | def _output(self, s):\r | |
790 | """Add a line of output to the current request buffer.\r | |
791 | \r | |
792 | Assumes that the line does *not* end with \\r\\n.\r | |
793 | """\r | |
794 | self._buffer.append(s)\r | |
795 | \r | |
796 | def _send_output(self, message_body=None):\r | |
797 | """Send the currently buffered request and clear the buffer.\r | |
798 | \r | |
799 | Appends an extra \\r\\n to the buffer.\r | |
800 | A message_body may be specified, to be appended to the request.\r | |
801 | """\r | |
802 | self._buffer.extend(("", ""))\r | |
803 | msg = "\r\n".join(self._buffer)\r | |
804 | del self._buffer[:]\r | |
805 | # If msg and message_body are sent in a single send() call,\r | |
806 | # it will avoid performance problems caused by the interaction\r | |
807 | # between delayed ack and the Nagle algorithm.\r | |
808 | if isinstance(message_body, str):\r | |
809 | msg += message_body\r | |
810 | message_body = None\r | |
811 | self.send(msg)\r | |
812 | if message_body is not None:\r | |
813 | #message_body was not a string (i.e. it is a file) and\r | |
814 | #we must run the risk of Nagle\r | |
815 | self.send(message_body)\r | |
816 | \r | |
817 | def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):\r | |
818 | """Send a request to the server.\r | |
819 | \r | |
820 | `method' specifies an HTTP request method, e.g. 'GET'.\r | |
821 | `url' specifies the object being requested, e.g. '/index.html'.\r | |
822 | `skip_host' if True does not add automatically a 'Host:' header\r | |
823 | `skip_accept_encoding' if True does not add automatically an\r | |
824 | 'Accept-Encoding:' header\r | |
825 | """\r | |
826 | \r | |
827 | # if a prior response has been completed, then forget about it.\r | |
828 | if self.__response and self.__response.isclosed():\r | |
829 | self.__response = None\r | |
830 | \r | |
831 | \r | |
832 | # in certain cases, we cannot issue another request on this connection.\r | |
833 | # this occurs when:\r | |
834 | # 1) we are in the process of sending a request. (_CS_REQ_STARTED)\r | |
835 | # 2) a response to a previous request has signalled that it is going\r | |
836 | # to close the connection upon completion.\r | |
837 | # 3) the headers for the previous response have not been read, thus\r | |
838 | # we cannot determine whether point (2) is true. (_CS_REQ_SENT)\r | |
839 | #\r | |
840 | # if there is no prior response, then we can request at will.\r | |
841 | #\r | |
842 | # if point (2) is true, then we will have passed the socket to the\r | |
843 | # response (effectively meaning, "there is no prior response"), and\r | |
844 | # will open a new one when a new request is made.\r | |
845 | #\r | |
846 | # Note: if a prior response exists, then we *can* start a new request.\r | |
847 | # We are not allowed to begin fetching the response to this new\r | |
848 | # request, however, until that prior response is complete.\r | |
849 | #\r | |
850 | if self.__state == _CS_IDLE:\r | |
851 | self.__state = _CS_REQ_STARTED\r | |
852 | else:\r | |
853 | raise CannotSendRequest()\r | |
854 | \r | |
855 | # Save the method we use, we need it later in the response phase\r | |
856 | self._method = method\r | |
857 | if not url:\r | |
858 | url = '/'\r | |
859 | hdr = '%s %s %s' % (method, url, self._http_vsn_str)\r | |
860 | \r | |
861 | self._output(hdr)\r | |
862 | \r | |
863 | if self._http_vsn == 11:\r | |
864 | # Issue some standard headers for better HTTP/1.1 compliance\r | |
865 | \r | |
866 | if not skip_host:\r | |
867 | # this header is issued *only* for HTTP/1.1\r | |
868 | # connections. more specifically, this means it is\r | |
869 | # only issued when the client uses the new\r | |
870 | # HTTPConnection() class. backwards-compat clients\r | |
871 | # will be using HTTP/1.0 and those clients may be\r | |
872 | # issuing this header themselves. we should NOT issue\r | |
873 | # it twice; some web servers (such as Apache) barf\r | |
874 | # when they see two Host: headers\r | |
875 | \r | |
876 | # If we need a non-standard port,include it in the\r | |
877 | # header. If the request is going through a proxy,\r | |
878 | # but the host of the actual URL, not the host of the\r | |
879 | # proxy.\r | |
880 | \r | |
881 | netloc = ''\r | |
882 | if url.startswith('http'):\r | |
883 | nil, netloc, nil, nil, nil = urlsplit(url)\r | |
884 | \r | |
885 | if netloc:\r | |
886 | try:\r | |
887 | netloc_enc = netloc.encode("ascii")\r | |
888 | except UnicodeEncodeError:\r | |
889 | netloc_enc = netloc.encode("idna")\r | |
890 | self.putheader('Host', netloc_enc)\r | |
891 | else:\r | |
892 | try:\r | |
893 | host_enc = self.host.encode("ascii")\r | |
894 | except UnicodeEncodeError:\r | |
895 | host_enc = self.host.encode("idna")\r | |
896 | # Wrap the IPv6 Host Header with [] (RFC 2732)\r | |
897 | if host_enc.find(':') >= 0:\r | |
898 | host_enc = "[" + host_enc + "]"\r | |
899 | if self.port == self.default_port:\r | |
900 | self.putheader('Host', host_enc)\r | |
901 | else:\r | |
902 | self.putheader('Host', "%s:%s" % (host_enc, self.port))\r | |
903 | \r | |
904 | # note: we are assuming that clients will not attempt to set these\r | |
905 | # headers since *this* library must deal with the\r | |
906 | # consequences. this also means that when the supporting\r | |
907 | # libraries are updated to recognize other forms, then this\r | |
908 | # code should be changed (removed or updated).\r | |
909 | \r | |
910 | # we only want a Content-Encoding of "identity" since we don't\r | |
911 | # support encodings such as x-gzip or x-deflate.\r | |
912 | if not skip_accept_encoding:\r | |
913 | self.putheader('Accept-Encoding', 'identity')\r | |
914 | \r | |
915 | # we can accept "chunked" Transfer-Encodings, but no others\r | |
916 | # NOTE: no TE header implies *only* "chunked"\r | |
917 | #self.putheader('TE', 'chunked')\r | |
918 | \r | |
919 | # if TE is supplied in the header, then it must appear in a\r | |
920 | # Connection header.\r | |
921 | #self.putheader('Connection', 'TE')\r | |
922 | \r | |
923 | else:\r | |
924 | # For HTTP/1.0, the server will assume "not chunked"\r | |
925 | pass\r | |
926 | \r | |
927 | def putheader(self, header, *values):\r | |
928 | """Send a request header line to the server.\r | |
929 | \r | |
930 | For example: h.putheader('Accept', 'text/html')\r | |
931 | """\r | |
932 | if self.__state != _CS_REQ_STARTED:\r | |
933 | raise CannotSendHeader()\r | |
934 | \r | |
935 | hdr = '%s: %s' % (header, '\r\n\t'.join([str(v) for v in values]))\r | |
936 | self._output(hdr)\r | |
937 | \r | |
938 | def endheaders(self, message_body=None):\r | |
939 | """Indicate that the last header line has been sent to the server.\r | |
940 | \r | |
941 | This method sends the request to the server. The optional\r | |
942 | message_body argument can be used to pass message body\r | |
943 | associated with the request. The message body will be sent in\r | |
944 | the same packet as the message headers if possible. The\r | |
945 | message_body should be a string.\r | |
946 | """\r | |
947 | if self.__state == _CS_REQ_STARTED:\r | |
948 | self.__state = _CS_REQ_SENT\r | |
949 | else:\r | |
950 | raise CannotSendHeader()\r | |
951 | self._send_output(message_body)\r | |
952 | \r | |
953 | def request(self, method, url, body=None, headers={}):\r | |
954 | """Send a complete request to the server."""\r | |
955 | self._send_request(method, url, body, headers)\r | |
956 | \r | |
957 | def _set_content_length(self, body):\r | |
958 | # Set the content-length based on the body.\r | |
959 | thelen = None\r | |
960 | try:\r | |
961 | thelen = str(len(body))\r | |
962 | except TypeError, te:\r | |
963 | # If this is a file-like object, try to\r | |
964 | # fstat its file descriptor\r | |
965 | try:\r | |
966 | thelen = str(os.fstat(body.fileno()).st_size)\r | |
967 | except (AttributeError, OSError):\r | |
968 | # Don't send a length if this failed\r | |
969 | if self.debuglevel > 0: print "Cannot stat!!"\r | |
970 | \r | |
971 | if thelen is not None:\r | |
972 | self.putheader('Content-Length', thelen)\r | |
973 | \r | |
974 | def _send_request(self, method, url, body, headers):\r | |
975 | # Honor explicitly requested Host: and Accept-Encoding: headers.\r | |
976 | header_names = dict.fromkeys([k.lower() for k in headers])\r | |
977 | skips = {}\r | |
978 | if 'host' in header_names:\r | |
979 | skips['skip_host'] = 1\r | |
980 | if 'accept-encoding' in header_names:\r | |
981 | skips['skip_accept_encoding'] = 1\r | |
982 | \r | |
983 | self.putrequest(method, url, **skips)\r | |
984 | \r | |
985 | if body and ('content-length' not in header_names):\r | |
986 | self._set_content_length(body)\r | |
987 | for hdr, value in headers.iteritems():\r | |
988 | self.putheader(hdr, value)\r | |
989 | self.endheaders(body)\r | |
990 | \r | |
991 | def getresponse(self, buffering=False):\r | |
992 | "Get the response from the server."\r | |
993 | \r | |
994 | # if a prior response has been completed, then forget about it.\r | |
995 | if self.__response and self.__response.isclosed():\r | |
996 | self.__response = None\r | |
997 | \r | |
998 | #\r | |
999 | # if a prior response exists, then it must be completed (otherwise, we\r | |
1000 | # cannot read this response's header to determine the connection-close\r | |
1001 | # behavior)\r | |
1002 | #\r | |
1003 | # note: if a prior response existed, but was connection-close, then the\r | |
1004 | # socket and response were made independent of this HTTPConnection\r | |
1005 | # object since a new request requires that we open a whole new\r | |
1006 | # connection\r | |
1007 | #\r | |
1008 | # this means the prior response had one of two states:\r | |
1009 | # 1) will_close: this connection was reset and the prior socket and\r | |
1010 | # response operate independently\r | |
1011 | # 2) persistent: the response was retained and we await its\r | |
1012 | # isclosed() status to become true.\r | |
1013 | #\r | |
1014 | if self.__state != _CS_REQ_SENT or self.__response:\r | |
1015 | raise ResponseNotReady()\r | |
1016 | \r | |
1017 | args = (self.sock,)\r | |
1018 | kwds = {"strict":self.strict, "method":self._method}\r | |
1019 | if self.debuglevel > 0:\r | |
1020 | args += (self.debuglevel,)\r | |
1021 | if buffering:\r | |
1022 | #only add this keyword if non-default, for compatibility with\r | |
1023 | #other response_classes.\r | |
1024 | kwds["buffering"] = True;\r | |
1025 | response = self.response_class(*args, **kwds)\r | |
1026 | \r | |
1027 | response.begin()\r | |
1028 | assert response.will_close != _UNKNOWN\r | |
1029 | self.__state = _CS_IDLE\r | |
1030 | \r | |
1031 | if response.will_close:\r | |
1032 | # this effectively passes the connection to the response\r | |
1033 | self.close()\r | |
1034 | else:\r | |
1035 | # remember this, so we can tell when it is complete\r | |
1036 | self.__response = response\r | |
1037 | \r | |
1038 | return response\r | |
1039 | \r | |
1040 | \r | |
1041 | class HTTP:\r | |
1042 | "Compatibility class with httplib.py from 1.5."\r | |
1043 | \r | |
1044 | _http_vsn = 10\r | |
1045 | _http_vsn_str = 'HTTP/1.0'\r | |
1046 | \r | |
1047 | debuglevel = 0\r | |
1048 | \r | |
1049 | _connection_class = HTTPConnection\r | |
1050 | \r | |
1051 | def __init__(self, host='', port=None, strict=None):\r | |
1052 | "Provide a default host, since the superclass requires one."\r | |
1053 | \r | |
1054 | # some joker passed 0 explicitly, meaning default port\r | |
1055 | if port == 0:\r | |
1056 | port = None\r | |
1057 | \r | |
1058 | # Note that we may pass an empty string as the host; this will throw\r | |
1059 | # an error when we attempt to connect. Presumably, the client code\r | |
1060 | # will call connect before then, with a proper host.\r | |
1061 | self._setup(self._connection_class(host, port, strict))\r | |
1062 | \r | |
1063 | def _setup(self, conn):\r | |
1064 | self._conn = conn\r | |
1065 | \r | |
1066 | # set up delegation to flesh out interface\r | |
1067 | self.send = conn.send\r | |
1068 | self.putrequest = conn.putrequest\r | |
1069 | self.putheader = conn.putheader\r | |
1070 | self.endheaders = conn.endheaders\r | |
1071 | self.set_debuglevel = conn.set_debuglevel\r | |
1072 | \r | |
1073 | conn._http_vsn = self._http_vsn\r | |
1074 | conn._http_vsn_str = self._http_vsn_str\r | |
1075 | \r | |
1076 | self.file = None\r | |
1077 | \r | |
1078 | def connect(self, host=None, port=None):\r | |
1079 | "Accept arguments to set the host/port, since the superclass doesn't."\r | |
1080 | \r | |
1081 | if host is not None:\r | |
1082 | self._conn._set_hostport(host, port)\r | |
1083 | self._conn.connect()\r | |
1084 | \r | |
1085 | def getfile(self):\r | |
1086 | "Provide a getfile, since the superclass' does not use this concept."\r | |
1087 | return self.file\r | |
1088 | \r | |
1089 | def getreply(self, buffering=False):\r | |
1090 | """Compat definition since superclass does not define it.\r | |
1091 | \r | |
1092 | Returns a tuple consisting of:\r | |
1093 | - server status code (e.g. '200' if all goes well)\r | |
1094 | - server "reason" corresponding to status code\r | |
1095 | - any RFC822 headers in the response from the server\r | |
1096 | """\r | |
1097 | try:\r | |
1098 | if not buffering:\r | |
1099 | response = self._conn.getresponse()\r | |
1100 | else:\r | |
1101 | #only add this keyword if non-default for compatibility\r | |
1102 | #with other connection classes\r | |
1103 | response = self._conn.getresponse(buffering)\r | |
1104 | except BadStatusLine, e:\r | |
1105 | ### hmm. if getresponse() ever closes the socket on a bad request,\r | |
1106 | ### then we are going to have problems with self.sock\r | |
1107 | \r | |
1108 | ### should we keep this behavior? do people use it?\r | |
1109 | # keep the socket open (as a file), and return it\r | |
1110 | self.file = self._conn.sock.makefile('rb', 0)\r | |
1111 | \r | |
1112 | # close our socket -- we want to restart after any protocol error\r | |
1113 | self.close()\r | |
1114 | \r | |
1115 | self.headers = None\r | |
1116 | return -1, e.line, None\r | |
1117 | \r | |
1118 | self.headers = response.msg\r | |
1119 | self.file = response.fp\r | |
1120 | return response.status, response.reason, response.msg\r | |
1121 | \r | |
1122 | def close(self):\r | |
1123 | self._conn.close()\r | |
1124 | \r | |
1125 | # note that self.file == response.fp, which gets closed by the\r | |
1126 | # superclass. just clear the object ref here.\r | |
1127 | ### hmm. messy. if status==-1, then self.file is owned by us.\r | |
1128 | ### well... we aren't explicitly closing, but losing this ref will\r | |
1129 | ### do it\r | |
1130 | self.file = None\r | |
1131 | \r | |
1132 | try:\r | |
1133 | import ssl\r | |
1134 | except ImportError:\r | |
1135 | pass\r | |
1136 | else:\r | |
1137 | class HTTPSConnection(HTTPConnection):\r | |
1138 | "This class allows communication via SSL."\r | |
1139 | \r | |
1140 | default_port = HTTPS_PORT\r | |
1141 | \r | |
1142 | def __init__(self, host, port=None, key_file=None, cert_file=None,\r | |
1143 | strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,\r | |
1144 | source_address=None):\r | |
1145 | HTTPConnection.__init__(self, host, port, strict, timeout,\r | |
1146 | source_address)\r | |
1147 | self.key_file = key_file\r | |
1148 | self.cert_file = cert_file\r | |
1149 | \r | |
1150 | def connect(self):\r | |
1151 | "Connect to a host on a given (SSL) port."\r | |
1152 | \r | |
1153 | sock = socket.create_connection((self.host, self.port),\r | |
1154 | self.timeout, self.source_address)\r | |
1155 | if self._tunnel_host:\r | |
1156 | self.sock = sock\r | |
1157 | self._tunnel()\r | |
1158 | self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)\r | |
1159 | \r | |
1160 | __all__.append("HTTPSConnection")\r | |
1161 | \r | |
1162 | class HTTPS(HTTP):\r | |
1163 | """Compatibility with 1.5 httplib interface\r | |
1164 | \r | |
1165 | Python 1.5.2 did not have an HTTPS class, but it defined an\r | |
1166 | interface for sending http requests that is also useful for\r | |
1167 | https.\r | |
1168 | """\r | |
1169 | \r | |
1170 | _connection_class = HTTPSConnection\r | |
1171 | \r | |
1172 | def __init__(self, host='', port=None, key_file=None, cert_file=None,\r | |
1173 | strict=None):\r | |
1174 | # provide a default host, pass the X509 cert info\r | |
1175 | \r | |
1176 | # urf. compensate for bad input.\r | |
1177 | if port == 0:\r | |
1178 | port = None\r | |
1179 | self._setup(self._connection_class(host, port, key_file,\r | |
1180 | cert_file, strict))\r | |
1181 | \r | |
1182 | # we never actually use these for anything, but we keep them\r | |
1183 | # here for compatibility with post-1.5.2 CVS.\r | |
1184 | self.key_file = key_file\r | |
1185 | self.cert_file = cert_file\r | |
1186 | \r | |
1187 | \r | |
1188 | def FakeSocket (sock, sslobj):\r | |
1189 | warnings.warn("FakeSocket is deprecated, and won't be in 3.x. " +\r | |
1190 | "Use the result of ssl.wrap_socket() directly instead.",\r | |
1191 | DeprecationWarning, stacklevel=2)\r | |
1192 | return sslobj\r | |
1193 | \r | |
1194 | \r | |
1195 | class HTTPException(Exception):\r | |
1196 | # Subclasses that define an __init__ must call Exception.__init__\r | |
1197 | # or define self.args. Otherwise, str() will fail.\r | |
1198 | pass\r | |
1199 | \r | |
1200 | class NotConnected(HTTPException):\r | |
1201 | pass\r | |
1202 | \r | |
1203 | class InvalidURL(HTTPException):\r | |
1204 | pass\r | |
1205 | \r | |
1206 | class UnknownProtocol(HTTPException):\r | |
1207 | def __init__(self, version):\r | |
1208 | self.args = version,\r | |
1209 | self.version = version\r | |
1210 | \r | |
1211 | class UnknownTransferEncoding(HTTPException):\r | |
1212 | pass\r | |
1213 | \r | |
1214 | class UnimplementedFileMode(HTTPException):\r | |
1215 | pass\r | |
1216 | \r | |
1217 | class IncompleteRead(HTTPException):\r | |
1218 | def __init__(self, partial, expected=None):\r | |
1219 | self.args = partial,\r | |
1220 | self.partial = partial\r | |
1221 | self.expected = expected\r | |
1222 | def __repr__(self):\r | |
1223 | if self.expected is not None:\r | |
1224 | e = ', %i more expected' % self.expected\r | |
1225 | else:\r | |
1226 | e = ''\r | |
1227 | return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e)\r | |
1228 | def __str__(self):\r | |
1229 | return repr(self)\r | |
1230 | \r | |
1231 | class ImproperConnectionState(HTTPException):\r | |
1232 | pass\r | |
1233 | \r | |
1234 | class CannotSendRequest(ImproperConnectionState):\r | |
1235 | pass\r | |
1236 | \r | |
1237 | class CannotSendHeader(ImproperConnectionState):\r | |
1238 | pass\r | |
1239 | \r | |
1240 | class ResponseNotReady(ImproperConnectionState):\r | |
1241 | pass\r | |
1242 | \r | |
1243 | class BadStatusLine(HTTPException):\r | |
1244 | def __init__(self, line):\r | |
1245 | if not line:\r | |
1246 | line = repr(line)\r | |
1247 | self.args = line,\r | |
1248 | self.line = line\r | |
1249 | \r | |
1250 | class LineTooLong(HTTPException):\r | |
1251 | def __init__(self, line_type):\r | |
1252 | HTTPException.__init__(self, "got more than %d bytes when reading %s"\r | |
1253 | % (_MAXLINE, line_type))\r | |
1254 | \r | |
1255 | # for backwards compatibility\r | |
1256 | error = HTTPException\r | |
1257 | \r | |
1258 | class LineAndFileWrapper:\r | |
1259 | """A limited file-like object for HTTP/0.9 responses."""\r | |
1260 | \r | |
1261 | # The status-line parsing code calls readline(), which normally\r | |
1262 | # get the HTTP status line. For a 0.9 response, however, this is\r | |
1263 | # actually the first line of the body! Clients need to get a\r | |
1264 | # readable file object that contains that line.\r | |
1265 | \r | |
1266 | def __init__(self, line, file):\r | |
1267 | self._line = line\r | |
1268 | self._file = file\r | |
1269 | self._line_consumed = 0\r | |
1270 | self._line_offset = 0\r | |
1271 | self._line_left = len(line)\r | |
1272 | \r | |
1273 | def __getattr__(self, attr):\r | |
1274 | return getattr(self._file, attr)\r | |
1275 | \r | |
1276 | def _done(self):\r | |
1277 | # called when the last byte is read from the line. After the\r | |
1278 | # call, all read methods are delegated to the underlying file\r | |
1279 | # object.\r | |
1280 | self._line_consumed = 1\r | |
1281 | self.read = self._file.read\r | |
1282 | self.readline = self._file.readline\r | |
1283 | self.readlines = self._file.readlines\r | |
1284 | \r | |
1285 | def read(self, amt=None):\r | |
1286 | if self._line_consumed:\r | |
1287 | return self._file.read(amt)\r | |
1288 | assert self._line_left\r | |
1289 | if amt is None or amt > self._line_left:\r | |
1290 | s = self._line[self._line_offset:]\r | |
1291 | self._done()\r | |
1292 | if amt is None:\r | |
1293 | return s + self._file.read()\r | |
1294 | else:\r | |
1295 | return s + self._file.read(amt - len(s))\r | |
1296 | else:\r | |
1297 | assert amt <= self._line_left\r | |
1298 | i = self._line_offset\r | |
1299 | j = i + amt\r | |
1300 | s = self._line[i:j]\r | |
1301 | self._line_offset = j\r | |
1302 | self._line_left -= amt\r | |
1303 | if self._line_left == 0:\r | |
1304 | self._done()\r | |
1305 | return s\r | |
1306 | \r | |
1307 | def readline(self):\r | |
1308 | if self._line_consumed:\r | |
1309 | return self._file.readline()\r | |
1310 | assert self._line_left\r | |
1311 | s = self._line[self._line_offset:]\r | |
1312 | self._done()\r | |
1313 | return s\r | |
1314 | \r | |
1315 | def readlines(self, size=None):\r | |
1316 | if self._line_consumed:\r | |
1317 | return self._file.readlines(size)\r | |
1318 | assert self._line_left\r | |
1319 | L = [self._line[self._line_offset:]]\r | |
1320 | self._done()\r | |
1321 | if size is None:\r | |
1322 | return L + self._file.readlines()\r | |
1323 | else:\r | |
1324 | return L + self._file.readlines(size)\r | |
1325 | \r | |
1326 | def test():\r | |
1327 | """Test this module.\r | |
1328 | \r | |
1329 | A hodge podge of tests collected here, because they have too many\r | |
1330 | external dependencies for the regular test suite.\r | |
1331 | """\r | |
1332 | \r | |
1333 | import sys\r | |
1334 | import getopt\r | |
1335 | opts, args = getopt.getopt(sys.argv[1:], 'd')\r | |
1336 | dl = 0\r | |
1337 | for o, a in opts:\r | |
1338 | if o == '-d': dl = dl + 1\r | |
1339 | host = 'www.python.org'\r | |
1340 | selector = '/'\r | |
1341 | if args[0:]: host = args[0]\r | |
1342 | if args[1:]: selector = args[1]\r | |
1343 | h = HTTP()\r | |
1344 | h.set_debuglevel(dl)\r | |
1345 | h.connect(host)\r | |
1346 | h.putrequest('GET', selector)\r | |
1347 | h.endheaders()\r | |
1348 | status, reason, headers = h.getreply()\r | |
1349 | print 'status =', status\r | |
1350 | print 'reason =', reason\r | |
1351 | print "read", len(h.getfile().read())\r | |
1352 | print\r | |
1353 | if headers:\r | |
1354 | for header in headers.headers: print header.strip()\r | |
1355 | print\r | |
1356 | \r | |
1357 | # minimal test that code to extract host from url works\r | |
1358 | class HTTP11(HTTP):\r | |
1359 | _http_vsn = 11\r | |
1360 | _http_vsn_str = 'HTTP/1.1'\r | |
1361 | \r | |
1362 | h = HTTP11('www.python.org')\r | |
1363 | h.putrequest('GET', 'http://www.python.org/~jeremy/')\r | |
1364 | h.endheaders()\r | |
1365 | h.getreply()\r | |
1366 | h.close()\r | |
1367 | \r | |
1368 | try:\r | |
1369 | import ssl\r | |
1370 | except ImportError:\r | |
1371 | pass\r | |
1372 | else:\r | |
1373 | \r | |
1374 | for host, selector in (('sourceforge.net', '/projects/python'),\r | |
1375 | ):\r | |
1376 | print "https://%s%s" % (host, selector)\r | |
1377 | hs = HTTPS()\r | |
1378 | hs.set_debuglevel(dl)\r | |
1379 | hs.connect(host)\r | |
1380 | hs.putrequest('GET', selector)\r | |
1381 | hs.endheaders()\r | |
1382 | status, reason, headers = hs.getreply()\r | |
1383 | print 'status =', status\r | |
1384 | print 'reason =', reason\r | |
1385 | print "read", len(hs.getfile().read())\r | |
1386 | print\r | |
1387 | if headers:\r | |
1388 | for header in headers.headers: print header.strip()\r | |
1389 | print\r | |
1390 | \r | |
1391 | if __name__ == '__main__':\r | |
1392 | test()\r |