blob: 76f7203d093a4e741d45a68b9c235bc716f74838 [file] [log] [blame]
Greg Stein5e0fa402000-06-26 08:28:01 +00001"""HTTP/1.1 client library
Guido van Rossum41999c11997-12-09 00:12:23 +00002
Greg Stein5e0fa402000-06-26 08:28:01 +00003<intro stuff goes here>
4<other stuff, too>
Guido van Rossum41999c11997-12-09 00:12:23 +00005
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006HTTPConnection goes through a number of "states", which define when a client
Greg Stein5e0fa402000-06-26 08:28:01 +00007may legally make another request or fetch the response for a particular
8request. This diagram details these state transitions:
Guido van Rossum41999c11997-12-09 00:12:23 +00009
Greg Stein5e0fa402000-06-26 08:28:01 +000010 (null)
11 |
12 | HTTPConnection()
13 v
14 Idle
15 |
16 | putrequest()
17 v
18 Request-started
19 |
20 | ( putheader() )* endheaders()
21 v
22 Request-sent
23 |
24 | response = getresponse()
25 v
26 Unread-response [Response-headers-read]
27 |\____________________
Tim Peters5ceadc82001-01-13 19:16:21 +000028 | |
29 | response.read() | putrequest()
30 v v
31 Idle Req-started-unread-response
32 ______/|
33 / |
34 response.read() | | ( putheader() )* endheaders()
35 v v
36 Request-started Req-sent-unread-response
37 |
38 | response.read()
39 v
40 Request-sent
Greg Stein5e0fa402000-06-26 08:28:01 +000041
42This diagram presents the following rules:
43 -- a second request may not be started until {response-headers-read}
44 -- a response [object] cannot be retrieved until {request-sent}
45 -- there is no differentiation between an unread response body and a
46 partially read response body
47
48Note: this enforcement is applied by the HTTPConnection class. The
49 HTTPResponse class does not enforce this state machine, which
50 implies sophisticated clients may accelerate the request/response
51 pipeline. Caution should be taken, though: accelerating the states
52 beyond the above pattern may imply knowledge of the server's
53 connection-close behavior for certain requests. For example, it
54 is impossible to tell whether the server will close the connection
55 UNTIL the response headers have been read; this means that further
56 requests cannot be placed into the pipeline until it is known that
57 the server will NOT be closing the connection.
58
59Logical State __state __response
60------------- ------- ----------
61Idle _CS_IDLE None
62Request-started _CS_REQ_STARTED None
63Request-sent _CS_REQ_SENT None
64Unread-response _CS_IDLE <response_class>
65Req-started-unread-response _CS_REQ_STARTED <response_class>
66Req-sent-unread-response _CS_REQ_SENT <response_class>
Guido van Rossum41999c11997-12-09 00:12:23 +000067"""
Guido van Rossum23acc951994-02-21 16:36:04 +000068
Jeremy Hylton6459c8d2001-10-11 17:47:22 +000069import errno
Jeremy Hyltonfb35f652007-08-03 20:30:33 +000070import io
Guido van Rossum65ab98c1995-08-07 20:13:02 +000071import mimetools
Jeremy Hylton6459c8d2001-10-11 17:47:22 +000072import socket
Jeremy Hylton8acf1e02002-03-08 19:35:51 +000073from urlparse import urlsplit
Guido van Rossum23acc951994-02-21 16:36:04 +000074
Guido van Rossum68937b42007-05-18 00:51:22 +000075from io import StringIO
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000076
Skip Montanaro951a8842001-06-01 16:25:38 +000077__all__ = ["HTTP", "HTTPResponse", "HTTPConnection", "HTTPSConnection",
78 "HTTPException", "NotConnected", "UnknownProtocol",
Jeremy Hylton7c75c992002-06-28 23:38:14 +000079 "UnknownTransferEncoding", "UnimplementedFileMode",
80 "IncompleteRead", "InvalidURL", "ImproperConnectionState",
81 "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
Georg Brandl6aab16e2006-02-17 19:17:25 +000082 "BadStatusLine", "error", "responses"]
Skip Montanaro2dd42762001-01-23 15:35:05 +000083
Guido van Rossum23acc951994-02-21 16:36:04 +000084HTTP_PORT = 80
Guido van Rossum09c8b6c1999-12-07 21:37:17 +000085HTTPS_PORT = 443
86
Greg Stein5e0fa402000-06-26 08:28:01 +000087_UNKNOWN = 'UNKNOWN'
88
89# connection states
90_CS_IDLE = 'Idle'
91_CS_REQ_STARTED = 'Request-started'
92_CS_REQ_SENT = 'Request-sent'
93
Martin v. Löwis39a31782004-09-18 09:03:49 +000094# status codes
95# informational
96CONTINUE = 100
97SWITCHING_PROTOCOLS = 101
98PROCESSING = 102
99
100# successful
101OK = 200
102CREATED = 201
103ACCEPTED = 202
104NON_AUTHORITATIVE_INFORMATION = 203
105NO_CONTENT = 204
106RESET_CONTENT = 205
107PARTIAL_CONTENT = 206
108MULTI_STATUS = 207
109IM_USED = 226
110
111# redirection
112MULTIPLE_CHOICES = 300
113MOVED_PERMANENTLY = 301
114FOUND = 302
115SEE_OTHER = 303
116NOT_MODIFIED = 304
117USE_PROXY = 305
118TEMPORARY_REDIRECT = 307
119
120# client error
121BAD_REQUEST = 400
122UNAUTHORIZED = 401
123PAYMENT_REQUIRED = 402
124FORBIDDEN = 403
125NOT_FOUND = 404
126METHOD_NOT_ALLOWED = 405
127NOT_ACCEPTABLE = 406
128PROXY_AUTHENTICATION_REQUIRED = 407
129REQUEST_TIMEOUT = 408
130CONFLICT = 409
131GONE = 410
132LENGTH_REQUIRED = 411
133PRECONDITION_FAILED = 412
134REQUEST_ENTITY_TOO_LARGE = 413
135REQUEST_URI_TOO_LONG = 414
136UNSUPPORTED_MEDIA_TYPE = 415
137REQUESTED_RANGE_NOT_SATISFIABLE = 416
138EXPECTATION_FAILED = 417
139UNPROCESSABLE_ENTITY = 422
140LOCKED = 423
141FAILED_DEPENDENCY = 424
142UPGRADE_REQUIRED = 426
143
144# server error
145INTERNAL_SERVER_ERROR = 500
146NOT_IMPLEMENTED = 501
147BAD_GATEWAY = 502
148SERVICE_UNAVAILABLE = 503
149GATEWAY_TIMEOUT = 504
150HTTP_VERSION_NOT_SUPPORTED = 505
151INSUFFICIENT_STORAGE = 507
152NOT_EXTENDED = 510
153
Georg Brandl6aab16e2006-02-17 19:17:25 +0000154# Mapping status codes to official W3C names
155responses = {
156 100: 'Continue',
157 101: 'Switching Protocols',
158
159 200: 'OK',
160 201: 'Created',
161 202: 'Accepted',
162 203: 'Non-Authoritative Information',
163 204: 'No Content',
164 205: 'Reset Content',
165 206: 'Partial Content',
166
167 300: 'Multiple Choices',
168 301: 'Moved Permanently',
169 302: 'Found',
170 303: 'See Other',
171 304: 'Not Modified',
172 305: 'Use Proxy',
173 306: '(Unused)',
174 307: 'Temporary Redirect',
175
176 400: 'Bad Request',
177 401: 'Unauthorized',
178 402: 'Payment Required',
179 403: 'Forbidden',
180 404: 'Not Found',
181 405: 'Method Not Allowed',
182 406: 'Not Acceptable',
183 407: 'Proxy Authentication Required',
184 408: 'Request Timeout',
185 409: 'Conflict',
186 410: 'Gone',
187 411: 'Length Required',
188 412: 'Precondition Failed',
189 413: 'Request Entity Too Large',
190 414: 'Request-URI Too Long',
191 415: 'Unsupported Media Type',
192 416: 'Requested Range Not Satisfiable',
193 417: 'Expectation Failed',
194
195 500: 'Internal Server Error',
196 501: 'Not Implemented',
197 502: 'Bad Gateway',
198 503: 'Service Unavailable',
199 504: 'Gateway Timeout',
200 505: 'HTTP Version Not Supported',
201}
202
Georg Brandl80ba8e82005-09-29 20:16:07 +0000203# maximal amount of data to read at one time in _safe_read
204MAXAMOUNT = 1048576
205
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000206class HTTPMessage(mimetools.Message):
207
208 def addheader(self, key, value):
209 """Add header for field key handling repeats."""
210 prev = self.dict.get(key)
211 if prev is None:
212 self.dict[key] = value
213 else:
214 combined = ", ".join((prev, value))
215 self.dict[key] = combined
216
217 def addcontinue(self, key, more):
218 """Add more field data from a continuation line."""
219 prev = self.dict[key]
220 self.dict[key] = prev + "\n " + more
221
222 def readheaders(self):
223 """Read header lines.
224
225 Read header lines up to the entirely blank line that terminates them.
226 The (normally blank) line that ends the headers is skipped, but not
227 included in the returned list. If a non-header line ends the headers,
228 (which is an error), an attempt is made to backspace over it; it is
229 never included in the returned list.
230
231 The variable self.status is set to the empty string if all went well,
232 otherwise it is an error message. The variable self.headers is a
233 completely uninterpreted list of lines contained in the header (so
234 printing them will reproduce the header exactly as it appears in the
235 file).
236
237 If multiple header fields with the same name occur, they are combined
238 according to the rules in RFC 2616 sec 4.2:
239
240 Appending each subsequent field-value to the first, each separated
241 by a comma. The order in which header fields with the same field-name
242 are received is significant to the interpretation of the combined
243 field value.
244 """
245 # XXX The implementation overrides the readheaders() method of
246 # rfc822.Message. The base class design isn't amenable to
247 # customized behavior here so the method here is a copy of the
248 # base class code with a few small changes.
249
250 self.dict = {}
251 self.unixfrom = ''
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000252 self.headers = hlist = []
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000253 self.status = ''
254 headerseen = ""
255 firstline = 1
256 startofline = unread = tell = None
257 if hasattr(self.fp, 'unread'):
258 unread = self.fp.unread
259 elif self.seekable:
260 tell = self.fp.tell
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000261 while True:
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000262 if tell:
263 try:
264 startofline = tell()
265 except IOError:
266 startofline = tell = None
267 self.seekable = 0
Jeremy Hylton811fc142007-08-03 13:30:02 +0000268 line = str(self.fp.readline(), "iso-8859-1")
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000269 if not line:
270 self.status = 'EOF in headers'
271 break
272 # Skip unix From name time lines
273 if firstline and line.startswith('From '):
274 self.unixfrom = self.unixfrom + line
275 continue
276 firstline = 0
277 if headerseen and line[0] in ' \t':
278 # XXX Not sure if continuation lines are handled properly
279 # for http and/or for repeating headers
280 # It's a continuation line.
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000281 hlist.append(line)
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000282 self.addcontinue(headerseen, line.strip())
283 continue
284 elif self.iscomment(line):
285 # It's a comment. Ignore it.
286 continue
287 elif self.islast(line):
288 # Note! No pushback here! The delimiter line gets eaten.
289 break
290 headerseen = self.isheader(line)
291 if headerseen:
292 # It's a legal header line, save it.
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000293 hlist.append(line)
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000294 self.addheader(headerseen, line[len(headerseen)+1:].strip())
295 continue
296 else:
297 # It's not a header line; throw it back and stop here.
298 if not self.dict:
299 self.status = 'No headers'
300 else:
301 self.status = 'Non-header line where header expected'
302 # Try to undo the read.
303 if unread:
304 unread(line)
305 elif tell:
306 self.fp.seek(startofline)
307 else:
308 self.status = self.status + '; bad seek'
309 break
Greg Stein5e0fa402000-06-26 08:28:01 +0000310
Jeremy Hylton97043c32007-08-04 02:34:24 +0000311class HTTPResponse:
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000312
313 # strict: If true, raise BadStatusLine if the status line can't be
314 # parsed as a valid HTTP/1.0 or 1.1 status line. By default it is
Skip Montanaro186bec22002-07-25 16:10:38 +0000315 # false because it prevents clients from talking to HTTP/0.9
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000316 # servers. Note that a response with a sufficiently corrupted
317 # status line will look like an HTTP/0.9 response.
318
319 # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
320
Jeremy Hylton811fc142007-08-03 13:30:02 +0000321 # The bytes from the socket object are iso-8859-1 strings.
322 # See RFC 2616 sec 2.2 which notes an exception for MIME-encoded
323 # text following RFC 2047. The basic status line parsing only
324 # accepts iso-8859-1.
325
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000326 def __init__(self, sock, debuglevel=0, strict=0, method=None):
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000327 self.fp = sock.makefile("rb", 0)
Jeremy Hylton30f86742000-09-18 22:50:38 +0000328 self.debuglevel = debuglevel
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000329 self.strict = strict
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000330 self._method = method
Greg Stein5e0fa402000-06-26 08:28:01 +0000331
Greg Steindd6eefb2000-07-18 09:09:48 +0000332 self.msg = None
Greg Stein5e0fa402000-06-26 08:28:01 +0000333
Greg Steindd6eefb2000-07-18 09:09:48 +0000334 # from the Status-Line of the response
Tim Peters07e99cb2001-01-14 23:47:14 +0000335 self.version = _UNKNOWN # HTTP-Version
336 self.status = _UNKNOWN # Status-Code
337 self.reason = _UNKNOWN # Reason-Phrase
Greg Stein5e0fa402000-06-26 08:28:01 +0000338
Tim Peters07e99cb2001-01-14 23:47:14 +0000339 self.chunked = _UNKNOWN # is "chunked" being used?
340 self.chunk_left = _UNKNOWN # bytes left to read in current chunk
341 self.length = _UNKNOWN # number of bytes left in response
342 self.will_close = _UNKNOWN # conn will close at end of response
Greg Stein5e0fa402000-06-26 08:28:01 +0000343
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000344 def _read_status(self):
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000345 # Initialize with Simple-Response defaults
Jeremy Hylton811fc142007-08-03 13:30:02 +0000346 line = str(self.fp.readline(), "iso-8859-1")
Jeremy Hylton30f86742000-09-18 22:50:38 +0000347 if self.debuglevel > 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000348 print("reply:", repr(line))
Jeremy Hyltonb6769522003-06-29 17:55:05 +0000349 if not line:
350 # Presumably, the server closed the connection before
351 # sending a valid response.
352 raise BadStatusLine(line)
Greg Steindd6eefb2000-07-18 09:09:48 +0000353 try:
Guido van Rossum34735a62000-12-15 15:09:42 +0000354 [version, status, reason] = line.split(None, 2)
Greg Steindd6eefb2000-07-18 09:09:48 +0000355 except ValueError:
356 try:
Guido van Rossum34735a62000-12-15 15:09:42 +0000357 [version, status] = line.split(None, 1)
Greg Steindd6eefb2000-07-18 09:09:48 +0000358 reason = ""
359 except ValueError:
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000360 # empty version will cause next test to fail and status
361 # will be treated as 0.9 response.
362 version = ""
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000363 if not version.startswith("HTTP/"):
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000364 if self.strict:
365 self.close()
366 raise BadStatusLine(line)
367 else:
368 # assume it's a Simple-Response from an 0.9 server
369 self.fp = LineAndFileWrapper(line, self.fp)
370 return "HTTP/0.9", 200, ""
Greg Stein5e0fa402000-06-26 08:28:01 +0000371
Jeremy Hylton23d40472001-04-13 14:57:08 +0000372 # The status code is a three-digit number
373 try:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000374 status = int(status)
Jeremy Hylton23d40472001-04-13 14:57:08 +0000375 if status < 100 or status > 999:
376 raise BadStatusLine(line)
377 except ValueError:
378 raise BadStatusLine(line)
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000379 return version, status, reason
Greg Stein5e0fa402000-06-26 08:28:01 +0000380
Jeremy Hylton39c03802002-07-12 14:04:09 +0000381 def begin(self):
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000382 if self.msg is not None:
383 # we've already started reading the response
384 return
385
386 # read until we get a non-100 response
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000387 while True:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000388 version, status, reason = self._read_status()
Martin v. Löwis39a31782004-09-18 09:03:49 +0000389 if status != CONTINUE:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000390 break
391 # skip the header from the 100 response
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000392 while True:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000393 skip = self.fp.readline().strip()
394 if not skip:
395 break
396 if self.debuglevel > 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000397 print("header:", skip)
Tim Petersc411dba2002-07-16 21:35:23 +0000398
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000399 self.status = status
400 self.reason = reason.strip()
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000401 if version == "HTTP/1.0":
Greg Steindd6eefb2000-07-18 09:09:48 +0000402 self.version = 10
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000403 elif version.startswith("HTTP/1."):
Tim Peters07e99cb2001-01-14 23:47:14 +0000404 self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000405 elif version == "HTTP/0.9":
Jeremy Hylton110941a2000-10-12 19:58:36 +0000406 self.version = 9
Greg Steindd6eefb2000-07-18 09:09:48 +0000407 else:
408 raise UnknownProtocol(version)
Greg Stein5e0fa402000-06-26 08:28:01 +0000409
Jeremy Hylton110941a2000-10-12 19:58:36 +0000410 if self.version == 9:
Georg Brandl0aade9a2005-06-26 22:06:54 +0000411 self.length = None
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000412 self.chunked = 0
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000413 self.will_close = 1
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000414 self.msg = HTTPMessage(StringIO())
Jeremy Hylton110941a2000-10-12 19:58:36 +0000415 return
416
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +0000417 self.msg = HTTPMessage(self.fp, 0)
Jeremy Hylton30f86742000-09-18 22:50:38 +0000418 if self.debuglevel > 0:
419 for hdr in self.msg.headers:
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000420 print("header:", hdr, end=" ")
Greg Stein5e0fa402000-06-26 08:28:01 +0000421
Greg Steindd6eefb2000-07-18 09:09:48 +0000422 # don't let the msg keep an fp
423 self.msg.fp = None
Greg Stein5e0fa402000-06-26 08:28:01 +0000424
Greg Steindd6eefb2000-07-18 09:09:48 +0000425 # are we using the chunked-style of transfer encoding?
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000426 tr_enc = self.msg.getheader("transfer-encoding")
Jeremy Hyltond229b3a2002-09-03 19:24:24 +0000427 if tr_enc and tr_enc.lower() == "chunked":
Greg Steindd6eefb2000-07-18 09:09:48 +0000428 self.chunked = 1
429 self.chunk_left = None
430 else:
431 self.chunked = 0
Greg Stein5e0fa402000-06-26 08:28:01 +0000432
Greg Steindd6eefb2000-07-18 09:09:48 +0000433 # will the connection close at the end of the response?
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000434 self.will_close = self._check_close()
Greg Stein5e0fa402000-06-26 08:28:01 +0000435
Greg Steindd6eefb2000-07-18 09:09:48 +0000436 # do we have a Content-Length?
437 # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +0000438 self.length = None
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000439 length = self.msg.getheader("content-length")
Greg Steindd6eefb2000-07-18 09:09:48 +0000440 if length and not self.chunked:
Jeremy Hylton30a81812000-09-14 20:34:27 +0000441 try:
442 self.length = int(length)
443 except ValueError:
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +0000444 pass
Greg Stein5e0fa402000-06-26 08:28:01 +0000445
Greg Steindd6eefb2000-07-18 09:09:48 +0000446 # does the body have a fixed length? (of zero)
Martin v. Löwis39a31782004-09-18 09:03:49 +0000447 if (status == NO_CONTENT or status == NOT_MODIFIED or
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000448 100 <= status < 200 or # 1xx codes
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000449 self._method == "HEAD"):
Greg Steindd6eefb2000-07-18 09:09:48 +0000450 self.length = 0
Greg Stein5e0fa402000-06-26 08:28:01 +0000451
Greg Steindd6eefb2000-07-18 09:09:48 +0000452 # if the connection remains open, and we aren't using chunked, and
453 # a content-length was not provided, then assume that the connection
454 # WILL close.
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +0000455 if (not self.will_close and
456 not self.chunked and
457 self.length is None):
Greg Steindd6eefb2000-07-18 09:09:48 +0000458 self.will_close = 1
Greg Stein5e0fa402000-06-26 08:28:01 +0000459
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000460 def _check_close(self):
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000461 conn = self.msg.getheader("connection")
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000462 if self.version == 11:
463 # An HTTP/1.1 proxy is assumed to stay open unless
464 # explicitly closed.
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000465 conn = self.msg.getheader("connection")
Raymond Hettingerbac788a2004-05-04 09:21:43 +0000466 if conn and "close" in conn.lower():
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000467 return True
468 return False
469
Jeremy Hylton2c178252004-08-07 16:28:14 +0000470 # Some HTTP/1.0 implementations have support for persistent
471 # connections, using rules different than HTTP/1.1.
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000472
473 # For older HTTP, Keep-Alive indiciates persistent connection.
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000474 if self.msg.getheader("keep-alive"):
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000475 return False
Tim Peters77c06fb2002-11-24 02:35:35 +0000476
Jeremy Hylton2c178252004-08-07 16:28:14 +0000477 # At least Akamai returns a "Connection: Keep-Alive" header,
478 # which was supposed to be sent by the client.
479 if conn and "keep-alive" in conn.lower():
480 return False
481
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000482 # Proxy-Connection is a netscape hack.
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000483 pconn = self.msg.getheader("proxy-connection")
Raymond Hettingerbac788a2004-05-04 09:21:43 +0000484 if pconn and "keep-alive" in pconn.lower():
Jeremy Hylton22b3a492002-11-13 17:27:43 +0000485 return False
486
487 # otherwise, assume it will close
488 return True
489
Greg Steindd6eefb2000-07-18 09:09:48 +0000490 def close(self):
491 if self.fp:
492 self.fp.close()
493 self.fp = None
Greg Stein5e0fa402000-06-26 08:28:01 +0000494
Greg Steindd6eefb2000-07-18 09:09:48 +0000495 def isclosed(self):
496 # NOTE: it is possible that we will not ever call self.close(). This
497 # case occurs when will_close is TRUE, length is None, and we
498 # read up to the last byte, but NOT past it.
499 #
500 # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
501 # called, meaning self.isclosed() is meaningful.
502 return self.fp is None
503
Jeremy Hylton2c178252004-08-07 16:28:14 +0000504 # XXX It would be nice to have readline and __iter__ for this, too.
505
Greg Steindd6eefb2000-07-18 09:09:48 +0000506 def read(self, amt=None):
507 if self.fp is None:
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000508 return ""
Greg Steindd6eefb2000-07-18 09:09:48 +0000509
510 if self.chunked:
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000511 return self._read_chunked(amt)
Tim Peters230a60c2002-11-09 05:08:07 +0000512
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000513 if amt is None:
Greg Steindd6eefb2000-07-18 09:09:48 +0000514 # unbounded read
Jeremy Hyltondef9d2a2004-11-07 16:13:49 +0000515 if self.length is None:
Greg Steindd6eefb2000-07-18 09:09:48 +0000516 s = self.fp.read()
517 else:
518 s = self._safe_read(self.length)
Jeremy Hyltondef9d2a2004-11-07 16:13:49 +0000519 self.length = 0
Tim Peters07e99cb2001-01-14 23:47:14 +0000520 self.close() # we read everything
Greg Steindd6eefb2000-07-18 09:09:48 +0000521 return s
522
523 if self.length is not None:
524 if amt > self.length:
525 # clip the read to the "end of response"
526 amt = self.length
Greg Steindd6eefb2000-07-18 09:09:48 +0000527
528 # we do not use _safe_read() here because this may be a .will_close
529 # connection, and the user is reading more bytes than will be provided
530 # (for example, reading in 1k chunks)
531 s = self.fp.read(amt)
Jeremy Hyltondef9d2a2004-11-07 16:13:49 +0000532 if self.length is not None:
533 self.length -= len(s)
Greg Steindd6eefb2000-07-18 09:09:48 +0000534
Greg Steindd6eefb2000-07-18 09:09:48 +0000535 return s
536
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000537 def _read_chunked(self, amt):
538 assert self.chunked != _UNKNOWN
539 chunk_left = self.chunk_left
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000540 value = ""
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000541
542 # XXX This accumulates chunks by repeated string concatenation,
543 # which is not efficient as the number or size of chunks gets big.
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000544 while True:
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000545 if chunk_left is None:
546 line = self.fp.readline()
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000547 i = line.find(";")
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000548 if i >= 0:
549 line = line[:i] # strip chunk-extensions
550 chunk_left = int(line, 16)
551 if chunk_left == 0:
552 break
553 if amt is None:
554 value += self._safe_read(chunk_left)
555 elif amt < chunk_left:
556 value += self._safe_read(amt)
557 self.chunk_left = chunk_left - amt
558 return value
559 elif amt == chunk_left:
560 value += self._safe_read(amt)
561 self._safe_read(2) # toss the CRLF at the end of the chunk
562 self.chunk_left = None
563 return value
564 else:
565 value += self._safe_read(chunk_left)
566 amt -= chunk_left
567
568 # we read the whole chunk, get another
569 self._safe_read(2) # toss the CRLF at the end of the chunk
570 chunk_left = None
571
572 # read and discard trailer up to the CRLF terminator
573 ### note: we shouldn't have any trailers!
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000574 while True:
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000575 line = self.fp.readline()
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000576 if line == "\r\n":
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000577 break
578
579 # we read everything; close the "file"
Jeremy Hyltond4c472c2002-09-03 20:49:06 +0000580 self.close()
581
582 return value
Tim Peters230a60c2002-11-09 05:08:07 +0000583
Greg Steindd6eefb2000-07-18 09:09:48 +0000584 def _safe_read(self, amt):
585 """Read the number of bytes requested, compensating for partial reads.
586
587 Normally, we have a blocking socket, but a read() can be interrupted
588 by a signal (resulting in a partial read).
589
590 Note that we cannot distinguish between EOF and an interrupt when zero
591 bytes have been read. IncompleteRead() will be raised in this
592 situation.
593
594 This function should be used when <amt> bytes "should" be present for
595 reading. If the bytes are truly not available (due to EOF), then the
596 IncompleteRead exception can be used to detect the problem.
597 """
Georg Brandl80ba8e82005-09-29 20:16:07 +0000598 s = []
Greg Steindd6eefb2000-07-18 09:09:48 +0000599 while amt > 0:
Georg Brandl80ba8e82005-09-29 20:16:07 +0000600 chunk = self.fp.read(min(amt, MAXAMOUNT))
Greg Steindd6eefb2000-07-18 09:09:48 +0000601 if not chunk:
602 raise IncompleteRead(s)
Georg Brandl80ba8e82005-09-29 20:16:07 +0000603 s.append(chunk)
Raymond Hettingerb2e0b922003-02-26 22:45:18 +0000604 amt -= len(chunk)
Jeremy Hyltone5d0e842007-08-03 13:45:24 +0000605 return "".join(s)
Greg Steindd6eefb2000-07-18 09:09:48 +0000606
607 def getheader(self, name, default=None):
608 if self.msg is None:
609 raise ResponseNotReady()
610 return self.msg.getheader(name, default)
Greg Stein5e0fa402000-06-26 08:28:01 +0000611
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000612 def getheaders(self):
613 """Return list of (header, value) tuples."""
614 if self.msg is None:
615 raise ResponseNotReady()
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000616 return list(self.msg.items())
Martin v. Löwisdeacce22004-08-18 12:46:26 +0000617
Greg Stein5e0fa402000-06-26 08:28:01 +0000618
619class HTTPConnection:
620
Greg Steindd6eefb2000-07-18 09:09:48 +0000621 _http_vsn = 11
622 _http_vsn_str = 'HTTP/1.1'
Greg Stein5e0fa402000-06-26 08:28:01 +0000623
Greg Steindd6eefb2000-07-18 09:09:48 +0000624 response_class = HTTPResponse
625 default_port = HTTP_PORT
626 auto_open = 1
Jeremy Hylton30f86742000-09-18 22:50:38 +0000627 debuglevel = 0
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000628 strict = 0
Greg Stein5e0fa402000-06-26 08:28:01 +0000629
Guido van Rossumd8faa362007-04-27 19:54:29 +0000630 def __init__(self, host, port=None, strict=None, timeout=None):
631 self.timeout = timeout
Greg Steindd6eefb2000-07-18 09:09:48 +0000632 self.sock = None
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000633 self._buffer = []
Greg Steindd6eefb2000-07-18 09:09:48 +0000634 self.__response = None
635 self.__state = _CS_IDLE
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000636 self._method = None
Tim Petersc411dba2002-07-16 21:35:23 +0000637
Greg Steindd6eefb2000-07-18 09:09:48 +0000638 self._set_hostport(host, port)
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000639 if strict is not None:
640 self.strict = strict
Greg Stein5e0fa402000-06-26 08:28:01 +0000641
Greg Steindd6eefb2000-07-18 09:09:48 +0000642 def _set_hostport(self, host, port):
643 if port is None:
Skip Montanaro10e6e0e2004-09-14 16:32:02 +0000644 i = host.rfind(':')
Skip Montanarocae14d22004-09-14 17:55:21 +0000645 j = host.rfind(']') # ipv6 addresses have [...]
646 if i > j:
Skip Montanaro9d389972002-03-24 16:53:50 +0000647 try:
648 port = int(host[i+1:])
649 except ValueError:
Jeremy Hyltonfbd79942002-07-02 20:19:08 +0000650 raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
Greg Steindd6eefb2000-07-18 09:09:48 +0000651 host = host[:i]
652 else:
653 port = self.default_port
Raymond Hettinger4d037912004-10-14 15:23:38 +0000654 if host and host[0] == '[' and host[-1] == ']':
Brett Cannon0a1af4a2004-09-15 23:26:23 +0000655 host = host[1:-1]
Greg Steindd6eefb2000-07-18 09:09:48 +0000656 self.host = host
657 self.port = port
Greg Stein5e0fa402000-06-26 08:28:01 +0000658
Jeremy Hylton30f86742000-09-18 22:50:38 +0000659 def set_debuglevel(self, level):
660 self.debuglevel = level
661
Greg Steindd6eefb2000-07-18 09:09:48 +0000662 def connect(self):
663 """Connect to the host and port specified in __init__."""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000664 self.sock = socket.create_connection((self.host,self.port),
665 self.timeout)
Greg Stein5e0fa402000-06-26 08:28:01 +0000666
Greg Steindd6eefb2000-07-18 09:09:48 +0000667 def close(self):
668 """Close the connection to the HTTP server."""
669 if self.sock:
Tim Peters07e99cb2001-01-14 23:47:14 +0000670 self.sock.close() # close it manually... there may be other refs
Greg Steindd6eefb2000-07-18 09:09:48 +0000671 self.sock = None
672 if self.__response:
673 self.__response.close()
674 self.__response = None
675 self.__state = _CS_IDLE
Greg Stein5e0fa402000-06-26 08:28:01 +0000676
Greg Steindd6eefb2000-07-18 09:09:48 +0000677 def send(self, str):
678 """Send `str' to the server."""
679 if self.sock is None:
680 if self.auto_open:
681 self.connect()
682 else:
683 raise NotConnected()
Greg Stein5e0fa402000-06-26 08:28:01 +0000684
Jeremy Hyltone3252ec2002-07-16 21:41:43 +0000685 # send the data to the server. if we get a broken pipe, then close
Greg Steindd6eefb2000-07-18 09:09:48 +0000686 # the socket. we want to reconnect when somebody tries to send again.
687 #
688 # NOTE: we DO propagate the error, though, because we cannot simply
689 # ignore the error... the caller will know if they can retry.
Jeremy Hylton30f86742000-09-18 22:50:38 +0000690 if self.debuglevel > 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000691 print("send:", repr(str))
Greg Steindd6eefb2000-07-18 09:09:48 +0000692 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000693 blocksize=8192
694 if hasattr(str,'read') :
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000695 if self.debuglevel > 0: print("sendIng a read()able")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000696 data=str.read(blocksize)
697 while data:
698 self.sock.sendall(data)
699 data=str.read(blocksize)
700 else:
701 self.sock.sendall(str)
Guido van Rossumb940e112007-01-10 16:19:56 +0000702 except socket.error as v:
Guido van Rossum89df2452007-03-19 22:26:27 +0000703 if v.args[0] == 32: # Broken pipe
Greg Steindd6eefb2000-07-18 09:09:48 +0000704 self.close()
705 raise
Greg Stein5e0fa402000-06-26 08:28:01 +0000706
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000707 def _output(self, s):
708 """Add a line of output to the current request buffer.
Tim Peters469cdad2002-08-08 20:19:19 +0000709
Jeremy Hyltone3252ec2002-07-16 21:41:43 +0000710 Assumes that the line does *not* end with \\r\\n.
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000711 """
712 self._buffer.append(s)
713
714 def _send_output(self):
715 """Send the currently buffered request and clear the buffer.
716
Jeremy Hyltone3252ec2002-07-16 21:41:43 +0000717 Appends an extra \\r\\n to the buffer.
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000718 """
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000719 self._buffer.extend((b"", b""))
720 msg = b"\r\n".join(self._buffer)
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000721 del self._buffer[:]
722 self.send(msg)
723
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000724 def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):
Greg Steindd6eefb2000-07-18 09:09:48 +0000725 """Send a request to the server.
Greg Stein5e0fa402000-06-26 08:28:01 +0000726
Greg Steindd6eefb2000-07-18 09:09:48 +0000727 `method' specifies an HTTP request method, e.g. 'GET'.
728 `url' specifies the object being requested, e.g. '/index.html'.
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000729 `skip_host' if True does not add automatically a 'Host:' header
730 `skip_accept_encoding' if True does not add automatically an
731 'Accept-Encoding:' header
Greg Steindd6eefb2000-07-18 09:09:48 +0000732 """
Greg Stein5e0fa402000-06-26 08:28:01 +0000733
Greg Stein616a58d2003-06-24 06:35:19 +0000734 # if a prior response has been completed, then forget about it.
Greg Steindd6eefb2000-07-18 09:09:48 +0000735 if self.__response and self.__response.isclosed():
736 self.__response = None
Greg Stein5e0fa402000-06-26 08:28:01 +0000737
Tim Peters58eb11c2004-01-18 20:29:55 +0000738
Greg Steindd6eefb2000-07-18 09:09:48 +0000739 # in certain cases, we cannot issue another request on this connection.
740 # this occurs when:
741 # 1) we are in the process of sending a request. (_CS_REQ_STARTED)
742 # 2) a response to a previous request has signalled that it is going
743 # to close the connection upon completion.
744 # 3) the headers for the previous response have not been read, thus
745 # we cannot determine whether point (2) is true. (_CS_REQ_SENT)
746 #
747 # if there is no prior response, then we can request at will.
748 #
749 # if point (2) is true, then we will have passed the socket to the
750 # response (effectively meaning, "there is no prior response"), and
751 # will open a new one when a new request is made.
752 #
753 # Note: if a prior response exists, then we *can* start a new request.
754 # We are not allowed to begin fetching the response to this new
755 # request, however, until that prior response is complete.
756 #
757 if self.__state == _CS_IDLE:
758 self.__state = _CS_REQ_STARTED
759 else:
760 raise CannotSendRequest()
Greg Stein5e0fa402000-06-26 08:28:01 +0000761
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000762 # Save the method we use, we need it later in the response phase
763 self._method = method
Greg Steindd6eefb2000-07-18 09:09:48 +0000764 if not url:
765 url = '/'
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000766 request = '%s %s %s' % (method, url, self._http_vsn_str)
Greg Stein5e0fa402000-06-26 08:28:01 +0000767
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000768 # Non-ASCII characters should have been eliminated earlier
769 self._output(request.encode('ascii'))
Greg Stein5e0fa402000-06-26 08:28:01 +0000770
Greg Steindd6eefb2000-07-18 09:09:48 +0000771 if self._http_vsn == 11:
772 # Issue some standard headers for better HTTP/1.1 compliance
Greg Stein5e0fa402000-06-26 08:28:01 +0000773
Jeremy Hylton3921ff62002-03-09 06:07:23 +0000774 if not skip_host:
775 # this header is issued *only* for HTTP/1.1
776 # connections. more specifically, this means it is
777 # only issued when the client uses the new
778 # HTTPConnection() class. backwards-compat clients
779 # will be using HTTP/1.0 and those clients may be
780 # issuing this header themselves. we should NOT issue
781 # it twice; some web servers (such as Apache) barf
782 # when they see two Host: headers
Guido van Rossumf6922aa2001-01-14 21:03:01 +0000783
Jeremy Hylton3921ff62002-03-09 06:07:23 +0000784 # If we need a non-standard port,include it in the
785 # header. If the request is going through a proxy,
786 # but the host of the actual URL, not the host of the
787 # proxy.
Jeremy Hylton8acf1e02002-03-08 19:35:51 +0000788
Jeremy Hylton3921ff62002-03-09 06:07:23 +0000789 netloc = ''
790 if url.startswith('http'):
791 nil, netloc, nil, nil, nil = urlsplit(url)
792
793 if netloc:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 try:
795 netloc_enc = netloc.encode("ascii")
796 except UnicodeEncodeError:
797 netloc_enc = netloc.encode("idna")
798 self.putheader('Host', netloc_enc)
Jeremy Hylton3921ff62002-03-09 06:07:23 +0000799 else:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000800 try:
801 host_enc = self.host.encode("ascii")
802 except UnicodeEncodeError:
803 host_enc = self.host.encode("idna")
804 if self.port == HTTP_PORT:
805 self.putheader('Host', host_enc)
806 else:
807 self.putheader('Host', "%s:%s" % (host_enc, self.port))
Greg Stein5e0fa402000-06-26 08:28:01 +0000808
Greg Steindd6eefb2000-07-18 09:09:48 +0000809 # note: we are assuming that clients will not attempt to set these
810 # headers since *this* library must deal with the
811 # consequences. this also means that when the supporting
812 # libraries are updated to recognize other forms, then this
813 # code should be changed (removed or updated).
Greg Stein5e0fa402000-06-26 08:28:01 +0000814
Greg Steindd6eefb2000-07-18 09:09:48 +0000815 # we only want a Content-Encoding of "identity" since we don't
816 # support encodings such as x-gzip or x-deflate.
Martin v. Löwisaf7dc8d2003-11-19 19:51:55 +0000817 if not skip_accept_encoding:
818 self.putheader('Accept-Encoding', 'identity')
Greg Stein5e0fa402000-06-26 08:28:01 +0000819
Greg Steindd6eefb2000-07-18 09:09:48 +0000820 # we can accept "chunked" Transfer-Encodings, but no others
821 # NOTE: no TE header implies *only* "chunked"
822 #self.putheader('TE', 'chunked')
Greg Stein5e0fa402000-06-26 08:28:01 +0000823
Greg Steindd6eefb2000-07-18 09:09:48 +0000824 # if TE is supplied in the header, then it must appear in a
825 # Connection header.
826 #self.putheader('Connection', 'TE')
Greg Stein5e0fa402000-06-26 08:28:01 +0000827
Greg Steindd6eefb2000-07-18 09:09:48 +0000828 else:
829 # For HTTP/1.0, the server will assume "not chunked"
830 pass
Greg Stein5e0fa402000-06-26 08:28:01 +0000831
Greg Steindd6eefb2000-07-18 09:09:48 +0000832 def putheader(self, header, value):
833 """Send a request header line to the server.
Greg Stein5e0fa402000-06-26 08:28:01 +0000834
Greg Steindd6eefb2000-07-18 09:09:48 +0000835 For example: h.putheader('Accept', 'text/html')
836 """
837 if self.__state != _CS_REQ_STARTED:
838 raise CannotSendHeader()
Greg Stein5e0fa402000-06-26 08:28:01 +0000839
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000840 header = '%s: %s' % (header, value)
841 self._output(header.encode('ascii'))
Greg Stein5e0fa402000-06-26 08:28:01 +0000842
Greg Steindd6eefb2000-07-18 09:09:48 +0000843 def endheaders(self):
844 """Indicate that the last header line has been sent to the server."""
Greg Stein5e0fa402000-06-26 08:28:01 +0000845
Greg Steindd6eefb2000-07-18 09:09:48 +0000846 if self.__state == _CS_REQ_STARTED:
847 self.__state = _CS_REQ_SENT
848 else:
849 raise CannotSendHeader()
Greg Stein5e0fa402000-06-26 08:28:01 +0000850
Jeremy Hylton8531b1b2002-07-16 21:21:11 +0000851 self._send_output()
Greg Stein5e0fa402000-06-26 08:28:01 +0000852
Greg Steindd6eefb2000-07-18 09:09:48 +0000853 def request(self, method, url, body=None, headers={}):
854 """Send a complete request to the server."""
Greg Steindd6eefb2000-07-18 09:09:48 +0000855 try:
856 self._send_request(method, url, body, headers)
Guido van Rossumb940e112007-01-10 16:19:56 +0000857 except socket.error as v:
Greg Steindd6eefb2000-07-18 09:09:48 +0000858 # trap 'Broken pipe' if we're allowed to automatically reconnect
Guido van Rossum89df2452007-03-19 22:26:27 +0000859 if v.args[0] != 32 or not self.auto_open:
Greg Steindd6eefb2000-07-18 09:09:48 +0000860 raise
861 # try one more time
862 self._send_request(method, url, body, headers)
Greg Stein5e0fa402000-06-26 08:28:01 +0000863
Greg Steindd6eefb2000-07-18 09:09:48 +0000864 def _send_request(self, method, url, body, headers):
Jeremy Hylton2c178252004-08-07 16:28:14 +0000865 # honour explicitly requested Host: and Accept-Encoding headers
866 header_names = dict.fromkeys([k.lower() for k in headers])
867 skips = {}
868 if 'host' in header_names:
869 skips['skip_host'] = 1
870 if 'accept-encoding' in header_names:
871 skips['skip_accept_encoding'] = 1
Greg Stein5e0fa402000-06-26 08:28:01 +0000872
Jeremy Hylton2c178252004-08-07 16:28:14 +0000873 self.putrequest(method, url, **skips)
874
875 if body and ('content-length' not in header_names):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000876 thelen=None
877 try:
878 thelen=str(len(body))
Guido van Rossumb940e112007-01-10 16:19:56 +0000879 except TypeError as te:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000880 # If this is a file-like object, try to
881 # fstat its file descriptor
882 import os
883 try:
884 thelen = str(os.fstat(body.fileno()).st_size)
885 except (AttributeError, OSError):
886 # Don't send a length if this failed
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000887 if self.debuglevel > 0: print("Cannot stat!!")
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000888
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889 if thelen is not None:
890 self.putheader('Content-Length',thelen)
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000891 for hdr, value in headers.items():
Greg Steindd6eefb2000-07-18 09:09:48 +0000892 self.putheader(hdr, value)
893 self.endheaders()
Greg Stein5e0fa402000-06-26 08:28:01 +0000894
Greg Steindd6eefb2000-07-18 09:09:48 +0000895 if body:
Martin v. Löwisdd5a8602007-06-30 09:22:09 +0000896 if isinstance(body, str): body = body.encode('ascii')
Greg Steindd6eefb2000-07-18 09:09:48 +0000897 self.send(body)
Greg Stein5e0fa402000-06-26 08:28:01 +0000898
Greg Steindd6eefb2000-07-18 09:09:48 +0000899 def getresponse(self):
Jeremy Hyltonfb35f652007-08-03 20:30:33 +0000900 """Get the response from the server."""
Greg Stein5e0fa402000-06-26 08:28:01 +0000901
Greg Stein616a58d2003-06-24 06:35:19 +0000902 # if a prior response has been completed, then forget about it.
Greg Steindd6eefb2000-07-18 09:09:48 +0000903 if self.__response and self.__response.isclosed():
904 self.__response = None
Greg Stein5e0fa402000-06-26 08:28:01 +0000905
Greg Steindd6eefb2000-07-18 09:09:48 +0000906 #
907 # if a prior response exists, then it must be completed (otherwise, we
908 # cannot read this response's header to determine the connection-close
909 # behavior)
910 #
911 # note: if a prior response existed, but was connection-close, then the
912 # socket and response were made independent of this HTTPConnection
913 # object since a new request requires that we open a whole new
914 # connection
915 #
916 # this means the prior response had one of two states:
917 # 1) will_close: this connection was reset and the prior socket and
918 # response operate independently
919 # 2) persistent: the response was retained and we await its
920 # isclosed() status to become true.
921 #
922 if self.__state != _CS_REQ_SENT or self.__response:
923 raise ResponseNotReady()
Greg Stein5e0fa402000-06-26 08:28:01 +0000924
Jeremy Hylton30f86742000-09-18 22:50:38 +0000925 if self.debuglevel > 0:
Jeremy Hyltond46aa372002-07-06 18:48:07 +0000926 response = self.response_class(self.sock, self.debuglevel,
Tim Petersc2659cf2003-05-12 20:19:37 +0000927 strict=self.strict,
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000928 method=self._method)
Jeremy Hylton30f86742000-09-18 22:50:38 +0000929 else:
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000930 response = self.response_class(self.sock, strict=self.strict,
931 method=self._method)
Greg Stein5e0fa402000-06-26 08:28:01 +0000932
Jeremy Hylton39c03802002-07-12 14:04:09 +0000933 response.begin()
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000934 assert response.will_close != _UNKNOWN
Greg Steindd6eefb2000-07-18 09:09:48 +0000935 self.__state = _CS_IDLE
Greg Stein5e0fa402000-06-26 08:28:01 +0000936
Greg Steindd6eefb2000-07-18 09:09:48 +0000937 if response.will_close:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000938 # this effectively passes the connection to the response
939 self.close()
Greg Steindd6eefb2000-07-18 09:09:48 +0000940 else:
941 # remember this, so we can tell when it is complete
942 self.__response = response
Greg Stein5e0fa402000-06-26 08:28:01 +0000943
Greg Steindd6eefb2000-07-18 09:09:48 +0000944 return response
Greg Stein5e0fa402000-06-26 08:28:01 +0000945
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946# The next several classes are used to define FakeSocket, a socket-like
Jeremy Hylton29d27ac2002-07-09 21:22:36 +0000947# interface to an SSL connection.
948
949# The primary complexity comes from faking a makefile() method. The
950# standard socket makefile() implementation calls dup() on the socket
951# file descriptor. As a consequence, clients can call close() on the
952# parent socket and its makefile children in any order. The underlying
953# socket isn't closed until they are all closed.
954
955# The implementation uses reference counting to keep the socket open
956# until the last client calls close(). SharedSocket keeps track of
957# the reference counting and SharedSocketClient provides an constructor
958# and close() method that call incref() and decref() correctly.
959
960class SharedSocket:
961
962 def __init__(self, sock):
963 self.sock = sock
964 self._refcnt = 0
965
966 def incref(self):
967 self._refcnt += 1
968
969 def decref(self):
970 self._refcnt -= 1
971 assert self._refcnt >= 0
972 if self._refcnt == 0:
973 self.sock.close()
974
975 def __del__(self):
976 self.sock.close()
977
978class SharedSocketClient:
979
980 def __init__(self, shared):
981 self._closed = 0
982 self._shared = shared
983 self._shared.incref()
984 self._sock = shared.sock
985
986 def close(self):
987 if not self._closed:
988 self._shared.decref()
989 self._closed = 1
990 self._shared = None
991
992class SSLFile(SharedSocketClient):
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000993 """File-like object wrapping an SSL socket."""
Greg Stein5e0fa402000-06-26 08:28:01 +0000994
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000995 BUFSIZE = 8192
Tim Petersc411dba2002-07-16 21:35:23 +0000996
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000997 def __init__(self, sock, ssl, bufsize=None):
Jeremy Hylton29d27ac2002-07-09 21:22:36 +0000998 SharedSocketClient.__init__(self, sock)
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +0000999 self._ssl = ssl
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001000 self._buf = b""
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001001 self._bufsize = bufsize or self.__class__.BUFSIZE
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001002
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001003 def _read(self):
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001004 buf = b""
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001005 # put in a loop so that we retry on transient errors
Raymond Hettingerb2e0b922003-02-26 22:45:18 +00001006 while True:
Greg Steindd6eefb2000-07-18 09:09:48 +00001007 try:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001008 buf = self._ssl.read(self._bufsize)
Guido van Rossumb940e112007-01-10 16:19:56 +00001009 except socket.sslerror as err:
Guido van Rossum89df2452007-03-19 22:26:27 +00001010 err_type = err.args[0]
Brett Cannon6f8fe152007-02-27 20:16:38 +00001011 if (err_type == socket.SSL_ERROR_WANT_READ
1012 or err_type == socket.SSL_ERROR_WANT_WRITE):
Jeremy Hylton6459c8d2001-10-11 17:47:22 +00001013 continue
Brett Cannon6f8fe152007-02-27 20:16:38 +00001014 if (err_type == socket.SSL_ERROR_ZERO_RETURN
1015 or err_type == socket.SSL_ERROR_EOF):
Jeremy Hylton6459c8d2001-10-11 17:47:22 +00001016 break
1017 raise
Guido van Rossumb940e112007-01-10 16:19:56 +00001018 except socket.error as err:
Guido van Rossum89df2452007-03-19 22:26:27 +00001019 err_type = err.args[0]
Brett Cannon6f8fe152007-02-27 20:16:38 +00001020 if err_type == errno.EINTR:
Jeremy Hylton6459c8d2001-10-11 17:47:22 +00001021 continue
Brett Cannon6f8fe152007-02-27 20:16:38 +00001022 if err_type == errno.EBADF:
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001023 # XXX socket was closed?
1024 break
Jeremy Hylton6459c8d2001-10-11 17:47:22 +00001025 raise
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001026 else:
Jeremy Hylton42dd01a2001-02-01 23:35:20 +00001027 break
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001028 return buf
1029
1030 def read(self, size=None):
1031 L = [self._buf]
Raymond Hettinger49227682003-03-06 16:31:48 +00001032 avail = len(self._buf)
1033 while size is None or avail < size:
1034 s = self._read()
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001035 if s == b"":
Raymond Hettinger49227682003-03-06 16:31:48 +00001036 break
1037 L.append(s)
1038 avail += len(s)
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001039 all = b"".join(L)
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001040 if size is None:
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001041 self._buf = b""
Raymond Hettinger49227682003-03-06 16:31:48 +00001042 return all
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001043 else:
Raymond Hettinger49227682003-03-06 16:31:48 +00001044 self._buf = all[size:]
1045 return all[:size]
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001046
1047 def readline(self):
1048 L = [self._buf]
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001049 self._buf = b""
Raymond Hettinger49227682003-03-06 16:31:48 +00001050 while 1:
1051 i = L[-1].find("\n")
1052 if i >= 0:
Raymond Hettingerb2e0b922003-02-26 22:45:18 +00001053 break
Raymond Hettinger49227682003-03-06 16:31:48 +00001054 s = self._read()
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001055 if s == b"":
Raymond Hettinger49227682003-03-06 16:31:48 +00001056 break
1057 L.append(s)
1058 if i == -1:
1059 # loop exited because there is no more data
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001060 return b"".join(L)
Raymond Hettinger49227682003-03-06 16:31:48 +00001061 else:
Jeremy Hylton0ee5eeb2007-08-04 03:25:17 +00001062 all = b"".join(L)
Raymond Hettinger49227682003-03-06 16:31:48 +00001063 # XXX could do enough bookkeeping not to do a 2nd search
1064 i = all.find("\n") + 1
1065 line = all[:i]
1066 self._buf = all[i:]
1067 return line
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001068
Martin v. Löwis11892ec2003-10-27 14:07:53 +00001069 def readlines(self, sizehint=0):
1070 total = 0
1071 list = []
1072 while True:
1073 line = self.readline()
1074 if not line:
1075 break
1076 list.append(line)
1077 total += len(line)
1078 if sizehint and total >= sizehint:
1079 break
1080 return list
1081
1082 def fileno(self):
1083 return self._sock.fileno()
1084
1085 def __iter__(self):
1086 return self
1087
Georg Brandla18af4e2007-04-21 15:47:16 +00001088 def __next__(self):
Martin v. Löwis11892ec2003-10-27 14:07:53 +00001089 line = self.readline()
1090 if not line:
1091 raise StopIteration
1092 return line
1093
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001094class FakeSocket(SharedSocketClient):
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001095
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001096 class _closedsocket:
1097 def __getattr__(self, name):
1098 raise error(9, 'Bad file descriptor')
1099
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001100 def __init__(self, sock, ssl):
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001101 sock = SharedSocket(sock)
1102 SharedSocketClient.__init__(self, sock)
1103 self._ssl = ssl
1104
1105 def close(self):
1106 SharedSocketClient.close(self)
1107 self._sock = self.__class__._closedsocket()
Jeremy Hyltonbe4fcf12002-06-28 22:38:01 +00001108
1109 def makefile(self, mode, bufsize=None):
1110 if mode != 'r' and mode != 'rb':
1111 raise UnimplementedFileMode()
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001112 return SSLFile(self._shared, self._ssl, bufsize)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001113
Greg Steindd6eefb2000-07-18 09:09:48 +00001114 def send(self, stuff, flags = 0):
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001115 return self._ssl.write(stuff)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001116
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001117 sendall = send
Andrew M. Kuchlinga3c0b932002-03-18 22:51:48 +00001118
Greg Steindd6eefb2000-07-18 09:09:48 +00001119 def recv(self, len = 1024, flags = 0):
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001120 return self._ssl.read(len)
Guido van Rossum23acc951994-02-21 16:36:04 +00001121
Greg Steindd6eefb2000-07-18 09:09:48 +00001122 def __getattr__(self, attr):
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001123 return getattr(self._sock, attr)
Guido van Rossum09c8b6c1999-12-07 21:37:17 +00001124
Guido van Rossum23acc951994-02-21 16:36:04 +00001125
Greg Stein5e0fa402000-06-26 08:28:01 +00001126class HTTPSConnection(HTTPConnection):
Greg Steindd6eefb2000-07-18 09:09:48 +00001127 "This class allows communication via SSL."
Greg Stein5e0fa402000-06-26 08:28:01 +00001128
Greg Steindd6eefb2000-07-18 09:09:48 +00001129 default_port = HTTPS_PORT
Greg Stein5e0fa402000-06-26 08:28:01 +00001130
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001131 def __init__(self, host, port=None, key_file=None, cert_file=None,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001132 strict=None, timeout=None):
1133 HTTPConnection.__init__(self, host, port, strict, timeout)
Jeremy Hylton7c75c992002-06-28 23:38:14 +00001134 self.key_file = key_file
1135 self.cert_file = cert_file
Greg Stein5e0fa402000-06-26 08:28:01 +00001136
Greg Steindd6eefb2000-07-18 09:09:48 +00001137 def connect(self):
1138 "Connect to a host on a given (SSL) port."
Greg Stein5e0fa402000-06-26 08:28:01 +00001139
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001140 sock = socket.create_connection((self.host, self.port), self.timeout)
Martin v. Löwis1867f242003-06-14 13:30:53 +00001141 ssl = socket.ssl(sock, self.key_file, self.cert_file)
Greg Steindd6eefb2000-07-18 09:09:48 +00001142 self.sock = FakeSocket(sock, ssl)
Greg Stein5e0fa402000-06-26 08:28:01 +00001143
1144
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001145class HTTP:
Greg Steindd6eefb2000-07-18 09:09:48 +00001146 "Compatibility class with httplib.py from 1.5."
Greg Stein5e0fa402000-06-26 08:28:01 +00001147
Greg Steindd6eefb2000-07-18 09:09:48 +00001148 _http_vsn = 10
1149 _http_vsn_str = 'HTTP/1.0'
Greg Stein5e0fa402000-06-26 08:28:01 +00001150
Greg Steindd6eefb2000-07-18 09:09:48 +00001151 debuglevel = 0
Greg Stein5e0fa402000-06-26 08:28:01 +00001152
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001153 _connection_class = HTTPConnection
1154
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001155 def __init__(self, host='', port=None, strict=None):
Greg Steindd6eefb2000-07-18 09:09:48 +00001156 "Provide a default host, since the superclass requires one."
Greg Stein5e0fa402000-06-26 08:28:01 +00001157
Greg Steindd6eefb2000-07-18 09:09:48 +00001158 # some joker passed 0 explicitly, meaning default port
1159 if port == 0:
1160 port = None
Greg Stein5e0fa402000-06-26 08:28:01 +00001161
Greg Steindd6eefb2000-07-18 09:09:48 +00001162 # Note that we may pass an empty string as the host; this will throw
1163 # an error when we attempt to connect. Presumably, the client code
1164 # will call connect before then, with a proper host.
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001165 self._setup(self._connection_class(host, port, strict))
Greg Stein5e0fa402000-06-26 08:28:01 +00001166
Greg Stein81937a42001-08-18 09:20:23 +00001167 def _setup(self, conn):
1168 self._conn = conn
1169
1170 # set up delegation to flesh out interface
1171 self.send = conn.send
1172 self.putrequest = conn.putrequest
1173 self.endheaders = conn.endheaders
1174 self.set_debuglevel = conn.set_debuglevel
1175
1176 conn._http_vsn = self._http_vsn
1177 conn._http_vsn_str = self._http_vsn_str
Greg Stein5e0fa402000-06-26 08:28:01 +00001178
Greg Steindd6eefb2000-07-18 09:09:48 +00001179 self.file = None
Greg Stein5e0fa402000-06-26 08:28:01 +00001180
Greg Steindd6eefb2000-07-18 09:09:48 +00001181 def connect(self, host=None, port=None):
1182 "Accept arguments to set the host/port, since the superclass doesn't."
Greg Stein5e0fa402000-06-26 08:28:01 +00001183
Greg Steindd6eefb2000-07-18 09:09:48 +00001184 if host is not None:
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001185 self._conn._set_hostport(host, port)
1186 self._conn.connect()
Greg Stein5e0fa402000-06-26 08:28:01 +00001187
Greg Steindd6eefb2000-07-18 09:09:48 +00001188 def getfile(self):
1189 "Provide a getfile, since the superclass' does not use this concept."
1190 return self.file
Greg Stein5e0fa402000-06-26 08:28:01 +00001191
Greg Steindd6eefb2000-07-18 09:09:48 +00001192 def putheader(self, header, *values):
1193 "The superclass allows only one value argument."
Guido van Rossum34735a62000-12-15 15:09:42 +00001194 self._conn.putheader(header, '\r\n\t'.join(values))
Greg Stein5e0fa402000-06-26 08:28:01 +00001195
Greg Steindd6eefb2000-07-18 09:09:48 +00001196 def getreply(self):
1197 """Compat definition since superclass does not define it.
Greg Stein5e0fa402000-06-26 08:28:01 +00001198
Greg Steindd6eefb2000-07-18 09:09:48 +00001199 Returns a tuple consisting of:
1200 - server status code (e.g. '200' if all goes well)
1201 - server "reason" corresponding to status code
1202 - any RFC822 headers in the response from the server
1203 """
1204 try:
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001205 response = self._conn.getresponse()
Guido van Rossumb940e112007-01-10 16:19:56 +00001206 except BadStatusLine as e:
Greg Steindd6eefb2000-07-18 09:09:48 +00001207 # keep the socket open (as a file), and return it
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001208 self.file = self._conn.sock.makefile('rb', 0)
Greg Stein5e0fa402000-06-26 08:28:01 +00001209
Greg Steindd6eefb2000-07-18 09:09:48 +00001210 # close our socket -- we want to restart after any protocol error
1211 self.close()
Greg Stein5e0fa402000-06-26 08:28:01 +00001212
Greg Steindd6eefb2000-07-18 09:09:48 +00001213 self.headers = None
1214 return -1, e.line, None
Greg Stein5e0fa402000-06-26 08:28:01 +00001215
Greg Steindd6eefb2000-07-18 09:09:48 +00001216 self.headers = response.msg
1217 self.file = response.fp
1218 return response.status, response.reason, response.msg
Greg Stein5e0fa402000-06-26 08:28:01 +00001219
Greg Steindd6eefb2000-07-18 09:09:48 +00001220 def close(self):
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001221 self._conn.close()
Greg Stein5e0fa402000-06-26 08:28:01 +00001222
Greg Steindd6eefb2000-07-18 09:09:48 +00001223 # note that self.file == response.fp, which gets closed by the
1224 # superclass. just clear the object ref here.
1225 ### hmm. messy. if status==-1, then self.file is owned by us.
1226 ### well... we aren't explicitly closing, but losing this ref will
1227 ### do it
1228 self.file = None
Greg Stein5e0fa402000-06-26 08:28:01 +00001229
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001230if hasattr(socket, 'ssl'):
1231 class HTTPS(HTTP):
1232 """Compatibility with 1.5 httplib interface
1233
1234 Python 1.5.2 did not have an HTTPS class, but it defined an
1235 interface for sending http requests that is also useful for
Tim Peters5ceadc82001-01-13 19:16:21 +00001236 https.
Jeremy Hylton29b8d5a2000-08-01 17:33:32 +00001237 """
1238
Martin v. Löwisd7bf9742000-09-21 22:09:47 +00001239 _connection_class = HTTPSConnection
Tim Peters5ceadc82001-01-13 19:16:21 +00001240
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001241 def __init__(self, host='', port=None, key_file=None, cert_file=None,
1242 strict=None):
Greg Stein81937a42001-08-18 09:20:23 +00001243 # provide a default host, pass the X509 cert info
1244
1245 # urf. compensate for bad input.
1246 if port == 0:
1247 port = None
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001248 self._setup(self._connection_class(host, port, key_file,
1249 cert_file, strict))
Greg Stein81937a42001-08-18 09:20:23 +00001250
1251 # we never actually use these for anything, but we keep them
1252 # here for compatibility with post-1.5.2 CVS.
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001253 self.key_file = key_file
1254 self.cert_file = cert_file
Greg Stein81937a42001-08-18 09:20:23 +00001255
Greg Stein5e0fa402000-06-26 08:28:01 +00001256
1257class HTTPException(Exception):
Jeremy Hylton12f4f352002-07-06 18:55:01 +00001258 # Subclasses that define an __init__ must call Exception.__init__
1259 # or define self.args. Otherwise, str() will fail.
Greg Steindd6eefb2000-07-18 09:09:48 +00001260 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001261
1262class NotConnected(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001263 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001264
Skip Montanaro9d389972002-03-24 16:53:50 +00001265class InvalidURL(HTTPException):
1266 pass
1267
Greg Stein5e0fa402000-06-26 08:28:01 +00001268class UnknownProtocol(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001269 def __init__(self, version):
Jeremy Hylton12f4f352002-07-06 18:55:01 +00001270 self.args = version,
Greg Steindd6eefb2000-07-18 09:09:48 +00001271 self.version = version
Greg Stein5e0fa402000-06-26 08:28:01 +00001272
1273class UnknownTransferEncoding(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001274 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001275
Greg Stein5e0fa402000-06-26 08:28:01 +00001276class UnimplementedFileMode(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001277 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001278
1279class IncompleteRead(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001280 def __init__(self, partial):
Jeremy Hylton12f4f352002-07-06 18:55:01 +00001281 self.args = partial,
Greg Steindd6eefb2000-07-18 09:09:48 +00001282 self.partial = partial
Greg Stein5e0fa402000-06-26 08:28:01 +00001283
1284class ImproperConnectionState(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001285 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001286
1287class CannotSendRequest(ImproperConnectionState):
Greg Steindd6eefb2000-07-18 09:09:48 +00001288 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001289
1290class CannotSendHeader(ImproperConnectionState):
Greg Steindd6eefb2000-07-18 09:09:48 +00001291 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001292
1293class ResponseNotReady(ImproperConnectionState):
Greg Steindd6eefb2000-07-18 09:09:48 +00001294 pass
Greg Stein5e0fa402000-06-26 08:28:01 +00001295
1296class BadStatusLine(HTTPException):
Greg Steindd6eefb2000-07-18 09:09:48 +00001297 def __init__(self, line):
Jeremy Hylton12f4f352002-07-06 18:55:01 +00001298 self.args = line,
Greg Steindd6eefb2000-07-18 09:09:48 +00001299 self.line = line
Greg Stein5e0fa402000-06-26 08:28:01 +00001300
1301# for backwards compatibility
1302error = HTTPException
1303
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001304class LineAndFileWrapper:
1305 """A limited file-like object for HTTP/0.9 responses."""
1306
1307 # The status-line parsing code calls readline(), which normally
1308 # get the HTTP status line. For a 0.9 response, however, this is
1309 # actually the first line of the body! Clients need to get a
1310 # readable file object that contains that line.
1311
1312 def __init__(self, line, file):
1313 self._line = line
1314 self._file = file
1315 self._line_consumed = 0
1316 self._line_offset = 0
1317 self._line_left = len(line)
1318
1319 def __getattr__(self, attr):
1320 return getattr(self._file, attr)
1321
1322 def _done(self):
1323 # called when the last byte is read from the line. After the
1324 # call, all read methods are delegated to the underlying file
Skip Montanaro74b9a7a2003-02-25 17:48:15 +00001325 # object.
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001326 self._line_consumed = 1
1327 self.read = self._file.read
1328 self.readline = self._file.readline
1329 self.readlines = self._file.readlines
1330
1331 def read(self, amt=None):
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001332 if self._line_consumed:
1333 return self._file.read(amt)
1334 assert self._line_left
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001335 if amt is None or amt > self._line_left:
1336 s = self._line[self._line_offset:]
1337 self._done()
1338 if amt is None:
1339 return s + self._file.read()
1340 else:
Tim Petersc411dba2002-07-16 21:35:23 +00001341 return s + self._file.read(amt - len(s))
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001342 else:
1343 assert amt <= self._line_left
1344 i = self._line_offset
1345 j = i + amt
1346 s = self._line[i:j]
1347 self._line_offset = j
1348 self._line_left -= amt
1349 if self._line_left == 0:
1350 self._done()
1351 return s
Tim Petersc411dba2002-07-16 21:35:23 +00001352
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001353 def readline(self):
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001354 if self._line_consumed:
1355 return self._file.readline()
1356 assert self._line_left
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001357 s = self._line[self._line_offset:]
1358 self._done()
1359 return s
1360
1361 def readlines(self, size=None):
Hye-Shik Chang39aef792004-06-05 13:30:56 +00001362 if self._line_consumed:
1363 return self._file.readlines(size)
1364 assert self._line_left
Jeremy Hyltond46aa372002-07-06 18:48:07 +00001365 L = [self._line[self._line_offset:]]
1366 self._done()
1367 if size is None:
1368 return L + self._file.readlines()
1369 else:
1370 return L + self._file.readlines(size)
Greg Stein5e0fa402000-06-26 08:28:01 +00001371
Guido van Rossum23acc951994-02-21 16:36:04 +00001372def test():
Guido van Rossum41999c11997-12-09 00:12:23 +00001373 """Test this module.
1374
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001375 A hodge podge of tests collected here, because they have too many
1376 external dependencies for the regular test suite.
Guido van Rossum41999c11997-12-09 00:12:23 +00001377 """
Greg Stein5e0fa402000-06-26 08:28:01 +00001378
Guido van Rossum41999c11997-12-09 00:12:23 +00001379 import sys
1380 import getopt
1381 opts, args = getopt.getopt(sys.argv[1:], 'd')
1382 dl = 0
1383 for o, a in opts:
1384 if o == '-d': dl = dl + 1
1385 host = 'www.python.org'
1386 selector = '/'
1387 if args[0:]: host = args[0]
1388 if args[1:]: selector = args[1]
1389 h = HTTP()
1390 h.set_debuglevel(dl)
1391 h.connect(host)
1392 h.putrequest('GET', selector)
1393 h.endheaders()
Greg Stein5e0fa402000-06-26 08:28:01 +00001394 status, reason, headers = h.getreply()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001395 print('status =', status)
1396 print('reason =', reason)
Jeremy Hylton97043c32007-08-04 02:34:24 +00001397 print('read', len(h.getfile().read()))
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001398 print()
Guido van Rossum41999c11997-12-09 00:12:23 +00001399 if headers:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001400 for header in headers.headers: print(header.strip())
1401 print()
Greg Stein5e0fa402000-06-26 08:28:01 +00001402
Jeremy Hylton8acf1e02002-03-08 19:35:51 +00001403 # minimal test that code to extract host from url works
1404 class HTTP11(HTTP):
1405 _http_vsn = 11
1406 _http_vsn_str = 'HTTP/1.1'
1407
1408 h = HTTP11('www.python.org')
1409 h.putrequest('GET', 'http://www.python.org/~jeremy/')
1410 h.endheaders()
1411 h.getreply()
1412 h.close()
1413
Greg Stein5e0fa402000-06-26 08:28:01 +00001414 if hasattr(socket, 'ssl'):
Tim Petersc411dba2002-07-16 21:35:23 +00001415
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001416 for host, selector in (('sourceforge.net', '/projects/python'),
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001417 ):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001418 print("https://%s%s" % (host, selector))
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001419 hs = HTTPS()
Jeremy Hylton8531b1b2002-07-16 21:21:11 +00001420 hs.set_debuglevel(dl)
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001421 hs.connect(host)
1422 hs.putrequest('GET', selector)
1423 hs.endheaders()
1424 status, reason, headers = hs.getreply()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001425 print('status =', status)
1426 print('reason =', reason)
1427 print("read", len(hs.getfile().read()))
1428 print()
Jeremy Hylton29d27ac2002-07-09 21:22:36 +00001429 if headers:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001430 for header in headers.headers: print(header.strip())
1431 print()
Guido van Rossum23acc951994-02-21 16:36:04 +00001432
Guido van Rossum23acc951994-02-21 16:36:04 +00001433if __name__ == '__main__':
Guido van Rossum41999c11997-12-09 00:12:23 +00001434 test()