| 1 | n/a | """HTTP/1.1 client library |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | <intro stuff goes here> |
|---|
| 4 | n/a | <other stuff, too> |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | HTTPConnection goes through a number of "states", which define when a client |
|---|
| 7 | n/a | may legally make another request or fetch the response for a particular |
|---|
| 8 | n/a | request. This diagram details these state transitions: |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | (null) |
|---|
| 11 | n/a | | |
|---|
| 12 | n/a | | HTTPConnection() |
|---|
| 13 | n/a | v |
|---|
| 14 | n/a | Idle |
|---|
| 15 | n/a | | |
|---|
| 16 | n/a | | putrequest() |
|---|
| 17 | n/a | v |
|---|
| 18 | n/a | Request-started |
|---|
| 19 | n/a | | |
|---|
| 20 | n/a | | ( putheader() )* endheaders() |
|---|
| 21 | n/a | v |
|---|
| 22 | n/a | Request-sent |
|---|
| 23 | n/a | | |
|---|
| 24 | n/a | | response = getresponse() |
|---|
| 25 | n/a | v |
|---|
| 26 | n/a | Unread-response [Response-headers-read] |
|---|
| 27 | n/a | |\____________________ |
|---|
| 28 | n/a | | | |
|---|
| 29 | n/a | | response.read() | putrequest() |
|---|
| 30 | n/a | v v |
|---|
| 31 | n/a | Idle Req-started-unread-response |
|---|
| 32 | n/a | ______/| |
|---|
| 33 | n/a | / | |
|---|
| 34 | n/a | response.read() | | ( putheader() )* endheaders() |
|---|
| 35 | n/a | v v |
|---|
| 36 | n/a | Request-started Req-sent-unread-response |
|---|
| 37 | n/a | | |
|---|
| 38 | n/a | | response.read() |
|---|
| 39 | n/a | v |
|---|
| 40 | n/a | Request-sent |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | This diagram presents the following rules: |
|---|
| 43 | n/a | -- a second request may not be started until {response-headers-read} |
|---|
| 44 | n/a | -- a response [object] cannot be retrieved until {request-sent} |
|---|
| 45 | n/a | -- there is no differentiation between an unread response body and a |
|---|
| 46 | n/a | partially read response body |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | Note: this enforcement is applied by the HTTPConnection class. The |
|---|
| 49 | n/a | HTTPResponse class does not enforce this state machine, which |
|---|
| 50 | n/a | implies sophisticated clients may accelerate the request/response |
|---|
| 51 | n/a | pipeline. Caution should be taken, though: accelerating the states |
|---|
| 52 | n/a | beyond the above pattern may imply knowledge of the server's |
|---|
| 53 | n/a | connection-close behavior for certain requests. For example, it |
|---|
| 54 | n/a | is impossible to tell whether the server will close the connection |
|---|
| 55 | n/a | UNTIL the response headers have been read; this means that further |
|---|
| 56 | n/a | requests cannot be placed into the pipeline until it is known that |
|---|
| 57 | n/a | the server will NOT be closing the connection. |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | Logical State __state __response |
|---|
| 60 | n/a | ------------- ------- ---------- |
|---|
| 61 | n/a | Idle _CS_IDLE None |
|---|
| 62 | n/a | Request-started _CS_REQ_STARTED None |
|---|
| 63 | n/a | Request-sent _CS_REQ_SENT None |
|---|
| 64 | n/a | Unread-response _CS_IDLE <response_class> |
|---|
| 65 | n/a | Req-started-unread-response _CS_REQ_STARTED <response_class> |
|---|
| 66 | n/a | Req-sent-unread-response _CS_REQ_SENT <response_class> |
|---|
| 67 | 1 | """ |
|---|
| 68 | n/a | |
|---|
| 69 | 1 | from array import array |
|---|
| 70 | 1 | import socket |
|---|
| 71 | 1 | from sys import py3kwarning |
|---|
| 72 | 1 | from urlparse import urlsplit |
|---|
| 73 | 1 | import warnings |
|---|
| 74 | 1 | with warnings.catch_warnings(): |
|---|
| 75 | 1 | if py3kwarning: |
|---|
| 76 | 0 | warnings.filterwarnings("ignore", ".*mimetools has been removed", |
|---|
| 77 | 0 | DeprecationWarning) |
|---|
| 78 | 1 | import mimetools |
|---|
| 79 | n/a | |
|---|
| 80 | 1 | try: |
|---|
| 81 | 1 | from cStringIO import StringIO |
|---|
| 82 | 0 | except ImportError: |
|---|
| 83 | 0 | from StringIO import StringIO |
|---|
| 84 | n/a | |
|---|
| 85 | 1 | __all__ = ["HTTP", "HTTPResponse", "HTTPConnection", |
|---|
| 86 | 1 | "HTTPException", "NotConnected", "UnknownProtocol", |
|---|
| 87 | 1 | "UnknownTransferEncoding", "UnimplementedFileMode", |
|---|
| 88 | 1 | "IncompleteRead", "InvalidURL", "ImproperConnectionState", |
|---|
| 89 | 1 | "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", |
|---|
| 90 | 1 | "BadStatusLine", "error", "responses"] |
|---|
| 91 | n/a | |
|---|
| 92 | 1 | HTTP_PORT = 80 |
|---|
| 93 | 1 | HTTPS_PORT = 443 |
|---|
| 94 | n/a | |
|---|
| 95 | 1 | _UNKNOWN = 'UNKNOWN' |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | # connection states |
|---|
| 98 | 1 | _CS_IDLE = 'Idle' |
|---|
| 99 | 1 | _CS_REQ_STARTED = 'Request-started' |
|---|
| 100 | 1 | _CS_REQ_SENT = 'Request-sent' |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | # status codes |
|---|
| 103 | n/a | # informational |
|---|
| 104 | 1 | CONTINUE = 100 |
|---|
| 105 | 1 | SWITCHING_PROTOCOLS = 101 |
|---|
| 106 | 1 | PROCESSING = 102 |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | # successful |
|---|
| 109 | 1 | OK = 200 |
|---|
| 110 | 1 | CREATED = 201 |
|---|
| 111 | 1 | ACCEPTED = 202 |
|---|
| 112 | 1 | NON_AUTHORITATIVE_INFORMATION = 203 |
|---|
| 113 | 1 | NO_CONTENT = 204 |
|---|
| 114 | 1 | RESET_CONTENT = 205 |
|---|
| 115 | 1 | PARTIAL_CONTENT = 206 |
|---|
| 116 | 1 | MULTI_STATUS = 207 |
|---|
| 117 | 1 | IM_USED = 226 |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | # redirection |
|---|
| 120 | 1 | MULTIPLE_CHOICES = 300 |
|---|
| 121 | 1 | MOVED_PERMANENTLY = 301 |
|---|
| 122 | 1 | FOUND = 302 |
|---|
| 123 | 1 | SEE_OTHER = 303 |
|---|
| 124 | 1 | NOT_MODIFIED = 304 |
|---|
| 125 | 1 | USE_PROXY = 305 |
|---|
| 126 | 1 | TEMPORARY_REDIRECT = 307 |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | # client error |
|---|
| 129 | 1 | BAD_REQUEST = 400 |
|---|
| 130 | 1 | UNAUTHORIZED = 401 |
|---|
| 131 | 1 | PAYMENT_REQUIRED = 402 |
|---|
| 132 | 1 | FORBIDDEN = 403 |
|---|
| 133 | 1 | NOT_FOUND = 404 |
|---|
| 134 | 1 | METHOD_NOT_ALLOWED = 405 |
|---|
| 135 | 1 | NOT_ACCEPTABLE = 406 |
|---|
| 136 | 1 | PROXY_AUTHENTICATION_REQUIRED = 407 |
|---|
| 137 | 1 | REQUEST_TIMEOUT = 408 |
|---|
| 138 | 1 | CONFLICT = 409 |
|---|
| 139 | 1 | GONE = 410 |
|---|
| 140 | 1 | LENGTH_REQUIRED = 411 |
|---|
| 141 | 1 | PRECONDITION_FAILED = 412 |
|---|
| 142 | 1 | REQUEST_ENTITY_TOO_LARGE = 413 |
|---|
| 143 | 1 | REQUEST_URI_TOO_LONG = 414 |
|---|
| 144 | 1 | UNSUPPORTED_MEDIA_TYPE = 415 |
|---|
| 145 | 1 | REQUESTED_RANGE_NOT_SATISFIABLE = 416 |
|---|
| 146 | 1 | EXPECTATION_FAILED = 417 |
|---|
| 147 | 1 | UNPROCESSABLE_ENTITY = 422 |
|---|
| 148 | 1 | LOCKED = 423 |
|---|
| 149 | 1 | FAILED_DEPENDENCY = 424 |
|---|
| 150 | 1 | UPGRADE_REQUIRED = 426 |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | # server error |
|---|
| 153 | 1 | INTERNAL_SERVER_ERROR = 500 |
|---|
| 154 | 1 | NOT_IMPLEMENTED = 501 |
|---|
| 155 | 1 | BAD_GATEWAY = 502 |
|---|
| 156 | 1 | SERVICE_UNAVAILABLE = 503 |
|---|
| 157 | 1 | GATEWAY_TIMEOUT = 504 |
|---|
| 158 | 1 | HTTP_VERSION_NOT_SUPPORTED = 505 |
|---|
| 159 | 1 | INSUFFICIENT_STORAGE = 507 |
|---|
| 160 | 1 | NOT_EXTENDED = 510 |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | # Mapping status codes to official W3C names |
|---|
| 163 | 1 | responses = { |
|---|
| 164 | 1 | 100: 'Continue', |
|---|
| 165 | 1 | 101: 'Switching Protocols', |
|---|
| 166 | n/a | |
|---|
| 167 | 1 | 200: 'OK', |
|---|
| 168 | 1 | 201: 'Created', |
|---|
| 169 | 1 | 202: 'Accepted', |
|---|
| 170 | 1 | 203: 'Non-Authoritative Information', |
|---|
| 171 | 1 | 204: 'No Content', |
|---|
| 172 | 1 | 205: 'Reset Content', |
|---|
| 173 | 1 | 206: 'Partial Content', |
|---|
| 174 | n/a | |
|---|
| 175 | 1 | 300: 'Multiple Choices', |
|---|
| 176 | 1 | 301: 'Moved Permanently', |
|---|
| 177 | 1 | 302: 'Found', |
|---|
| 178 | 1 | 303: 'See Other', |
|---|
| 179 | 1 | 304: 'Not Modified', |
|---|
| 180 | 1 | 305: 'Use Proxy', |
|---|
| 181 | 1 | 306: '(Unused)', |
|---|
| 182 | 1 | 307: 'Temporary Redirect', |
|---|
| 183 | n/a | |
|---|
| 184 | 1 | 400: 'Bad Request', |
|---|
| 185 | 1 | 401: 'Unauthorized', |
|---|
| 186 | 1 | 402: 'Payment Required', |
|---|
| 187 | 1 | 403: 'Forbidden', |
|---|
| 188 | 1 | 404: 'Not Found', |
|---|
| 189 | 1 | 405: 'Method Not Allowed', |
|---|
| 190 | 1 | 406: 'Not Acceptable', |
|---|
| 191 | 1 | 407: 'Proxy Authentication Required', |
|---|
| 192 | 1 | 408: 'Request Timeout', |
|---|
| 193 | 1 | 409: 'Conflict', |
|---|
| 194 | 1 | 410: 'Gone', |
|---|
| 195 | 1 | 411: 'Length Required', |
|---|
| 196 | 1 | 412: 'Precondition Failed', |
|---|
| 197 | 1 | 413: 'Request Entity Too Large', |
|---|
| 198 | 1 | 414: 'Request-URI Too Long', |
|---|
| 199 | 1 | 415: 'Unsupported Media Type', |
|---|
| 200 | 1 | 416: 'Requested Range Not Satisfiable', |
|---|
| 201 | 1 | 417: 'Expectation Failed', |
|---|
| 202 | n/a | |
|---|
| 203 | 1 | 500: 'Internal Server Error', |
|---|
| 204 | 1 | 501: 'Not Implemented', |
|---|
| 205 | 1 | 502: 'Bad Gateway', |
|---|
| 206 | 1 | 503: 'Service Unavailable', |
|---|
| 207 | 1 | 504: 'Gateway Timeout', |
|---|
| 208 | 1 | 505: 'HTTP Version Not Supported', |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | # maximal amount of data to read at one time in _safe_read |
|---|
| 212 | 1 | MAXAMOUNT = 1048576 |
|---|
| 213 | n/a | |
|---|
| 214 | 2 | class HTTPMessage(mimetools.Message): |
|---|
| 215 | n/a | |
|---|
| 216 | 1 | def addheader(self, key, value): |
|---|
| 217 | n/a | """Add header for field key handling repeats.""" |
|---|
| 218 | 624 | prev = self.dict.get(key) |
|---|
| 219 | 624 | if prev is None: |
|---|
| 220 | 623 | self.dict[key] = value |
|---|
| 221 | n/a | else: |
|---|
| 222 | 1 | combined = ", ".join((prev, value)) |
|---|
| 223 | 1 | self.dict[key] = combined |
|---|
| 224 | n/a | |
|---|
| 225 | 1 | def addcontinue(self, key, more): |
|---|
| 226 | n/a | """Add more field data from a continuation line.""" |
|---|
| 227 | 2 | prev = self.dict[key] |
|---|
| 228 | 2 | self.dict[key] = prev + "\n " + more |
|---|
| 229 | n/a | |
|---|
| 230 | 1 | def readheaders(self): |
|---|
| 231 | n/a | """Read header lines. |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | Read header lines up to the entirely blank line that terminates them. |
|---|
| 234 | n/a | The (normally blank) line that ends the headers is skipped, but not |
|---|
| 235 | n/a | included in the returned list. If a non-header line ends the headers, |
|---|
| 236 | n/a | (which is an error), an attempt is made to backspace over it; it is |
|---|
| 237 | n/a | never included in the returned list. |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | The variable self.status is set to the empty string if all went well, |
|---|
| 240 | n/a | otherwise it is an error message. The variable self.headers is a |
|---|
| 241 | n/a | completely uninterpreted list of lines contained in the header (so |
|---|
| 242 | n/a | printing them will reproduce the header exactly as it appears in the |
|---|
| 243 | n/a | file). |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | If multiple header fields with the same name occur, they are combined |
|---|
| 246 | n/a | according to the rules in RFC 2616 sec 4.2: |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | Appending each subsequent field-value to the first, each separated |
|---|
| 249 | n/a | by a comma. The order in which header fields with the same field-name |
|---|
| 250 | n/a | are received is significant to the interpretation of the combined |
|---|
| 251 | n/a | field value. |
|---|
| 252 | n/a | """ |
|---|
| 253 | n/a | # XXX The implementation overrides the readheaders() method of |
|---|
| 254 | n/a | # rfc822.Message. The base class design isn't amenable to |
|---|
| 255 | n/a | # customized behavior here so the method here is a copy of the |
|---|
| 256 | n/a | # base class code with a few small changes. |
|---|
| 257 | n/a | |
|---|
| 258 | 134 | self.dict = {} |
|---|
| 259 | 134 | self.unixfrom = '' |
|---|
| 260 | 134 | self.headers = hlist = [] |
|---|
| 261 | 134 | self.status = '' |
|---|
| 262 | 134 | headerseen = "" |
|---|
| 263 | 134 | firstline = 1 |
|---|
| 264 | 134 | startofline = unread = tell = None |
|---|
| 265 | 134 | if hasattr(self.fp, 'unread'): |
|---|
| 266 | 0 | unread = self.fp.unread |
|---|
| 267 | 134 | elif self.seekable: |
|---|
| 268 | 1 | tell = self.fp.tell |
|---|
| 269 | 760 | while True: |
|---|
| 270 | 760 | if tell: |
|---|
| 271 | 1 | try: |
|---|
| 272 | 1 | startofline = tell() |
|---|
| 273 | 0 | except IOError: |
|---|
| 274 | 0 | startofline = tell = None |
|---|
| 275 | 0 | self.seekable = 0 |
|---|
| 276 | 760 | line = self.fp.readline() |
|---|
| 277 | 760 | if not line: |
|---|
| 278 | 2 | self.status = 'EOF in headers' |
|---|
| 279 | 2 | break |
|---|
| 280 | n/a | # Skip unix From name time lines |
|---|
| 281 | 758 | if firstline and line.startswith('From '): |
|---|
| 282 | 0 | self.unixfrom = self.unixfrom + line |
|---|
| 283 | 0 | continue |
|---|
| 284 | 758 | firstline = 0 |
|---|
| 285 | 758 | if headerseen and line[0] in ' \t': |
|---|
| 286 | n/a | # XXX Not sure if continuation lines are handled properly |
|---|
| 287 | n/a | # for http and/or for repeating headers |
|---|
| 288 | n/a | # It's a continuation line. |
|---|
| 289 | 2 | hlist.append(line) |
|---|
| 290 | 2 | self.addcontinue(headerseen, line.strip()) |
|---|
| 291 | 2 | continue |
|---|
| 292 | 756 | elif self.iscomment(line): |
|---|
| 293 | n/a | # It's a comment. Ignore it. |
|---|
| 294 | 0 | continue |
|---|
| 295 | 756 | elif self.islast(line): |
|---|
| 296 | n/a | # Note! No pushback here! The delimiter line gets eaten. |
|---|
| 297 | 132 | break |
|---|
| 298 | 624 | headerseen = self.isheader(line) |
|---|
| 299 | 624 | if headerseen: |
|---|
| 300 | n/a | # It's a legal header line, save it. |
|---|
| 301 | 624 | hlist.append(line) |
|---|
| 302 | 624 | self.addheader(headerseen, line[len(headerseen)+1:].strip()) |
|---|
| 303 | 624 | continue |
|---|
| 304 | n/a | else: |
|---|
| 305 | n/a | # It's not a header line; throw it back and stop here. |
|---|
| 306 | 0 | if not self.dict: |
|---|
| 307 | 0 | self.status = 'No headers' |
|---|
| 308 | n/a | else: |
|---|
| 309 | 0 | self.status = 'Non-header line where header expected' |
|---|
| 310 | n/a | # Try to undo the read. |
|---|
| 311 | 0 | if unread: |
|---|
| 312 | 0 | unread(line) |
|---|
| 313 | 0 | elif tell: |
|---|
| 314 | 0 | self.fp.seek(startofline) |
|---|
| 315 | n/a | else: |
|---|
| 316 | 0 | self.status = self.status + '; bad seek' |
|---|
| 317 | 0 | break |
|---|
| 318 | n/a | |
|---|
| 319 | 2 | class HTTPResponse: |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | # strict: If true, raise BadStatusLine if the status line can't be |
|---|
| 322 | n/a | # parsed as a valid HTTP/1.0 or 1.1 status line. By default it is |
|---|
| 323 | n/a | # false because it prevents clients from talking to HTTP/0.9 |
|---|
| 324 | n/a | # servers. Note that a response with a sufficiently corrupted |
|---|
| 325 | n/a | # status line will look like an HTTP/0.9 response. |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details. |
|---|
| 328 | n/a | |
|---|
| 329 | 1 | def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False): |
|---|
| 330 | 140 | if buffering: |
|---|
| 331 | n/a | # The caller won't be using any sock.recv() calls, so buffering |
|---|
| 332 | n/a | # is fine and recommended for performance. |
|---|
| 333 | 76 | self.fp = sock.makefile('rb') |
|---|
| 334 | n/a | else: |
|---|
| 335 | n/a | # The buffer size is specified as zero, because the headers of |
|---|
| 336 | n/a | # the response are read with readline(). If the reads were |
|---|
| 337 | n/a | # buffered the readline() calls could consume some of the |
|---|
| 338 | n/a | # response, which make be read via a recv() on the underlying |
|---|
| 339 | n/a | # socket. |
|---|
| 340 | 64 | self.fp = sock.makefile('rb', 0) |
|---|
| 341 | 136 | self.debuglevel = debuglevel |
|---|
| 342 | 136 | self.strict = strict |
|---|
| 343 | 136 | self._method = method |
|---|
| 344 | n/a | |
|---|
| 345 | 136 | self.msg = None |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | # from the Status-Line of the response |
|---|
| 348 | 136 | self.version = _UNKNOWN # HTTP-Version |
|---|
| 349 | 136 | self.status = _UNKNOWN # Status-Code |
|---|
| 350 | 136 | self.reason = _UNKNOWN # Reason-Phrase |
|---|
| 351 | n/a | |
|---|
| 352 | 136 | self.chunked = _UNKNOWN # is "chunked" being used? |
|---|
| 353 | 136 | self.chunk_left = _UNKNOWN # bytes left to read in current chunk |
|---|
| 354 | 136 | self.length = _UNKNOWN # number of bytes left in response |
|---|
| 355 | 136 | self.will_close = _UNKNOWN # conn will close at end of response |
|---|
| 356 | n/a | |
|---|
| 357 | 1 | def _read_status(self): |
|---|
| 358 | n/a | # Initialize with Simple-Response defaults |
|---|
| 359 | 136 | line = self.fp.readline() |
|---|
| 360 | 136 | if self.debuglevel > 0: |
|---|
| 361 | 0 | print "reply:", repr(line) |
|---|
| 362 | 136 | if not line: |
|---|
| 363 | n/a | # Presumably, the server closed the connection before |
|---|
| 364 | n/a | # sending a valid response. |
|---|
| 365 | 1 | raise BadStatusLine(line) |
|---|
| 366 | 135 | try: |
|---|
| 367 | 135 | [version, status, reason] = line.split(None, 2) |
|---|
| 368 | 2 | except ValueError: |
|---|
| 369 | 2 | try: |
|---|
| 370 | 2 | [version, status] = line.split(None, 1) |
|---|
| 371 | 1 | reason = "" |
|---|
| 372 | 1 | except ValueError: |
|---|
| 373 | n/a | # empty version will cause next test to fail and status |
|---|
| 374 | n/a | # will be treated as 0.9 response. |
|---|
| 375 | 1 | version = "" |
|---|
| 376 | 135 | if not version.startswith('HTTP/'): |
|---|
| 377 | 1 | if self.strict: |
|---|
| 378 | 0 | self.close() |
|---|
| 379 | 0 | raise BadStatusLine(line) |
|---|
| 380 | n/a | else: |
|---|
| 381 | n/a | # assume it's a Simple-Response from an 0.9 server |
|---|
| 382 | 1 | self.fp = LineAndFileWrapper(line, self.fp) |
|---|
| 383 | 1 | return "HTTP/0.9", 200, "" |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | # The status code is a three-digit number |
|---|
| 386 | 134 | try: |
|---|
| 387 | 134 | status = int(status) |
|---|
| 388 | 133 | if status < 100 or status > 999: |
|---|
| 389 | 0 | raise BadStatusLine(line) |
|---|
| 390 | 1 | except ValueError: |
|---|
| 391 | 1 | raise BadStatusLine(line) |
|---|
| 392 | 133 | return version, status, reason |
|---|
| 393 | n/a | |
|---|
| 394 | 1 | def begin(self): |
|---|
| 395 | 136 | if self.msg is not None: |
|---|
| 396 | n/a | # we've already started reading the response |
|---|
| 397 | 0 | return |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | # read until we get a non-100 response |
|---|
| 400 | 136 | while True: |
|---|
| 401 | 136 | version, status, reason = self._read_status() |
|---|
| 402 | 134 | if status != CONTINUE: |
|---|
| 403 | 134 | break |
|---|
| 404 | n/a | # skip the header from the 100 response |
|---|
| 405 | 0 | while True: |
|---|
| 406 | 0 | skip = self.fp.readline().strip() |
|---|
| 407 | 0 | if not skip: |
|---|
| 408 | 0 | break |
|---|
| 409 | 0 | if self.debuglevel > 0: |
|---|
| 410 | 0 | print "header:", skip |
|---|
| 411 | n/a | |
|---|
| 412 | 134 | self.status = status |
|---|
| 413 | 134 | self.reason = reason.strip() |
|---|
| 414 | 134 | if version == 'HTTP/1.0': |
|---|
| 415 | 59 | self.version = 10 |
|---|
| 416 | 75 | elif version.startswith('HTTP/1.'): |
|---|
| 417 | 74 | self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 |
|---|
| 418 | 1 | elif version == 'HTTP/0.9': |
|---|
| 419 | 1 | self.version = 9 |
|---|
| 420 | n/a | else: |
|---|
| 421 | 0 | raise UnknownProtocol(version) |
|---|
| 422 | n/a | |
|---|
| 423 | 134 | if self.version == 9: |
|---|
| 424 | 1 | self.length = None |
|---|
| 425 | 1 | self.chunked = 0 |
|---|
| 426 | 1 | self.will_close = 1 |
|---|
| 427 | 1 | self.msg = HTTPMessage(StringIO()) |
|---|
| 428 | 1 | return |
|---|
| 429 | n/a | |
|---|
| 430 | 133 | self.msg = HTTPMessage(self.fp, 0) |
|---|
| 431 | 133 | if self.debuglevel > 0: |
|---|
| 432 | 0 | for hdr in self.msg.headers: |
|---|
| 433 | 0 | print "header:", hdr, |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | # don't let the msg keep an fp |
|---|
| 436 | 133 | self.msg.fp = None |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | # are we using the chunked-style of transfer encoding? |
|---|
| 439 | 133 | tr_enc = self.msg.getheader('transfer-encoding') |
|---|
| 440 | 133 | if tr_enc and tr_enc.lower() == "chunked": |
|---|
| 441 | 4 | self.chunked = 1 |
|---|
| 442 | 4 | self.chunk_left = None |
|---|
| 443 | n/a | else: |
|---|
| 444 | 129 | self.chunked = 0 |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | # will the connection close at the end of the response? |
|---|
| 447 | 133 | self.will_close = self._check_close() |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | # do we have a Content-Length? |
|---|
| 450 | n/a | # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" |
|---|
| 451 | 133 | length = self.msg.getheader('content-length') |
|---|
| 452 | 133 | if length and not self.chunked: |
|---|
| 453 | 79 | try: |
|---|
| 454 | 79 | self.length = int(length) |
|---|
| 455 | 0 | except ValueError: |
|---|
| 456 | 0 | self.length = None |
|---|
| 457 | n/a | else: |
|---|
| 458 | 79 | if self.length < 0: # ignore nonsensical negative lengths |
|---|
| 459 | 1 | self.length = None |
|---|
| 460 | n/a | else: |
|---|
| 461 | 54 | self.length = None |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | # does the body have a fixed length? (of zero) |
|---|
| 464 | 133 | if (status == NO_CONTENT or status == NOT_MODIFIED or |
|---|
| 465 | 131 | 100 <= status < 200 or # 1xx codes |
|---|
| 466 | 131 | self._method == 'HEAD'): |
|---|
| 467 | 5 | self.length = 0 |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | # if the connection remains open, and we aren't using chunked, and |
|---|
| 470 | n/a | # a content-length was not provided, then assume that the connection |
|---|
| 471 | n/a | # WILL close. |
|---|
| 472 | 133 | if not self.will_close and \ |
|---|
| 473 | 27 | not self.chunked and \ |
|---|
| 474 | 23 | self.length is None: |
|---|
| 475 | 3 | self.will_close = 1 |
|---|
| 476 | n/a | |
|---|
| 477 | 1 | def _check_close(self): |
|---|
| 478 | 133 | conn = self.msg.getheader('connection') |
|---|
| 479 | 133 | if self.version == 11: |
|---|
| 480 | n/a | # An HTTP/1.1 proxy is assumed to stay open unless |
|---|
| 481 | n/a | # explicitly closed. |
|---|
| 482 | 74 | conn = self.msg.getheader('connection') |
|---|
| 483 | 74 | if conn and "close" in conn.lower(): |
|---|
| 484 | 47 | return True |
|---|
| 485 | 27 | return False |
|---|
| 486 | n/a | |
|---|
| 487 | n/a | # Some HTTP/1.0 implementations have support for persistent |
|---|
| 488 | n/a | # connections, using rules different than HTTP/1.1. |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | # For older HTTP, Keep-Alive indicates persistent connection. |
|---|
| 491 | 59 | if self.msg.getheader('keep-alive'): |
|---|
| 492 | 0 | return False |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | # At least Akamai returns a "Connection: Keep-Alive" header, |
|---|
| 495 | n/a | # which was supposed to be sent by the client. |
|---|
| 496 | 59 | if conn and "keep-alive" in conn.lower(): |
|---|
| 497 | 0 | return False |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | # Proxy-Connection is a netscape hack. |
|---|
| 500 | 59 | pconn = self.msg.getheader('proxy-connection') |
|---|
| 501 | 59 | if pconn and "keep-alive" in pconn.lower(): |
|---|
| 502 | 0 | return False |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | # otherwise, assume it will close |
|---|
| 505 | 59 | return True |
|---|
| 506 | n/a | |
|---|
| 507 | 1 | def close(self): |
|---|
| 508 | 122 | if self.fp: |
|---|
| 509 | 101 | self.fp.close() |
|---|
| 510 | 101 | self.fp = None |
|---|
| 511 | n/a | |
|---|
| 512 | 1 | def isclosed(self): |
|---|
| 513 | n/a | # NOTE: it is possible that we will not ever call self.close(). This |
|---|
| 514 | n/a | # case occurs when will_close is TRUE, length is None, and we |
|---|
| 515 | n/a | # read up to the last byte, but NOT past it. |
|---|
| 516 | n/a | # |
|---|
| 517 | n/a | # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be |
|---|
| 518 | n/a | # called, meaning self.isclosed() is meaningful. |
|---|
| 519 | 13 | return self.fp is None |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | # XXX It would be nice to have readline and __iter__ for this, too. |
|---|
| 522 | n/a | |
|---|
| 523 | 1 | def read(self, amt=None): |
|---|
| 524 | 1073 | if self.fp is None: |
|---|
| 525 | 58 | return '' |
|---|
| 526 | n/a | |
|---|
| 527 | 1015 | if self._method == 'HEAD': |
|---|
| 528 | 3 | self.close() |
|---|
| 529 | 3 | return '' |
|---|
| 530 | n/a | |
|---|
| 531 | 1012 | if self.chunked: |
|---|
| 532 | 3 | return self._read_chunked(amt) |
|---|
| 533 | n/a | |
|---|
| 534 | 1009 | if amt is None: |
|---|
| 535 | n/a | # unbounded read |
|---|
| 536 | 27 | if self.length is None: |
|---|
| 537 | 14 | s = self.fp.read() |
|---|
| 538 | n/a | else: |
|---|
| 539 | 13 | s = self._safe_read(self.length) |
|---|
| 540 | 12 | self.length = 0 |
|---|
| 541 | 26 | self.close() # we read everything |
|---|
| 542 | 26 | return s |
|---|
| 543 | n/a | |
|---|
| 544 | 982 | if self.length is not None: |
|---|
| 545 | 968 | if amt > self.length: |
|---|
| 546 | n/a | # clip the read to the "end of response" |
|---|
| 547 | 43 | amt = self.length |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | # we do not use _safe_read() here because this may be a .will_close |
|---|
| 550 | n/a | # connection, and the user is reading more bytes than will be provided |
|---|
| 551 | n/a | # (for example, reading in 1k chunks) |
|---|
| 552 | 982 | s = self.fp.read(amt) |
|---|
| 553 | 982 | if self.length is not None: |
|---|
| 554 | 968 | self.length -= len(s) |
|---|
| 555 | 968 | if not self.length: |
|---|
| 556 | 44 | self.close() |
|---|
| 557 | 982 | return s |
|---|
| 558 | n/a | |
|---|
| 559 | 1 | def _read_chunked(self, amt): |
|---|
| 560 | 3 | assert self.chunked != _UNKNOWN |
|---|
| 561 | 3 | chunk_left = self.chunk_left |
|---|
| 562 | 3 | value = [] |
|---|
| 563 | 9 | while True: |
|---|
| 564 | 9 | if chunk_left is None: |
|---|
| 565 | 9 | line = self.fp.readline() |
|---|
| 566 | 9 | i = line.find(';') |
|---|
| 567 | 9 | if i >= 0: |
|---|
| 568 | 0 | line = line[:i] # strip chunk-extensions |
|---|
| 569 | 9 | try: |
|---|
| 570 | 9 | chunk_left = int(line, 16) |
|---|
| 571 | 2 | except ValueError: |
|---|
| 572 | n/a | # close the connection as protocol synchronisation is |
|---|
| 573 | n/a | # probably lost |
|---|
| 574 | 2 | self.close() |
|---|
| 575 | 2 | raise IncompleteRead(''.join(value)) |
|---|
| 576 | 7 | if chunk_left == 0: |
|---|
| 577 | 1 | break |
|---|
| 578 | 6 | if amt is None: |
|---|
| 579 | 6 | value.append(self._safe_read(chunk_left)) |
|---|
| 580 | 0 | elif amt < chunk_left: |
|---|
| 581 | 0 | value.append(self._safe_read(amt)) |
|---|
| 582 | 0 | self.chunk_left = chunk_left - amt |
|---|
| 583 | 0 | return ''.join(value) |
|---|
| 584 | 0 | elif amt == chunk_left: |
|---|
| 585 | 0 | value.append(self._safe_read(amt)) |
|---|
| 586 | 0 | self._safe_read(2) # toss the CRLF at the end of the chunk |
|---|
| 587 | 0 | self.chunk_left = None |
|---|
| 588 | 0 | return ''.join(value) |
|---|
| 589 | n/a | else: |
|---|
| 590 | 0 | value.append(self._safe_read(chunk_left)) |
|---|
| 591 | 0 | amt -= chunk_left |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | # we read the whole chunk, get another |
|---|
| 594 | 6 | self._safe_read(2) # toss the CRLF at the end of the chunk |
|---|
| 595 | 6 | chunk_left = None |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | # read and discard trailer up to the CRLF terminator |
|---|
| 598 | n/a | ### note: we shouldn't have any trailers! |
|---|
| 599 | 1 | while True: |
|---|
| 600 | 1 | line = self.fp.readline() |
|---|
| 601 | 1 | if not line: |
|---|
| 602 | n/a | # a vanishingly small number of sites EOF without |
|---|
| 603 | n/a | # sending the trailer |
|---|
| 604 | 1 | break |
|---|
| 605 | 0 | if line == '\r\n': |
|---|
| 606 | 0 | break |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | # we read everything; close the "file" |
|---|
| 609 | 1 | self.close() |
|---|
| 610 | n/a | |
|---|
| 611 | 1 | return ''.join(value) |
|---|
| 612 | n/a | |
|---|
| 613 | 1 | def _safe_read(self, amt): |
|---|
| 614 | n/a | """Read the number of bytes requested, compensating for partial reads. |
|---|
| 615 | n/a | |
|---|
| 616 | n/a | Normally, we have a blocking socket, but a read() can be interrupted |
|---|
| 617 | n/a | by a signal (resulting in a partial read). |
|---|
| 618 | n/a | |
|---|
| 619 | n/a | Note that we cannot distinguish between EOF and an interrupt when zero |
|---|
| 620 | n/a | bytes have been read. IncompleteRead() will be raised in this |
|---|
| 621 | n/a | situation. |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | This function should be used when <amt> bytes "should" be present for |
|---|
| 624 | n/a | reading. If the bytes are truly not available (due to EOF), then the |
|---|
| 625 | n/a | IncompleteRead exception can be used to detect the problem. |
|---|
| 626 | n/a | """ |
|---|
| 627 | n/a | # NOTE(gps): As of svn r74426 socket._fileobject.read(x) will never |
|---|
| 628 | n/a | # return less than x bytes unless EOF is encountered. It now handles |
|---|
| 629 | n/a | # signal interruptions (socket.error EINTR) internally. This code |
|---|
| 630 | n/a | # never caught that exception anyways. It seems largely pointless. |
|---|
| 631 | n/a | # self.fp.read(amt) will work fine. |
|---|
| 632 | 25 | s = [] |
|---|
| 633 | 47 | while amt > 0: |
|---|
| 634 | 23 | chunk = self.fp.read(min(amt, MAXAMOUNT)) |
|---|
| 635 | 23 | if not chunk: |
|---|
| 636 | 1 | raise IncompleteRead(''.join(s), amt) |
|---|
| 637 | 22 | s.append(chunk) |
|---|
| 638 | 22 | amt -= len(chunk) |
|---|
| 639 | 24 | return ''.join(s) |
|---|
| 640 | n/a | |
|---|
| 641 | 1 | def getheader(self, name, default=None): |
|---|
| 642 | 45 | if self.msg is None: |
|---|
| 643 | 0 | raise ResponseNotReady() |
|---|
| 644 | 45 | return self.msg.getheader(name, default) |
|---|
| 645 | n/a | |
|---|
| 646 | 1 | def getheaders(self): |
|---|
| 647 | n/a | """Return list of (header, value) tuples.""" |
|---|
| 648 | 0 | if self.msg is None: |
|---|
| 649 | 0 | raise ResponseNotReady() |
|---|
| 650 | 0 | return self.msg.items() |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | |
|---|
| 653 | 2 | class HTTPConnection: |
|---|
| 654 | n/a | |
|---|
| 655 | 1 | _http_vsn = 11 |
|---|
| 656 | 1 | _http_vsn_str = 'HTTP/1.1' |
|---|
| 657 | n/a | |
|---|
| 658 | 1 | response_class = HTTPResponse |
|---|
| 659 | 1 | default_port = HTTP_PORT |
|---|
| 660 | 1 | auto_open = 1 |
|---|
| 661 | 1 | debuglevel = 0 |
|---|
| 662 | 1 | strict = 0 |
|---|
| 663 | n/a | |
|---|
| 664 | 1 | def __init__(self, host, port=None, strict=None, |
|---|
| 665 | 1 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None): |
|---|
| 666 | 141 | self.timeout = timeout |
|---|
| 667 | 141 | self.source_address = source_address |
|---|
| 668 | 141 | self.sock = None |
|---|
| 669 | 141 | self._buffer = [] |
|---|
| 670 | 141 | self.__response = None |
|---|
| 671 | 141 | self.__state = _CS_IDLE |
|---|
| 672 | 141 | self._method = None |
|---|
| 673 | 141 | self._tunnel_host = None |
|---|
| 674 | 141 | self._tunnel_port = None |
|---|
| 675 | 141 | self._tunnel_headers = {} |
|---|
| 676 | n/a | |
|---|
| 677 | 141 | self._set_hostport(host, port) |
|---|
| 678 | 139 | if strict is not None: |
|---|
| 679 | 0 | self.strict = strict |
|---|
| 680 | n/a | |
|---|
| 681 | 1 | def set_tunnel(self, host, port=None, headers=None): |
|---|
| 682 | n/a | """ Sets up the host and the port for the HTTP CONNECT Tunnelling. |
|---|
| 683 | n/a | |
|---|
| 684 | n/a | The headers argument should be a mapping of extra HTTP headers |
|---|
| 685 | n/a | to send with the CONNECT request. |
|---|
| 686 | n/a | """ |
|---|
| 687 | 0 | self._tunnel_host = host |
|---|
| 688 | 0 | self._tunnel_port = port |
|---|
| 689 | 0 | if headers: |
|---|
| 690 | 0 | self._tunnel_headers = headers |
|---|
| 691 | n/a | else: |
|---|
| 692 | 0 | self._tunnel_headers.clear() |
|---|
| 693 | n/a | |
|---|
| 694 | 1 | def _set_hostport(self, host, port): |
|---|
| 695 | 141 | if port is None: |
|---|
| 696 | 105 | i = host.rfind(':') |
|---|
| 697 | 105 | j = host.rfind(']') # ipv6 addresses have [...] |
|---|
| 698 | 105 | if i > j: |
|---|
| 699 | 53 | try: |
|---|
| 700 | 53 | port = int(host[i+1:]) |
|---|
| 701 | 2 | except ValueError: |
|---|
| 702 | 2 | raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) |
|---|
| 703 | 51 | host = host[:i] |
|---|
| 704 | n/a | else: |
|---|
| 705 | 52 | port = self.default_port |
|---|
| 706 | 103 | if host and host[0] == '[' and host[-1] == ']': |
|---|
| 707 | 2 | host = host[1:-1] |
|---|
| 708 | 139 | self.host = host |
|---|
| 709 | 139 | self.port = port |
|---|
| 710 | n/a | |
|---|
| 711 | 1 | def set_debuglevel(self, level): |
|---|
| 712 | 41 | self.debuglevel = level |
|---|
| 713 | n/a | |
|---|
| 714 | 1 | def _tunnel(self): |
|---|
| 715 | 0 | self._set_hostport(self._tunnel_host, self._tunnel_port) |
|---|
| 716 | 0 | self.send("CONNECT %s:%d HTTP/1.0\r\n" % (self.host, self.port)) |
|---|
| 717 | 0 | for header, value in self._tunnel_headers.iteritems(): |
|---|
| 718 | 0 | self.send("%s: %s\r\n" % (header, value)) |
|---|
| 719 | 0 | self.send("\r\n") |
|---|
| 720 | 0 | response = self.response_class(self.sock, strict = self.strict, |
|---|
| 721 | 0 | method = self._method) |
|---|
| 722 | 0 | (version, code, message) = response._read_status() |
|---|
| 723 | n/a | |
|---|
| 724 | 0 | if code != 200: |
|---|
| 725 | 0 | self.close() |
|---|
| 726 | 0 | raise socket.error("Tunnel connection failed: %d %s" % (code, |
|---|
| 727 | 0 | message.strip())) |
|---|
| 728 | 0 | while True: |
|---|
| 729 | 0 | line = response.fp.readline() |
|---|
| 730 | 0 | if line == '\r\n': break |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | |
|---|
| 733 | 1 | def connect(self): |
|---|
| 734 | n/a | """Connect to the host and port specified in __init__.""" |
|---|
| 735 | 119 | self.sock = socket.create_connection((self.host,self.port), |
|---|
| 736 | 119 | self.timeout, self.source_address) |
|---|
| 737 | n/a | |
|---|
| 738 | 117 | if self._tunnel_host: |
|---|
| 739 | 0 | self._tunnel() |
|---|
| 740 | n/a | |
|---|
| 741 | 1 | def close(self): |
|---|
| 742 | n/a | """Close the connection to the HTTP server.""" |
|---|
| 743 | 125 | if self.sock: |
|---|
| 744 | 118 | self.sock.close() # close it manually... there may be other refs |
|---|
| 745 | 118 | self.sock = None |
|---|
| 746 | 125 | if self.__response: |
|---|
| 747 | 2 | self.__response.close() |
|---|
| 748 | 2 | self.__response = None |
|---|
| 749 | 125 | self.__state = _CS_IDLE |
|---|
| 750 | n/a | |
|---|
| 751 | 1 | def send(self, str): |
|---|
| 752 | n/a | """Send `str' to the server.""" |
|---|
| 753 | 143 | if self.sock is None: |
|---|
| 754 | 105 | if self.auto_open: |
|---|
| 755 | 105 | self.connect() |
|---|
| 756 | n/a | else: |
|---|
| 757 | 0 | raise NotConnected() |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | # send the data to the server. if we get a broken pipe, then close |
|---|
| 760 | n/a | # the socket. we want to reconnect when somebody tries to send again. |
|---|
| 761 | n/a | # |
|---|
| 762 | n/a | # NOTE: we DO propagate the error, though, because we cannot simply |
|---|
| 763 | n/a | # ignore the error... the caller will know if they can retry. |
|---|
| 764 | 141 | if self.debuglevel > 0: |
|---|
| 765 | 0 | print "send:", repr(str) |
|---|
| 766 | 141 | try: |
|---|
| 767 | 141 | blocksize=8192 |
|---|
| 768 | 141 | if hasattr(str,'read') and not isinstance(str, array): |
|---|
| 769 | 2 | if self.debuglevel > 0: print "sendIng a read()able" |
|---|
| 770 | 2 | data=str.read(blocksize) |
|---|
| 771 | 5 | while data: |
|---|
| 772 | 3 | self.sock.sendall(data) |
|---|
| 773 | 3 | data=str.read(blocksize) |
|---|
| 774 | n/a | else: |
|---|
| 775 | 139 | self.sock.sendall(str) |
|---|
| 776 | 0 | except socket.error, v: |
|---|
| 777 | 0 | if v.args[0] == 32: # Broken pipe |
|---|
| 778 | 0 | self.close() |
|---|
| 779 | 0 | raise |
|---|
| 780 | n/a | |
|---|
| 781 | 1 | def _output(self, s): |
|---|
| 782 | n/a | """Add a line of output to the current request buffer. |
|---|
| 783 | n/a | |
|---|
| 784 | n/a | Assumes that the line does *not* end with \\r\\n. |
|---|
| 785 | n/a | """ |
|---|
| 786 | 633 | self._buffer.append(s) |
|---|
| 787 | n/a | |
|---|
| 788 | 1 | def _send_output(self, message_body=None): |
|---|
| 789 | n/a | """Send the currently buffered request and clear the buffer. |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | Appends an extra \\r\\n to the buffer. |
|---|
| 792 | n/a | A message_body may be specified, to be appended to the request. |
|---|
| 793 | n/a | """ |
|---|
| 794 | 139 | self._buffer.extend(("", "")) |
|---|
| 795 | 139 | msg = "\r\n".join(self._buffer) |
|---|
| 796 | 139 | del self._buffer[:] |
|---|
| 797 | n/a | # If msg and message_body are sent in a single send() call, |
|---|
| 798 | n/a | # it will avoid performance problems caused by the interaction |
|---|
| 799 | n/a | # between delayed ack and the Nagle algorithim. |
|---|
| 800 | 139 | if isinstance(message_body, str): |
|---|
| 801 | 44 | msg += message_body |
|---|
| 802 | 44 | message_body = None |
|---|
| 803 | 139 | self.send(msg) |
|---|
| 804 | 137 | if message_body is not None: |
|---|
| 805 | n/a | #message_body was not a string (i.e. it is a file) and |
|---|
| 806 | n/a | #we must run the risk of Nagle |
|---|
| 807 | 1 | self.send(message_body) |
|---|
| 808 | n/a | |
|---|
| 809 | 1 | def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0): |
|---|
| 810 | n/a | """Send a request to the server. |
|---|
| 811 | n/a | |
|---|
| 812 | n/a | `method' specifies an HTTP request method, e.g. 'GET'. |
|---|
| 813 | n/a | `url' specifies the object being requested, e.g. '/index.html'. |
|---|
| 814 | n/a | `skip_host' if True does not add automatically a 'Host:' header |
|---|
| 815 | n/a | `skip_accept_encoding' if True does not add automatically an |
|---|
| 816 | n/a | 'Accept-Encoding:' header |
|---|
| 817 | n/a | """ |
|---|
| 818 | n/a | |
|---|
| 819 | n/a | # if a prior response has been completed, then forget about it. |
|---|
| 820 | 139 | if self.__response and self.__response.isclosed(): |
|---|
| 821 | 8 | self.__response = None |
|---|
| 822 | n/a | |
|---|
| 823 | n/a | |
|---|
| 824 | n/a | # in certain cases, we cannot issue another request on this connection. |
|---|
| 825 | n/a | # this occurs when: |
|---|
| 826 | n/a | # 1) we are in the process of sending a request. (_CS_REQ_STARTED) |
|---|
| 827 | n/a | # 2) a response to a previous request has signalled that it is going |
|---|
| 828 | n/a | # to close the connection upon completion. |
|---|
| 829 | n/a | # 3) the headers for the previous response have not been read, thus |
|---|
| 830 | n/a | # we cannot determine whether point (2) is true. (_CS_REQ_SENT) |
|---|
| 831 | n/a | # |
|---|
| 832 | n/a | # if there is no prior response, then we can request at will. |
|---|
| 833 | n/a | # |
|---|
| 834 | n/a | # if point (2) is true, then we will have passed the socket to the |
|---|
| 835 | n/a | # response (effectively meaning, "there is no prior response"), and |
|---|
| 836 | n/a | # will open a new one when a new request is made. |
|---|
| 837 | n/a | # |
|---|
| 838 | n/a | # Note: if a prior response exists, then we *can* start a new request. |
|---|
| 839 | n/a | # We are not allowed to begin fetching the response to this new |
|---|
| 840 | n/a | # request, however, until that prior response is complete. |
|---|
| 841 | n/a | # |
|---|
| 842 | 139 | if self.__state == _CS_IDLE: |
|---|
| 843 | 139 | self.__state = _CS_REQ_STARTED |
|---|
| 844 | n/a | else: |
|---|
| 845 | 0 | raise CannotSendRequest() |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | # Save the method we use, we need it later in the response phase |
|---|
| 848 | 139 | self._method = method |
|---|
| 849 | 139 | if not url: |
|---|
| 850 | 10 | url = '/' |
|---|
| 851 | 139 | str = '%s %s %s' % (method, url, self._http_vsn_str) |
|---|
| 852 | n/a | |
|---|
| 853 | 139 | self._output(str) |
|---|
| 854 | n/a | |
|---|
| 855 | 139 | if self._http_vsn == 11: |
|---|
| 856 | n/a | # Issue some standard headers for better HTTP/1.1 compliance |
|---|
| 857 | n/a | |
|---|
| 858 | 120 | if not skip_host: |
|---|
| 859 | n/a | # this header is issued *only* for HTTP/1.1 |
|---|
| 860 | n/a | # connections. more specifically, this means it is |
|---|
| 861 | n/a | # only issued when the client uses the new |
|---|
| 862 | n/a | # HTTPConnection() class. backwards-compat clients |
|---|
| 863 | n/a | # will be using HTTP/1.0 and those clients may be |
|---|
| 864 | n/a | # issuing this header themselves. we should NOT issue |
|---|
| 865 | n/a | # it twice; some web servers (such as Apache) barf |
|---|
| 866 | n/a | # when they see two Host: headers |
|---|
| 867 | n/a | |
|---|
| 868 | n/a | # If we need a non-standard port,include it in the |
|---|
| 869 | n/a | # header. If the request is going through a proxy, |
|---|
| 870 | n/a | # but the host of the actual URL, not the host of the |
|---|
| 871 | n/a | # proxy. |
|---|
| 872 | n/a | |
|---|
| 873 | 78 | netloc = '' |
|---|
| 874 | 78 | if url.startswith('http'): |
|---|
| 875 | 0 | nil, netloc, nil, nil, nil = urlsplit(url) |
|---|
| 876 | n/a | |
|---|
| 877 | 78 | if netloc: |
|---|
| 878 | 0 | try: |
|---|
| 879 | 0 | netloc_enc = netloc.encode("ascii") |
|---|
| 880 | 0 | except UnicodeEncodeError: |
|---|
| 881 | 0 | netloc_enc = netloc.encode("idna") |
|---|
| 882 | 0 | self.putheader('Host', netloc_enc) |
|---|
| 883 | n/a | else: |
|---|
| 884 | 78 | try: |
|---|
| 885 | 78 | host_enc = self.host.encode("ascii") |
|---|
| 886 | 0 | except UnicodeEncodeError: |
|---|
| 887 | 0 | host_enc = self.host.encode("idna") |
|---|
| 888 | 78 | if self.port == self.default_port: |
|---|
| 889 | 10 | self.putheader('Host', host_enc) |
|---|
| 890 | n/a | else: |
|---|
| 891 | 68 | self.putheader('Host', "%s:%s" % (host_enc, self.port)) |
|---|
| 892 | n/a | |
|---|
| 893 | n/a | # note: we are assuming that clients will not attempt to set these |
|---|
| 894 | n/a | # headers since *this* library must deal with the |
|---|
| 895 | n/a | # consequences. this also means that when the supporting |
|---|
| 896 | n/a | # libraries are updated to recognize other forms, then this |
|---|
| 897 | n/a | # code should be changed (removed or updated). |
|---|
| 898 | n/a | |
|---|
| 899 | n/a | # we only want a Content-Encoding of "identity" since we don't |
|---|
| 900 | n/a | # support encodings such as x-gzip or x-deflate. |
|---|
| 901 | 120 | if not skip_accept_encoding: |
|---|
| 902 | 83 | self.putheader('Accept-Encoding', 'identity') |
|---|
| 903 | n/a | |
|---|
| 904 | n/a | # we can accept "chunked" Transfer-Encodings, but no others |
|---|
| 905 | n/a | # NOTE: no TE header implies *only* "chunked" |
|---|
| 906 | n/a | #self.putheader('TE', 'chunked') |
|---|
| 907 | n/a | |
|---|
| 908 | n/a | # if TE is supplied in the header, then it must appear in a |
|---|
| 909 | n/a | # Connection header. |
|---|
| 910 | n/a | #self.putheader('Connection', 'TE') |
|---|
| 911 | n/a | |
|---|
| 912 | n/a | else: |
|---|
| 913 | n/a | # For HTTP/1.0, the server will assume "not chunked" |
|---|
| 914 | 139 | pass |
|---|
| 915 | n/a | |
|---|
| 916 | 1 | def putheader(self, header, *values): |
|---|
| 917 | n/a | """Send a request header line to the server. |
|---|
| 918 | n/a | |
|---|
| 919 | n/a | For example: h.putheader('Accept', 'text/html') |
|---|
| 920 | n/a | """ |
|---|
| 921 | 494 | if self.__state != _CS_REQ_STARTED: |
|---|
| 922 | 0 | raise CannotSendHeader() |
|---|
| 923 | n/a | |
|---|
| 924 | 494 | str = '%s: %s' % (header, '\r\n\t'.join(values)) |
|---|
| 925 | 494 | self._output(str) |
|---|
| 926 | n/a | |
|---|
| 927 | 1 | def endheaders(self, message_body=None): |
|---|
| 928 | n/a | """Indicate that the last header line has been sent to the server. |
|---|
| 929 | n/a | |
|---|
| 930 | n/a | This method sends the request to the server. The optional |
|---|
| 931 | n/a | message_body argument can be used to pass message body |
|---|
| 932 | n/a | associated with the request. The message body will be sent in |
|---|
| 933 | n/a | the same packet as the message headers if possible. The |
|---|
| 934 | n/a | message_body should be a string. |
|---|
| 935 | n/a | """ |
|---|
| 936 | 139 | if self.__state == _CS_REQ_STARTED: |
|---|
| 937 | 139 | self.__state = _CS_REQ_SENT |
|---|
| 938 | n/a | else: |
|---|
| 939 | 0 | raise CannotSendHeader() |
|---|
| 940 | 139 | self._send_output(message_body) |
|---|
| 941 | n/a | |
|---|
| 942 | 1 | def request(self, method, url, body=None, headers={}): |
|---|
| 943 | n/a | """Send a complete request to the server.""" |
|---|
| 944 | n/a | |
|---|
| 945 | 76 | try: |
|---|
| 946 | 76 | self._send_request(method, url, body, headers) |
|---|
| 947 | 1 | except socket.error, v: |
|---|
| 948 | n/a | # trap 'Broken pipe' if we're allowed to automatically reconnect |
|---|
| 949 | 1 | if v.args[0] != 32 or not self.auto_open: |
|---|
| 950 | 1 | raise |
|---|
| 951 | n/a | # try one more time |
|---|
| 952 | 0 | self._send_request(method, url, body, headers) |
|---|
| 953 | n/a | |
|---|
| 954 | 1 | def _set_content_length(self, body): |
|---|
| 955 | n/a | # Set the content-length based on the body. |
|---|
| 956 | 7 | thelen = None |
|---|
| 957 | 7 | try: |
|---|
| 958 | 7 | thelen = str(len(body)) |
|---|
| 959 | 1 | except TypeError, te: |
|---|
| 960 | n/a | # If this is a file-like object, try to |
|---|
| 961 | n/a | # fstat its file descriptor |
|---|
| 962 | 1 | import os |
|---|
| 963 | 1 | try: |
|---|
| 964 | 1 | thelen = str(os.fstat(body.fileno()).st_size) |
|---|
| 965 | 0 | except (AttributeError, OSError): |
|---|
| 966 | n/a | # Don't send a length if this failed |
|---|
| 967 | 0 | if self.debuglevel > 0: print "Cannot stat!!" |
|---|
| 968 | n/a | |
|---|
| 969 | 7 | if thelen is not None: |
|---|
| 970 | 7 | self.putheader('Content-Length', thelen) |
|---|
| 971 | n/a | |
|---|
| 972 | 1 | def _send_request(self, method, url, body, headers): |
|---|
| 973 | n/a | # honour explicitly requested Host: and Accept-Encoding headers |
|---|
| 974 | 214 | header_names = dict.fromkeys([k.lower() for k in headers]) |
|---|
| 975 | 76 | skips = {} |
|---|
| 976 | 76 | if 'host' in header_names: |
|---|
| 977 | 42 | skips['skip_host'] = 1 |
|---|
| 978 | 76 | if 'accept-encoding' in header_names: |
|---|
| 979 | 1 | skips['skip_accept_encoding'] = 1 |
|---|
| 980 | n/a | |
|---|
| 981 | 76 | self.putrequest(method, url, **skips) |
|---|
| 982 | n/a | |
|---|
| 983 | 76 | if body and ('content-length' not in header_names): |
|---|
| 984 | 7 | self._set_content_length(body) |
|---|
| 985 | 214 | for hdr, value in headers.iteritems(): |
|---|
| 986 | 138 | self.putheader(hdr, value) |
|---|
| 987 | 76 | self.endheaders(body) |
|---|
| 988 | n/a | |
|---|
| 989 | 1 | def getresponse(self, buffering=False): |
|---|
| 990 | n/a | "Get the response from the server." |
|---|
| 991 | n/a | |
|---|
| 992 | n/a | # if a prior response has been completed, then forget about it. |
|---|
| 993 | 129 | if self.__response and self.__response.isclosed(): |
|---|
| 994 | 0 | self.__response = None |
|---|
| 995 | n/a | |
|---|
| 996 | n/a | # |
|---|
| 997 | n/a | # if a prior response exists, then it must be completed (otherwise, we |
|---|
| 998 | n/a | # cannot read this response's header to determine the connection-close |
|---|
| 999 | n/a | # behavior) |
|---|
| 1000 | n/a | # |
|---|
| 1001 | n/a | # note: if a prior response existed, but was connection-close, then the |
|---|
| 1002 | n/a | # socket and response were made independent of this HTTPConnection |
|---|
| 1003 | n/a | # object since a new request requires that we open a whole new |
|---|
| 1004 | n/a | # connection |
|---|
| 1005 | n/a | # |
|---|
| 1006 | n/a | # this means the prior response had one of two states: |
|---|
| 1007 | n/a | # 1) will_close: this connection was reset and the prior socket and |
|---|
| 1008 | n/a | # response operate independently |
|---|
| 1009 | n/a | # 2) persistent: the response was retained and we await its |
|---|
| 1010 | n/a | # isclosed() status to become true. |
|---|
| 1011 | n/a | # |
|---|
| 1012 | 129 | if self.__state != _CS_REQ_SENT or self.__response: |
|---|
| 1013 | 0 | raise ResponseNotReady() |
|---|
| 1014 | n/a | |
|---|
| 1015 | 129 | args = (self.sock,) |
|---|
| 1016 | 129 | kwds = {"strict":self.strict, "method":self._method} |
|---|
| 1017 | 129 | if self.debuglevel > 0: |
|---|
| 1018 | 0 | args += (self.debuglevel,) |
|---|
| 1019 | 129 | if buffering: |
|---|
| 1020 | n/a | #only add this keyword if non-default, for compatibility with |
|---|
| 1021 | n/a | #other response_classes. |
|---|
| 1022 | 76 | kwds["buffering"] = True; |
|---|
| 1023 | 129 | response = self.response_class(*args, **kwds) |
|---|
| 1024 | n/a | |
|---|
| 1025 | 125 | response.begin() |
|---|
| 1026 | 124 | assert response.will_close != _UNKNOWN |
|---|
| 1027 | 124 | self.__state = _CS_IDLE |
|---|
| 1028 | n/a | |
|---|
| 1029 | 124 | if response.will_close: |
|---|
| 1030 | n/a | # this effectively passes the connection to the response |
|---|
| 1031 | 107 | self.close() |
|---|
| 1032 | n/a | else: |
|---|
| 1033 | n/a | # remember this, so we can tell when it is complete |
|---|
| 1034 | 17 | self.__response = response |
|---|
| 1035 | n/a | |
|---|
| 1036 | 124 | return response |
|---|
| 1037 | n/a | |
|---|
| 1038 | n/a | |
|---|
| 1039 | 2 | class HTTP: |
|---|
| 1040 | 1 | "Compatibility class with httplib.py from 1.5." |
|---|
| 1041 | n/a | |
|---|
| 1042 | 1 | _http_vsn = 10 |
|---|
| 1043 | 1 | _http_vsn_str = 'HTTP/1.0' |
|---|
| 1044 | n/a | |
|---|
| 1045 | 1 | debuglevel = 0 |
|---|
| 1046 | n/a | |
|---|
| 1047 | 1 | _connection_class = HTTPConnection |
|---|
| 1048 | n/a | |
|---|
| 1049 | 1 | def __init__(self, host='', port=None, strict=None): |
|---|
| 1050 | n/a | "Provide a default host, since the superclass requires one." |
|---|
| 1051 | n/a | |
|---|
| 1052 | n/a | # some joker passed 0 explicitly, meaning default port |
|---|
| 1053 | 23 | if port == 0: |
|---|
| 1054 | 0 | port = None |
|---|
| 1055 | n/a | |
|---|
| 1056 | n/a | # Note that we may pass an empty string as the host; this will throw |
|---|
| 1057 | n/a | # an error when we attempt to connect. Presumably, the client code |
|---|
| 1058 | n/a | # will call connect before then, with a proper host. |
|---|
| 1059 | 23 | self._setup(self._connection_class(host, port, strict)) |
|---|
| 1060 | n/a | |
|---|
| 1061 | 1 | def _setup(self, conn): |
|---|
| 1062 | 22 | self._conn = conn |
|---|
| 1063 | n/a | |
|---|
| 1064 | n/a | # set up delegation to flesh out interface |
|---|
| 1065 | 22 | self.send = conn.send |
|---|
| 1066 | 22 | self.putrequest = conn.putrequest |
|---|
| 1067 | 22 | self.putheader = conn.putheader |
|---|
| 1068 | 22 | self.endheaders = conn.endheaders |
|---|
| 1069 | 22 | self.set_debuglevel = conn.set_debuglevel |
|---|
| 1070 | n/a | |
|---|
| 1071 | 22 | conn._http_vsn = self._http_vsn |
|---|
| 1072 | 22 | conn._http_vsn_str = self._http_vsn_str |
|---|
| 1073 | n/a | |
|---|
| 1074 | 22 | self.file = None |
|---|
| 1075 | n/a | |
|---|
| 1076 | 1 | def connect(self, host=None, port=None): |
|---|
| 1077 | n/a | "Accept arguments to set the host/port, since the superclass doesn't." |
|---|
| 1078 | n/a | |
|---|
| 1079 | 0 | if host is not None: |
|---|
| 1080 | 0 | self._conn._set_hostport(host, port) |
|---|
| 1081 | 0 | self._conn.connect() |
|---|
| 1082 | n/a | |
|---|
| 1083 | 1 | def getfile(self): |
|---|
| 1084 | n/a | "Provide a getfile, since the superclass' does not use this concept." |
|---|
| 1085 | 17 | return self.file |
|---|
| 1086 | n/a | |
|---|
| 1087 | 1 | def getreply(self, buffering=False): |
|---|
| 1088 | n/a | """Compat definition since superclass does not define it. |
|---|
| 1089 | n/a | |
|---|
| 1090 | n/a | Returns a tuple consisting of: |
|---|
| 1091 | n/a | - server status code (e.g. '200' if all goes well) |
|---|
| 1092 | n/a | - server "reason" corresponding to status code |
|---|
| 1093 | n/a | - any RFC822 headers in the response from the server |
|---|
| 1094 | n/a | """ |
|---|
| 1095 | 17 | try: |
|---|
| 1096 | 17 | if not buffering: |
|---|
| 1097 | 17 | response = self._conn.getresponse() |
|---|
| 1098 | n/a | else: |
|---|
| 1099 | n/a | #only add this keyword if non-default for compatibility |
|---|
| 1100 | n/a | #with other connection classes |
|---|
| 1101 | 0 | response = self._conn.getresponse(buffering) |
|---|
| 1102 | 1 | except BadStatusLine, e: |
|---|
| 1103 | n/a | ### hmm. if getresponse() ever closes the socket on a bad request, |
|---|
| 1104 | n/a | ### then we are going to have problems with self.sock |
|---|
| 1105 | n/a | |
|---|
| 1106 | n/a | ### should we keep this behavior? do people use it? |
|---|
| 1107 | n/a | # keep the socket open (as a file), and return it |
|---|
| 1108 | 1 | self.file = self._conn.sock.makefile('rb', 0) |
|---|
| 1109 | n/a | |
|---|
| 1110 | n/a | # close our socket -- we want to restart after any protocol error |
|---|
| 1111 | 1 | self.close() |
|---|
| 1112 | n/a | |
|---|
| 1113 | 1 | self.headers = None |
|---|
| 1114 | 1 | return -1, e.line, None |
|---|
| 1115 | n/a | |
|---|
| 1116 | 16 | self.headers = response.msg |
|---|
| 1117 | 16 | self.file = response.fp |
|---|
| 1118 | 16 | return response.status, response.reason, response.msg |
|---|
| 1119 | n/a | |
|---|
| 1120 | 1 | def close(self): |
|---|
| 1121 | 1 | self._conn.close() |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | # note that self.file == response.fp, which gets closed by the |
|---|
| 1124 | n/a | # superclass. just clear the object ref here. |
|---|
| 1125 | n/a | ### hmm. messy. if status==-1, then self.file is owned by us. |
|---|
| 1126 | n/a | ### well... we aren't explicitly closing, but losing this ref will |
|---|
| 1127 | n/a | ### do it |
|---|
| 1128 | 1 | self.file = None |
|---|
| 1129 | n/a | |
|---|
| 1130 | 1 | try: |
|---|
| 1131 | 1 | import ssl |
|---|
| 1132 | 0 | except ImportError: |
|---|
| 1133 | 0 | pass |
|---|
| 1134 | n/a | else: |
|---|
| 1135 | 2 | class HTTPSConnection(HTTPConnection): |
|---|
| 1136 | 1 | "This class allows communication via SSL." |
|---|
| 1137 | n/a | |
|---|
| 1138 | 1 | default_port = HTTPS_PORT |
|---|
| 1139 | n/a | |
|---|
| 1140 | 1 | def __init__(self, host, port=None, key_file=None, cert_file=None, |
|---|
| 1141 | 1 | strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
|---|
| 1142 | 1 | source_address=None): |
|---|
| 1143 | 3 | HTTPConnection.__init__(self, host, port, strict, timeout, |
|---|
| 1144 | 3 | source_address) |
|---|
| 1145 | 3 | self.key_file = key_file |
|---|
| 1146 | 3 | self.cert_file = cert_file |
|---|
| 1147 | n/a | |
|---|
| 1148 | 1 | def connect(self): |
|---|
| 1149 | n/a | "Connect to a host on a given (SSL) port." |
|---|
| 1150 | n/a | |
|---|
| 1151 | 1 | sock = socket.create_connection((self.host, self.port), |
|---|
| 1152 | 1 | self.timeout, self.source_address) |
|---|
| 1153 | 1 | if self._tunnel_host: |
|---|
| 1154 | 0 | self.sock = sock |
|---|
| 1155 | 0 | self._tunnel() |
|---|
| 1156 | 1 | self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file) |
|---|
| 1157 | n/a | |
|---|
| 1158 | 1 | __all__.append("HTTPSConnection") |
|---|
| 1159 | n/a | |
|---|
| 1160 | 2 | class HTTPS(HTTP): |
|---|
| 1161 | n/a | """Compatibility with 1.5 httplib interface |
|---|
| 1162 | n/a | |
|---|
| 1163 | n/a | Python 1.5.2 did not have an HTTPS class, but it defined an |
|---|
| 1164 | n/a | interface for sending http requests that is also useful for |
|---|
| 1165 | n/a | https. |
|---|
| 1166 | 1 | """ |
|---|
| 1167 | n/a | |
|---|
| 1168 | 1 | _connection_class = HTTPSConnection |
|---|
| 1169 | n/a | |
|---|
| 1170 | 1 | def __init__(self, host='', port=None, key_file=None, cert_file=None, |
|---|
| 1171 | 1 | strict=None): |
|---|
| 1172 | n/a | # provide a default host, pass the X509 cert info |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | # urf. compensate for bad input. |
|---|
| 1175 | 1 | if port == 0: |
|---|
| 1176 | 1 | port = None |
|---|
| 1177 | 1 | self._setup(self._connection_class(host, port, key_file, |
|---|
| 1178 | 1 | cert_file, strict)) |
|---|
| 1179 | n/a | |
|---|
| 1180 | n/a | # we never actually use these for anything, but we keep them |
|---|
| 1181 | n/a | # here for compatibility with post-1.5.2 CVS. |
|---|
| 1182 | 1 | self.key_file = key_file |
|---|
| 1183 | 1 | self.cert_file = cert_file |
|---|
| 1184 | n/a | |
|---|
| 1185 | n/a | |
|---|
| 1186 | 1 | def FakeSocket (sock, sslobj): |
|---|
| 1187 | 0 | warnings.warn("FakeSocket is deprecated, and won't be in 3.x. " + |
|---|
| 1188 | 0 | "Use the result of ssl.wrap_socket() directly instead.", |
|---|
| 1189 | 0 | DeprecationWarning, stacklevel=2) |
|---|
| 1190 | 0 | return sslobj |
|---|
| 1191 | n/a | |
|---|
| 1192 | n/a | |
|---|
| 1193 | 2 | class HTTPException(Exception): |
|---|
| 1194 | n/a | # Subclasses that define an __init__ must call Exception.__init__ |
|---|
| 1195 | n/a | # or define self.args. Otherwise, str() will fail. |
|---|
| 1196 | 1 | pass |
|---|
| 1197 | n/a | |
|---|
| 1198 | 2 | class NotConnected(HTTPException): |
|---|
| 1199 | 1 | pass |
|---|
| 1200 | n/a | |
|---|
| 1201 | 2 | class InvalidURL(HTTPException): |
|---|
| 1202 | 1 | pass |
|---|
| 1203 | n/a | |
|---|
| 1204 | 2 | class UnknownProtocol(HTTPException): |
|---|
| 1205 | 1 | def __init__(self, version): |
|---|
| 1206 | 0 | self.args = version, |
|---|
| 1207 | 0 | self.version = version |
|---|
| 1208 | n/a | |
|---|
| 1209 | 2 | class UnknownTransferEncoding(HTTPException): |
|---|
| 1210 | 1 | pass |
|---|
| 1211 | n/a | |
|---|
| 1212 | 2 | class UnimplementedFileMode(HTTPException): |
|---|
| 1213 | 1 | pass |
|---|
| 1214 | n/a | |
|---|
| 1215 | 2 | class IncompleteRead(HTTPException): |
|---|
| 1216 | 1 | def __init__(self, partial, expected=None): |
|---|
| 1217 | 3 | self.args = partial, |
|---|
| 1218 | 3 | self.partial = partial |
|---|
| 1219 | 3 | self.expected = expected |
|---|
| 1220 | 1 | def __repr__(self): |
|---|
| 1221 | 6 | if self.expected is not None: |
|---|
| 1222 | 2 | e = ', %i more expected' % self.expected |
|---|
| 1223 | n/a | else: |
|---|
| 1224 | 4 | e = '' |
|---|
| 1225 | 6 | return 'IncompleteRead(%i bytes read%s)' % (len(self.partial), e) |
|---|
| 1226 | 1 | def __str__(self): |
|---|
| 1227 | 3 | return repr(self) |
|---|
| 1228 | n/a | |
|---|
| 1229 | 2 | class ImproperConnectionState(HTTPException): |
|---|
| 1230 | 1 | pass |
|---|
| 1231 | n/a | |
|---|
| 1232 | 2 | class CannotSendRequest(ImproperConnectionState): |
|---|
| 1233 | 1 | pass |
|---|
| 1234 | n/a | |
|---|
| 1235 | 2 | class CannotSendHeader(ImproperConnectionState): |
|---|
| 1236 | 1 | pass |
|---|
| 1237 | n/a | |
|---|
| 1238 | 2 | class ResponseNotReady(ImproperConnectionState): |
|---|
| 1239 | 1 | pass |
|---|
| 1240 | n/a | |
|---|
| 1241 | 2 | class BadStatusLine(HTTPException): |
|---|
| 1242 | 1 | def __init__(self, line): |
|---|
| 1243 | 3 | if not line: |
|---|
| 1244 | 2 | line = repr(line) |
|---|
| 1245 | 3 | self.args = line, |
|---|
| 1246 | 3 | self.line = line |
|---|
| 1247 | n/a | |
|---|
| 1248 | n/a | # for backwards compatibility |
|---|
| 1249 | 1 | error = HTTPException |
|---|
| 1250 | n/a | |
|---|
| 1251 | 2 | class LineAndFileWrapper: |
|---|
| 1252 | 1 | """A limited file-like object for HTTP/0.9 responses.""" |
|---|
| 1253 | n/a | |
|---|
| 1254 | n/a | # The status-line parsing code calls readline(), which normally |
|---|
| 1255 | n/a | # get the HTTP status line. For a 0.9 response, however, this is |
|---|
| 1256 | n/a | # actually the first line of the body! Clients need to get a |
|---|
| 1257 | n/a | # readable file object that contains that line. |
|---|
| 1258 | n/a | |
|---|
| 1259 | 1 | def __init__(self, line, file): |
|---|
| 1260 | 1 | self._line = line |
|---|
| 1261 | 1 | self._file = file |
|---|
| 1262 | 1 | self._line_consumed = 0 |
|---|
| 1263 | 1 | self._line_offset = 0 |
|---|
| 1264 | 1 | self._line_left = len(line) |
|---|
| 1265 | n/a | |
|---|
| 1266 | 1 | def __getattr__(self, attr): |
|---|
| 1267 | 5 | return getattr(self._file, attr) |
|---|
| 1268 | n/a | |
|---|
| 1269 | 1 | def _done(self): |
|---|
| 1270 | n/a | # called when the last byte is read from the line. After the |
|---|
| 1271 | n/a | # call, all read methods are delegated to the underlying file |
|---|
| 1272 | n/a | # object. |
|---|
| 1273 | 1 | self._line_consumed = 1 |
|---|
| 1274 | 1 | self.read = self._file.read |
|---|
| 1275 | 1 | self.readline = self._file.readline |
|---|
| 1276 | 1 | self.readlines = self._file.readlines |
|---|
| 1277 | n/a | |
|---|
| 1278 | 1 | def read(self, amt=None): |
|---|
| 1279 | 0 | if self._line_consumed: |
|---|
| 1280 | 0 | return self._file.read(amt) |
|---|
| 1281 | 0 | assert self._line_left |
|---|
| 1282 | 0 | if amt is None or amt > self._line_left: |
|---|
| 1283 | 0 | s = self._line[self._line_offset:] |
|---|
| 1284 | 0 | self._done() |
|---|
| 1285 | 0 | if amt is None: |
|---|
| 1286 | 0 | return s + self._file.read() |
|---|
| 1287 | n/a | else: |
|---|
| 1288 | 0 | return s + self._file.read(amt - len(s)) |
|---|
| 1289 | n/a | else: |
|---|
| 1290 | 0 | assert amt <= self._line_left |
|---|
| 1291 | 0 | i = self._line_offset |
|---|
| 1292 | 0 | j = i + amt |
|---|
| 1293 | 0 | s = self._line[i:j] |
|---|
| 1294 | 0 | self._line_offset = j |
|---|
| 1295 | 0 | self._line_left -= amt |
|---|
| 1296 | 0 | if self._line_left == 0: |
|---|
| 1297 | 0 | self._done() |
|---|
| 1298 | 0 | return s |
|---|
| 1299 | n/a | |
|---|
| 1300 | 1 | def readline(self): |
|---|
| 1301 | 2 | if self._line_consumed: |
|---|
| 1302 | 1 | return self._file.readline() |
|---|
| 1303 | 1 | assert self._line_left |
|---|
| 1304 | 1 | s = self._line[self._line_offset:] |
|---|
| 1305 | 1 | self._done() |
|---|
| 1306 | 1 | return s |
|---|
| 1307 | n/a | |
|---|
| 1308 | 1 | def readlines(self, size=None): |
|---|
| 1309 | 0 | if self._line_consumed: |
|---|
| 1310 | 0 | return self._file.readlines(size) |
|---|
| 1311 | 0 | assert self._line_left |
|---|
| 1312 | 0 | L = [self._line[self._line_offset:]] |
|---|
| 1313 | 0 | self._done() |
|---|
| 1314 | 0 | if size is None: |
|---|
| 1315 | 0 | return L + self._file.readlines() |
|---|
| 1316 | n/a | else: |
|---|
| 1317 | 0 | return L + self._file.readlines(size) |
|---|
| 1318 | n/a | |
|---|
| 1319 | 1 | def test(): |
|---|
| 1320 | n/a | """Test this module. |
|---|
| 1321 | n/a | |
|---|
| 1322 | n/a | A hodge podge of tests collected here, because they have too many |
|---|
| 1323 | n/a | external dependencies for the regular test suite. |
|---|
| 1324 | n/a | """ |
|---|
| 1325 | n/a | |
|---|
| 1326 | 0 | import sys |
|---|
| 1327 | 0 | import getopt |
|---|
| 1328 | 0 | opts, args = getopt.getopt(sys.argv[1:], 'd') |
|---|
| 1329 | 0 | dl = 0 |
|---|
| 1330 | 0 | for o, a in opts: |
|---|
| 1331 | 0 | if o == '-d': dl = dl + 1 |
|---|
| 1332 | 0 | host = 'www.python.org' |
|---|
| 1333 | 0 | selector = '/' |
|---|
| 1334 | 0 | if args[0:]: host = args[0] |
|---|
| 1335 | 0 | if args[1:]: selector = args[1] |
|---|
| 1336 | 0 | h = HTTP() |
|---|
| 1337 | 0 | h.set_debuglevel(dl) |
|---|
| 1338 | 0 | h.connect(host) |
|---|
| 1339 | 0 | h.putrequest('GET', selector) |
|---|
| 1340 | 0 | h.endheaders() |
|---|
| 1341 | 0 | status, reason, headers = h.getreply() |
|---|
| 1342 | 0 | print 'status =', status |
|---|
| 1343 | 0 | print 'reason =', reason |
|---|
| 1344 | 0 | print "read", len(h.getfile().read()) |
|---|
| 1345 | 0 | print |
|---|
| 1346 | 0 | if headers: |
|---|
| 1347 | 0 | for header in headers.headers: print header.strip() |
|---|
| 1348 | 0 | print |
|---|
| 1349 | n/a | |
|---|
| 1350 | n/a | # minimal test that code to extract host from url works |
|---|
| 1351 | 0 | class HTTP11(HTTP): |
|---|
| 1352 | 0 | _http_vsn = 11 |
|---|
| 1353 | 0 | _http_vsn_str = 'HTTP/1.1' |
|---|
| 1354 | n/a | |
|---|
| 1355 | 0 | h = HTTP11('www.python.org') |
|---|
| 1356 | 0 | h.putrequest('GET', 'http://www.python.org/~jeremy/') |
|---|
| 1357 | 0 | h.endheaders() |
|---|
| 1358 | 0 | h.getreply() |
|---|
| 1359 | 0 | h.close() |
|---|
| 1360 | n/a | |
|---|
| 1361 | 0 | try: |
|---|
| 1362 | 0 | import ssl |
|---|
| 1363 | 0 | except ImportError: |
|---|
| 1364 | 0 | pass |
|---|
| 1365 | n/a | else: |
|---|
| 1366 | n/a | |
|---|
| 1367 | 0 | for host, selector in (('sourceforge.net', '/projects/python'), |
|---|
| 1368 | n/a | ): |
|---|
| 1369 | 0 | print "https://%s%s" % (host, selector) |
|---|
| 1370 | 0 | hs = HTTPS() |
|---|
| 1371 | 0 | hs.set_debuglevel(dl) |
|---|
| 1372 | 0 | hs.connect(host) |
|---|
| 1373 | 0 | hs.putrequest('GET', selector) |
|---|
| 1374 | 0 | hs.endheaders() |
|---|
| 1375 | 0 | status, reason, headers = hs.getreply() |
|---|
| 1376 | 0 | print 'status =', status |
|---|
| 1377 | 0 | print 'reason =', reason |
|---|
| 1378 | 0 | print "read", len(hs.getfile().read()) |
|---|
| 1379 | 0 | print |
|---|
| 1380 | 0 | if headers: |
|---|
| 1381 | 0 | for header in headers.headers: print header.strip() |
|---|
| 1382 | 0 | print |
|---|
| 1383 | n/a | |
|---|
| 1384 | 1 | if __name__ == '__main__': |
|---|
| 1385 | 0 | test() |
|---|