annotate Lib/httplib.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents a0005d26773d
children c425f66db458
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1 """HTTP/1.1 client library
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
2
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
3 <intro stuff goes here>
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
4 <other stuff, too>
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
5
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
6 HTTPConnection go through a number of "states", which defines when a client
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
20379
aff8d14b28ad Fix for SF buf #458835
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20303
diff changeset
69 import errno
aff8d14b28ad Fix for SF buf #458835
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20303
diff changeset
70 import mimetools
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 import socket
22221
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
72 from urlparse import urlsplit
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
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
74 try:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
75 from cStringIO import StringIO
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
76 except ImportError:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
77 from StringIO import StringIO
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
78
18013
99aea6f5cebd more public symbols for __all__
Skip Montanaro <skip@pobox.com>
parents: 17482
diff changeset
79 __all__ = ["HTTP", "HTTPResponse", "HTTPConnection", "HTTPSConnection",
99aea6f5cebd more public symbols for __all__
Skip Montanaro <skip@pobox.com>
parents: 17482
diff changeset
80 "HTTPException", "NotConnected", "UnknownProtocol",
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
81 "UnknownTransferEncoding", "UnimplementedFileMode",
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
82 "IncompleteRead", "InvalidURL", "ImproperConnectionState",
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
83 "CannotSendRequest", "CannotSendHeader", "ResponseNotReady",
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
84 "BadStatusLine", "error"]
16160
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15878
diff changeset
85
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 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
87 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
88
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
89 _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
90
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
91 # 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
92 _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
93 _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
94 _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
95
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
96 class HTTPMessage(mimetools.Message):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
97
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
98 def addheader(self, key, value):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
99 """Add header for field key handling repeats."""
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
100 prev = self.dict.get(key)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
101 if prev is None:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
102 self.dict[key] = value
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
103 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
104 combined = ", ".join((prev, value))
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
105 self.dict[key] = combined
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
106
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
107 def addcontinue(self, key, more):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
108 """Add more field data from a continuation line."""
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
109 prev = self.dict[key]
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
110 self.dict[key] = prev + "\n " + more
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
111
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
112 def readheaders(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
113 """Read header lines.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
114
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
115 Read header lines up to the entirely blank line that terminates them.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
116 The (normally blank) line that ends the headers is skipped, but not
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
117 included in the returned list. If a non-header line ends the headers,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
118 (which is an error), an attempt is made to backspace over it; it is
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
119 never included in the returned list.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
120
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
121 The variable self.status is set to the empty string if all went well,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
122 otherwise it is an error message. The variable self.headers is a
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
123 completely uninterpreted list of lines contained in the header (so
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
124 printing them will reproduce the header exactly as it appears in the
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
125 file).
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
126
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
127 If multiple header fields with the same name occur, they are combined
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
128 according to the rules in RFC 2616 sec 4.2:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
129
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
130 Appending each subsequent field-value to the first, each separated
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
131 by a comma. The order in which header fields with the same field-name
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
132 are received is significant to the interpretation of the combined
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
133 field value.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
134 """
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
135 # XXX The implementation overrides the readheaders() method of
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
136 # rfc822.Message. The base class design isn't amenable to
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
137 # customized behavior here so the method here is a copy of the
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
138 # base class code with a few small changes.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
139
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
140 self.dict = {}
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
141 self.unixfrom = ''
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
142 self.headers = list = []
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
143 self.status = ''
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
144 headerseen = ""
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
145 firstline = 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
146 startofline = unread = tell = None
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
147 if hasattr(self.fp, 'unread'):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
148 unread = self.fp.unread
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
149 elif self.seekable:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
150 tell = self.fp.tell
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
151 while 1:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
152 if tell:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
153 try:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
154 startofline = tell()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
155 except IOError:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
156 startofline = tell = None
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
157 self.seekable = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
158 line = self.fp.readline()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
159 if not line:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
160 self.status = 'EOF in headers'
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
161 break
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
162 # Skip unix From name time lines
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
163 if firstline and line.startswith('From '):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
164 self.unixfrom = self.unixfrom + line
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
165 continue
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
166 firstline = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
167 if headerseen and line[0] in ' \t':
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
168 # XXX Not sure if continuation lines are handled properly
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
169 # for http and/or for repeating headers
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
170 # It's a continuation line.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
171 list.append(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
172 x = self.dict[headerseen] + "\n " + line.strip()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
173 self.addcontinue(headerseen, line.strip())
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
174 continue
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
175 elif self.iscomment(line):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
176 # It's a comment. Ignore it.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
177 continue
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
178 elif self.islast(line):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
179 # Note! No pushback here! The delimiter line gets eaten.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
180 break
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
181 headerseen = self.isheader(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
182 if headerseen:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
183 # It's a legal header line, save it.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
184 list.append(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
185 self.addheader(headerseen, line[len(headerseen)+1:].strip())
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
186 continue
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
187 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
188 # It's not a header line; throw it back and stop here.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
189 if not self.dict:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
190 self.status = 'No headers'
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
191 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
192 self.status = 'Non-header line where header expected'
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
193 # Try to undo the read.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
194 if unread:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
195 unread(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
196 elif tell:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
197 self.fp.seek(startofline)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
198 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
199 self.status = self.status + '; bad seek'
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
200 break
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
201
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
202 class HTTPResponse:
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
203
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
204 # strict: If true, raise BadStatusLine if the status line can't be
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
205 # parsed as a valid HTTP/1.0 or 1.1 status line. By default it is
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
206 # false because it prevents clients from talking to HTTP/0.9
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
207 # servers. Note that a response with a sufficiently corrupted
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
208 # status line will look like an HTTP/0.9 response.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
209
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
210 # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
211
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
212 def __init__(self, sock, debuglevel=0, strict=0):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
213 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
214 self.debuglevel = debuglevel
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
215 self.strict = strict
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
216
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
217 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
218
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
219 # from the Status-Line of the response
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
220 self.version = _UNKNOWN # HTTP-Version
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
221 self.status = _UNKNOWN # Status-Code
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
222 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
223
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
224 self.chunked = _UNKNOWN # is "chunked" being used?
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
225 self.chunk_left = _UNKNOWN # bytes left to read in current chunk
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
226 self.length = _UNKNOWN # number of bytes left in response
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
227 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
228
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
229 def _read_status(self):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
230 # Initialize with Simple-Response defaults
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
231 line = self.fp.readline()
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
232 if self.debuglevel > 0:
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
233 print "reply:", repr(line)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
234 try:
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
235 [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
236 except ValueError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
237 try:
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
238 [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
239 reason = ""
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
240 except ValueError:
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
241 # empty version will cause next test to fail and status
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
242 # will be treated as 0.9 response.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
243 version = ""
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
244 if not version.startswith('HTTP/'):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
245 if self.strict:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
246 self.close()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
247 raise BadStatusLine(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
248 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
249 # assume it's a Simple-Response from an 0.9 server
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
250 self.fp = LineAndFileWrapper(line, self.fp)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
251 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
252
17482
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
253 # 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
254 try:
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
255 status = int(status)
17482
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
256 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
257 raise BadStatusLine(line)
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
258 except ValueError:
2265032efc9f SF patch #405845 by Martin von Löwis
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16400
diff changeset
259 raise BadStatusLine(line)
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
260 return version, status, reason
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
261
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
262 def begin(self):
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
263 if self.msg is not None:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
264 # we've already started reading the response
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
265 return
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
266
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
267 # read until we get a non-100 response
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
268 while 1:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
269 version, status, reason = self._read_status()
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
270 if status != 100:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
271 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
272 # skip the header from the 100 response
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
273 while 1:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
274 skip = self.fp.readline().strip()
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
275 if not skip:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
276 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
277 if self.debuglevel > 0:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
278 print "header:", skip
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
279
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
280 self.status = status
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
281 self.reason = reason.strip()
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
282 if version == 'HTTP/1.0':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
283 self.version = 10
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
284 elif version.startswith('HTTP/1.'):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
285 self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1
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
286 elif version == 'HTTP/0.9':
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
287 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
288 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
289 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
290
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
291 if self.version == 9:
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
292 self.chunked = 0
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
293 self.will_close = 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
294 self.msg = HTTPMessage(StringIO())
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
295 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
296
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
297 self.msg = HTTPMessage(self.fp, 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
298 if self.debuglevel > 0:
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
299 for hdr in self.msg.headers:
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
300 print "header:", hdr,
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
302 # don't let the msg keep an fp
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
303 self.msg.fp = None
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
304
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
305 # are we using the chunked-style of transfer encoding?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
306 tr_enc = self.msg.getheader('transfer-encoding')
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
307 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
308 self.chunked = 1
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
309 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
310 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
311 self.chunked = 0
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
313 # will the connection close at the end of the response?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
314 conn = self.msg.getheader('connection')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
315 if conn:
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
316 conn = conn.lower()
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
317 # a "Connection: close" will always close the connection. if we
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
318 # don't see that and this is not HTTP/1.1, then the connection will
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
319 # close unless we see a Keep-Alive header.
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
320 self.will_close = conn.find('close') != -1 or \
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
321 ( self.version != 11 and \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
322 not self.msg.getheader('keep-alive') )
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
323 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
324 # for HTTP/1.1, the connection will always remain open
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
325 # otherwise, it will remain open IFF we see a Keep-Alive header
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
326 self.will_close = self.version != 11 and \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
327 not self.msg.getheader('keep-alive')
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
328
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
329 # 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
330 # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
331 length = self.msg.getheader('content-length')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
332 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
333 try:
eeb8ee488106 cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14140
diff changeset
334 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
335 except ValueError:
eeb8ee488106 cope with weird Content-Length values returned from servers by
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14140
diff changeset
336 self.length = None
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
337 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
338 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
339
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
340 # does the body have a fixed length? (of zero)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
341 if (status == 204 or # No Content
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
342 status == 304 or # Not Modified
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
343 100 <= status < 200): # 1xx codes
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
344 self.length = 0
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
346 # 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
347 # 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
348 # WILL close.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
349 if not self.will_close and \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
350 not self.chunked and \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
351 self.length is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
352 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
353
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
354 def close(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
355 if self.fp:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
356 self.fp.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
357 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
358
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
359 def isclosed(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
360 # 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
361 # 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
362 # 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
363 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
364 # 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
365 # 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
366 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
367
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
368 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
369 if self.fp is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
370 return ''
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
372 if self.chunked:
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
373 return self._read_chunked(amt)
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25422
diff changeset
374
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
375 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
376 # unbounded read
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
377 if self.will_close:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
378 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
379 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
380 s = self._safe_read(self.length)
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
381 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
382 return s
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
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 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
385 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
386 # 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
387 amt = self.length
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
388 self.length -= amt
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
390 # 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
391 # 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
392 # (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
393 s = self.fp.read(amt)
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
394
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
395 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
396
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
397 def _read_chunked(self, amt):
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
398 assert self.chunked != _UNKNOWN
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
399 chunk_left = self.chunk_left
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
400 value = ''
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
401
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
402 # XXX This accumulates chunks by repeated string concatenation,
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
403 # which is not efficient as the number or size of chunks gets big.
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
404 while 1:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
405 if chunk_left is None:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
406 line = self.fp.readline()
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
407 i = line.find(';')
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
408 if i >= 0:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
409 line = line[:i] # strip chunk-extensions
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
410 chunk_left = int(line, 16)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
411 if chunk_left == 0:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
412 break
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
413 if amt is None:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
414 value += self._safe_read(chunk_left)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
415 elif amt < chunk_left:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
416 value += self._safe_read(amt)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
417 self.chunk_left = chunk_left - amt
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
418 return value
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
419 elif amt == chunk_left:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
420 value += self._safe_read(amt)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
421 self._safe_read(2) # toss the CRLF at the end of the chunk
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
422 self.chunk_left = None
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
423 return value
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
424 else:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
425 value += self._safe_read(chunk_left)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
426 amt -= chunk_left
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
427
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
428 # we read the whole chunk, get another
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
429 self._safe_read(2) # toss the CRLF at the end of the chunk
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
430 chunk_left = None
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
431
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
432 # read and discard trailer up to the CRLF terminator
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
433 ### note: we shouldn't have any trailers!
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
434 while 1:
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
435 line = self.fp.readline()
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
436 if line == '\r\n':
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
437 break
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
438
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
439 # we read everything; close the "file"
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
440 # XXX Shouldn't the client close the file?
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
441 self.close()
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
442
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
443 return value
25622
a0005d26773d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 25422
diff changeset
444
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
445 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
446 """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
447
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
448 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
449 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
450
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
451 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
452 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
453 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
454
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
455 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
456 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
457 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
458 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
459 s = ''
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
460 while amt > 0:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
461 chunk = self.fp.read(amt)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
462 if not chunk:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
463 raise IncompleteRead(s)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
464 s = s + chunk
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
465 amt = amt - len(chunk)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
466 return 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
467
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
468 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
469 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
470 raise ResponseNotReady()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
471 return self.msg.getheader(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
472
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
474 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
475
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
476 _http_vsn = 11
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
477 _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
478
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
479 response_class = HTTPResponse
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
480 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
481 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
482 debuglevel = 0
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
483 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
484
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
485 def __init__(self, host, port=None, strict=None):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
486 self.sock = None
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
487 self._buffer = []
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
488 self.__response = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
489 self.__state = _CS_IDLE
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
490
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
491 self._set_hostport(host, port)
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
492 if strict is not None:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
493 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
494
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
495 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
496 if port is None:
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
497 i = host.find(':')
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
498 if i >= 0:
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
499 try:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
500 port = int(host[i+1:])
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
501 except ValueError:
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
502 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
503 host = host[:i]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
504 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
505 port = self.default_port
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
506 self.host = host
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
507 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
508
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
509 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
510 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
511
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
512 def connect(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
513 """Connect to the host and port specified in __init__."""
18902
edf058b16ddf Initialize msg to avoid unbound locals.
Martin v. Löwis <martin@v.loewis.de>
parents: 18834
diff changeset
514 msg = "getaddrinfo returns an empty list"
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
515 for res in socket.getaddrinfo(self.host, self.port, 0,
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
516 socket.SOCK_STREAM):
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
517 af, socktype, proto, canonname, sa = res
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
518 try:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
519 self.sock = socket.socket(af, socktype, proto)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
520 if self.debuglevel > 0:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
521 print "connect: (%s, %s)" % (self.host, self.port)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
522 self.sock.connect(sa)
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
523 except socket.error, msg:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
524 if self.debuglevel > 0:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
525 print 'connect fail:', (self.host, self.port)
20303
faec69c11640 Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents: 19367
diff changeset
526 if self.sock:
faec69c11640 Only close sockets if they have been created. Reported by Blake Winton.
Martin v. Löwis <martin@v.loewis.de>
parents: 19367
diff changeset
527 self.sock.close()
18834
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
528 self.sock = None
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
529 continue
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
530 break
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
531 if not self.sock:
7e52bf52ae33 Untabify IPv6 changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 18825
diff changeset
532 raise socket.error, msg
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
533
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
534 def close(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
535 """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
536 if self.sock:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
537 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
538 self.sock = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
539 if self.__response:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
540 self.__response.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
541 self.__response = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
542 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
543
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
544 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
545 """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
546 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
547 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
548 self.connect()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
549 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
550 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
551
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
552 # send the data to the server. if we get a broken pipe, then close
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
553 # 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
554 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
555 # 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
556 # 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
557 if self.debuglevel > 0:
1b20ad6eb501 Do not close socket when a Content-Length is 0. This make the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14560
diff changeset
558 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
559 try:
21963
2faa23facef7 The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents: 20380
diff changeset
560 self.sock.sendall(str)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
561 except socket.error, v:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15859
diff changeset
562 if v[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
563 self.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
564 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
565
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
566 def _output(self, s):
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
567 """Add a line of output to the current request buffer.
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
568
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
569 Assumes that the line does *not* end with \\r\\n.
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
570 """
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
571 self._buffer.append(s)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
572
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
573 def _send_output(self):
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
574 """Send the currently buffered request and clear the buffer.
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
575
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
576 Appends an extra \\r\\n to the buffer.
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
577 """
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
578 self._buffer.extend(("", ""))
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
579 msg = "\r\n".join(self._buffer)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
580 del self._buffer[:]
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
581 self.send(msg)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
582
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
583 def putrequest(self, method, url, skip_host=0):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
584 """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
585
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
586 `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
587 `url' specifies the object being requested, e.g. '/index.html'.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
588 """
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
589
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
590 # check if a prior response has been completed
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
591 # XXX What if it hasn't?
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
592 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
593 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
594
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
595 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
596 # 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
597 # this occurs when:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
598 # 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
599 # 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
600 # 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
601 # 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
602 # 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
603 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
604 # 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
605 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
606 # 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
607 # 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
608 # 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
609 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
610 # 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
611 # 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
612 # 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
613 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
614 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
615 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
616 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
617 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
618
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
619 if not url:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
620 url = '/'
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
621 str = '%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
622
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
623 self._output(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
624
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
625 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
626 # 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
627
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
628 if not skip_host:
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
629 # this header is issued *only* for HTTP/1.1
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
630 # connections. more specifically, this means it is
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
631 # only issued when the client uses the new
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
632 # HTTPConnection() class. backwards-compat clients
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
633 # will be using HTTP/1.0 and those clients may be
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
634 # issuing this header themselves. we should NOT issue
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
635 # it twice; some web servers (such as Apache) barf
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
636 # 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
637
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
638 # If we need a non-standard port,include it in the
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
639 # header. If the request is going through a proxy,
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
640 # but the host of the actual URL, not the host of the
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
641 # proxy.
22221
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
642
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
643 netloc = ''
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
644 if url.startswith('http'):
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
645 nil, netloc, nil, nil, nil = urlsplit(url)
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
646
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
647 if netloc:
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
648 self.putheader('Host', netloc)
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
649 elif self.port == HTTP_PORT:
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
650 self.putheader('Host', self.host)
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
651 else:
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
652 self.putheader('Host', "%s:%s" % (self.host, 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
653
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
654 # 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
655 # 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
656 # 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
657 # 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
658 # 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
659
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
660 # 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
661 # support encodings such as x-gzip or x-deflate.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
662 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
663
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
664 # 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
665 # 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
666 #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
667
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
668 # 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
669 # Connection header.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
670 #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
671
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
672 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
673 # 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
674 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
675
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
676 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
677 """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
678
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
679 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
680 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
681 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
682 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
683
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
684 str = '%s: %s' % (header, value)
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
685 self._output(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
686
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
687 def endheaders(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
688 """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
689
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
690 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
691 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
692 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
693 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
694
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
695 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
696
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
697 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
698 """Send a complete 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
699
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
700 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
701 self._send_request(method, url, body, headers)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
702 except socket.error, v:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
703 # trap 'Broken pipe' if we're allowed to automatically reconnect
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
704 if v[0] != 32 or not self.auto_open:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
705 raise
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
706 # 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
707 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
708
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
709 def _send_request(self, method, url, body, headers):
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
710 # If headers already contains a host header, then define the
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
711 # optional skip_host argument to putrequest(). The check is
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
712 # harder because field names are case insensitive.
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
713 if 'Host' in (headers
22229
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
714 or [k for k in headers.iterkeys() if k.lower() == "host"]):
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
715 self.putrequest(method, url, skip_host=1)
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
716 else:
9968d8dddd53 I *think* this should go in. May be wrong -- please howl in protest
Michael W. Hudson <mwh@python.net>
parents: 22221
diff changeset
717 self.putrequest(method, url)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
718
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
719 if body:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
720 self.putheader('Content-Length', str(len(body)))
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
721 for hdr, value in headers.items():
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
722 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
723 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
724
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
725 if body:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
726 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
727
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
728 def getresponse(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
729 "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
730
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
731 # check if a prior response has been completed
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
732 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
733 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
734
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
735 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
736 # 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
737 # 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
738 # behavior)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
739 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
740 # 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
741 # 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
742 # 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
743 # connection
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
744 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
745 # 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
746 # 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
747 # response operate independently
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
748 # 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
749 # 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
750 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
751 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
752 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
753
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
754 if self.debuglevel > 0:
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
755 response = self.response_class(self.sock, self.debuglevel,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
756 strict=self.strict)
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
757 else:
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
758 response = self.response_class(self.sock, strict=self.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
759
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
760 response.begin()
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
761 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
762 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
763
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
764 if response.will_close:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
765 # this effectively passes the connection to the response
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
766 self.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
767 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
768 # 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
769 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
770
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
771 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
772
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
773 # The next several classes are used to define FakeSocket,a socket-like
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
774 # interface to an SSL connection.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
775
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
776 # The primary complexity comes from faking a makefile() method. The
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
777 # standard socket makefile() implementation calls dup() on the socket
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
778 # file descriptor. As a consequence, clients can call close() on the
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
779 # parent socket and its makefile children in any order. The underlying
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
780 # socket isn't closed until they are all closed.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
781
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
782 # The implementation uses reference counting to keep the socket open
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
783 # until the last client calls close(). SharedSocket keeps track of
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
784 # the reference counting and SharedSocketClient provides an constructor
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
785 # and close() method that call incref() and decref() correctly.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
786
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
787 class SharedSocket:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
788
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
789 def __init__(self, sock):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
790 self.sock = sock
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
791 self._refcnt = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
792
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
793 def incref(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
794 self._refcnt += 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
795
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
796 def decref(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
797 self._refcnt -= 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
798 assert self._refcnt >= 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
799 if self._refcnt == 0:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
800 self.sock.close()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
801
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
802 def __del__(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
803 self.sock.close()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
804
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
805 class SharedSocketClient:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
806
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
807 def __init__(self, shared):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
808 self._closed = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
809 self._shared = shared
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
810 self._shared.incref()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
811 self._sock = shared.sock
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
812
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
813 def close(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
814 if not self._closed:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
815 self._shared.decref()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
816 self._closed = 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
817 self._shared = None
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
818
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
819 class SSLFile(SharedSocketClient):
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
820 """File-like object wrapping an SSL socket."""
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
821
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
822 BUFSIZE = 8192
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
823
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
824 def __init__(self, sock, ssl, bufsize=None):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
825 SharedSocketClient.__init__(self, sock)
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
826 self._ssl = ssl
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
827 self._buf = ''
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
828 self._bufsize = bufsize or self.__class__.BUFSIZE
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
829
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
830 def _read(self):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
831 buf = ''
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
832 # put in a loop so that we retry on transient errors
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
833 while 1:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
834 try:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
835 buf = self._ssl.read(self._bufsize)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
836 except socket.sslerror, err:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
837 if (err[0] == socket.SSL_ERROR_WANT_READ
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
838 or err[0] == socket.SSL_ERROR_WANT_WRITE):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
839 continue
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
840 if (err[0] == socket.SSL_ERROR_ZERO_RETURN
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
841 or err[0] == socket.SSL_ERROR_EOF):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
842 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
843 raise
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
844 except socket.error, err:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
845 if err[0] == errno.EINTR:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
846 continue
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
847 if err[0] == errno.EBADF:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
848 # XXX socket was closed?
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
849 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
850 raise
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
851 else:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
852 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
853 return buf
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
854
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
855 def read(self, size=None):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
856 L = [self._buf]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
857 avail = len(self._buf)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
858 while size is None or avail < size:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
859 s = self._read()
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
860 if s == '':
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
861 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
862 L.append(s)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
863 avail += len(s)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
864 all = "".join(L)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
865 if size is None:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
866 self._buf = ''
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
867 return all
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
868 else:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
869 self._buf = all[size:]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
870 return all[:size]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
871
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
872 def readline(self):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
873 L = [self._buf]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
874 self._buf = ''
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
875 while 1:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
876 i = L[-1].find("\n")
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
877 if i >= 0:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
878 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
879 s = self._read()
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
880 if s == '':
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
881 break
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
882 L.append(s)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
883 if i == -1:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
884 # loop exited because there is no more data
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
885 return "".join(L)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
886 else:
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
887 all = "".join(L)
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
888 # XXX could do enough bookkeeping not to do a 2nd search
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
889 i = all.find("\n") + 1
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
890 line = all[:i]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
891 self._buf = all[i:]
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
892 return line
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
893
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
894 class FakeSocket(SharedSocketClient):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
895
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
896 class _closedsocket:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
897 def __getattr__(self, name):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
898 raise error(9, 'Bad file descriptor')
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
899
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
900 def __init__(self, sock, ssl):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
901 sock = SharedSocket(sock)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
902 SharedSocketClient.__init__(self, sock)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
903 self._ssl = ssl
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
904
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
905 def close(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
906 SharedSocketClient.close(self)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
907 self._sock = self.__class__._closedsocket()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
908
14140
f7a8c40b3880 add docstring explaining makefile limitation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13787
diff changeset
909 def makefile(self, mode, bufsize=None):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
910 if mode != 'r' and mode != 'rb':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
911 raise UnimplementedFileMode()
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
912 return SSLFile(self._shared, self._ssl, bufsize)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
913
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
914 def send(self, stuff, flags = 0):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
915 return self._ssl.write(stuff)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
916
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
917 sendall = send
22526
0c1912f9d1ce backport akuchling's checkin of
Michael W. Hudson <mwh@python.net>
parents: 22229
diff changeset
918
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
919 def recv(self, len = 1024, flags = 0):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
920 return self._ssl.read(len)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
921
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
922 def __getattr__(self, attr):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
923 return getattr(self._sock, attr)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
924
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
925
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
926 class HTTPSConnection(HTTPConnection):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
927 "This class allows communication via SSL."
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
928
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
929 default_port = HTTPS_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
930
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
931 def __init__(self, host, port=None, key_file=None, cert_file=None,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
932 strict=None):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
933 HTTPConnection.__init__(self, host, port, strict)
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
934 self.key_file = key_file
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
935 self.cert_file = cert_file
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
936
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
937 def connect(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
938 "Connect to a host on a given (SSL) 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
939
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
940 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
941 sock.connect((self.host, self.port))
15503
39742c91add3 Hoepeful fix for SF bug #123924: Windows - using OpenSSL, problem with
Guido van Rossum <guido@python.org>
parents: 15182
diff changeset
942 realsock = sock
39742c91add3 Hoepeful fix for SF bug #123924: Windows - using OpenSSL, problem with
Guido van Rossum <guido@python.org>
parents: 15182
diff changeset
943 if hasattr(sock, "_sock"):
39742c91add3 Hoepeful fix for SF bug #123924: Windows - using OpenSSL, problem with
Guido van Rossum <guido@python.org>
parents: 15182
diff changeset
944 realsock = sock._sock
39742c91add3 Hoepeful fix for SF bug #123924: Windows - using OpenSSL, problem with
Guido van Rossum <guido@python.org>
parents: 15182
diff changeset
945 ssl = socket.ssl(realsock, self.key_file, self.cert_file)
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
946 self.sock = FakeSocket(sock, ssl)
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
947
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
948
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
949 class HTTP:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
950 "Compatibility class with httplib.py from 1.5."
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
951
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
952 _http_vsn = 10
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
953 _http_vsn_str = 'HTTP/1.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
954
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
955 debuglevel = 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
956
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
957 _connection_class = HTTPConnection
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
958
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
959 def __init__(self, host='', port=None, strict=None):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
960 "Provide a default host, since the superclass requires one."
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
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
962 # some joker passed 0 explicitly, meaning default port
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
963 if port == 0:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
964 port = 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
965
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
966 # Note that we may pass an empty string as the host; this will throw
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
967 # an error when we attempt to connect. Presumably, the client code
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
968 # will call connect before then, with a proper host.
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
969 self._setup(self._connection_class(host, port, strict))
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
970
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
971 def _setup(self, conn):
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
972 self._conn = conn
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
973
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
974 # set up delegation to flesh out interface
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
975 self.send = conn.send
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
976 self.putrequest = conn.putrequest
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
977 self.endheaders = conn.endheaders
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
978 self.set_debuglevel = conn.set_debuglevel
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
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
980 conn._http_vsn = self._http_vsn
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
981 conn._http_vsn_str = 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
982
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
983 self.file = 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
984
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
985 def connect(self, host=None, port=None):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
986 "Accept arguments to set the host/port, since the superclass doesn't."
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
987
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
988 if host is not None:
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
989 self._conn._set_hostport(host, port)
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
990 self._conn.connect()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
991
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
992 def getfile(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
993 "Provide a getfile, since the superclass' does not use this concept."
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
994 return self.file
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
995
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
996 def putheader(self, header, *values):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
997 "The superclass allows only one value argument."
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
998 self._conn.putheader(header, '\r\n\t'.join(values))
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
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1000 def getreply(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1001 """Compat definition since superclass does not define it.
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
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1003 Returns a tuple consisting of:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1004 - server status code (e.g. '200' if all goes well)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1005 - server "reason" corresponding to status code
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1006 - any RFC822 headers in the response from the server
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1007 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1008 try:
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1009 response = self._conn.getresponse()
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1010 except BadStatusLine, e:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1011 ### hmm. if getresponse() ever closes the socket on a bad request,
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1012 ### then we are going to have problems with self.sock
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1013
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1014 ### should we keep this behavior? do people use it?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1015 # keep the socket open (as a file), and return it
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1016 self.file = self._conn.sock.makefile('rb', 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
1017
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1018 # close our socket -- we want to restart after any protocol error
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1019 self.close()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1020
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1021 self.headers = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1022 return -1, e.line, 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
1023
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1024 self.headers = response.msg
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1025 self.file = response.fp
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1026 return response.status, response.reason, response.msg
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1027
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1028 def close(self):
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1029 self._conn.close()
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1030
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1031 # note that self.file == response.fp, which gets closed by the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1032 # superclass. just clear the object ref here.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1033 ### hmm. messy. if status==-1, then self.file is owned by us.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1034 ### well... we aren't explicitly closing, but losing this ref will
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1035 ### do it
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13019
diff changeset
1036 self.file = 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
1037
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1038 if hasattr(socket, 'ssl'):
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1039 class HTTPS(HTTP):
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1040 """Compatibility with 1.5 httplib interface
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1041
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1042 Python 1.5.2 did not have an HTTPS class, but it defined an
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1043 interface for sending http requests that is also useful for
15845
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
1044 https.
13787
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1045 """
1f966f0d467c add support for HTTPS
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13636
diff changeset
1046
14740
8bb6dca66ecd Indent _connection_class so that it becomes HTTPS._connection_class.
Martin v. Löwis <martin@v.loewis.de>
parents: 14672
diff changeset
1047 _connection_class = HTTPSConnection
15845
210bbe53b6d4 No text file relying on significant trailing whitespace is robust under
Tim Peters <tim.peters@gmail.com>
parents: 15844
diff changeset
1048
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1049 def __init__(self, host='', port=None, key_file=None, cert_file=None,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1050 strict=None):
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1051 # provide a default host, pass the X509 cert info
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1052
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1053 # urf. compensate for bad input.
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1054 if port == 0:
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1055 port = None
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1056 self._setup(self._connection_class(host, port, key_file,
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1057 cert_file, strict))
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1058
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1059 # we never actually use these for anything, but we keep them
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1060 # here for compatibility with post-1.5.2 CVS.
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1061 self.key_file = key_file
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1062 self.cert_file = cert_file
19367
17802077e77c Resolve patch #449367.
Greg Stein <gstein@lyra.org>
parents: 18902
diff changeset
1063
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1064
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1065 class HTTPException(Exception):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1066 # Subclasses that define an __init__ must call Exception.__init__
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1067 # 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
1068 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
1069
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1070 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
1071 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
1072
24083
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
1073 class InvalidURL(HTTPException):
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
1074 pass
d7126c34128a Backport various bug fixes from trunk.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23189
diff changeset
1075
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1076 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
1077 def __init__(self, version):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1078 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
1079 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
1080
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1081 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
1082 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
1083
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1084 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
1085 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
1086
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1087 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
1088 def __init__(self, partial):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1089 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
1090 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
1091
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1092 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
1093 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
1094
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1095 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
1096 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
1097
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1098 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
1099 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
1100
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1101 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
1102 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
1103
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1104 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
1105 def __init__(self, line):
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1106 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
1107 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
1108
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1109 # 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
1110 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
1111
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1112 class LineAndFileWrapper:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1113 """A limited file-like object for HTTP/0.9 responses."""
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1114
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1115 # The status-line parsing code calls readline(), which normally
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1116 # get the HTTP status line. For a 0.9 response, however, this is
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1117 # actually the first line of the body! Clients need to get a
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1118 # readable file object that contains that line.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1119
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1120 def __init__(self, line, file):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1121 self._line = line
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1122 self._file = file
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1123 self._line_consumed = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1124 self._line_offset = 0
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1125 self._line_left = len(line)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1126
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1127 def __getattr__(self, attr):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1128 return getattr(self._file, attr)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1129
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1130 def _done(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1131 # called when the last byte is read from the line. After the
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1132 # call, all read methods are delegated to the underlying file
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1133 # obhect.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1134 self._line_consumed = 1
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1135 self.read = self._file.read
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1136 self.readline = self._file.readline
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1137 self.readlines = self._file.readlines
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1138
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1139 def read(self, amt=None):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1140 assert not self._line_consumed and self._line_left
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1141 if amt is None or amt > self._line_left:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1142 s = self._line[self._line_offset:]
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1143 self._done()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1144 if amt is None:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1145 return s + self._file.read()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1146 else:
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
1147 return s + self._file.read(amt - len(s))
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1148 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1149 assert amt <= self._line_left
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1150 i = self._line_offset
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1151 j = i + amt
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1152 s = self._line[i:j]
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1153 self._line_offset = j
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1154 self._line_left -= amt
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1155 if self._line_left == 0:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1156 self._done()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1157 return s
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
1158
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1159 def readline(self):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1160 s = self._line[self._line_offset:]
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1161 self._done()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1162 return s
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1163
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1164 def readlines(self, size=None):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1165 L = [self._line[self._line_offset:]]
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1166 self._done()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1167 if size is None:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1168 return L + self._file.readlines()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1169 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1170 return L + self._file.readlines(size)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1171
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 def test():
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1173 """Test this module.
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1174
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1175 A hodge podge of tests collected here, because they have too many
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1176 external dependencies for the regular test suite.
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1177 """
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1178
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1179 import sys
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1180 import getopt
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1181 opts, args = getopt.getopt(sys.argv[1:], 'd')
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1182 dl = 0
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1183 for o, a in opts:
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1184 if o == '-d': dl = dl + 1
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1185 host = 'www.python.org'
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1186 selector = '/'
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1187 if args[0:]: host = args[0]
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1188 if args[1:]: selector = args[1]
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1189 h = HTTP()
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1190 h.set_debuglevel(dl)
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1191 h.connect(host)
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1192 h.putrequest('GET', selector)
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1193 h.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
1194 status, reason, headers = h.getreply()
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1195 print 'status =', status
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1196 print 'reason =', reason
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1197 print "read", len(h.getfile().read())
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1198 print
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1199 if headers:
15582
6179d4afa4e4 Get rid of string functions.
Guido van Rossum <guido@python.org>
parents: 15503
diff changeset
1200 for header in headers.headers: print header.strip()
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1201 print
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1202
22221
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1203 # minimal test that code to extract host from url works
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1204 class HTTP11(HTTP):
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1205 _http_vsn = 11
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1206 _http_vsn_str = 'HTTP/1.1'
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1207
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1208 h = HTTP11('www.python.org')
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1209 h.putrequest('GET', 'http://www.python.org/~jeremy/')
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1210 h.endheaders()
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1211 h.getreply()
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1212 h.close()
d6d12e21d44f backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21963
diff changeset
1213
13019
4a3e9871b41b initial commit of a new HTTP library, supporting HTTP/1.1 and persistent
Greg Stein <gstein@lyra.org>
parents: 12716
diff changeset
1214 if hasattr(socket, 'ssl'):
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
1215
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1216 for host, selector in (('sourceforge.net', '/projects/python'),
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1217 ('dbserv2.theopalgroup.com', '/mediumfile'),
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1218 ('dbserv2.theopalgroup.com', '/smallfile'),
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1219 ):
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1220 print "https://%s%s" % (host, selector)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1221 hs = HTTPS()
25422
f749d50dccdc Dump trunk version of httplib.py onto branch.
Michael W. Hudson <mwh@python.net>
parents: 24217
diff changeset
1222 hs.set_debuglevel(dl)
24217
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1223 hs.connect(host)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1224 hs.putrequest('GET', selector)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1225 hs.endheaders()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1226 status, reason, headers = hs.getreply()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1227 print 'status =', status
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1228 print 'reason =', reason
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1229 print "read", len(hs.getfile().read())
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1230 print
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1231 if headers:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1232 for header in headers.headers: print header.strip()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1233 print
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1234
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1235 # Test a buggy server -- returns garbled status line.
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1236 # http://www.yahoo.com/promotions/mom_com97/supermom.html
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1237 c = HTTPConnection("promotions.yahoo.com")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1238 c.set_debuglevel(1)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1239 c.connect()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1240 c.request("GET", "/promotions/mom_com97/supermom.html")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1241 r = c.getresponse()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1242 print r.status, r.version
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1243 lines = r.read().split("\n")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1244 print "\n".join(lines[:5])
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1245
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1246 c = HTTPConnection("promotions.yahoo.com", strict=1)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1247 c.set_debuglevel(1)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1248 c.connect()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1249 c.request("GET", "/promotions/mom_com97/supermom.html")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1250 try:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1251 r = c.getresponse()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1252 except BadStatusLine, err:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1253 print "strict mode failed as expected"
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1254 print err
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1255 else:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1256 print "XXX strict mode should have failed"
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1257
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1258 for strict in 0, 1:
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1259 h = HTTP(strict=strict)
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1260 h.connect("promotions.yahoo.com")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1261 h.putrequest('GET', "/promotions/mom_com97/supermom.html")
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1262 h.endheaders()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1263 status, reason, headers = h.getreply()
494a8beb4582 Backport changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24083
diff changeset
1264 assert (strict and status == -1) or status == 200, (strict, status)
3073
d6b130fa91b6 fixed the test program
Guido van Rossum <guido@python.org>
parents: 2919
diff changeset
1265
1555
6d35308261f4 Towards a standard access mechanism for URLs.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 if __name__ == '__main__':
6787
f6954274e1ca Doc strings by Mitch Chapman (with a little reformatting).
Guido van Rossum <guido@python.org>
parents: 5974
diff changeset
1267 test()