Mercurial > cpython-lru_cache-weakref
annotate Lib/http/client.py @ 48715:48dddfd2fbf3
RPC_WSTR is not available Visual Studio 2003 or earlier.
(Maybe I should have defined RPC_WSTR on old compiler,
but uuidcreate() is only place using it, so I simply replaced
with unsigned short *)
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Sun, 17 Aug 2008 07:26:26 +0000 |
| parents | aca5b2e6a66f |
| children | b0fcb17f7efa |
| 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:
38453
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 |
|
42431
7401ff78eb29
A few local changes for consistency of string literals.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42426
diff
changeset
|
69 import io |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 import socket |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
71 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
|
72 import email.message |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47635
diff
changeset
|
73 from urllib.parse import urlsplit |
|
43292
876b7ef4775d
Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
74 import warnings |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
76 __all__ = ["HTTPResponse", "HTTPConnection", |
|
18013
99aea6f5cebd
more public symbols for __all__
Skip Montanaro <skip@pobox.com>
parents:
17482
diff
changeset
|
77 "HTTPException", "NotConnected", "UnknownProtocol", |
|
24042
63fbbed10714
Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24038
diff
changeset
|
78 "UnknownTransferEncoding", "UnimplementedFileMode", |
|
63fbbed10714
Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24038
diff
changeset
|
79 "IncompleteRead", "InvalidURL", "ImproperConnectionState", |
|
63fbbed10714
Simplify HTTPSConnection constructor.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24038
diff
changeset
|
80 "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
81 "BadStatusLine", "error", "responses"] |
|
16160
46920b60cd33
added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents:
15878
diff
changeset
|
82 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 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
|
84 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
|
85 |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
86 _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
|
87 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
88 # 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
|
89 _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
|
90 _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
|
91 _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
|
92 |
|
33506
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
93 # status codes |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
94 # informational |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
95 CONTINUE = 100 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
96 SWITCHING_PROTOCOLS = 101 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
97 PROCESSING = 102 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
98 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
99 # successful |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
100 OK = 200 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
101 CREATED = 201 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
102 ACCEPTED = 202 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
103 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
|
104 NO_CONTENT = 204 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
105 RESET_CONTENT = 205 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
106 PARTIAL_CONTENT = 206 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
107 MULTI_STATUS = 207 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
108 IM_USED = 226 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
109 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
110 # redirection |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
111 MULTIPLE_CHOICES = 300 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
112 MOVED_PERMANENTLY = 301 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
113 FOUND = 302 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
114 SEE_OTHER = 303 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
115 NOT_MODIFIED = 304 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
116 USE_PROXY = 305 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
117 TEMPORARY_REDIRECT = 307 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
118 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
119 # client error |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
120 BAD_REQUEST = 400 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
121 UNAUTHORIZED = 401 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
122 PAYMENT_REQUIRED = 402 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
123 FORBIDDEN = 403 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
124 NOT_FOUND = 404 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
125 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
|
126 NOT_ACCEPTABLE = 406 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
127 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
|
128 REQUEST_TIMEOUT = 408 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
129 CONFLICT = 409 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
130 GONE = 410 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
131 LENGTH_REQUIRED = 411 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
132 PRECONDITION_FAILED = 412 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 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
|
137 EXPECTATION_FAILED = 417 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
138 UNPROCESSABLE_ENTITY = 422 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
139 LOCKED = 423 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
140 FAILED_DEPENDENCY = 424 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
141 UPGRADE_REQUIRED = 426 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
142 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
143 # server error |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
144 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
|
145 NOT_IMPLEMENTED = 501 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
146 BAD_GATEWAY = 502 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
147 SERVICE_UNAVAILABLE = 503 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
148 GATEWAY_TIMEOUT = 504 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
149 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
|
150 INSUFFICIENT_STORAGE = 507 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
151 NOT_EXTENDED = 510 |
|
4b8198a74873
Patch #1025790: Add status code constants to httplib.
Martin v. Löwis <martin@v.loewis.de>
parents:
33496
diff
changeset
|
152 |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
153 # 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
|
154 responses = { |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
155 100: 'Continue', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
156 101: 'Switching Protocols', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
157 |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
158 200: 'OK', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
159 201: 'Created', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
160 202: 'Accepted', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
161 203: 'Non-Authoritative Information', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
162 204: 'No Content', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
163 205: 'Reset Content', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
164 206: 'Partial Content', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
165 |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
166 300: 'Multiple Choices', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
167 301: 'Moved Permanently', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
168 302: 'Found', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
169 303: 'See Other', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
170 304: 'Not Modified', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
171 305: 'Use Proxy', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
172 306: '(Unused)', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
173 307: 'Temporary Redirect', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
174 |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
175 400: 'Bad Request', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
176 401: 'Unauthorized', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
177 402: 'Payment Required', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
178 403: 'Forbidden', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
179 404: 'Not Found', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
180 405: 'Method Not Allowed', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
181 406: 'Not Acceptable', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
182 407: 'Proxy Authentication Required', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
183 408: 'Request Timeout', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
184 409: 'Conflict', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
185 410: 'Gone', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
186 411: 'Length Required', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
187 412: 'Precondition Failed', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
188 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
|
189 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
|
190 415: 'Unsupported Media Type', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
191 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
|
192 417: 'Expectation Failed', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
193 |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
194 500: 'Internal Server Error', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
195 501: 'Not Implemented', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
196 502: 'Bad Gateway', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
197 503: 'Service Unavailable', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
198 504: 'Gateway Timeout', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
199 505: 'HTTP Version Not Supported', |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
200 } |
|
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
35652
diff
changeset
|
201 |
|
35652
6a20831d357d
bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents:
35158
diff
changeset
|
202 # 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
|
203 MAXAMOUNT = 1048576 |
|
6a20831d357d
bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents:
35158
diff
changeset
|
204 |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
205 class HTTPMessage(email.message.Message): |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
206 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
|
207 """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
|
208 |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
215 """ |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
216 # XXX: copied from rfc822.Message for compatibility |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
217 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
|
218 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
|
219 lst = [] |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
220 hit = 0 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
221 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
|
222 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
|
223 hit = 1 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
224 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
|
225 hit = 0 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
226 if hit: |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
227 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
|
228 return lst |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
229 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
230 def parse_headers(fp): |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
231 """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
|
232 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
237 to parse. |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
238 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
239 """ |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
240 # XXX: Copied from http.server.BaseHTTPRequestHandler.parse_request, |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
241 # maybe we can just call this function from there. |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
242 headers = [] |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
243 while True: |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
244 line = fp.readline() |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
245 headers.append(line) |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
246 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
|
247 break |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
248 hstring = b''.join(headers).decode('iso-8859-1') |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
249 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
250 return email.parser.Parser(_class=HTTPMessage).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
|
251 |
|
42440
05728e52b283
HTTPResponse should not inherit from io.IOBase.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42431
diff
changeset
|
252 class HTTPResponse: |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
253 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
254 # strict: If true, raise BadStatusLine if the status line can't be |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
255 # parsed as a valid HTTP/1.0 or 1.1 status line. By default it is |
| 24378 | 256 # false because it prevents clients from talking to HTTP/0.9 |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
257 # servers. Note that a response with a sufficiently corrupted |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
258 # status line will look like an HTTP/0.9 response. |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
259 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
260 # 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
|
261 |
|
42425
7794da1334a0
Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42133
diff
changeset
|
262 # 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
|
263 # 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
|
264 # 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
|
265 # accepts iso-8859-1. |
|
7794da1334a0
Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42133
diff
changeset
|
266 |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
267 def __init__(self, sock, debuglevel=0, strict=0, method=None): |
|
42452
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
268 # XXX If the response includes a content-length header, we |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
269 # need to make sure that the client doesn't read more than the |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
270 # specified number of bytes. If it does, it will block until |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
271 # the server times out and closes the connection. (The only |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
272 # applies to HTTP/1.1 connections.) Since some clients access |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
273 # self.fp directly rather than calling read(), this is a little |
|
f4409c5d7994
Change urllib to use HTTPConnection rather than old HTTP class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42444
diff
changeset
|
274 # tricky. |
|
42426
ae72bf5ae465
Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42425
diff
changeset
|
275 self.fp = sock.makefile("rb", 0) |
|
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
|
276 self.debuglevel = debuglevel |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
277 self.strict = strict |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
278 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
|
279 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
280 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
|
281 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
282 # from the Status-Line of the response |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
283 self.version = _UNKNOWN # HTTP-Version |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
284 self.status = _UNKNOWN # Status-Code |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
285 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
|
286 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
287 self.chunked = _UNKNOWN # is "chunked" being used? |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
288 self.chunk_left = _UNKNOWN # bytes left to read in current chunk |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
289 self.length = _UNKNOWN # number of bytes left in response |
|
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
290 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
|
291 |
|
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
|
292 def _read_status(self): |
|
42444
12dbaecfb44b
Make sure LineAndFileWrapper gets bytes() as its first argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42443
diff
changeset
|
293 # Initialize with Simple-Response defaults. |
|
42425
7794da1334a0
Fix status line parsing for http response.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42133
diff
changeset
|
294 line = str(self.fp.readline(), "iso-8859-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
|
295 if self.debuglevel > 0: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
296 print("reply:", repr(line)) |
|
29688
9fe67f7f3459
Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29584
diff
changeset
|
297 if not line: |
|
9fe67f7f3459
Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29584
diff
changeset
|
298 # 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
|
299 # sending a valid response. |
|
9fe67f7f3459
Fix sf bug 666219: assertion error in httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29584
diff
changeset
|
300 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
|
301 try: |
|
15582
6179d4afa4e4
Get rid of string functions.
Guido van Rossum <guido@python.org>
parents:
15503
diff
changeset
|
302 [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
|
303 except ValueError: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
304 try: |
|
15582
6179d4afa4e4
Get rid of string functions.
Guido van Rossum <guido@python.org>
parents:
15503
diff
changeset
|
305 [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
|
306 reason = "" |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
307 except ValueError: |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
308 # empty version will cause next test to fail and status |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
309 # will be treated as 0.9 response. |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
310 version = "" |
|
42426
ae72bf5ae465
Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42425
diff
changeset
|
311 if not version.startswith("HTTP/"): |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
312 if self.strict: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
313 self.close() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
314 raise BadStatusLine(line) |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
315 else: |
|
42444
12dbaecfb44b
Make sure LineAndFileWrapper gets bytes() as its first argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42443
diff
changeset
|
316 # Assume it's a Simple-Response from an 0.9 server. |
|
12dbaecfb44b
Make sure LineAndFileWrapper gets bytes() as its first argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42443
diff
changeset
|
317 # We have to convert the first line back to raw bytes |
|
12dbaecfb44b
Make sure LineAndFileWrapper gets bytes() as its first argument.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42443
diff
changeset
|
318 # because self.fp.readline() needs to return bytes. |
|
42964
a7951abb8534
Make test_urllib be strict about str/bytes.
Guido van Rossum <guido@python.org>
parents:
42590
diff
changeset
|
319 self.fp = LineAndFileWrapper(bytes(line, "ascii"), self.fp) |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
320 return "HTTP/0.9", 200, "" |
|
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
|
321 |
|
17482
2265032efc9f
SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16400
diff
changeset
|
322 # 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
|
323 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
|
324 status = int(status) |
|
17482
2265032efc9f
SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16400
diff
changeset
|
325 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
|
326 raise BadStatusLine(line) |
|
2265032efc9f
SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16400
diff
changeset
|
327 except ValueError: |
|
2265032efc9f
SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16400
diff
changeset
|
328 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
|
329 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
|
330 |
|
24216
c8a19a428358
Change _begin() back to begin().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24168
diff
changeset
|
331 def begin(self): |
|
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
|
332 if self.msg is not None: |
|
9377158652dd
Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23620
diff
changeset
|
333 # 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
|
334 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
|
335 |
|
9377158652dd
Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23620
diff
changeset
|
336 # read until we get a non-100 response |
| 28083 | 337 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
|
338 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
|
339 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
|
340 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
|
341 # skip the header from the 100 response |
| 28083 | 342 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
|
343 skip = self.fp.readline().strip() |
|
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 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
|
345 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
|
346 if self.debuglevel > 0: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
347 print("header:", skip) |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24262
diff
changeset
|
348 |
|
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
|
349 self.status = status |
|
15582
6179d4afa4e4
Get rid of string functions.
Guido van Rossum <guido@python.org>
parents:
15503
diff
changeset
|
350 self.reason = reason.strip() |
|
42426
ae72bf5ae465
Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42425
diff
changeset
|
351 if version == "HTTP/1.0": |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
352 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
|
353 elif version.startswith("HTTP/1."): |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
354 self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 |
|
42426
ae72bf5ae465
Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42425
diff
changeset
|
355 elif version == "HTTP/0.9": |
|
15182
b3387eeba68d
If the status line is invalid, assume it is a pre-1.0 response. The
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14740
diff
changeset
|
356 self.version = 9 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
357 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
358 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
|
359 |
|
15182
b3387eeba68d
If the status line is invalid, assume it is a pre-1.0 response. The
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14740
diff
changeset
|
360 if self.version == 9: |
|
35158
daebfde0e1cf
backport bug [ 1155638 ] self.length shield exception in httplib
Georg Brandl <georg@python.org>
parents:
33933
diff
changeset
|
361 self.length = 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
|
362 self.chunked = 0 |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
363 self.will_close = 1 |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
364 self.msg = email.message_from_string('') |
|
15182
b3387eeba68d
If the status line is invalid, assume it is a pre-1.0 response. The
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14740
diff
changeset
|
365 return |
|
b3387eeba68d
If the status line is invalid, assume it is a pre-1.0 response. The
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14740
diff
changeset
|
366 |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
367 self.msg = parse_headers(self.fp) |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
368 |
|
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
|
369 if self.debuglevel > 0: |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
370 for hdr in self.msg: |
|
42426
ae72bf5ae465
Make consistent use of "" for string literals in new classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42425
diff
changeset
|
371 print("header:", hdr, end=" ") |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
373 # are we using the chunked-style of transfer encoding? |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
374 tr_enc = self.msg.get("transfer-encoding") |
|
25107
0ecb3cabf40b
SF bug [ 600488 ] Robustness tweak to httplib.py
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24674
diff
changeset
|
375 if tr_enc and tr_enc.lower() == "chunked": |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
376 self.chunked = 1 |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
377 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
|
378 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
379 self.chunked = 0 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
381 # 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
|
382 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
|
383 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
384 # 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
|
385 # 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
|
386 self.length = None |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
387 length = self.msg.get("content-length") |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
388 |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
389 # are we using the chunked-style of transfer encoding? |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
390 tr_enc = self.msg.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
|
391 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
|
392 try: |
|
eeb8ee488106
cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
14140
diff
changeset
|
393 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
|
394 except ValueError: |
|
45504
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
395 self.length = None |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
396 else: |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
397 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
|
398 self.length = None |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
399 else: |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
400 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
|
401 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
402 # 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
|
403 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
|
404 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
|
405 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
|
406 self.length = 0 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
408 # 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
|
409 # 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
|
410 # WILL close. |
|
42442
0fa84ffba769
Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42440
diff
changeset
|
411 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
|
412 not self.chunked and |
|
0fa84ffba769
Fix several more paths from the SSL code.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42440
diff
changeset
|
413 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
|
414 self.will_close = 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
|
415 |
|
26187
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
416 def _check_close(self): |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
417 conn = self.msg.get("connection") |
|
26187
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
418 if self.version == 11: |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
419 # 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
|
420 # explicitly closed. |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
421 conn = self.msg.get("connection") |
|
31967
3d046d063a84
Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents:
31389
diff
changeset
|
422 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
|
423 return True |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
424 return False |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
425 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
426 # 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
|
427 # 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
|
428 |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
429 # For older HTTP, Keep-Alive indicates persistent connection. |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
430 if self.msg.get("keep-alive"): |
|
26187
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
431 return False |
|
26387
fcf9734e1021
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26188
diff
changeset
|
432 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
433 # 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
|
434 # 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
|
435 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
|
436 return False |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
437 |
|
26187
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
438 # Proxy-Connection is a netscape hack. |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
439 pconn = self.msg.get("proxy-connection") |
|
31967
3d046d063a84
Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents:
31389
diff
changeset
|
440 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
|
441 return False |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
442 |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
443 # otherwise, assume it will close |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
444 return True |
|
ae169f5b94e0
Fix SF bug #637789: Handle Proxy-Connection header.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
26126
diff
changeset
|
445 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
446 def close(self): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
447 if self.fp: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
448 self.fp.close() |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
449 self.fp = 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
|
450 |
|
42497
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
451 # 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
|
452 |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
453 # 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
|
454 # the "raw stream" that BufferedReader expects. |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
455 |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
456 @property |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
457 def closed(self): |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
458 return self.isclosed() |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
459 |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
460 def flush(self): |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
461 self.fp.flush() |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
462 |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
463 # End of "raw stream" methods |
|
17cdbedbb15d
Fix several failing tests in test_urllib2net.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42466
diff
changeset
|
464 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
465 def isclosed(self): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
466 # 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
|
467 # 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
|
468 # 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
|
469 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
470 # 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
|
471 # 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
|
472 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
|
473 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
474 # XXX It would be nice to have readline and __iter__ for this, too. |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
475 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
476 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
|
477 if self.fp is None: |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
478 return b"" |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
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 if self.chunked: |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
481 return self._read_chunked(amt) |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25112
diff
changeset
|
482 |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
483 if amt is None: |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
484 # unbounded read |
|
33933
eb231544c1ad
Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33761
diff
changeset
|
485 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
|
486 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
|
487 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
488 s = self._safe_read(self.length) |
|
33933
eb231544c1ad
Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33761
diff
changeset
|
489 self.length = 0 |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
490 self.close() # 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
|
491 return s |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
493 if self.length is not None: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
494 if amt > self.length: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
495 # clip the read to the "end of response" |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
496 amt = self.length |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
498 # 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
|
499 # 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
|
500 # (for example, reading in 1k chunks) |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
501 s = self.fp.read(amt) |
|
33933
eb231544c1ad
Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33761
diff
changeset
|
502 if self.length is not None: |
|
eb231544c1ad
Fix for SF bug 988120 via patch 1061941.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
33761
diff
changeset
|
503 self.length -= len(s) |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
504 if not self.length: |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
505 self.close() |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
506 return s |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
507 |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
508 def _read_chunked(self, amt): |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
509 assert self.chunked != _UNKNOWN |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
510 chunk_left = self.chunk_left |
|
44967
221dec03e261
#1929: fix httplib _read_chunked (str/bytes confusion).
Georg Brandl <georg@python.org>
parents:
44230
diff
changeset
|
511 value = b"" |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
512 |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
513 # XXX This accumulates chunks by repeated string concatenation, |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
514 # which is not efficient as the number or size of chunks gets big. |
| 28083 | 515 while True: |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
516 if chunk_left is None: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
517 line = self.fp.readline() |
|
44967
221dec03e261
#1929: fix httplib _read_chunked (str/bytes confusion).
Georg Brandl <georg@python.org>
parents:
44230
diff
changeset
|
518 i = line.find(b";") |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
519 if i >= 0: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
520 line = line[:i] # strip chunk-extensions |
|
45504
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
521 try: |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
522 chunk_left = int(line, 16) |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
523 except ValueError: |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
524 # close the connection as protocol synchronisation is |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
525 # probably lost |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
526 self.close() |
|
1b6fb5699634
Merged revisions 61034-61036,61038-61048 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
527 raise IncompleteRead(value) |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
528 if chunk_left == 0: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
529 break |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
530 if amt is None: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
531 value += self._safe_read(chunk_left) |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
532 elif amt < chunk_left: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
533 value += self._safe_read(amt) |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
534 self.chunk_left = chunk_left - amt |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
535 return value |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
536 elif amt == chunk_left: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
537 value += self._safe_read(amt) |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
538 self._safe_read(2) # toss the CRLF at the end of the chunk |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
539 self.chunk_left = None |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
540 return value |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
541 else: |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
542 value += self._safe_read(chunk_left) |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
543 amt -= chunk_left |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
544 |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
545 # we read the whole chunk, get another |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
546 self._safe_read(2) # toss the CRLF at the end of the chunk |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
547 chunk_left = None |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
548 |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
549 # 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
|
550 ### note: we shouldn't have any trailers! |
| 28083 | 551 while True: |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
552 line = self.fp.readline() |
|
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
|
553 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
|
554 # 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
|
555 # 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
|
556 break |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
557 if line == b"\r\n": |
|
25112
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
558 break |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
559 |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
560 # we read everything; close the "file" |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
561 self.close() |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
562 |
|
f0ff060cd194
Move code for reading chunked responses in helper function,
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
25107
diff
changeset
|
563 return value |
|
26126
98f3fd1ed42f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
25112
diff
changeset
|
564 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
565 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
|
566 """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
|
567 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
568 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
|
569 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
|
570 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
571 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
|
572 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
|
573 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
|
574 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
575 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
|
576 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
|
577 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
|
578 """ |
|
35652
6a20831d357d
bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents:
35158
diff
changeset
|
579 s = [] |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
580 while amt > 0: |
|
35652
6a20831d357d
bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents:
35158
diff
changeset
|
581 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
|
582 if not chunk: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
583 raise IncompleteRead(s) |
|
35652
6a20831d357d
bug [ 1296004 ] MemoryError in httplib
Georg Brandl <georg@python.org>
parents:
35158
diff
changeset
|
584 s.append(chunk) |
| 28083 | 585 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
|
586 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
|
587 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
588 def getheader(self, name, default=None): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
589 if self.msg is None: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
590 raise ResponseNotReady() |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47559
diff
changeset
|
591 return ', '.join(self.msg.get_all(name, default)) |
|
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
|
592 |
|
33173
e05e9d05830d
Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents:
33001
diff
changeset
|
593 def getheaders(self): |
|
e05e9d05830d
Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents:
33001
diff
changeset
|
594 """Return list of (header, value) tuples.""" |
|
e05e9d05830d
Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents:
33001
diff
changeset
|
595 if self.msg is None: |
|
e05e9d05830d
Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents:
33001
diff
changeset
|
596 raise ResponseNotReady() |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
597 return list(self.msg.items()) |
|
33173
e05e9d05830d
Patch #800236: add HTTPResponse.getheaders().
Martin v. Löwis <martin@v.loewis.de>
parents:
33001
diff
changeset
|
598 |
|
1555
6d35308261f4
Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
599 |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
600 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
|
601 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
602 _http_vsn = 11 |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
603 _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
|
604 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
605 response_class = HTTPResponse |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
606 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
|
607 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
|
608 debuglevel = 0 |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
609 strict = 0 |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
610 |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47318
diff
changeset
|
611 def __init__(self, host, port=None, strict=None, |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47318
diff
changeset
|
612 timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
613 self.timeout = timeout |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
614 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
|
615 self._buffer = [] |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
616 self.__response = None |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
617 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
|
618 self._method = None |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24262
diff
changeset
|
619 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
620 self._set_hostport(host, port) |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
621 if strict is not None: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
622 self.strict = strict |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
623 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
624 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
|
625 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
|
626 i = host.rfind(':') |
|
33491
0c299cabf4f0
missed the obvious test case and corresponding fix
Skip Montanaro <skip@pobox.com>
parents:
33488
diff
changeset
|
627 j = host.rfind(']') # ipv6 addresses have [...] |
|
0c299cabf4f0
missed the obvious test case and corresponding fix
Skip Montanaro <skip@pobox.com>
parents:
33488
diff
changeset
|
628 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
|
629 try: |
|
7224a1df4809
add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents:
22403
diff
changeset
|
630 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
|
631 except ValueError: |
|
24088
b74d4a59793a
Convert raise to call exception class. Add whitespace.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24042
diff
changeset
|
632 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
|
633 host = host[:i] |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
634 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
635 port = self.default_port |
|
33761
d1228127d294
SF bug #1046855: httplib index out of range
Raymond Hettinger <python@rcn.com>
parents:
33506
diff
changeset
|
636 if host and host[0] == '[' and host[-1] == ']': |
| 33496 | 637 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
|
638 self.host = host |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
639 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
|
640 |
|
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
|
641 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
|
642 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
|
643 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
644 def connect(self): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
645 """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
|
646 self.sock = socket.create_connection((self.host,self.port), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
647 self.timeout) |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
648 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
649 def close(self): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
650 """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
|
651 if self.sock: |
|
15868
3060cbfa01be
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15859
diff
changeset
|
652 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
|
653 self.sock = None |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
654 if self.__response: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
655 self.__response.close() |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
656 self.__response = None |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
657 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
|
658 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
659 def send(self, str): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
660 """Send `str' to the server.""" |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
661 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
|
662 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
|
663 self.connect() |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
664 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
665 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
|
666 |
|
24264
18c1b290e484
Fix typos and such caught by the pycheckerbot.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24263
diff
changeset
|
667 # send the data to the server. if we get a broken pipe, then close |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
668 # the socket. we want to reconnect when somebody tries to send again. |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
669 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
670 # NOTE: we DO propagate the error, though, because we cannot simply |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
671 # ignore the error... the caller will know if they can retry. |
|
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
|
672 if self.debuglevel > 0: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
673 print("send:", repr(str)) |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
674 try: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
675 blocksize=8192 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
676 if hasattr(str,'read') : |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
677 if self.debuglevel > 0: print("sendIng a read()able") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
678 data=str.read(blocksize) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
679 while data: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
680 self.sock.sendall(data) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
681 data=str.read(blocksize) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
682 else: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
683 self.sock.sendall(str) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
684 except socket.error as v: |
|
41238
f9973a6e1afe
Make test_socket_ssl.py pass by fixing some code that was
Guido van Rossum <guido@python.org>
parents:
40981
diff
changeset
|
685 if v.args[0] == 32: # Broken pipe |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
686 self.close() |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
687 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
|
688 |
|
24262
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
689 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
|
690 """Add a line of output to the current request buffer. |
|
24674
3269bdd65988
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24378
diff
changeset
|
691 |
|
24264
18c1b290e484
Fix typos and such caught by the pycheckerbot.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24263
diff
changeset
|
692 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
|
693 """ |
|
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
694 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
|
695 |
|
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
696 def _send_output(self): |
|
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
697 """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
|
698 |
|
24264
18c1b290e484
Fix typos and such caught by the pycheckerbot.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24263
diff
changeset
|
699 Appends an extra \\r\\n to the buffer. |
|
24262
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
700 """ |
| 42133 | 701 self._buffer.extend((b"", b"")) |
| 702 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
|
703 del self._buffer[:] |
|
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
704 self.send(msg) |
|
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
705 |
|
30976
ead4c61219d0
Patch #831747: Add skip_accept_encoding parameter to putrequest.
Martin v. Löwis <martin@v.loewis.de>
parents:
30888
diff
changeset
|
706 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
|
707 """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
|
708 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
709 `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
|
710 `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
|
711 `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
|
712 `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
|
713 '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
|
714 """ |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
715 |
|
29584
da0248fcfc52
Deal with a couple XXX comments which asked questions.
Greg Stein <gstein@lyra.org>
parents:
29465
diff
changeset
|
716 # 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
|
717 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
|
718 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
|
719 |
|
31389
aca1ce1b4b61
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
30976
diff
changeset
|
720 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
721 # 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
|
722 # this occurs when: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
723 # 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
|
724 # 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
|
725 # 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
|
726 # 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
|
727 # 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
|
728 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
729 # 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
|
730 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
731 # 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
|
732 # 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
|
733 # 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
|
734 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
735 # 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
|
736 # 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
|
737 # 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
|
738 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
739 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
|
740 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
|
741 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
742 raise CannotSendRequest() |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
743 |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
744 # 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
|
745 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
|
746 if not url: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
747 url = '/' |
| 42133 | 748 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
|
749 |
| 42133 | 750 # Non-ASCII characters should have been eliminated earlier |
| 751 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
|
752 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
753 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
|
754 # 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
|
755 |
| 22202 | 756 if not skip_host: |
| 757 # this header is issued *only* for HTTP/1.1 | |
| 758 # connections. more specifically, this means it is | |
| 759 # only issued when the client uses the new | |
| 760 # HTTPConnection() class. backwards-compat clients | |
| 761 # will be using HTTP/1.0 and those clients may be | |
| 762 # issuing this header themselves. we should NOT issue | |
| 763 # it twice; some web servers (such as Apache) barf | |
| 764 # 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
|
765 |
| 22202 | 766 # If we need a non-standard port,include it in the |
| 767 # header. If the request is going through a proxy, | |
| 768 # but the host of the actual URL, not the host of the | |
| 769 # proxy. | |
|
22192
044f9e71320f
SF bug report #405939: wrong Host header with proxy
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21962
diff
changeset
|
770 |
| 22202 | 771 netloc = '' |
| 772 if url.startswith('http'): | |
| 773 nil, netloc, nil, nil, nil = urlsplit(url) | |
| 774 | |
| 775 if netloc: | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
776 try: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
777 netloc_enc = netloc.encode("ascii") |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
778 except UnicodeEncodeError: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
779 netloc_enc = netloc.encode("idna") |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
780 self.putheader('Host', netloc_enc) |
| 22202 | 781 else: |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
782 try: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
783 host_enc = self.host.encode("ascii") |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
784 except UnicodeEncodeError: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
785 host_enc = self.host.encode("idna") |
|
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
|
786 if self.port == self.default_port: |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
787 self.putheader('Host', host_enc) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
788 else: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
789 host_enc = host_enc.decode("ascii") |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
36559
diff
changeset
|
790 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
|
791 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
792 # 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
|
793 # 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
|
794 # 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
|
795 # 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
|
796 # 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
|
797 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
798 # 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
|
799 # 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
|
800 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
|
801 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
|
802 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
803 # 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
|
804 # 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
|
805 #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
|
806 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
807 # 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
|
808 # Connection header. |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
809 #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
|
810 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
811 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
812 # 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
|
813 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
|
814 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
815 def putheader(self, header, value): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
816 """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
|
817 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
818 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
|
819 """ |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
820 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
|
821 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
|
822 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
823 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
|
824 header = header.encode('ascii') |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
825 if hasattr(value, 'encode'): |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
826 value = value.encode('ascii') |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
827 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
|
828 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
|
829 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
830 def endheaders(self): |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
831 """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
|
832 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
833 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
|
834 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
|
835 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
836 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
|
837 |
|
24262
3b571e32a6e4
Send HTTP requests with a single send() call instead of many.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24216
diff
changeset
|
838 self._send_output() |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
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 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
|
841 """Send a complete request to the server.""" |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
842 try: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
843 self._send_request(method, url, body, headers) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
844 except socket.error as v: |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
845 # trap 'Broken pipe' if we're allowed to automatically reconnect |
|
41238
f9973a6e1afe
Make test_socket_ssl.py pass by fixing some code that was
Guido van Rossum <guido@python.org>
parents:
40981
diff
changeset
|
846 if v.args[0] != 32 or not self.auto_open: |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
847 raise |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
848 # try one more time |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
849 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
|
850 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
851 def _send_request(self, method, url, body, headers): |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
852 # honour explicitly requested Host: and Accept-Encoding headers |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
853 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
|
854 skips = {} |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
855 if 'host' in header_names: |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
856 skips['skip_host'] = 1 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
857 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
|
858 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
|
859 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
860 self.putrequest(method, url, **skips) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
861 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32252
diff
changeset
|
862 if body and ('content-length' not in header_names): |
| 42527 | 863 thelen = None |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
864 try: |
| 42527 | 865 thelen = str(len(body)) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
866 except TypeError as te: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
867 # If this is a file-like object, try to |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
868 # fstat its file descriptor |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
869 import os |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
870 try: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
871 thelen = str(os.fstat(body.fileno()).st_size) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
872 except (AttributeError, OSError): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
873 # Don't send a length if this failed |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
874 if self.debuglevel > 0: print("Cannot stat!!") |
|
40742
f1701ba2452b
Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40587
diff
changeset
|
875 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
876 if thelen is not None: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
877 self.putheader('Content-Length',thelen) |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
878 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
|
879 self.putheader(hdr, value) |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
880 self.endheaders() |
|
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 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
882 if body: |
| 42133 | 883 if isinstance(body, str): body = body.encode('ascii') |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
884 self.send(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
|
885 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
886 def getresponse(self): |
|
42431
7401ff78eb29
A few local changes for consistency of string literals.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42426
diff
changeset
|
887 """Get the response from 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
|
888 |
|
29584
da0248fcfc52
Deal with a couple XXX comments which asked questions.
Greg Stein <gstein@lyra.org>
parents:
29465
diff
changeset
|
889 # 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
|
890 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
|
891 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
|
892 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
893 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
894 # 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
|
895 # 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
|
896 # behavior) |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
897 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
898 # 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
|
899 # 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
|
900 # 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
|
901 # connection |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
902 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
903 # 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
|
904 # 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
|
905 # response operate independently |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
906 # 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
|
907 # 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
|
908 # |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
909 if self.__state != _CS_REQ_SENT or self.__response: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
910 raise ResponseNotReady() |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
911 |
|
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
|
912 if self.debuglevel > 0: |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
913 response = self.response_class(self.sock, self.debuglevel, |
|
29087
526f37fb380d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28978
diff
changeset
|
914 strict=self.strict, |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
915 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
|
916 else: |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
917 response = self.response_class(self.sock, strict=self.strict, |
|
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28671
diff
changeset
|
918 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
|
919 |
|
24216
c8a19a428358
Change _begin() back to begin().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24168
diff
changeset
|
920 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
|
921 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
|
922 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
|
923 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
924 if response.will_close: |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
925 # 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
|
926 self.close() |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
927 else: |
|
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
928 # 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
|
929 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
|
930 |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
931 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
|
932 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
933 try: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
934 import ssl |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
935 except ImportError: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
936 pass |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
937 else: |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
938 class HTTPSConnection(HTTPConnection): |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
939 "This class allows communication via SSL." |
|
14140
f7a8c40b3880
add docstring explaining makefile limitation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13787
diff
changeset
|
940 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
941 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
|
942 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
943 def __init__(self, host, port=None, key_file=None, cert_file=None, |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47318
diff
changeset
|
944 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
945 HTTPConnection.__init__(self, host, port, strict, timeout) |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
946 self.key_file = key_file |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
947 self.cert_file = cert_file |
|
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
|
948 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
949 def connect(self): |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
950 "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
|
951 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
952 sock = socket.create_connection((self.host, self.port), self.timeout) |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43351
diff
changeset
|
953 self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file) |
|
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
|
954 |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
955 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
956 def FakeSocket (sock, sslobj): |
|
43292
876b7ef4775d
Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
957 warnings.warn("FakeSocket is deprecated, and won't be in 3.x. " + |
|
43400
1bac386c17cc
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43351
diff
changeset
|
958 "Use the result of ssl.wrap_socket() directly instead.", |
|
43292
876b7ef4775d
Merged revisions 57778-58052 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
43095
diff
changeset
|
959 DeprecationWarning, stacklevel=2) |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
960 return sslobj |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
961 |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42964
diff
changeset
|
962 __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
|
963 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
964 class HTTPException(Exception): |
| 24120 | 965 # Subclasses that define an __init__ must call Exception.__init__ |
| 966 # 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
|
967 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
|
968 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
969 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
|
970 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
|
971 |
|
22494
7224a1df4809
add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents:
22403
diff
changeset
|
972 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
|
973 pass |
|
7224a1df4809
add InvalidURL exception - raised if port is given but empty or non-numeric
Skip Montanaro <skip@pobox.com>
parents:
22403
diff
changeset
|
974 |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
975 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
|
976 def __init__(self, version): |
| 24120 | 977 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
|
978 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
|
979 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
980 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
|
981 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
|
982 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
983 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
|
984 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
|
985 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
986 class IncompleteRead(HTTPException): |
|
13636
b5120f58c5da
no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents:
13019
diff
changeset
|
987 def __init__(self, partial): |
| 24120 | 988 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
|
989 self.partial = partial |
|
13019
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
990 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
991 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
|
992 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
|
993 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
994 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
|
995 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
|
996 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
997 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
|
998 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
|
999 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
1000 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
|
1001 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
|
1002 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
1003 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
|
1004 def __init__(self, line): |
| 24120 | 1005 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
|
1006 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
|
1007 |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
1008 # 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
|
1009 error = HTTPException |
|
4a3e9871b41b
initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents:
12716
diff
changeset
|
1010 |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1011 class LineAndFileWrapper: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1012 """A limited file-like object for HTTP/0.9 responses.""" |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1013 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1014 # The status-line parsing code calls readline(), which normally |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1015 # get the HTTP status line. For a 0.9 response, however, this is |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1016 # actually the first line of the body! Clients need to get a |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1017 # readable file object that contains that line. |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1018 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1019 def __init__(self, line, file): |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1020 self._line = line |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1021 self._file = file |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1022 self._line_consumed = 0 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1023 self._line_offset = 0 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1024 self._line_left = len(line) |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1025 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1026 def __getattr__(self, attr): |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1027 return getattr(self._file, attr) |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1028 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1029 def _done(self): |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1030 # called when the last byte is read from the line. After the |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1031 # call, all read methods are delegated to the underlying file |
| 28063 | 1032 # object. |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1033 self._line_consumed = 1 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1034 self.read = self._file.read |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1035 self.readline = self._file.readline |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1036 self.readlines = self._file.readlines |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1037 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1038 def read(self, amt=None): |
|
32252
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1039 if self._line_consumed: |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1040 return self._file.read(amt) |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1041 assert self._line_left |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1042 if amt is None or amt > self._line_left: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1043 s = self._line[self._line_offset:] |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1044 self._done() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1045 if amt is None: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1046 return s + self._file.read() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1047 else: |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24262
diff
changeset
|
1048 return s + self._file.read(amt - len(s)) |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1049 else: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1050 assert amt <= self._line_left |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1051 i = self._line_offset |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1052 j = i + amt |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1053 s = self._line[i:j] |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1054 self._line_offset = j |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1055 self._line_left -= amt |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1056 if self._line_left == 0: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1057 self._done() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1058 return s |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24262
diff
changeset
|
1059 |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1060 def readline(self): |
|
32252
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1061 if self._line_consumed: |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1062 return self._file.readline() |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1063 assert self._line_left |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1064 s = self._line[self._line_offset:] |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1065 self._done() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1066 return s |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1067 |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1068 def readlines(self, size=None): |
|
32252
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1069 if self._line_consumed: |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1070 return self._file.readlines(size) |
|
a72bc4257336
Fix a bug that robotparser starves memory when the server responses
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31967
diff
changeset
|
1071 assert self._line_left |
|
24119
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1072 L = [self._line[self._line_offset:]] |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1073 self._done() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1074 if size is None: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1075 return L + self._file.readlines() |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1076 else: |
|
f33bb9c131f8
Handle HTTP/0.9 responses.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24088
diff
changeset
|
1077 return L + self._file.readlines(size) |
