Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 1 | """HTTP/1.1 client library |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 2 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 3 | <intro stuff goes here> |
| 4 | <other stuff, too> |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 5 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 6 | HTTPConnection go through a number of "states", which defines when a client |
| 7 | may legally make another request or fetch the response for a particular |
| 8 | request. This diagram details these state transitions: |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 9 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 10 | (null) |
| 11 | | |
| 12 | | HTTPConnection() |
| 13 | v |
| 14 | Idle |
| 15 | | |
| 16 | | putrequest() |
| 17 | v |
| 18 | Request-started |
| 19 | | |
| 20 | | ( putheader() )* endheaders() |
| 21 | v |
| 22 | Request-sent |
| 23 | | |
| 24 | | response = getresponse() |
| 25 | v |
| 26 | Unread-response [Response-headers-read] |
| 27 | |\____________________ |
| 28 | | \ |
| 29 | | response.read() | putrequest() |
| 30 | v v |
| 31 | Idle Req-started-unread-response |
| 32 | _______/| |
| 33 | / | |
| 34 | response.read() | | ( putheader() )* endheaders() |
| 35 | v v |
| 36 | Request-started Req-sent-unread-response |
| 37 | | |
| 38 | | response.read() |
| 39 | v |
| 40 | Request-sent |
| 41 | |
| 42 | This diagram presents the following rules: |
| 43 | -- a second request may not be started until {response-headers-read} |
| 44 | -- a response [object] cannot be retrieved until {request-sent} |
| 45 | -- there is no differentiation between an unread response body and a |
| 46 | partially read response body |
| 47 | |
| 48 | Note: this enforcement is applied by the HTTPConnection class. The |
| 49 | HTTPResponse class does not enforce this state machine, which |
| 50 | implies sophisticated clients may accelerate the request/response |
| 51 | pipeline. Caution should be taken, though: accelerating the states |
| 52 | beyond the above pattern may imply knowledge of the server's |
| 53 | connection-close behavior for certain requests. For example, it |
| 54 | is impossible to tell whether the server will close the connection |
| 55 | UNTIL the response headers have been read; this means that further |
| 56 | requests cannot be placed into the pipeline until it is known that |
| 57 | the server will NOT be closing the connection. |
| 58 | |
| 59 | Logical State __state __response |
| 60 | ------------- ------- ---------- |
| 61 | Idle _CS_IDLE None |
| 62 | Request-started _CS_REQ_STARTED None |
| 63 | Request-sent _CS_REQ_SENT None |
| 64 | Unread-response _CS_IDLE <response_class> |
| 65 | Req-started-unread-response _CS_REQ_STARTED <response_class> |
| 66 | Req-sent-unread-response _CS_REQ_SENT <response_class> |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 67 | """ |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 69 | import socket |
Guido van Rossum | 65ab98c | 1995-08-07 20:13:02 +0000 | [diff] [blame] | 70 | import mimetools |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 72 | try: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 73 | from cStringIO import StringIO |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 74 | except ImportError: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 75 | from StringIO import StringIO |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 77 | HTTP_PORT = 80 |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 78 | HTTPS_PORT = 443 |
| 79 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 80 | _UNKNOWN = 'UNKNOWN' |
| 81 | |
| 82 | # connection states |
| 83 | _CS_IDLE = 'Idle' |
| 84 | _CS_REQ_STARTED = 'Request-started' |
| 85 | _CS_REQ_SENT = 'Request-sent' |
| 86 | |
| 87 | |
| 88 | class HTTPResponse: |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 89 | def __init__(self, sock, debuglevel=0): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 90 | self.fp = sock.makefile('rb', 0) |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 91 | self.debuglevel = debuglevel |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 92 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 93 | self.msg = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 94 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 95 | # from the Status-Line of the response |
| 96 | self.version = _UNKNOWN # HTTP-Version |
| 97 | self.status = _UNKNOWN # Status-Code |
| 98 | self.reason = _UNKNOWN # Reason-Phrase |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 99 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 100 | self.chunked = _UNKNOWN # is "chunked" being used? |
| 101 | self.chunk_left = _UNKNOWN # bytes left to read in current chunk |
| 102 | self.length = _UNKNOWN # number of bytes left in response |
| 103 | self.will_close = _UNKNOWN # conn will close at end of response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 104 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 105 | def begin(self): |
| 106 | if self.msg is not None: |
| 107 | # we've already started reading the response |
| 108 | return |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 109 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 110 | line = self.fp.readline() |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 111 | if self.debuglevel > 0: |
| 112 | print "reply:", repr(line) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 113 | try: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 114 | [version, status, reason] = line.split(None, 2) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 115 | except ValueError: |
| 116 | try: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 117 | [version, status] = line.split(None, 1) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 118 | reason = "" |
| 119 | except ValueError: |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 120 | version = "HTTP/0.9" |
| 121 | status = "200" |
| 122 | reason = "" |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 123 | if version[:5] != 'HTTP/': |
| 124 | self.close() |
| 125 | raise BadStatusLine(line) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 126 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 127 | self.status = status = int(status) |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 128 | self.reason = reason.strip() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 129 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 130 | if version == 'HTTP/1.0': |
| 131 | self.version = 10 |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 132 | elif version.startswith('HTTP/1.'): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 133 | self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 134 | elif version == 'HTTP/0.9': |
| 135 | self.version = 9 |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 136 | else: |
| 137 | raise UnknownProtocol(version) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 138 | |
Jeremy Hylton | 110941a | 2000-10-12 19:58:36 +0000 | [diff] [blame] | 139 | if self.version == 9: |
| 140 | self.msg = mimetools.Message(StringIO()) |
| 141 | return |
| 142 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 143 | self.msg = mimetools.Message(self.fp, 0) |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 144 | if self.debuglevel > 0: |
| 145 | for hdr in self.msg.headers: |
| 146 | print "header:", hdr, |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 147 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 148 | # don't let the msg keep an fp |
| 149 | self.msg.fp = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 150 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 151 | # are we using the chunked-style of transfer encoding? |
| 152 | tr_enc = self.msg.getheader('transfer-encoding') |
| 153 | if tr_enc: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 154 | if tr_enc.lower() != 'chunked': |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 155 | raise UnknownTransferEncoding() |
| 156 | self.chunked = 1 |
| 157 | self.chunk_left = None |
| 158 | else: |
| 159 | self.chunked = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 160 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 161 | # will the connection close at the end of the response? |
| 162 | conn = self.msg.getheader('connection') |
| 163 | if conn: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 164 | conn = conn.lower() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 165 | # a "Connection: close" will always close the connection. if we |
| 166 | # don't see that and this is not HTTP/1.1, then the connection will |
| 167 | # close unless we see a Keep-Alive header. |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 168 | self.will_close = conn.find('close') != -1 or \ |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 169 | ( self.version != 11 and \ |
| 170 | not self.msg.getheader('keep-alive') ) |
| 171 | else: |
| 172 | # for HTTP/1.1, the connection will always remain open |
| 173 | # otherwise, it will remain open IFF we see a Keep-Alive header |
| 174 | self.will_close = self.version != 11 and \ |
| 175 | not self.msg.getheader('keep-alive') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 176 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 177 | # do we have a Content-Length? |
| 178 | # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" |
| 179 | length = self.msg.getheader('content-length') |
| 180 | if length and not self.chunked: |
Jeremy Hylton | 30a8181 | 2000-09-14 20:34:27 +0000 | [diff] [blame] | 181 | try: |
| 182 | self.length = int(length) |
| 183 | except ValueError: |
| 184 | self.length = None |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 185 | else: |
| 186 | self.length = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 187 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 188 | # does the body have a fixed length? (of zero) |
| 189 | if (status == 204 or # No Content |
| 190 | status == 304 or # Not Modified |
| 191 | 100 <= status < 200): # 1xx codes |
| 192 | self.length = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 193 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 194 | # if the connection remains open, and we aren't using chunked, and |
| 195 | # a content-length was not provided, then assume that the connection |
| 196 | # WILL close. |
| 197 | if not self.will_close and \ |
| 198 | not self.chunked and \ |
| 199 | self.length is None: |
| 200 | self.will_close = 1 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 201 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 202 | def close(self): |
| 203 | if self.fp: |
| 204 | self.fp.close() |
| 205 | self.fp = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 206 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 207 | def isclosed(self): |
| 208 | # NOTE: it is possible that we will not ever call self.close(). This |
| 209 | # case occurs when will_close is TRUE, length is None, and we |
| 210 | # read up to the last byte, but NOT past it. |
| 211 | # |
| 212 | # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be |
| 213 | # called, meaning self.isclosed() is meaningful. |
| 214 | return self.fp is None |
| 215 | |
| 216 | def read(self, amt=None): |
| 217 | if self.fp is None: |
| 218 | return '' |
| 219 | |
| 220 | if self.chunked: |
| 221 | chunk_left = self.chunk_left |
| 222 | value = '' |
| 223 | while 1: |
| 224 | if chunk_left is None: |
| 225 | line = self.fp.readline() |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 226 | i = line.find(';') |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 227 | if i >= 0: |
| 228 | line = line[:i] # strip chunk-extensions |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 229 | chunk_left = int(line, 16) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 230 | if chunk_left == 0: |
| 231 | break |
| 232 | if amt is None: |
| 233 | value = value + self._safe_read(chunk_left) |
| 234 | elif amt < chunk_left: |
| 235 | value = value + self._safe_read(amt) |
| 236 | self.chunk_left = chunk_left - amt |
| 237 | return value |
| 238 | elif amt == chunk_left: |
| 239 | value = value + self._safe_read(amt) |
| 240 | self._safe_read(2) # toss the CRLF at the end of the chunk |
| 241 | self.chunk_left = None |
| 242 | return value |
| 243 | else: |
| 244 | value = value + self._safe_read(chunk_left) |
| 245 | amt = amt - chunk_left |
| 246 | |
| 247 | # we read the whole chunk, get another |
| 248 | self._safe_read(2) # toss the CRLF at the end of the chunk |
| 249 | chunk_left = None |
| 250 | |
| 251 | # read and discard trailer up to the CRLF terminator |
| 252 | ### note: we shouldn't have any trailers! |
| 253 | while 1: |
| 254 | line = self.fp.readline() |
| 255 | if line == '\r\n': |
| 256 | break |
| 257 | |
| 258 | # we read everything; close the "file" |
| 259 | self.close() |
| 260 | |
| 261 | return value |
| 262 | |
| 263 | elif amt is None: |
| 264 | # unbounded read |
| 265 | if self.will_close: |
| 266 | s = self.fp.read() |
| 267 | else: |
| 268 | s = self._safe_read(self.length) |
| 269 | self.close() # we read everything |
| 270 | return s |
| 271 | |
| 272 | if self.length is not None: |
| 273 | if amt > self.length: |
| 274 | # clip the read to the "end of response" |
| 275 | amt = self.length |
| 276 | self.length = self.length - amt |
| 277 | |
| 278 | # we do not use _safe_read() here because this may be a .will_close |
| 279 | # connection, and the user is reading more bytes than will be provided |
| 280 | # (for example, reading in 1k chunks) |
| 281 | s = self.fp.read(amt) |
| 282 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 283 | return s |
| 284 | |
| 285 | def _safe_read(self, amt): |
| 286 | """Read the number of bytes requested, compensating for partial reads. |
| 287 | |
| 288 | Normally, we have a blocking socket, but a read() can be interrupted |
| 289 | by a signal (resulting in a partial read). |
| 290 | |
| 291 | Note that we cannot distinguish between EOF and an interrupt when zero |
| 292 | bytes have been read. IncompleteRead() will be raised in this |
| 293 | situation. |
| 294 | |
| 295 | This function should be used when <amt> bytes "should" be present for |
| 296 | reading. If the bytes are truly not available (due to EOF), then the |
| 297 | IncompleteRead exception can be used to detect the problem. |
| 298 | """ |
| 299 | s = '' |
| 300 | while amt > 0: |
| 301 | chunk = self.fp.read(amt) |
| 302 | if not chunk: |
| 303 | raise IncompleteRead(s) |
| 304 | s = s + chunk |
| 305 | amt = amt - len(chunk) |
| 306 | return s |
| 307 | |
| 308 | def getheader(self, name, default=None): |
| 309 | if self.msg is None: |
| 310 | raise ResponseNotReady() |
| 311 | return self.msg.getheader(name, default) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 312 | |
| 313 | |
| 314 | class HTTPConnection: |
| 315 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 316 | _http_vsn = 11 |
| 317 | _http_vsn_str = 'HTTP/1.1' |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 318 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 319 | response_class = HTTPResponse |
| 320 | default_port = HTTP_PORT |
| 321 | auto_open = 1 |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 322 | debuglevel = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 323 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 324 | def __init__(self, host, port=None): |
| 325 | self.sock = None |
| 326 | self.__response = None |
| 327 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 328 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 329 | self._set_hostport(host, port) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 330 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 331 | def _set_hostport(self, host, port): |
| 332 | if port is None: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 333 | i = host.find(':') |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 334 | if i >= 0: |
| 335 | port = int(host[i+1:]) |
| 336 | host = host[:i] |
| 337 | else: |
| 338 | port = self.default_port |
| 339 | self.host = host |
| 340 | self.port = port |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 341 | |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 342 | def set_debuglevel(self, level): |
| 343 | self.debuglevel = level |
| 344 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 345 | def connect(self): |
| 346 | """Connect to the host and port specified in __init__.""" |
| 347 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 348 | if self.debuglevel > 0: |
| 349 | print "connect: (%s, %s)" % (self.host, self.port) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 350 | self.sock.connect((self.host, self.port)) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 351 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 352 | def close(self): |
| 353 | """Close the connection to the HTTP server.""" |
| 354 | if self.sock: |
| 355 | self.sock.close() # close it manually... there may be other refs |
| 356 | self.sock = None |
| 357 | if self.__response: |
| 358 | self.__response.close() |
| 359 | self.__response = None |
| 360 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 361 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 362 | def send(self, str): |
| 363 | """Send `str' to the server.""" |
| 364 | if self.sock is None: |
| 365 | if self.auto_open: |
| 366 | self.connect() |
| 367 | else: |
| 368 | raise NotConnected() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 369 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 370 | # send the data to the server. if we get a broken pipe, then close |
| 371 | # the socket. we want to reconnect when somebody tries to send again. |
| 372 | # |
| 373 | # NOTE: we DO propagate the error, though, because we cannot simply |
| 374 | # ignore the error... the caller will know if they can retry. |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 375 | if self.debuglevel > 0: |
| 376 | print "send:", repr(str) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 377 | try: |
| 378 | self.sock.send(str) |
| 379 | except socket.error, v: |
| 380 | if v[0] == 32: # Broken pipe |
| 381 | self.close() |
| 382 | raise |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 383 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 384 | def putrequest(self, method, url): |
| 385 | """Send a request to the server. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 386 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 387 | `method' specifies an HTTP request method, e.g. 'GET'. |
| 388 | `url' specifies the object being requested, e.g. '/index.html'. |
| 389 | """ |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 390 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 391 | # check if a prior response has been completed |
| 392 | if self.__response and self.__response.isclosed(): |
| 393 | self.__response = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 394 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 395 | # |
| 396 | # in certain cases, we cannot issue another request on this connection. |
| 397 | # this occurs when: |
| 398 | # 1) we are in the process of sending a request. (_CS_REQ_STARTED) |
| 399 | # 2) a response to a previous request has signalled that it is going |
| 400 | # to close the connection upon completion. |
| 401 | # 3) the headers for the previous response have not been read, thus |
| 402 | # we cannot determine whether point (2) is true. (_CS_REQ_SENT) |
| 403 | # |
| 404 | # if there is no prior response, then we can request at will. |
| 405 | # |
| 406 | # if point (2) is true, then we will have passed the socket to the |
| 407 | # response (effectively meaning, "there is no prior response"), and |
| 408 | # will open a new one when a new request is made. |
| 409 | # |
| 410 | # Note: if a prior response exists, then we *can* start a new request. |
| 411 | # We are not allowed to begin fetching the response to this new |
| 412 | # request, however, until that prior response is complete. |
| 413 | # |
| 414 | if self.__state == _CS_IDLE: |
| 415 | self.__state = _CS_REQ_STARTED |
| 416 | else: |
| 417 | raise CannotSendRequest() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 418 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 419 | if not url: |
| 420 | url = '/' |
| 421 | str = '%s %s %s\r\n' % (method, url, self._http_vsn_str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 422 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 423 | try: |
| 424 | self.send(str) |
| 425 | except socket.error, v: |
| 426 | # trap 'Broken pipe' if we're allowed to automatically reconnect |
| 427 | if v[0] != 32 or not self.auto_open: |
| 428 | raise |
| 429 | # try one more time (the socket was closed; this will reopen) |
| 430 | self.send(str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 431 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 432 | if self._http_vsn == 11: |
| 433 | # Issue some standard headers for better HTTP/1.1 compliance |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 434 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 435 | # this header is issued *only* for HTTP/1.1 connections. more |
| 436 | # specifically, this means it is only issued when the client uses |
| 437 | # the new HTTPConnection() class. backwards-compat clients will |
| 438 | # be using HTTP/1.0 and those clients may be issuing this header |
| 439 | # themselves. we should NOT issue it twice; some web servers (such |
| 440 | # as Apache) barf when they see two Host: headers |
| 441 | self.putheader('Host', self.host) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 442 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 443 | # note: we are assuming that clients will not attempt to set these |
| 444 | # headers since *this* library must deal with the |
| 445 | # consequences. this also means that when the supporting |
| 446 | # libraries are updated to recognize other forms, then this |
| 447 | # code should be changed (removed or updated). |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 448 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 449 | # we only want a Content-Encoding of "identity" since we don't |
| 450 | # support encodings such as x-gzip or x-deflate. |
| 451 | self.putheader('Accept-Encoding', 'identity') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 452 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 453 | # we can accept "chunked" Transfer-Encodings, but no others |
| 454 | # NOTE: no TE header implies *only* "chunked" |
| 455 | #self.putheader('TE', 'chunked') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 456 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 457 | # if TE is supplied in the header, then it must appear in a |
| 458 | # Connection header. |
| 459 | #self.putheader('Connection', 'TE') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 460 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 461 | else: |
| 462 | # For HTTP/1.0, the server will assume "not chunked" |
| 463 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 464 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 465 | def putheader(self, header, value): |
| 466 | """Send a request header line to the server. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 467 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 468 | For example: h.putheader('Accept', 'text/html') |
| 469 | """ |
| 470 | if self.__state != _CS_REQ_STARTED: |
| 471 | raise CannotSendHeader() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 472 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 473 | str = '%s: %s\r\n' % (header, value) |
| 474 | self.send(str) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 475 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 476 | def endheaders(self): |
| 477 | """Indicate that the last header line has been sent to the server.""" |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 478 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 479 | if self.__state == _CS_REQ_STARTED: |
| 480 | self.__state = _CS_REQ_SENT |
| 481 | else: |
| 482 | raise CannotSendHeader() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 483 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 484 | self.send('\r\n') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 485 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 486 | def request(self, method, url, body=None, headers={}): |
| 487 | """Send a complete request to the server.""" |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 488 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 489 | try: |
| 490 | self._send_request(method, url, body, headers) |
| 491 | except socket.error, v: |
| 492 | # trap 'Broken pipe' if we're allowed to automatically reconnect |
| 493 | if v[0] != 32 or not self.auto_open: |
| 494 | raise |
| 495 | # try one more time |
| 496 | self._send_request(method, url, body, headers) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 497 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 498 | def _send_request(self, method, url, body, headers): |
| 499 | self.putrequest(method, url) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 500 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 501 | if body: |
| 502 | self.putheader('Content-Length', str(len(body))) |
| 503 | for hdr, value in headers.items(): |
| 504 | self.putheader(hdr, value) |
| 505 | self.endheaders() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 506 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 507 | if body: |
| 508 | self.send(body) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 509 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 510 | def getresponse(self): |
| 511 | "Get the response from the server." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 512 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 513 | # check if a prior response has been completed |
| 514 | if self.__response and self.__response.isclosed(): |
| 515 | self.__response = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 516 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 517 | # |
| 518 | # if a prior response exists, then it must be completed (otherwise, we |
| 519 | # cannot read this response's header to determine the connection-close |
| 520 | # behavior) |
| 521 | # |
| 522 | # note: if a prior response existed, but was connection-close, then the |
| 523 | # socket and response were made independent of this HTTPConnection |
| 524 | # object since a new request requires that we open a whole new |
| 525 | # connection |
| 526 | # |
| 527 | # this means the prior response had one of two states: |
| 528 | # 1) will_close: this connection was reset and the prior socket and |
| 529 | # response operate independently |
| 530 | # 2) persistent: the response was retained and we await its |
| 531 | # isclosed() status to become true. |
| 532 | # |
| 533 | if self.__state != _CS_REQ_SENT or self.__response: |
| 534 | raise ResponseNotReady() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 535 | |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 536 | if self.debuglevel > 0: |
| 537 | response = self.response_class(self.sock, self.debuglevel) |
| 538 | else: |
| 539 | response = self.response_class(self.sock) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 540 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 541 | response.begin() |
| 542 | self.__state = _CS_IDLE |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 543 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 544 | if response.will_close: |
| 545 | # this effectively passes the connection to the response |
| 546 | self.close() |
| 547 | else: |
| 548 | # remember this, so we can tell when it is complete |
| 549 | self.__response = response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 550 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 551 | return response |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 552 | |
| 553 | |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 554 | class FakeSocket: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 555 | def __init__(self, sock, ssl): |
| 556 | self.__sock = sock |
| 557 | self.__ssl = ssl |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 558 | |
Jeremy Hylton | 4d746fc | 2000-08-23 20:34:17 +0000 | [diff] [blame] | 559 | def makefile(self, mode, bufsize=None): |
| 560 | """Return a readable file-like object with data from socket. |
| 561 | |
| 562 | This method offers only partial support for the makefile |
| 563 | interface of a real socket. It only supports modes 'r' and |
| 564 | 'rb' and the bufsize argument is ignored. |
| 565 | |
| 566 | The returned object contains *all* of the file data |
| 567 | """ |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 568 | if mode != 'r' and mode != 'rb': |
| 569 | raise UnimplementedFileMode() |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 570 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 571 | msgbuf = "" |
| 572 | while 1: |
| 573 | try: |
| 574 | msgbuf = msgbuf + self.__ssl.read() |
| 575 | except socket.sslerror, msg: |
| 576 | break |
| 577 | return StringIO(msgbuf) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 578 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 579 | def send(self, stuff, flags = 0): |
| 580 | return self.__ssl.write(stuff) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 581 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 582 | def recv(self, len = 1024, flags = 0): |
| 583 | return self.__ssl.read(len) |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 584 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 585 | def __getattr__(self, attr): |
| 586 | return getattr(self.__sock, attr) |
Guido van Rossum | 09c8b6c | 1999-12-07 21:37:17 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 588 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 589 | class HTTPSConnection(HTTPConnection): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 590 | "This class allows communication via SSL." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 591 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 592 | default_port = HTTPS_PORT |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 593 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 594 | def __init__(self, host, port=None, **x509): |
| 595 | keys = x509.keys() |
| 596 | try: |
| 597 | keys.remove('key_file') |
| 598 | except ValueError: |
| 599 | pass |
| 600 | try: |
| 601 | keys.remove('cert_file') |
| 602 | except ValueError: |
| 603 | pass |
| 604 | if keys: |
| 605 | raise IllegalKeywordArgument() |
| 606 | HTTPConnection.__init__(self, host, port) |
| 607 | self.key_file = x509.get('key_file') |
| 608 | self.cert_file = x509.get('cert_file') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 609 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 610 | def connect(self): |
| 611 | "Connect to a host on a given (SSL) port." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 612 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 613 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 614 | sock.connect((self.host, self.port)) |
Guido van Rossum | 0aee722 | 2000-12-11 20:32:20 +0000 | [diff] [blame] | 615 | realsock = sock |
| 616 | if hasattr(sock, "_sock"): |
| 617 | realsock = sock._sock |
| 618 | ssl = socket.ssl(realsock, self.key_file, self.cert_file) |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 619 | self.sock = FakeSocket(sock, ssl) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 620 | |
| 621 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 622 | class HTTP: |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 623 | "Compatibility class with httplib.py from 1.5." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 624 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 625 | _http_vsn = 10 |
| 626 | _http_vsn_str = 'HTTP/1.0' |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 627 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 628 | debuglevel = 0 |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 629 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 630 | _connection_class = HTTPConnection |
| 631 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 632 | def __init__(self, host='', port=None, **x509): |
| 633 | "Provide a default host, since the superclass requires one." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 634 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 635 | # some joker passed 0 explicitly, meaning default port |
| 636 | if port == 0: |
| 637 | port = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 638 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 639 | # Note that we may pass an empty string as the host; this will throw |
| 640 | # an error when we attempt to connect. Presumably, the client code |
| 641 | # will call connect before then, with a proper host. |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 642 | self._conn = self._connection_class(host, port) |
| 643 | # set up delegation to flesh out interface |
| 644 | self.send = self._conn.send |
| 645 | self.putrequest = self._conn.putrequest |
| 646 | self.endheaders = self._conn.endheaders |
Jeremy Hylton | 4d746fc | 2000-08-23 20:34:17 +0000 | [diff] [blame] | 647 | self._conn._http_vsn = self._http_vsn |
| 648 | self._conn._http_vsn_str = self._http_vsn_str |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 649 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 650 | # we never actually use these for anything, but we keep them here for |
| 651 | # compatibility with post-1.5.2 CVS. |
| 652 | self.key_file = x509.get('key_file') |
| 653 | self.cert_file = x509.get('cert_file') |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 654 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 655 | self.file = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 656 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 657 | def connect(self, host=None, port=None): |
| 658 | "Accept arguments to set the host/port, since the superclass doesn't." |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 659 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 660 | if host is not None: |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 661 | self._conn._set_hostport(host, port) |
| 662 | self._conn.connect() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 663 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 664 | def set_debuglevel(self, debuglevel): |
Jeremy Hylton | 30f8674 | 2000-09-18 22:50:38 +0000 | [diff] [blame] | 665 | self._conn.set_debuglevel(debuglevel) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 666 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 667 | def getfile(self): |
| 668 | "Provide a getfile, since the superclass' does not use this concept." |
| 669 | return self.file |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 670 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 671 | def putheader(self, header, *values): |
| 672 | "The superclass allows only one value argument." |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 673 | self._conn.putheader(header, '\r\n\t'.join(values)) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 674 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 675 | def getreply(self): |
| 676 | """Compat definition since superclass does not define it. |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 677 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 678 | Returns a tuple consisting of: |
| 679 | - server status code (e.g. '200' if all goes well) |
| 680 | - server "reason" corresponding to status code |
| 681 | - any RFC822 headers in the response from the server |
| 682 | """ |
| 683 | try: |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 684 | response = self._conn.getresponse() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 685 | except BadStatusLine, e: |
| 686 | ### hmm. if getresponse() ever closes the socket on a bad request, |
| 687 | ### then we are going to have problems with self.sock |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 688 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 689 | ### should we keep this behavior? do people use it? |
| 690 | # keep the socket open (as a file), and return it |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 691 | self.file = self._conn.sock.makefile('rb', 0) |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 692 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 693 | # close our socket -- we want to restart after any protocol error |
| 694 | self.close() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 695 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 696 | self.headers = None |
| 697 | return -1, e.line, None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 698 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 699 | self.headers = response.msg |
| 700 | self.file = response.fp |
| 701 | return response.status, response.reason, response.msg |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 702 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 703 | def close(self): |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 704 | self._conn.close() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 705 | |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 706 | # note that self.file == response.fp, which gets closed by the |
| 707 | # superclass. just clear the object ref here. |
| 708 | ### hmm. messy. if status==-1, then self.file is owned by us. |
| 709 | ### well... we aren't explicitly closing, but losing this ref will |
| 710 | ### do it |
| 711 | self.file = None |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 712 | |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 713 | if hasattr(socket, 'ssl'): |
| 714 | class HTTPS(HTTP): |
| 715 | """Compatibility with 1.5 httplib interface |
| 716 | |
| 717 | Python 1.5.2 did not have an HTTPS class, but it defined an |
| 718 | interface for sending http requests that is also useful for |
| 719 | https. |
| 720 | """ |
| 721 | |
Martin v. Löwis | d7bf974 | 2000-09-21 22:09:47 +0000 | [diff] [blame] | 722 | _connection_class = HTTPSConnection |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 723 | |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 724 | |
| 725 | class HTTPException(Exception): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 726 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 727 | |
| 728 | class NotConnected(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 729 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 730 | |
| 731 | class UnknownProtocol(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 732 | def __init__(self, version): |
| 733 | self.version = version |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 734 | |
| 735 | class UnknownTransferEncoding(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 736 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 737 | |
| 738 | class IllegalKeywordArgument(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 739 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 740 | |
| 741 | class UnimplementedFileMode(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 742 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 743 | |
| 744 | class IncompleteRead(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 745 | def __init__(self, partial): |
| 746 | self.partial = partial |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 747 | |
| 748 | class ImproperConnectionState(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 749 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 750 | |
| 751 | class CannotSendRequest(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 752 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 753 | |
| 754 | class CannotSendHeader(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 755 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 756 | |
| 757 | class ResponseNotReady(ImproperConnectionState): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 758 | pass |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 759 | |
| 760 | class BadStatusLine(HTTPException): |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 761 | def __init__(self, line): |
| 762 | self.line = line |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 763 | |
| 764 | # for backwards compatibility |
| 765 | error = HTTPException |
| 766 | |
| 767 | |
| 768 | # |
| 769 | # snarfed from httplib.py for now... |
| 770 | # |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 771 | def test(): |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 772 | """Test this module. |
| 773 | |
| 774 | The test consists of retrieving and displaying the Python |
| 775 | home page, along with the error code and error string returned |
| 776 | by the www.python.org server. |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 777 | """ |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 778 | |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 779 | import sys |
| 780 | import getopt |
| 781 | opts, args = getopt.getopt(sys.argv[1:], 'd') |
| 782 | dl = 0 |
| 783 | for o, a in opts: |
| 784 | if o == '-d': dl = dl + 1 |
| 785 | host = 'www.python.org' |
| 786 | selector = '/' |
| 787 | if args[0:]: host = args[0] |
| 788 | if args[1:]: selector = args[1] |
| 789 | h = HTTP() |
| 790 | h.set_debuglevel(dl) |
| 791 | h.connect(host) |
| 792 | h.putrequest('GET', selector) |
| 793 | h.endheaders() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 794 | status, reason, headers = h.getreply() |
| 795 | print 'status =', status |
| 796 | print 'reason =', reason |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 797 | print |
| 798 | if headers: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 799 | for header in headers.headers: print header.strip() |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 800 | print |
| 801 | print h.getfile().read() |
Greg Stein | 5e0fa40 | 2000-06-26 08:28:01 +0000 | [diff] [blame] | 802 | |
| 803 | if hasattr(socket, 'ssl'): |
Jeremy Hylton | 29b8d5a | 2000-08-01 17:33:32 +0000 | [diff] [blame] | 804 | host = 'sourceforge.net' |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 805 | hs = HTTPS() |
| 806 | hs.connect(host) |
| 807 | hs.putrequest('GET', selector) |
| 808 | hs.endheaders() |
| 809 | status, reason, headers = hs.getreply() |
| 810 | print 'status =', status |
| 811 | print 'reason =', reason |
| 812 | print |
| 813 | if headers: |
Guido van Rossum | 34735a6 | 2000-12-15 15:09:42 +0000 | [diff] [blame^] | 814 | for header in headers.headers: print header.strip() |
Greg Stein | dd6eefb | 2000-07-18 09:09:48 +0000 | [diff] [blame] | 815 | print |
| 816 | print hs.getfile().read() |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 817 | |
Guido van Rossum | a0dfc7a | 1995-09-07 19:28:19 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | 23acc95 | 1994-02-21 16:36:04 +0000 | [diff] [blame] | 819 | if __name__ == '__main__': |
Guido van Rossum | 41999c1 | 1997-12-09 00:12:23 +0000 | [diff] [blame] | 820 | test() |