1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
import logging
from util import date_time_string from handler.base_handler import StreamRequestHandler
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
class BaseHTTPRequestHandler(StreamRequestHandler):
def __init__(self, server, request, client_address): self.request_line = None self.method = None self.path = None self.version = None self.headers = None self.body = None StreamRequestHandler.__init__(self, server, request, client_address)
def handle(self): try: if not self.parse_request(): return method_name = 'do_' + self.method if not hasattr(self, method_name): self.write_error(404, None) self.send() return method = getattr(self, method_name) method() self.send() except Exception as e: logging.exception(e)
def do_GET(self): msg = '<h1>Hello World</h1>' self.write_response(200, 'Success') self.write_header('Content-Length', len(msg)) self.end_headers() self.write_content(msg)
def parse_headers(self): headers = {} while True: line = self.readline() if line: key, value = line.split(":", 1) key = key.strip() value = value.strip() headers[key] = value else: break return headers
def parse_request(self): first_line = self.readline() self.request_line = first_line if not self.request_line: return words = first_line.split() self.method, self.path, self.version = words self.headers = self.parse_headers()
key = 'Content-Length' if key in self.headers.keys(): body_length = int(self.headers[key]) self.body = self.read(body_length)
return True
def write_header(self, key, value): msg = '%s: %s\r\n' % (key, value) self.write_content(msg)
default_http_version = 'HTTP/1.1'
def write_response(self, code, msg=None): logging.info("%s, code: %s." % (self.request_line, code)) if msg == None: msg = self.responses[code][0] response_line = '%s %d %s\r\n' % (self.default_http_version, code, msg) self.write_content(response_line) self.write_header('Server', '%s: %s' % (self.server.server_name, self.server.version)) self.write_header('Date', date_time_string()) pass
DEFAULT_ERROR_MESSAGE_TEMPLATE = r''' <head> <title>Error response</title> </head> <body> <h1>Error response</h1> <p>Error code %(code)d. <p>Message: %(message)s. <p>Error code explanation: %(code)s = %(explain)s. </body> '''
responses = { 100: ('Continue', 'Request received, please continue'), 101: ('Switching Protocols', 'Switching to new protocol; obey Upgrade header'),
200: ('OK', 'Request fulfilled, document follows'), 201: ('Created', 'Document created, URL follows'), 202: ('Accepted', 'Request accepted, processing continues off-line'), 203: ('Non-Authoritative Information', 'Request fulfilled from cache'), 204: ('No Content', 'Request fulfilled, nothing follows'), 205: ('Reset Content', 'Clear input form for further input.'), 206: ('Partial Content', 'Partial content follows.'),
300: ('Multiple Choices', 'Object has several resources -- see URI list'), 301: ('Moved Permanently', 'Object moved permanently -- see URI list'), 302: ('Found', 'Object moved temporarily -- see URI list'), 303: ('See Other', 'Object moved -- see Method and URL list'), 304: ('Not Modified', 'Document has not changed since given time'), 305: ('Use Proxy', 'You must use proxy specified in Location to access this ' 'resource.'), 307: ('Temporary Redirect', 'Object moved temporarily -- see URI list'),
400: ('Bad Request', 'Bad request syntax or unsupported method'), 401: ('Unauthorized', 'No permission -- see authorization schemes'), 402: ('Payment Required', 'No payment -- see charging schemes'), 403: ('Forbidden', 'Request forbidden -- authorization will not help'), 404: ('Not Found', 'Nothing matches the given URI'), 405: ('Method Not Allowed', 'Specified method is invalid for this resource.'), 406: ('Not Acceptable', 'URI not available in preferred format.'), 407: ('Proxy Authentication Required', 'You must authenticate with ' 'this proxy before proceeding.'), 408: ('Request Timeout', 'Request timed out; try again later.'), 409: ('Conflict', 'Request conflict.'), 410: ('Gone', 'URI no longer exists and has been permanently removed.'), 411: ('Length Required', 'Client must specify Content-Length.'), 412: ('Precondition Failed', 'Precondition in headers is false.'), 413: ('Request Entity Too Large', 'Entity is too large.'), 414: ('Request-URI Too Long', 'URI is too long.'), 415: ('Unsupported Media Type', 'Entity body in unsupported format.'), 416: ('Requested Range Not Satisfiable', 'Cannot satisfy request range.'), 417: ('Expectation Failed', 'Expect condition could not be satisfied.'),
500: ('Internal Server Error', 'Server got itself in trouble'), 501: ('Not Implemented', 'Server does not support this operation'), 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'), 503: ('Service Unavailable', 'The server cannot process the request due to a high load'), 504: ('Gateway Timeout', 'The gateway server did not receive a timely response'), 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'), }
def write_error(self, code, msg=None): s_msg, l_msg = self.responses[code] if msg: s_msg = msg response_content = self.DEFAULT_ERROR_MESSAGE_TEMPLATE % { 'code': code, 'message': s_msg, 'explain': l_msg } self.write_response(code, s_msg) self.end_headers() self.write_content(response_content)
def end_headers(self): self.write_content('\r\n')
|