30
26
self.method = environ['REQUEST_METHOD']
31
27
self.host_port = shift_path_info(environ)
32
28
self.application = shift_path_info(environ)
33
path = environ['PATH_INFO'].split('/')
34
self.scheme = path.pop(0)
29
self.path = environ['PATH_INFO'].split('/')[1:]
36
30
self.query_string = environ['QUERY_STRING']
37
31
return self.handle_request()
39
33
def handle_request(self):
40
"""Select the method to handle the request and return a response."""
41
if self.application != 'archive':
42
return self.send_response(httplib.NOT_FOUND)
43
34
if self.method == 'PUT':
44
35
return self.do_PUT()
45
elif self.method == 'POST':
36
if self.method == 'POST':
46
37
return self.do_POST()
47
elif self.method == 'GET':
38
if self.method == 'GET':
48
39
return self.do_GET()
49
return self.send_response(httplib.METHOD_NOT_ALLOWED)
51
def send_response(self, code, response='', reason=None, headers={}):
52
"""Set the status code and reason, then return the response."""
41
def send_response(self, code, headers={}, reason=None):
54
reason = httplib.responses[code]
43
reason = httplib.responses[code].upper()
55
44
response_code = '%s %s' % (code, reason)
56
45
response_headers = {'content-type': 'application/json'}
57
46
response_headers.update(headers.items())
58
47
self.start_response(response_code, response_headers.items())
62
50
"""Create an archive or message on PUT."""
64
52
# This expected path is /archive/archive_id.
66
54
self.store.put_archive(self.path[0])
67
return self.send_response(httplib.CREATED)
55
self.send_response(httplib.CREATED)
68
57
except Exception, error:
69
return self.send_response(
70
59
httplib.BAD_REQUEST, reason=error.__doc__)
71
elif len(self.path) == 2:
61
if len(self.path) == 2:
72
62
# This expected path is /archive/archive_id/message_id.
74
64
put_input = self.environ['wsgi.input']
75
65
message = put_input.read(int(self.environ['CONTENT_LENGTH']))
76
66
self.store.put_message(self.path[0], self.path[1], message)
77
return self.send_response(httplib.CREATED)
78
except Exception, error:
79
return self.send_response(
80
httplib.BAD_REQUEST, reason=error.__doc__)
67
self.send_response(httplib.CREATED)
70
self.send_response(httplib.BAD_REQUEST)
83
74
"""Change a message on POST."""
87
78
# This expected path is /archive/archive_id/message_id.
88
79
response = self.store.hide_message(
89
80
self.path[0], self.path[1], self.query_string)
90
response = simplejson.dumps(response)
91
return self.send_response(httplib.OK, response=response)
92
except Exception, error:
93
return self.send_response(
94
httplib.BAD_REQUEST, reason=error.__doc__)
81
self.send_response(httplib.OK)
82
return [simplejson.dumps(response)]
84
self.send_response(httplib.BAD_REQUEST)
97
88
"""Retrieve a list of messages on GET."""
99
90
response = self.store.get_messages(
100
91
self.path[0], self.query_string)
101
response = simplejson.dumps(response)
102
return self.send_response(httplib.OK, response=response)
92
self.send_response(httplib.OK)
93
return [simplejson.dumps(response)]
103
94
except Exception, error:
104
return self.send_response(
105
httplib.BAD_REQUEST, reason=error.__doc__)
95
self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
107
98
def log_message(self, format, *args):
108
99
"""Override log_message to use standard Python logging."""
173
164
os.kill(self.pid, SIGKILL)
167
def application(environ, start_response):
168
start_response('200 OK', [('Content-Type', 'text/plain')])
176
172
if __name__ == '__main__':
177
173
app = GrackleService(MemoryStore({}))
178
174
service = make_server('', 8787, app)