-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzigmo.py
348 lines (282 loc) · 8.71 KB
/
zigmo.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# -*- coding: utf-8 -*-
import re
import urllib
# from wsgiref.simple_server import make_server
from wsgi_server import make_server
from concurrent import Future
_RESPONSE_STATUSES = {
# Informational
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
# Successful
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
207: 'Multi Status',
226: 'IM Used',
# Redirection
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
307: 'Temporary Redirect',
# Client Error
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Request Entity Too Large',
414: 'Request URI Too Long',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
418: "I'm a teapot",
422: 'Unprocessable Entity',
423: 'Locked',
424: 'Failed Dependency',
426: 'Upgrade Required',
# Server Error
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
507: 'Insufficient Storage',
510: 'Not Extended',
}
_RESPONSE_HEADERS = (
'Accept-Ranges',
'Age',
'Allow',
'Cache-Control',
'Connection',
'Content-Encoding',
'Content-Language',
'Content-Length',
'Content-Location',
'Content-MD5',
'Content-Disposition',
'Content-Range',
'Content-Type',
'Date',
'ETag',
'Expires',
'Last-Modified',
'Link',
'Location',
'P3P',
'Pragma',
'Proxy-Authenticate',
'Refresh',
'Retry-After',
'Server',
'Set-Cookie',
'Strict-Transport-Security',
'Trailer',
'Transfer-Encoding',
'Vary',
'Via',
'Warning',
'WWW-Authenticate',
'X-Frame-Options',
'X-XSS-Protection',
'X-Content-Type-Options',
'X-Forwarded-Proto',
'X-Powered-By',
'X-UA-Compatible',
)
_UPPER_CASE_RESPONSE_HEADERS = (header.upper() for header in _RESPONSE_HEADERS)
_HEADER_X_POWERED_BY = {'X-Powered-By': 'zigmo/0.1'}
# Error Processing
class HandlerError(Exception):
CODE = 500
class HandlerNotFound(HandlerError):
CODE = 404
class MethodNotImplement(Exception):
pass
class MethodNotAllowed(HandlerError):
CODE = 405
def to_string(s):
if isinstance(s, str):
return s
if isinstance(s, unicode):
return s.encode('utf-8')
return str(s)
def quote(s, encoding='utf-8'):
if isinstance(s, unicode):
s.encode(encoding)
return urllib.quote(s)
class Request(object):
def __init__(self, environ):
self._environ = environ
self._headers = {}
self._cookies = {}
@property
def url(self):
return self._environ['PATH_INFO']
@property
def headers(self):
return self._get_headers()
@property
def query_string(self):
return self._environ.get('QUERY_STRING', '')
@property
def method(self):
return self._environ['REQUEST_METHOD']
@property
def cookies(self):
return self._get_cookies()
def get_cookie(self, name, default=None):
pass
def _get_cookies(self):
if not self._cookies:
cookies = {}
cookie_str = self._environ.get('HTTP_COOKIE')
if cookie_str:
for cookie in cookie_str.split('; '):
if '=' in cookie:
k, v = cookie.split('=')
cookies[k] = v
self._cookies = cookies
return self._cookies
def _get_headers(self):
if not self._headers:
headers = {}
for k, v in self._environ.items():
if k.startswith('HTTP_'):
key = k[5:].replace('_', '-').upper()
headers[key] = v.encode('utf-8')
self._headers = headers
return self._headers
class Response(object):
def __init__(self):
self._status = '200 OK'
self._headers = {'Content-Type': 'text/html; charset=utf-8'}
self._headers.update(_HEADER_X_POWERED_BY)
self._cookies = {}
self._body = {}
def set_response_code(self, code):
if code not in _RESPONSE_STATUSES:
raise ValueError('Bad response code: %s' % code)
self._status = '%s %s' % (code, _RESPONSE_STATUSES[code])
def set_header(self, name, value):
key = name.upper()
if key not in _UPPER_CASE_RESPONSE_HEADERS:
key = name
self._headers[key] = to_string(value)
@property
def status(self):
return self._status
@property
def headers(self):
header_items = self._headers.items()
if self._cookies:
for cookie in self._cookies.values():
header_items.append(('Set-Cookie', cookie))
self._headers.update(_HEADER_X_POWERED_BY)
return header_items
def set_cookie(self, name, value, max_age=None, expires=None, path='/',
domain=None, secure=False, http_only=True):
cookie = ['%s=%s' % (quote(name), quote(value))]
if expires:
cookie.append('Expires=%s' % expires)
elif isinstance(max_age, (int, long)):
cookie.append('Max-Age=%d' % max_age)
cookie.append('Path=%s' % path)
if domain:
cookie.append('Domain=%s' % domain)
if secure:
cookie.append('Secure')
if http_only:
cookie.append('HttpOnly')
self._cookies[name] = ';'.join(cookie)
class Controller(object):
ALLOWED_METHOD = ['get', 'post', 'put', 'patch', 'delete', 'head']
def __init__(self, url_regex, handler):
self.url_regex = self.compile_url_pattern(url_regex)
self.handler = handler
self.methods = self.collect_method()
@classmethod
def compile_url_pattern(cls, url_regex):
if not url_regex.startswith('$'):
url_regex += '$'
return re.compile(url_regex)
def collect_method(self):
# desert use hasattr()
# refer: https://hynek.me/articles/hasattr/
handler_methods = []
for method in self.ALLOWED_METHOD:
try:
getattr(self.handler, method)
handler_methods.append(method)
except AttributeError:
continue
if not handler_methods:
raise MethodNotImplement()
return handler_methods
class Application(object):
def __init__(self, url_handler):
self.url_handler = url_handler
self.url_spec = self.build_url_spec()
def build_url_spec(self):
return [
Controller(url_regex, handler)
for url_regex, handler in self.url_handler
]
def dispatch(self, url, method):
for controller in self.url_spec:
if controller.url_regex.match(url):
method = method.lower()
if method in controller.methods:
return getattr(controller.handler, method)
else:
raise MethodNotAllowed()
raise HandlerNotFound()
@classmethod
def execute_handler(cls, func, request, response):
result = func(request=request, response=response)
if isinstance(result, Future):
return result.result
return result
class BaseHandler(object):
pass
def handle_error(error, response, start_response):
response.set_response_code(error.CODE)
start_response(response.status, response.headers)
return [response.status]
def build_wsgi_app(application):
def wsgi(environ, start_response):
request = Request(environ)
response = Response()
try:
func = application.dispatch(request.url, request.method)
content = application.execute_handler(func, request, response)
except (HandlerNotFound, MethodNotAllowed) as error:
return handle_error(error, response, start_response)
start_response(response.status, response.headers)
del request
del response
return [content]
return wsgi
def run_server(host='localhost', port=9090, application=None):
print 'Running server on %s:%s' % (host, port)
wsgi = build_wsgi_app(application)
server = make_server(host, port, wsgi)
server.serve_forever()