annotate Lib/http/client.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 03cc1b55faae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1 """HTTP/1.1 client library
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
2
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
3 <intro stuff goes here>
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
4 <other stuff, too>
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
5
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38104
diff changeset
6 HTTPConnection goes through a number of "states", which define when a client
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
7 may legally make another request or fetch the response for a particular
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
8 request. This diagram details these state transitions:
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
10 (null)
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
11 |
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
12 | HTTPConnection()
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
13 v
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
14 Idle
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
15 |
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
16 | putrequest()
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
17 v
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
18 Request-started
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
19 |
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
20 | ( putheader() )* endheaders()
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
21 v
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
22 Request-sent
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
23 |
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
24 | response = getresponse()
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
25 v
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
26 Unread-response [Response-headers-read]
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
27 |\____________________
15845
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
28 | |
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
29 | response.read() | putrequest()
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
30 v v
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
31 Idle Req-started-unread-response
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
32 ______/|
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
33 / |
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
34 response.read() | | ( putheader() )* endheaders()
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
35 v v
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
36 Request-started Req-sent-unread-response
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
37 |
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
38 | response.read()
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
39 v
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
40 Request-sent
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
41
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
42 This diagram presents the following rules:
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
43 -- a second request may not be started until {response-headers-read}
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
44 -- a response [object] cannot be retrieved until {request-sent}
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
45 -- there is no differentiation between an unread response body and a
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
46 partially read response body
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
47
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
48 Note: this enforcement is applied by the HTTPConnection class. The
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
49 HTTPResponse class does not enforce this state machine, which
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
50 implies sophisticated clients may accelerate the request/response
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
51 pipeline. Caution should be taken, though: accelerating the states
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
52 beyond the above pattern may imply knowledge of the server's
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
53 connection-close behavior for certain requests. For example, it
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
54 is impossible to tell whether the server will close the connection
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
55 UNTIL the response headers have been read; this means that further
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
56 requests cannot be placed into the pipeline until it is known that
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
57 the server will NOT be closing the connection.
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
58
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
59 Logical State __state __response
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
60 ------------- ------- ----------
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
61 Idle _CS_IDLE None
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
62 Request-started _CS_REQ_STARTED None
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
63 Request-sent _CS_REQ_SENT None
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
64 Unread-response _CS_IDLE <response_class>
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
65 Req-started-unread-response _CS_REQ_STARTED <response_class>
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
66 Req-sent-unread-response _CS_REQ_SENT <response_class>
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
67 """
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
69 import email.parser
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
70 import email.message
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
71 import io
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
72 import os
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
73 import socket
67046
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
74 import collections
48334
5a416a6417d3 Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 47635
diff changeset
75 from urllib.parse import urlsplit
43292
876b7ef4775d Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43095
diff changeset
76 import warnings
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
78 __all__ = ["HTTPResponse", "HTTPConnection",
18013
99aea6f5cebd more public symbols for __all__
Skip Montanaro <skip@pobox.com>
parents: 17482
diff changeset
79 "HTTPException", "NotConnected", "UnknownProtocol",
24042
63fbbed10714 Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24038
diff changeset
80 "UnknownTransferEncoding", "UnimplementedFileMode",
63fbbed10714 Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24038
diff changeset
81 "IncompleteRead", "InvalidURL", "ImproperConnectionState",
63fbbed10714 Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24038
diff changeset
82 "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
83 "BadStatusLine", "error", "responses"]
16160
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15878
diff changeset
84
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 HTTP_PORT = 80
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
86 HTTPS_PORT = 443
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
87
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
88 _UNKNOWN = 'UNKNOWN'
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
89
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
90 # connection states
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
91 _CS_IDLE = 'Idle'
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
92 _CS_REQ_STARTED = 'Request-started'
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
93 _CS_REQ_SENT = 'Request-sent'
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
94
33506
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
95 # status codes
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
96 # informational
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
97 CONTINUE = 100
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
98 SWITCHING_PROTOCOLS = 101
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
99 PROCESSING = 102
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
100
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
101 # successful
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
102 OK = 200
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
103 CREATED = 201
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
104 ACCEPTED = 202
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
105 NON_AUTHORITATIVE_INFORMATION = 203
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
106 NO_CONTENT = 204
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
107 RESET_CONTENT = 205
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
108 PARTIAL_CONTENT = 206
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
109 MULTI_STATUS = 207
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
110 IM_USED = 226
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
111
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
112 # redirection
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
113 MULTIPLE_CHOICES = 300
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
114 MOVED_PERMANENTLY = 301
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
115 FOUND = 302
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
116 SEE_OTHER = 303
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
117 NOT_MODIFIED = 304
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
118 USE_PROXY = 305
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
119 TEMPORARY_REDIRECT = 307
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
120
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
121 # client error
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
122 BAD_REQUEST = 400
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
123 UNAUTHORIZED = 401
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
124 PAYMENT_REQUIRED = 402
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
125 FORBIDDEN = 403
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
126 NOT_FOUND = 404
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
127 METHOD_NOT_ALLOWED = 405
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
128 NOT_ACCEPTABLE = 406
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
129 PROXY_AUTHENTICATION_REQUIRED = 407
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
130 REQUEST_TIMEOUT = 408
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
131 CONFLICT = 409
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
132 GONE = 410
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
133 LENGTH_REQUIRED = 411
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
134 PRECONDITION_FAILED = 412
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
135 REQUEST_ENTITY_TOO_LARGE = 413
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
136 REQUEST_URI_TOO_LONG = 414
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
137 UNSUPPORTED_MEDIA_TYPE = 415
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
138 REQUESTED_RANGE_NOT_SATISFIABLE = 416
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
139 EXPECTATION_FAILED = 417
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
140 UNPROCESSABLE_ENTITY = 422
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
141 LOCKED = 423
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
142 FAILED_DEPENDENCY = 424
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
143 UPGRADE_REQUIRED = 426
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
144 PRECONDITION_REQUIRED = 428
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
145 TOO_MANY_REQUESTS = 429
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
146 REQUEST_HEADER_FIELDS_TOO_LARGE = 431
33506
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
147
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
148 # server error
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
149 INTERNAL_SERVER_ERROR = 500
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
150 NOT_IMPLEMENTED = 501
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
151 BAD_GATEWAY = 502
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
152 SERVICE_UNAVAILABLE = 503
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
153 GATEWAY_TIMEOUT = 504
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
154 HTTP_VERSION_NOT_SUPPORTED = 505
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
155 INSUFFICIENT_STORAGE = 507
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
156 NOT_EXTENDED = 510
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
157 NETWORK_AUTHENTICATION_REQUIRED = 511
33506
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
158
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
159 # Mapping status codes to official W3C names
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
160 responses = {
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
161 100: 'Continue',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
162 101: 'Switching Protocols',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
163
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
164 200: 'OK',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
165 201: 'Created',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
166 202: 'Accepted',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
167 203: 'Non-Authoritative Information',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
168 204: 'No Content',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
169 205: 'Reset Content',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
170 206: 'Partial Content',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
171
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
172 300: 'Multiple Choices',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
173 301: 'Moved Permanently',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
174 302: 'Found',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
175 303: 'See Other',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
176 304: 'Not Modified',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
177 305: 'Use Proxy',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
178 306: '(Unused)',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
179 307: 'Temporary Redirect',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
180
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
181 400: 'Bad Request',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
182 401: 'Unauthorized',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
183 402: 'Payment Required',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
184 403: 'Forbidden',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
185 404: 'Not Found',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
186 405: 'Method Not Allowed',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
187 406: 'Not Acceptable',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
188 407: 'Proxy Authentication Required',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
189 408: 'Request Timeout',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
190 409: 'Conflict',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
191 410: 'Gone',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
192 411: 'Length Required',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
193 412: 'Precondition Failed',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
194 413: 'Request Entity Too Large',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
195 414: 'Request-URI Too Long',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
196 415: 'Unsupported Media Type',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
197 416: 'Requested Range Not Satisfiable',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
198 417: 'Expectation Failed',
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
199 428: 'Precondition Required',
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
200 429: 'Too Many Requests',
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
201 431: 'Request Header Fields Too Large',
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
202
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
203 500: 'Internal Server Error',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
204 501: 'Not Implemented',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
205 502: 'Bad Gateway',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
206 503: 'Service Unavailable',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
207 504: 'Gateway Timeout',
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
208 505: 'HTTP Version Not Supported',
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76607
diff changeset
209 511: 'Network Authentication Required',
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
210 }
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 35652
diff changeset
211
35652
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
212 # maximal amount of data to read at one time in _safe_read
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
213 MAXAMOUNT = 1048576
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
214
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
215 # maximal line length when calling readline().
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
216 _MAXLINE = 65536
86679
e445d02e5306 Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
Georg Brandl <georg@python.org>
parents: 83275
diff changeset
217 _MAXHEADERS = 100
e445d02e5306 Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
Georg Brandl <georg@python.org>
parents: 83275
diff changeset
218
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
219
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
220 class HTTPMessage(email.message.Message):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
221 # XXX The only usage of this method is in
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
222 # http.server.CGIHTTPRequestHandler. Maybe move the code there so
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
223 # that it doesn't need to be part of the public API. The API has
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
224 # never been defined so this could cause backwards compatibility
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
225 # issues.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
226
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
227 def getallmatchingheaders(self, name):
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
228 """Find all header lines matching a given header name.
24123
aa6217bf9776 Fix for SF bug #432621: httplib: multiple Set-Cookie headers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24120
diff changeset
229
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
230 Look through the list of headers and find all lines matching a given
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
231 header name (and their continuation lines). A list of the lines is
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
232 returned, without interpretation. If the header does not occur, an
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
233 empty list is returned. If the header occurs multiple times, all
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
234 occurrences are returned. Case is not important in the header name.
24123
aa6217bf9776 Fix for SF bug #432621: httplib: multiple Set-Cookie headers
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24120
diff changeset
235
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
236 """
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
237 name = name.lower() + ':'
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
238 n = len(name)
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
239 lst = []
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
240 hit = 0
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
241 for line in self.keys():
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
242 if line[:n].lower() == name:
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
243 hit = 1
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
244 elif not line[:1].isspace():
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
245 hit = 0
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
246 if hit:
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
247 lst.append(line)
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
248 return lst
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
249
52369
8d6918af232b Fix compatibility issue with HTTPMessage class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
250 def parse_headers(fp, _class=HTTPMessage):
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
251 """Parses only RFC2822 headers from a file pointer.
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
252
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
253 email Parser wants to see strings rather than bytes.
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
254 But a TextIOWrapper around self.rfile would buffer too many bytes
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
255 from the stream, bytes which we later need to read as bytes.
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
256 So we read the correct bytes here, as bytes, for email Parser
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
257 to parse.
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
258
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
259 """
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
260 headers = []
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
261 while True:
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
262 line = fp.readline(_MAXLINE + 1)
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
263 if len(line) > _MAXLINE:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
264 raise LineTooLong("header line")
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
265 headers.append(line)
86679
e445d02e5306 Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
Georg Brandl <georg@python.org>
parents: 83275
diff changeset
266 if len(headers) > _MAXHEADERS:
e445d02e5306 Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
Georg Brandl <georg@python.org>
parents: 83275
diff changeset
267 raise HTTPException("got more than %d headers" % _MAXHEADERS)
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
268 if line in (b'\r\n', b'\n', b''):
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
269 break
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
270 hstring = b''.join(headers).decode('iso-8859-1')
52369
8d6918af232b Fix compatibility issue with HTTPMessage class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
271 return email.parser.Parser(_class=_class).parsestr(hstring)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
272
24119
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
273
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
274 class HTTPResponse(io.RawIOBase):
24119
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
275
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
276 # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
277
42425
7794da1334a0 Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42133
diff changeset
278 # The bytes from the socket object are iso-8859-1 strings.
7794da1334a0 Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42133
diff changeset
279 # See RFC 2616 sec 2.2 which notes an exception for MIME-encoded
7794da1334a0 Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42133
diff changeset
280 # text following RFC 2047. The basic status line parsing only
7794da1334a0 Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42133
diff changeset
281 # accepts iso-8859-1.
7794da1334a0 Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42133
diff changeset
282
82735
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
283 def __init__(self, sock, debuglevel=0, method=None, url=None):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
284 # If the response includes a content-length header, we need to
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
285 # make sure that the client doesn't read more than the
42452
f4409c5d7994 Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42444
diff changeset
286 # specified number of bytes. If it does, it will block until
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
287 # the server times out and closes the connection. This will
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
288 # happen if a self.fp.read() is done (without a size) whether
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
289 # self.fp is buffered or not. So, no self.fp.read() by
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
290 # clients unless they know what they are doing.
51449
ccc7188fdd28 revert 69416; it didn't help much
Benjamin Peterson <benjamin@python.org>
parents: 51447
diff changeset
291 self.fp = sock.makefile("rb")
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
292 self.debuglevel = debuglevel
28978
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
293 self._method = method
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
294
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
295 # The HTTPResponse object is returned via urllib. The clients
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
296 # of http and urllib expect different attributes for the
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
297 # headers. headers is used here and supports urllib. msg is
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
298 # provided as a backwards compatibility layer for http
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
299 # clients.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
300
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
301 self.headers = self.msg = None
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
302
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
303 # from the Status-Line of the response
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
304 self.version = _UNKNOWN # HTTP-Version
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
305 self.status = _UNKNOWN # Status-Code
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
306 self.reason = _UNKNOWN # Reason-Phrase
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
307
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
308 self.chunked = _UNKNOWN # is "chunked" being used?
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
309 self.chunk_left = _UNKNOWN # bytes left to read in current chunk
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
310 self.length = _UNKNOWN # number of bytes left in response
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
311 self.will_close = _UNKNOWN # conn will close at end of response
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
312
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
313 def _read_status(self):
67020
0b94635ed2f6 Fix Issue6791 - Limit the HTTP header readline with _MAXLENGTH. Patch by Antoine Pitrou
Senthil Kumaran <orsenthil@gmail.com>
parents: 66988
diff changeset
314 line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
0b94635ed2f6 Fix Issue6791 - Limit the HTTP header readline with _MAXLENGTH. Patch by Antoine Pitrou
Senthil Kumaran <orsenthil@gmail.com>
parents: 66988
diff changeset
315 if len(line) > _MAXLINE:
0b94635ed2f6 Fix Issue6791 - Limit the HTTP header readline with _MAXLENGTH. Patch by Antoine Pitrou
Senthil Kumaran <orsenthil@gmail.com>
parents: 66988
diff changeset
316 raise LineTooLong("status line")
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
317 if self.debuglevel > 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
318 print("reply:", repr(line))
29688
9fe67f7f3459 Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29584
diff changeset
319 if not line:
9fe67f7f3459 Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29584
diff changeset
320 # Presumably, the server closed the connection before
9fe67f7f3459 Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29584
diff changeset
321 # sending a valid response.
9fe67f7f3459 Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29584
diff changeset
322 raise BadStatusLine(line)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
323 try:
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
324 version, status, reason = line.split(None, 2)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
325 except ValueError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
326 try:
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
327 version, status = line.split(None, 1)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
328 reason = ""
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
329 except ValueError:
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
330 # empty version will cause next test to fail.
24119
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
331 version = ""
42426
ae72bf5ae465 Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42425
diff changeset
332 if not version.startswith("HTTP/"):
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
333 self._close_conn()
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
334 raise BadStatusLine(line)
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
335
17482
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
336 # The status code is a three-digit number
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
337 try:
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
338 status = int(status)
17482
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
339 if status < 100 or status > 999:
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
340 raise BadStatusLine(line)
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
341 except ValueError:
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
342 raise BadStatusLine(line)
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
343 return version, status, reason
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
344
24216
c8a19a428358 Change _begin() back to begin().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24168
diff changeset
345 def begin(self):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
346 if self.headers is not None:
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
347 # we've already started reading the response
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
348 return
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
349
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
350 # read until we get a non-100 response
28083
29965d0d4650 Module review:
Raymond Hettinger <python@rcn.com>
parents: 28063
diff changeset
351 while True:
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
352 version, status, reason = self._read_status()
33506
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
353 if status != CONTINUE:
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
354 break
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
355 # skip the header from the 100 response
28083
29965d0d4650 Module review:
Raymond Hettinger <python@rcn.com>
parents: 28063
diff changeset
356 while True:
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
357 skip = self.fp.readline(_MAXLINE + 1)
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
358 if len(skip) > _MAXLINE:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
359 raise LineTooLong("header line")
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
360 skip = skip.strip()
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
361 if not skip:
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
362 break
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
363 if self.debuglevel > 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
364 print("header:", skip)
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24262
diff changeset
365
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
366 self.code = self.status = status
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
367 self.reason = reason.strip()
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
368 if version in ("HTTP/1.0", "HTTP/0.9"):
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
369 # Some servers might still return "0.9", treat it as 1.0 anyway
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
370 self.version = 10
42426
ae72bf5ae465 Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42425
diff changeset
371 elif version.startswith("HTTP/1."):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
372 self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
373 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
374 raise UnknownProtocol(version)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
375
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
376 self.headers = self.msg = parse_headers(self.fp)
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
377
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
378 if self.debuglevel > 0:
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
379 for hdr in self.headers:
42426
ae72bf5ae465 Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42425
diff changeset
380 print("header:", hdr, end=" ")
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
382 # are we using the chunked-style of transfer encoding?
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
383 tr_enc = self.headers.get("transfer-encoding")
25107
0ecb3cabf40b SF bug [ 600488 ] Robustness tweak to httplib.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24674
diff changeset
384 if tr_enc and tr_enc.lower() == "chunked":
50540
4229a4b14f1d Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49907
diff changeset
385 self.chunked = True
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
386 self.chunk_left = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
387 else:
50540
4229a4b14f1d Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49907
diff changeset
388 self.chunked = False
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
390 # will the connection close at the end of the response?
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
391 self.will_close = self._check_close()
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
392
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
393 # do we have a Content-Length?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
394 # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
42442
0fa84ffba769 Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42440
diff changeset
395 self.length = None
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
396 length = self.headers.get("content-length")
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
397
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47559
diff changeset
398 # are we using the chunked-style of transfer encoding?
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
399 tr_enc = self.headers.get("transfer-encoding")
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
400 if length and not self.chunked:
14560
eeb8ee488106 cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14140
diff changeset
401 try:
eeb8ee488106 cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14140
diff changeset
402 self.length = int(length)
eeb8ee488106 cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14140
diff changeset
403 except ValueError:
45504
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
404 self.length = None
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
405 else:
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
406 if self.length < 0: # ignore nonsensical negative lengths
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
407 self.length = None
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
408 else:
1b6fb5699634 Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
409 self.length = None
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
410
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
411 # does the body have a fixed length? (of zero)
33506
4b8198a74873 Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 33496
diff changeset
412 if (status == NO_CONTENT or status == NOT_MODIFIED or
28978
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
413 100 <= status < 200 or # 1xx codes
42426
ae72bf5ae465 Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42425
diff changeset
414 self._method == "HEAD"):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
415 self.length = 0
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
417 # if the connection remains open, and we aren't using chunked, and
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
418 # a content-length was not provided, then assume that the connection
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
419 # WILL close.
42442
0fa84ffba769 Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42440
diff changeset
420 if (not self.will_close and
0fa84ffba769 Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42440
diff changeset
421 not self.chunked and
0fa84ffba769 Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42440
diff changeset
422 self.length is None):
50540
4229a4b14f1d Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49907
diff changeset
423 self.will_close = True
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
424
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
425 def _check_close(self):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
426 conn = self.headers.get("connection")
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
427 if self.version == 11:
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
428 # An HTTP/1.1 proxy is assumed to stay open unless
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
429 # explicitly closed.
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
430 conn = self.headers.get("connection")
31967
3d046d063a84 Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents: 31389
diff changeset
431 if conn and "close" in conn.lower():
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
432 return True
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
433 return False
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
434
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
435 # Some HTTP/1.0 implementations have support for persistent
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
436 # connections, using rules different than HTTP/1.1.
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
437
44230
6b517ac10e8e Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
438 # For older HTTP, Keep-Alive indicates persistent connection.
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
439 if self.headers.get("keep-alive"):
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
440 return False
26387
fcf9734e1021 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26188
diff changeset
441
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
442 # At least Akamai returns a "Connection: Keep-Alive" header,
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
443 # which was supposed to be sent by the client.
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
444 if conn and "keep-alive" in conn.lower():
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
445 return False
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
446
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
447 # Proxy-Connection is a netscape hack.
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
448 pconn = self.headers.get("proxy-connection")
31967
3d046d063a84 Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents: 31389
diff changeset
449 if pconn and "keep-alive" in pconn.lower():
26187
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
450 return False
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
451
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
452 # otherwise, assume it will close
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
453 return True
ae169f5b94e0 Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26126
diff changeset
454
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
455 def _close_conn(self):
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
456 fp = self.fp
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
457 self.fp = None
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
458 fp.close()
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
459
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
460 def close(self):
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
461 super().close() # set "closed" flag
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
462 if self.fp:
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
463 self._close_conn()
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
464
42497
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
465 # These implementations are for the benefit of io.BufferedReader.
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
466
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
467 # XXX This class should probably be revised to act more like
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
468 # the "raw stream" that BufferedReader expects.
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
469
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
470 def flush(self):
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
471 super().flush()
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
472 if self.fp:
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
473 self.fp.flush()
42497
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
474
52631
d85090a480ba An HTTPResponse is, by its nature, readable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52522
diff changeset
475 def readable(self):
d85090a480ba An HTTPResponse is, by its nature, readable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52522
diff changeset
476 return True
d85090a480ba An HTTPResponse is, by its nature, readable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52522
diff changeset
477
42497
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
478 # End of "raw stream" methods
17cdbedbb15d Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42466
diff changeset
479
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
480 def isclosed(self):
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
481 """True if the connection is closed."""
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
482 # NOTE: it is possible that we will not ever call self.close(). This
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
483 # case occurs when will_close is TRUE, length is None, and we
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
484 # read up to the last byte, but NOT past it.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
485 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
486 # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
487 # called, meaning self.isclosed() is meaningful.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
488 return self.fp is None
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
489
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
490 def read(self, amt=None):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
491 if self.fp is None:
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
492 return b""
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493
61061
6950eab51ffe Merged revisions 80587 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57802
diff changeset
494 if self._method == "HEAD":
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
495 self._close_conn()
61061
6950eab51ffe Merged revisions 80587 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57802
diff changeset
496 return b""
6950eab51ffe Merged revisions 80587 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57802
diff changeset
497
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
498 if amt is not None:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
499 # Amount is given, so call base class version
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
500 # (which is implemented in terms of self.readinto)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
501 return super(HTTPResponse, self).read(amt)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
502 else:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
503 # Amount is not given (unbounded read) so we must check self.length
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
504 # and self.chunked
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25112
diff changeset
505
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
506 if self.chunked:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
507 return self._readall_chunked()
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
508
33933
eb231544c1ad Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33761
diff changeset
509 if self.length is None:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
510 s = self.fp.read()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
511 else:
81950
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
512 try:
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
513 s = self._safe_read(self.length)
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
514 except IncompleteRead:
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
515 self._close_conn()
81950
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
516 raise
33933
eb231544c1ad Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33761
diff changeset
517 self.length = 0
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
518 self._close_conn() # we read everything
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
519 return s
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
521 def readinto(self, b):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
522 if self.fp is None:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
523 return 0
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
524
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
525 if self._method == "HEAD":
82028
0461ed77ee4e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81954 82027
diff changeset
526 self._close_conn()
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
527 return 0
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
528
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
529 if self.chunked:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
530 return self._readinto_chunked(b)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
531
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
532 if self.length is not None:
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
533 if len(b) > self.length:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
534 # clip the read to the "end of response"
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
535 b = memoryview(b)[0:self.length]
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
537 # we do not use _safe_read() here because this may be a .will_close
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
538 # connection, and the user is reading more bytes than will be provided
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
539 # (for example, reading in 1k chunks)
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
540 n = self.fp.readinto(b)
81951
7d504068bc58 Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80860 81950
diff changeset
541 if not n:
81950
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
542 # Ideally, we would raise IncompleteRead if the content-length
03c536afeb7e Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80859
diff changeset
543 # wasn't satisfied, but it might break compatibility.
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
544 self._close_conn()
81953
41d2c18abbf4 Simplify code in HTTPResponse.read()
Antoine Pitrou <solipsis@pitrou.net>
parents: 81950
diff changeset
545 elif self.length is not None:
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
546 self.length -= n
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
547 if not self.length:
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
548 self._close_conn()
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
549 return n
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
550
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
551 def _read_next_chunk_size(self):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
552 # Read the next chunk size from the file
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
553 line = self.fp.readline(_MAXLINE + 1)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
554 if len(line) > _MAXLINE:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
555 raise LineTooLong("chunk size")
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
556 i = line.find(b";")
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
557 if i >= 0:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
558 line = line[:i] # strip chunk-extensions
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
559 try:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
560 return int(line, 16)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
561 except ValueError:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
562 # close the connection as protocol synchronisation is
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
563 # probably lost
82028
0461ed77ee4e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81954 82027
diff changeset
564 self._close_conn()
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
565 raise
25112
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
566
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
567 def _read_and_discard_trailer(self):
25112
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
568 # read and discard trailer up to the CRLF terminator
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
569 ### note: we shouldn't have any trailers!
28083
29965d0d4650 Module review:
Raymond Hettinger <python@rcn.com>
parents: 28063
diff changeset
570 while True:
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
571 line = self.fp.readline(_MAXLINE + 1)
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
572 if len(line) > _MAXLINE:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
573 raise LineTooLong("trailer line")
45307
f1205121e848 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60735-60751 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44967
diff changeset
574 if not line:
f1205121e848 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60735-60751 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44967
diff changeset
575 # a vanishingly small number of sites EOF without
f1205121e848 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60735-60751 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44967
diff changeset
576 # sending the trailer
f1205121e848 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60735-60751 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44967
diff changeset
577 break
76606
1a9d38966395 httplib - minor update to check empty response
Senthil Kumaran <senthil@uthcode.com>
parents: 76491
diff changeset
578 if line in (b'\r\n', b'\n', b''):
25112
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
579 break
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
580
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
581 def _readall_chunked(self):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
582 assert self.chunked != _UNKNOWN
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
583 chunk_left = self.chunk_left
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
584 value = []
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
585 while True:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
586 if chunk_left is None:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
587 try:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
588 chunk_left = self._read_next_chunk_size()
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
589 if chunk_left == 0:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
590 break
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
591 except ValueError:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
592 raise IncompleteRead(b''.join(value))
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
593 value.append(self._safe_read(chunk_left))
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
594
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
595 # we read the whole chunk, get another
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
596 self._safe_read(2) # toss the CRLF at the end of the chunk
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
597 chunk_left = None
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
598
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
599 self._read_and_discard_trailer()
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
600
25112
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
601 # we read everything; close the "file"
82027
6cc5bbfcf04e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81953
diff changeset
602 self._close_conn()
25112
f0ff060cd194 Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 25107
diff changeset
603
55834
266d7dc3d655 Fixes issue #6838: use a list to accumulate the value instead of repeatedly concatenating strings.
Chris Withers <chris@simplistix.co.uk>
parents: 55444
diff changeset
604 return b''.join(value)
26126
98f3fd1ed42f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25112
diff changeset
605
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
606 def _readinto_chunked(self, b):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
607 assert self.chunked != _UNKNOWN
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
608 chunk_left = self.chunk_left
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
609
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
610 total_bytes = 0
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
611 mvb = memoryview(b)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
612 while True:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
613 if chunk_left is None:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
614 try:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
615 chunk_left = self._read_next_chunk_size()
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
616 if chunk_left == 0:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
617 break
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
618 except ValueError:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
619 raise IncompleteRead(bytes(b[0:total_bytes]))
73876
daaacc0ec584 Fix dangling whitespace
Antoine Pitrou <solipsis@pitrou.net>
parents: 73875
diff changeset
620
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
621 if len(mvb) < chunk_left:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
622 n = self._safe_readinto(mvb)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
623 self.chunk_left = chunk_left - n
74271
4b21f651eeee Issue #13713: fix a regression in HTTP chunked reading after 806cfe39f729
Antoine Pitrou <solipsis@pitrou.net>
parents: 73876
diff changeset
624 return total_bytes + n
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
625 elif len(mvb) == chunk_left:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
626 n = self._safe_readinto(mvb)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
627 self._safe_read(2) # toss the CRLF at the end of the chunk
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
628 self.chunk_left = None
74271
4b21f651eeee Issue #13713: fix a regression in HTTP chunked reading after 806cfe39f729
Antoine Pitrou <solipsis@pitrou.net>
parents: 73876
diff changeset
629 return total_bytes + n
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
630 else:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
631 temp_mvb = mvb[0:chunk_left]
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
632 n = self._safe_readinto(temp_mvb)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
633 mvb = mvb[n:]
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
634 total_bytes += n
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
635
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
636 # we read the whole chunk, get another
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
637 self._safe_read(2) # toss the CRLF at the end of the chunk
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
638 chunk_left = None
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
639
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
640 self._read_and_discard_trailer()
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
641
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
642 # we read everything; close the "file"
82028
0461ed77ee4e Issue #16723: httplib.HTTPResponse no longer marked closed when the connection
Serhiy Storchaka <storchaka@gmail.com>
parents: 81954 82027
diff changeset
643 self._close_conn()
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
644
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
645 return total_bytes
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
646
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
647 def _safe_read(self, amt):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
648 """Read the number of bytes requested, compensating for partial reads.
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
649
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
650 Normally, we have a blocking socket, but a read() can be interrupted
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
651 by a signal (resulting in a partial read).
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
652
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
653 Note that we cannot distinguish between EOF and an interrupt when zero
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
654 bytes have been read. IncompleteRead() will be raised in this
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
655 situation.
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
656
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
657 This function should be used when <amt> bytes "should" be present for
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
658 reading. If the bytes are truly not available (due to EOF), then the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
659 IncompleteRead exception can be used to detect the problem.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
660 """
35652
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
661 s = []
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
662 while amt > 0:
35652
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
663 chunk = self.fp.read(min(amt, MAXAMOUNT))
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
664 if not chunk:
51953
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
665 raise IncompleteRead(b''.join(s), amt)
35652
6a20831d357d bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents: 35158
diff changeset
666 s.append(chunk)
28083
29965d0d4650 Module review:
Raymond Hettinger <python@rcn.com>
parents: 28063
diff changeset
667 amt -= len(chunk)
43351
84dc86f21196 Fix for bug 1148: str/bytes issue in httplib's _safe_read().
Guido van Rossum <guido@python.org>
parents: 43292
diff changeset
668 return b"".join(s)
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
669
73875
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
670 def _safe_readinto(self, b):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
671 """Same as _safe_read, but for reading into a buffer."""
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
672 total_bytes = 0
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
673 mvb = memoryview(b)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
674 while total_bytes < len(b):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
675 if MAXAMOUNT < len(mvb):
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
676 temp_mvb = mvb[0:MAXAMOUNT]
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
677 n = self.fp.readinto(temp_mvb)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
678 else:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
679 n = self.fp.readinto(mvb)
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
680 if not n:
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
681 raise IncompleteRead(bytes(mvb[0:total_bytes]), len(b))
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
682 mvb = mvb[n:]
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
683 total_bytes += n
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
684 return total_bytes
806cfe39f729 Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72982
diff changeset
685
51532
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
686 def fileno(self):
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
687 return self.fp.fileno()
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
688
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
689 def getheader(self, name, default=None):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
690 if self.headers is None:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
691 raise ResponseNotReady()
63538
904ef4bbff5e Merged revisions 83521 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 62067
diff changeset
692 headers = self.headers.get_all(name) or default
904ef4bbff5e Merged revisions 83521 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 62067
diff changeset
693 if isinstance(headers, str) or not hasattr(headers, '__iter__'):
904ef4bbff5e Merged revisions 83521 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 62067
diff changeset
694 return headers
904ef4bbff5e Merged revisions 83521 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 62067
diff changeset
695 else:
904ef4bbff5e Merged revisions 83521 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 62067
diff changeset
696 return ', '.join(headers)
11527
28b8585354de OpenSSL support. This is based on patches for a version of SSLeay by
Guido van Rossum <guido@python.org>
parents: 7122
diff changeset
697
33173
e05e9d05830d Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents: 33001
diff changeset
698 def getheaders(self):
e05e9d05830d Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents: 33001
diff changeset
699 """Return list of (header, value) tuples."""
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
700 if self.headers is None:
33173
e05e9d05830d Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents: 33001
diff changeset
701 raise ResponseNotReady()
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
702 return list(self.headers.items())
33173
e05e9d05830d Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents: 33001
diff changeset
703
51532
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
704 # We override IOBase.__iter__ so that it doesn't check for closed-ness
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
705
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
706 def __iter__(self):
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
707 return self
af469dc15fa7 Issue #4631: Fix urlopen() result when an HTTP response uses chunked encoding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51449
diff changeset
708
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
709 # For compatibility with old-style urllib responses.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
710
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
711 def info(self):
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
712 return self.headers
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
713
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
714 def geturl(self):
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
715 return self.url
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
716
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
717 def getcode(self):
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
718 return self.status
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
720 class HTTPConnection:
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
721
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
722 _http_vsn = 11
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
723 _http_vsn_str = 'HTTP/1.1'
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
724
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
725 response_class = HTTPResponse
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
726 default_port = HTTP_PORT
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
727 auto_open = 1
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
728 debuglevel = 0
81251
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
729 # TCP Maximum Segment Size (MSS) is determined by the TCP stack on
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
730 # a per-connection basis. There is no simple and efficient
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
731 # platform independent mechanism for determining the MSS, so
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
732 # instead a reasonable estimate is chosen. The getsockopt()
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
733 # interface using the TCP_MAXSEG parameter may be a suitable
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
734 # approach on some operating systems. A value of 16KiB is chosen
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
735 # as a reasonable estimate of the maximum MSS.
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
736 mss = 16384
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
737
82735
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
738 def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
739 source_address=None):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
740 self.timeout = timeout
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 57801
diff changeset
741 self.source_address = source_address
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
742 self.sock = None
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
743 self._buffer = []
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
744 self.__response = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
745 self.__state = _CS_IDLE
28978
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
746 self._method = None
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
747 self._tunnel_host = None
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
748 self._tunnel_port = None
68009
92629ccaae76 Merged revisions 87627,87638,87739,87760,87771,87787,87984,87986,88108,88115,88144,88165,88329,88364-88365,88369-88370,88423-88424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 67029
diff changeset
749 self._tunnel_headers = {}
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24262
diff changeset
750
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
751 self._set_hostport(host, port)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
752
57801
724a0da85c42 Merged revisions 76908 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56288
diff changeset
753 def set_tunnel(self, host, port=None, headers=None):
724a0da85c42 Merged revisions 76908 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56288
diff changeset
754 """ Sets up the host and the port for the HTTP CONNECT Tunnelling.
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
755
57801
724a0da85c42 Merged revisions 76908 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56288
diff changeset
756 The headers argument should be a mapping of extra HTTP headers
724a0da85c42 Merged revisions 76908 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56288
diff changeset
757 to send with the CONNECT request.
724a0da85c42 Merged revisions 76908 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56288
diff changeset
758 """
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
759 self._tunnel_host = host
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
760 self._tunnel_port = port
57802
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
761 if headers:
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
762 self._tunnel_headers = headers
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
763 else:
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
764 self._tunnel_headers.clear()
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
765
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
766 def _set_hostport(self, host, port):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
767 if port is None:
33488
71ed4b35fdc4 Search from the end of the host/port combination to find the colon which
Skip Montanaro <skip@pobox.com>
parents: 33173
diff changeset
768 i = host.rfind(':')
33491
0c299cabf4f0 missed the obvious test case and corresponding fix
Skip Montanaro <skip@pobox.com>
parents: 33488
diff changeset
769 j = host.rfind(']') # ipv6 addresses have [...]
0c299cabf4f0 missed the obvious test case and corresponding fix
Skip Montanaro <skip@pobox.com>
parents: 33488
diff changeset
770 if i > j:
22494
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
771 try:
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
772 port = int(host[i+1:])
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
773 except ValueError:
72981
6ac59218c049 Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents: 72692
diff changeset
774 if host[i+1:] == "": # http://foo.com:/ == http://foo.com/
6ac59218c049 Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents: 72692
diff changeset
775 port = self.default_port
6ac59218c049 Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents: 72692
diff changeset
776 else:
6ac59218c049 Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents: 72692
diff changeset
777 raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
778 host = host[:i]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
779 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
780 port = self.default_port
33761
d1228127d294 SF bug #1046855: httplib index out of range
Raymond Hettinger <python@rcn.com>
parents: 33506
diff changeset
781 if host and host[0] == '[' and host[-1] == ']':
33496
9e2b94a3b5dc Remove tabs.
Brett Cannon <bcannon@gmail.com>
parents: 33493
diff changeset
782 host = host[1:-1]
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
783 self.host = host
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
784 self.port = port
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
785
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
786 def set_debuglevel(self, level):
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
787 self.debuglevel = level
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
788
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
789 def _tunnel(self):
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
790 self._set_hostport(self._tunnel_host, self._tunnel_port)
68009
92629ccaae76 Merged revisions 87627,87638,87739,87760,87771,87787,87984,87986,88108,88115,88144,88165,88329,88364-88365,88369-88370,88423-88424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 67029
diff changeset
791 connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self.host, self.port)
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
792 connect_bytes = connect_str.encode("ascii")
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
793 self.send(connect_bytes)
68009
92629ccaae76 Merged revisions 87627,87638,87739,87760,87771,87787,87984,87986,88108,88115,88144,88165,88329,88364-88365,88369-88370,88423-88424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 67029
diff changeset
794 for header, value in self._tunnel_headers.items():
57802
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
795 header_str = "%s: %s\r\n" % (header, value)
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67721
diff changeset
796 header_bytes = header_str.encode("latin-1")
57802
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
797 self.send(header_bytes)
68009
92629ccaae76 Merged revisions 87627,87638,87739,87760,87771,87787,87984,87986,88108,88115,88144,88165,88329,88364-88365,88369-88370,88423-88424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 67029
diff changeset
798 self.send(b'\r\n')
57802
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
799
67721
08673e5f549a #10983: fix several bugs in the _tunnel implementation that seem to have missed while porting between branches. A unittest is needed!
Georg Brandl <georg@python.org>
parents: 67720
diff changeset
800 response = self.response_class(self.sock, method=self._method)
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
801 (version, code, message) = response._read_status()
57802
dc1aadf9fa4a Merged revisions 76910 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 56293
diff changeset
802
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
803 if code != 200:
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
804 self.close()
80933
50d19c2fac82 Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80860
diff changeset
805 raise OSError("Tunnel connection failed: %d %s" % (code,
50d19c2fac82 Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80860
diff changeset
806 message.strip()))
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
807 while True:
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
808 line = response.fp.readline(_MAXLINE + 1)
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
809 if len(line) > _MAXLINE:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
810 raise LineTooLong("header line")
76491
7787a9aebdc6 3.2 - Fix for Issue13684 - httplib tunnel infinite loop
Senthil Kumaran <senthil@uthcode.com>
parents: 72981
diff changeset
811 if not line:
7787a9aebdc6 3.2 - Fix for Issue13684 - httplib tunnel infinite loop
Senthil Kumaran <senthil@uthcode.com>
parents: 72981
diff changeset
812 # for sites which EOF without sending a trailer
7787a9aebdc6 3.2 - Fix for Issue13684 - httplib tunnel infinite loop
Senthil Kumaran <senthil@uthcode.com>
parents: 72981
diff changeset
813 break
76606
1a9d38966395 httplib - minor update to check empty response
Senthil Kumaran <senthil@uthcode.com>
parents: 76491
diff changeset
814 if line in (b'\r\n', b'\n', b''):
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
815 break
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
816
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
817 def connect(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
818 """Connect to the host and port specified in __init__."""
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
819 self.sock = socket.create_connection((self.host,self.port),
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 57801
diff changeset
820 self.timeout, self.source_address)
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
821 if self._tunnel_host:
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
822 self._tunnel()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
823
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
824 def close(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
825 """Close the connection to the HTTP server."""
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
826 if self.sock:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
827 self.sock.close() # close it manually... there may be other refs
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
828 self.sock = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
829 if self.__response:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
830 self.__response.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
831 self.__response = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
832 self.__state = _CS_IDLE
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
833
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
834 def send(self, data):
67046
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
835 """Send `data' to the server.
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
836 ``data`` can be a string object, a bytes object, an array object, a
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
837 file-like object that supports a .read() method, or an iterable object.
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
838 """
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
839
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
840 if self.sock is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
841 if self.auto_open:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
842 self.connect()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
843 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
844 raise NotConnected()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
845
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
846 if self.debuglevel > 0:
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
847 print("send:", repr(data))
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
848 blocksize = 8192
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
849 if hasattr(data, "read") :
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
850 if self.debuglevel > 0:
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
851 print("sendIng a read()able")
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
852 encode = False
56293
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
853 try:
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
854 mode = data.mode
56293
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
855 except AttributeError:
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
856 # io.BytesIO and other file-like objects don't have a `mode`
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
857 # attribute.
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
858 pass
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
859 else:
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
860 if "b" not in mode:
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
861 encode = True
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
862 if self.debuglevel > 0:
a17cd9fcdd46 Merged revisions 75137 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55834
diff changeset
863 print("encoding file using iso-8859-1")
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
864 while 1:
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
865 datablock = data.read(blocksize)
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
866 if not datablock:
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
867 break
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
868 if encode:
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
869 datablock = datablock.encode("iso-8859-1")
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
870 self.sock.sendall(datablock)
83275
70c7687de1cd Issue #16658: add missing return to HTTPConnection.send().
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 82028
diff changeset
871 return
67046
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
872 try:
65053
bebe5e35661e Merged revisions 85169 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63538
diff changeset
873 self.sock.sendall(data)
67046
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
874 except TypeError:
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
875 if isinstance(data, collections.Iterable):
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
876 for d in data:
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
877 self.sock.sendall(d)
6f56b247a7cd Issue3243 - Support iterable bodies in httplib. Patch contributions by Xuanji Li and Chris AtLee.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67020
diff changeset
878 else:
71727
0a9b0712f51a Fix spacing in string literal.
Georg Brandl <georg@python.org>
parents: 71688
diff changeset
879 raise TypeError("data should be a bytes-like object "
0a9b0712f51a Fix spacing in string literal.
Georg Brandl <georg@python.org>
parents: 71688
diff changeset
880 "or an iterable, got %r" % type(data))
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
881
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
882 def _output(self, s):
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
883 """Add a line of output to the current request buffer.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24378
diff changeset
884
24264
18c1b290e484 Fix typos and such caught by the pycheckerbot.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24263
diff changeset
885 Assumes that the line does *not* end with \\r\\n.
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
886 """
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
887 self._buffer.append(s)
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
888
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
889 def _send_output(self, message_body=None):
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
890 """Send the currently buffered request and clear the buffer.
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
891
24264
18c1b290e484 Fix typos and such caught by the pycheckerbot.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24263
diff changeset
892 Appends an extra \\r\\n to the buffer.
50842
507874f655ac Merged revisions 68458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
893 A message_body may be specified, to be appended to the request.
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
894 """
42133
a8582e25748b Fix test_httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
895 self._buffer.extend((b"", b""))
a8582e25748b Fix test_httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
896 msg = b"\r\n".join(self._buffer)
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
897 del self._buffer[:]
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
898 # If msg and message_body are sent in a single send() call,
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
899 # it will avoid performance problems caused by the interaction
81251
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
900 # between delayed ack and the Nagle algorithm. However,
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
901 # there is no performance gain if the message is larger
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
902 # than MSS (and there is a memory penalty for the message
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
903 # copy).
002bf9857278 Issue #16833: In http.client.HTTPConnection, do not concatenate the request headers and body when the payload exceeds 16 KB, since it can consume more memory for no benefit.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80933
diff changeset
904 if isinstance(message_body, bytes) and len(message_body) < self.mss:
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
905 msg += message_body
50842
507874f655ac Merged revisions 68458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
906 message_body = None
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
907 self.send(msg)
50842
507874f655ac Merged revisions 68458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
908 if message_body is not None:
52370
8b691690babd Fix some string encoding issues with entity bodies in HTTP requests.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52369
diff changeset
909 # message_body was not a string (i.e. it is a file), and
8b691690babd Fix some string encoding issues with entity bodies in HTTP requests.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52369
diff changeset
910 # we must run the risk of Nagle.
50842
507874f655ac Merged revisions 68458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50540
diff changeset
911 self.send(message_body)
24262
3b571e32a6e4 Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24216
diff changeset
912
30976
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
913 def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
914 """Send a request to the server.
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
915
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
916 `method' specifies an HTTP request method, e.g. 'GET'.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
917 `url' specifies the object being requested, e.g. '/index.html'.
30976
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
918 `skip_host' if True does not add automatically a 'Host:' header
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
919 `skip_accept_encoding' if True does not add automatically an
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
920 'Accept-Encoding:' header
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
921 """
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
922
29584
da0248fcfc52 Deal with a couple XXX comments which asked questions.
Greg Stein <gstein@lyra.org>
parents: 29465
diff changeset
923 # if a prior response has been completed, then forget about it.
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
924 if self.__response and self.__response.isclosed():
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
925 self.__response = None
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
926
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 30976
diff changeset
927
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
928 # in certain cases, we cannot issue another request on this connection.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
929 # this occurs when:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
930 # 1) we are in the process of sending a request. (_CS_REQ_STARTED)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
931 # 2) a response to a previous request has signalled that it is going
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
932 # to close the connection upon completion.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
933 # 3) the headers for the previous response have not been read, thus
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
934 # we cannot determine whether point (2) is true. (_CS_REQ_SENT)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
935 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
936 # if there is no prior response, then we can request at will.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
937 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
938 # if point (2) is true, then we will have passed the socket to the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
939 # response (effectively meaning, "there is no prior response"), and
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
940 # will open a new one when a new request is made.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
941 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
942 # Note: if a prior response exists, then we *can* start a new request.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
943 # We are not allowed to begin fetching the response to this new
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
944 # request, however, until that prior response is complete.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
945 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
946 if self.__state == _CS_IDLE:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
947 self.__state = _CS_REQ_STARTED
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
948 else:
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
949 raise CannotSendRequest(self.__state)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
950
28978
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
951 # Save the method we use, we need it later in the response phase
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
952 self._method = method
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
953 if not url:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
954 url = '/'
42133
a8582e25748b Fix test_httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
955 request = '%s %s %s' % (method, url, self._http_vsn_str)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
956
42133
a8582e25748b Fix test_httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
957 # Non-ASCII characters should have been eliminated earlier
a8582e25748b Fix test_httplib.
Martin v. Löwis <martin@v.loewis.de>
parents: 41824
diff changeset
958 self._output(request.encode('ascii'))
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
959
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
960 if self._http_vsn == 11:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
961 # Issue some standard headers for better HTTP/1.1 compliance
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
962
22202
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
963 if not skip_host:
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
964 # this header is issued *only* for HTTP/1.1
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
965 # connections. more specifically, this means it is
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
966 # only issued when the client uses the new
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
967 # HTTPConnection() class. backwards-compat clients
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
968 # will be using HTTP/1.0 and those clients may be
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
969 # issuing this header themselves. we should NOT issue
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
970 # it twice; some web servers (such as Apache) barf
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
971 # when they see two Host: headers
15859
31967a233600 SF Patch #103232 by dougfort: Preserve Nonstandard Port Number in Host
Guido van Rossum <guido@python.org>
parents: 15845
diff changeset
972
22202
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
973 # If we need a non-standard port,include it in the
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
974 # header. If the request is going through a proxy,
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
975 # but the host of the actual URL, not the host of the
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
976 # proxy.
22192
044f9e71320f SF bug report #405939: wrong Host header with proxy
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21962
diff changeset
977
22202
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
978 netloc = ''
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
979 if url.startswith('http'):
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
980 nil, netloc, nil, nil, nil = urlsplit(url)
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
981
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
982 if netloc:
38104
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
983 try:
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
984 netloc_enc = netloc.encode("ascii")
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
985 except UnicodeEncodeError:
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
986 netloc_enc = netloc.encode("idna")
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
987 self.putheader('Host', netloc_enc)
22202
5f2c0f6f0af7 Fix SF bug 525520.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22192
diff changeset
988 else:
38104
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
989 try:
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
990 host_enc = self.host.encode("ascii")
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
991 except UnicodeEncodeError:
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
992 host_enc = self.host.encode("idna")
66196
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
993
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
994 # As per RFC 273, IPv6 address should be wrapped with []
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
995 # when used as Host header
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
996
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
997 if self.host.find(':') >= 0:
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
998 host_enc = b'[' + host_enc + b']'
0f398188c7f6 Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65079
diff changeset
999
48494
aca5b2e6a66f Merged revisions 64623,64640,64665,64687,64689-64690,64719,64721,64735,64742,64744-64746,64756-64761,64767-64769,64771-64772,64774-64775,64788,64793,64835-64836 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48334
diff changeset
1000 if self.port == self.default_port:
38104
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
1001 self.putheader('Host', host_enc)
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
1002 else:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1003 host_enc = host_enc.decode("ascii")
38104
88b89f96b97f RFE #1472176: In httplib, don't encode the netloc and hostname with "idna" if not necessary.
Georg Brandl <georg@python.org>
parents: 36559
diff changeset
1004 self.putheader('Host', "%s:%s" % (host_enc, self.port))
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1005
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1006 # note: we are assuming that clients will not attempt to set these
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1007 # headers since *this* library must deal with the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1008 # consequences. this also means that when the supporting
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1009 # libraries are updated to recognize other forms, then this
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1010 # code should be changed (removed or updated).
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1011
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1012 # we only want a Content-Encoding of "identity" since we don't
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1013 # support encodings such as x-gzip or x-deflate.
30976
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
1014 if not skip_accept_encoding:
ead4c61219d0 Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents: 30888
diff changeset
1015 self.putheader('Accept-Encoding', 'identity')
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1016
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1017 # we can accept "chunked" Transfer-Encodings, but no others
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1018 # NOTE: no TE header implies *only* "chunked"
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1019 #self.putheader('TE', 'chunked')
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1020
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1021 # if TE is supplied in the header, then it must appear in a
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1022 # Connection header.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1023 #self.putheader('Connection', 'TE')
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1024
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1025 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1026 # For HTTP/1.0, the server will assume "not chunked"
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1027 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1028
49734
b0fcb17f7efa Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48494
diff changeset
1029 def putheader(self, header, *values):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1030 """Send a request header line to the server.
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1031
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1032 For example: h.putheader('Accept', 'text/html')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1033 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1034 if self.__state != _CS_REQ_STARTED:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1035 raise CannotSendHeader()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1036
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1037 if hasattr(header, 'encode'):
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1038 header = header.encode('ascii')
49734
b0fcb17f7efa Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48494
diff changeset
1039 values = list(values)
b0fcb17f7efa Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48494
diff changeset
1040 for i, one_value in enumerate(values):
b0fcb17f7efa Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48494
diff changeset
1041 if hasattr(one_value, 'encode'):
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67721
diff changeset
1042 values[i] = one_value.encode('latin-1')
65079
32c32b70aa33 Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65053
diff changeset
1043 elif isinstance(one_value, int):
32c32b70aa33 Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65053
diff changeset
1044 values[i] = str(one_value).encode('ascii')
49734
b0fcb17f7efa Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48494
diff changeset
1045 value = b'\r\n\t'.join(values)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1046 header = header + b': ' + value
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1047 self._output(header)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1048
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1049 def endheaders(self, message_body=None):
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1050 """Indicate that the last header line has been sent to the server.
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1051
72692
befa7b926aad Issue #13073 - Address the review comments made by Ezio.
Senthil Kumaran <senthil@uthcode.com>
parents: 71727
diff changeset
1052 This method sends the request to the server. The optional message_body
befa7b926aad Issue #13073 - Address the review comments made by Ezio.
Senthil Kumaran <senthil@uthcode.com>
parents: 71727
diff changeset
1053 argument can be used to pass a message body associated with the
befa7b926aad Issue #13073 - Address the review comments made by Ezio.
Senthil Kumaran <senthil@uthcode.com>
parents: 71727
diff changeset
1054 request. The message body will be sent in the same packet as the
befa7b926aad Issue #13073 - Address the review comments made by Ezio.
Senthil Kumaran <senthil@uthcode.com>
parents: 71727
diff changeset
1055 message headers if it is a string, otherwise it is sent as a separate
befa7b926aad Issue #13073 - Address the review comments made by Ezio.
Senthil Kumaran <senthil@uthcode.com>
parents: 71727
diff changeset
1056 packet.
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1057 """
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1058 if self.__state == _CS_REQ_STARTED:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1059 self.__state = _CS_REQ_SENT
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1060 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1061 raise CannotSendHeader()
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1062 self._send_output(message_body)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1063
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1064 def request(self, method, url, body=None, headers={}):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1065 """Send a complete request to the server."""
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
1066 self._send_request(method, url, body, headers)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1067
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1068 def _set_content_length(self, body):
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1069 # Set the content-length based on the body.
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1070 thelen = None
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1071 try:
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1072 thelen = str(len(body))
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1073 except TypeError as te:
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1074 # If this is a file-like object, try to
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1075 # fstat its file descriptor
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1076 try:
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1077 thelen = str(os.fstat(body.fileno()).st_size)
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1078 except (AttributeError, OSError):
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1079 # Don't send a length if this failed
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1080 if self.debuglevel > 0: print("Cannot stat!!")
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1081
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1082 if thelen is not None:
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1083 self.putheader('Content-Length', thelen)
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1084
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1085 def _send_request(self, method, url, body, headers):
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
1086 # Honor explicitly requested Host: and Accept-Encoding: headers.
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1087 header_names = dict.fromkeys([k.lower() for k in headers])
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1088 skips = {}
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1089 if 'host' in header_names:
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1090 skips['skip_host'] = 1
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1091 if 'accept-encoding' in header_names:
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1092 skips['skip_accept_encoding'] = 1
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1093
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1094 self.putrequest(method, url, **skips)
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32252
diff changeset
1095
77048
6da1ab5f777d Fix Issue14721: Send Content-length: 0 for empty body () in the http.client requests
Senthil Kumaran <senthil@uthcode.com>
parents: 76606
diff changeset
1096 if body is not None and ('content-length' not in header_names):
49907
75a8caa24186 Merged revisions 67442 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49734
diff changeset
1097 self._set_content_length(body)
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
1098 for hdr, value in headers.items():
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1099 self.putheader(hdr, value)
52366
75e3fe3c0726 Simplify complex expression.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 51953
diff changeset
1100 if isinstance(body, str):
52370
8b691690babd Fix some string encoding issues with entity bodies in HTTP requests.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52369
diff changeset
1101 # RFC 2616 Section 3.7.1 says that text default has a
8b691690babd Fix some string encoding issues with entity bodies in HTTP requests.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52369
diff changeset
1102 # default charset of iso-8859-1.
8b691690babd Fix some string encoding issues with entity bodies in HTTP requests.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52369
diff changeset
1103 body = body.encode('iso-8859-1')
52366
75e3fe3c0726 Simplify complex expression.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 51953
diff changeset
1104 self.endheaders(body)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1105
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1106 def getresponse(self):
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1107 """Get the response from the server.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1108
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1109 If the HTTPConnection is in the correct state, returns an
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1110 instance of HTTPResponse or of whatever object is returned by
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1111 class the response_class variable.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1112
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1113 If a request has not been sent or if a previous response has
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1114 not be handled, ResponseNotReady is raised. If the HTTP
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1115 response indicates that the connection should be closed, then
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1116 it will be closed before the response is returned. When the
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1117 connection is closed, the underlying socket is closed.
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1118 """
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1119
29584
da0248fcfc52 Deal with a couple XXX comments which asked questions.
Greg Stein <gstein@lyra.org>
parents: 29465
diff changeset
1120 # if a prior response has been completed, then forget about it.
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1121 if self.__response and self.__response.isclosed():
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1122 self.__response = None
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1123
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1124 # if a prior response exists, then it must be completed (otherwise, we
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1125 # cannot read this response's header to determine the connection-close
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1126 # behavior)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1127 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1128 # note: if a prior response existed, but was connection-close, then the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1129 # socket and response were made independent of this HTTPConnection
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1130 # object since a new request requires that we open a whole new
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1131 # connection
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1132 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1133 # this means the prior response had one of two states:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1134 # 1) will_close: this connection was reset and the prior socket and
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1135 # response operate independently
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1136 # 2) persistent: the response was retained and we await its
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1137 # isclosed() status to become true.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1138 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1139 if self.__state != _CS_REQ_SENT or self.__response:
52522
9eceb618274a Simplify the Request class. The basic components of the parsed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52375
diff changeset
1140 raise ResponseNotReady(self.__state)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1141
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
1142 if self.debuglevel > 0:
24119
f33bb9c131f8 Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24088
diff changeset
1143 response = self.response_class(self.sock, self.debuglevel,
28978
83fe0f5c9359 SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28671
diff changeset
1144 method=self._method)
14672
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
1145 else:
66988
b6d3fe70802f Issue #10711: Remove HTTP 0.9 support from http.client. The `strict`
Antoine Pitrou <solipsis@pitrou.net>
parents: 66187
diff changeset
1146 response = self.response_class(self.sock, method=self._method)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1147
24216
c8a19a428358 Change _begin() back to begin().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24168
diff changeset
1148 response.begin()
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
1149 assert response.will_close != _UNKNOWN
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1150 self.__state = _CS_IDLE
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1151
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1152 if response.will_close:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1153 # this effectively passes the connection to the response
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1154 self.close()
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1155 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1156 # remember this, so we can tell when it is complete
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1157 self.__response = response
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1158
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1159 return response
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1160
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1161 try:
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1162 import ssl
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1163 except ImportError:
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1164 pass
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1165 else:
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1166 class HTTPSConnection(HTTPConnection):
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1167 "This class allows communication via SSL."
14140
f7a8c40b3880 add docstring explaining makefile limitation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13787
diff changeset
1168
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1169 default_port = HTTPS_PORT
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
1170
65247
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1171 # XXX Should key_file and cert_file be deprecated in favour of context?
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1172
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1173 def __init__(self, host, port=None, key_file=None, cert_file=None,
82735
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
1174 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
1175 source_address=None, *, context=None,
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
1176 check_hostname=None):
c9bf987d80f1 #17460: Remove the strict argument of HTTPConnection and removing the
Senthil Kumaran <senthil@uthcode.com>
parents: 82029
diff changeset
1177 super(HTTPSConnection, self).__init__(host, port, timeout,
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 57801
diff changeset
1178 source_address)
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1179 self.key_file = key_file
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1180 self.cert_file = cert_file
65247
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1181 if context is None:
87463
03cc1b55faae Issue #19735: Implement private function ssl._create_stdlib_context() to
Christian Heimes <christian@cheimes.de>
parents: 86681
diff changeset
1182 context = ssl._create_stdlib_context()
65247
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1183 will_verify = context.verify_mode != ssl.CERT_NONE
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1184 if check_hostname is None:
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1185 check_hostname = will_verify
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1186 elif check_hostname and not will_verify:
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1187 raise ValueError("check_hostname needs a SSL context with "
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1188 "either CERT_OPTIONAL or CERT_REQUIRED")
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1189 if key_file or cert_file:
65249
a1567893783f Typo. It is rather obvious that this code isn't tested :/
Antoine Pitrou <solipsis@pitrou.net>
parents: 65247
diff changeset
1190 context.load_cert_chain(cert_file, key_file)
65247
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1191 self._context = context
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1192 self._check_hostname = check_hostname
24038
9377158652dd Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23620
diff changeset
1193
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1194 def connect(self):
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1195 "Connect to a host on a given (SSL) port."
24168
fdd836575248 Fix for SF bug 579107.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24123
diff changeset
1196
52375
a726f7928561 Remove special logic that closes HTTPConnection socket on EPIPE.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52370
diff changeset
1197 sock = socket.create_connection((self.host, self.port),
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 57801
diff changeset
1198 self.timeout, self.source_address)
55444
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
1199
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
1200 if self._tunnel_host:
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
1201 self.sock = sock
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
1202 self._tunnel()
5112a56df977 Backport the changes made in revision 74198, fixing the issue 1424152
Senthil Kumaran <orsenthil@gmail.com>
parents: 52631
diff changeset
1203
65593
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65249
diff changeset
1204 server_hostname = self.host if ssl.HAS_SNI else None
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65249
diff changeset
1205 self.sock = self._context.wrap_socket(sock,
846c0e1342d0 Issue #5639: Add a *server_hostname* argument to `SSLContext.wrap_socket`
Antoine Pitrou <solipsis@pitrou.net>
parents: 65249
diff changeset
1206 server_hostname=server_hostname)
65247
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1207 try:
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1208 if self._check_hostname:
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1209 ssl.match_hostname(self.sock.getpeercert(), self.host)
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1210 except Exception:
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1211 self.sock.shutdown(socket.SHUT_RDWR)
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1212 self.sock.close()
12327b9d3325 Issue #9003: http.client.HTTPSConnection, urllib.request.HTTPSHandler and
Antoine Pitrou <solipsis@pitrou.net>
parents: 65149
diff changeset
1213 raise
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1214
43095
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42964
diff changeset
1215 __all__.append("HTTPSConnection")
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1216
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1217 class HTTPException(Exception):
24120
b51a578f5769 Fix SF bug #575360
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24119
diff changeset
1218 # Subclasses that define an __init__ must call Exception.__init__
b51a578f5769 Fix SF bug #575360
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24119
diff changeset
1219 # or define self.args. Otherwise, str() will fail.
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1220 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1221
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1222 class NotConnected(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1223 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1224
22494
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
1225 class InvalidURL(HTTPException):
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
1226 pass
7224a1df4809 add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents: 22403
diff changeset
1227
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1228 class UnknownProtocol(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1229 def __init__(self, version):
24120
b51a578f5769 Fix SF bug #575360
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24119
diff changeset
1230 self.args = version,
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1231 self.version = version
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1232
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1233 class UnknownTransferEncoding(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1234 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1235
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1236 class UnimplementedFileMode(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1237 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1238
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1239 class IncompleteRead(HTTPException):
51953
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1240 def __init__(self, partial, expected=None):
24120
b51a578f5769 Fix SF bug #575360
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24119
diff changeset
1241 self.args = partial,
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1242 self.partial = partial
51953
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1243 self.expected = expected
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1244 def __repr__(self):
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1245 if self.expected is not None:
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1246 e = ', %i more expected' % self.expected
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1247 else:
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1248 e = ''
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1249 return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e)
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1250 def __str__(self):
9c56eb17f334 Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51532
diff changeset
1251 return repr(self)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1252
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1253 class ImproperConnectionState(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1254 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1255
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1256 class CannotSendRequest(ImproperConnectionState):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1257 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1258
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1259 class CannotSendHeader(ImproperConnectionState):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1260 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1261
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1262 class ResponseNotReady(ImproperConnectionState):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1263 pass
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1264
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1265 class BadStatusLine(HTTPException):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1266 def __init__(self, line):
59875
d9b8f273ab8d Merged revisions 78417 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58114
diff changeset
1267 if not line:
d9b8f273ab8d Merged revisions 78417 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58114
diff changeset
1268 line = repr(line)
24120
b51a578f5769 Fix SF bug #575360
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24119
diff changeset
1269 self.args = line,
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1270 self.line = line
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1271
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
1272 class LineTooLong(HTTPException):
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
1273 def __init__(self, line_type):
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
1274 HTTPException.__init__(self, "got more than %d bytes when reading %s"
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
1275 % (_MAXLINE, line_type))
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66196
diff changeset
1276
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1277 # for backwards compatibility
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1278 error = HTTPException