28
24
self.environ = environ
29
25
self.start_response = start_response
30
26
self.method = environ['REQUEST_METHOD']
31
if '://' in environ['PATH_INFO']:
32
# All the needed information is embedded in PATH_INFO.
33
shift_path_info(environ) # shift the host and or port.
34
self.application = shift_path_info(environ)
35
path = environ['PATH_INFO'].split('/')
36
path.pop(0) # Pop the scheme.
38
elif environ['SCRIPT_NAME'] == '':
39
# Remove the application to set the path.
40
self.application = shift_path_info(environ)
41
self.path = environ['PATH_INFO'].split('/')
43
self.application = environ['SCRIPT_NAME']
44
self.path = environ['PATH_INFO'].split('/')
27
self.host_port = shift_path_info(environ)
28
self.application = shift_path_info(environ)
29
path = environ['PATH_INFO'].split('/')
30
self.scheme = path.pop(0)
45
32
self.query_string = environ['QUERY_STRING']
46
33
return self.handle_request()
48
35
def handle_request(self):
49
"""Select the method to handle the request and return a response."""
50
if self.application != 'archive':
51
return self.send_response(httplib.NOT_FOUND)
52
elif self.method == 'PUT':
36
if self.method == 'PUT':
53
37
return self.do_PUT()
54
elif self.method == 'POST':
38
if self.method == 'POST':
55
39
return self.do_POST()
56
elif self.method == 'GET':
40
if self.method == 'GET':
57
41
return self.do_GET()
58
return self.send_response(httplib.METHOD_NOT_ALLOWED)
60
43
def send_response(self, code, response='', reason=None, headers={}):
61
"""Set the status code and reason, then return the response."""
63
45
reason = httplib.responses[code]
64
response_status = '%s %s' % (code, reason)
46
response_code = '%s %s' % (code, reason)
65
47
response_headers = {'content-type': 'application/json'}
66
48
response_headers.update(headers.items())
67
self.start_response(response_status, response_headers.items())
49
self.start_response(response_code, response_headers.items())
182
164
os.kill(self.pid, SIGKILL)
167
def application(environ, start_response):
168
start_response('200 OK', [('Content-Type', 'text/plain')])
185
172
if __name__ == '__main__':
186
173
app = GrackleService(MemoryStore({}))
187
174
service = make_server('', 8787, app)