A class for HTTP requests Attributes: blob : the Blob instance of the request errors : a list of caught exceptions from parsing method : the method of the request (e.g. GET, PUT, POST, etc.) uri : the URI being requested (host not included)
| 87 | |
| 88 | |
| 89 | class HTTPRequest(object): |
| 90 | """ |
| 91 | A class for HTTP requests |
| 92 | |
| 93 | Attributes: |
| 94 | blob : the Blob instance of the request |
| 95 | errors : a list of caught exceptions from parsing |
| 96 | method : the method of the request (e.g. GET, PUT, POST, etc.) |
| 97 | uri : the URI being requested (host not included) |
| 98 | version : the HTTP version (e.g. "1.1" for "HTTP/1.1") |
| 99 | headers : a dictionary containing the headers and values |
| 100 | body : bytestring of the reassembled body, after the headers |
| 101 | """ |
| 102 | _methods = ( |
| 103 | 'GET', 'PUT', 'ICY', |
| 104 | 'COPY', 'HEAD', 'LOCK', 'MOVE', 'POLL', 'POST', |
| 105 | 'BCOPY', 'BMOVE', 'MKCOL', 'TRACE', 'LABEL', 'MERGE', |
| 106 | 'DELETE', 'SEARCH', 'UNLOCK', 'REPORT', 'UPDATE', 'NOTIFY', |
| 107 | 'BDELETE', 'CONNECT', 'OPTIONS', 'CHECKIN', |
| 108 | 'PROPFIND', 'CHECKOUT', 'CCM_POST', |
| 109 | 'SUBSCRIBE', 'PROPPATCH', 'BPROPFIND', |
| 110 | 'BPROPPATCH', 'UNCHECKOUT', 'MKACTIVITY', |
| 111 | 'MKWORKSPACE', 'UNSUBSCRIBE', 'RPC_CONNECT', |
| 112 | 'VERSION-CONTROL', |
| 113 | 'BASELINE-CONTROL' |
| 114 | ) |
| 115 | |
| 116 | def __init__(self, blob): |
| 117 | self.errors = [] |
| 118 | self.headers = {} |
| 119 | self.body = b'' |
| 120 | self.blob = blob |
| 121 | data = io.BytesIO(blob.data) |
| 122 | rawline = data.readline() |
| 123 | try: |
| 124 | line = rawline.decode('utf-8') |
| 125 | except UnicodeDecodeError: |
| 126 | line = '' |
| 127 | l = line.strip().split() |
| 128 | if len(l) != 3 or l[0] not in self._methods or not l[2].startswith('HTTP'): |
| 129 | self.errors.append(dshell.core.DataError('invalid HTTP request: {!r}'.format(rawline))) |
| 130 | self.method = '' |
| 131 | self.uri = '' |
| 132 | self.version = '' |
| 133 | return |
| 134 | else: |
| 135 | self.method = l[0] |
| 136 | self.uri = l[1] |
| 137 | self.version = l[2][5:] |
| 138 | self.headers = parse_headers(self, data) |
| 139 | self.body = parse_body(self, data, self.headers) |
| 140 | |
| 141 | |
| 142 | class HTTPResponse(object): |