6
10
from signal import SIGKILL
13
from wsgiref.headers import Headers
9
14
from wsgiref.simple_server import make_server
10
15
from wsgiref.util import shift_path_info
11
17
from grackle.store import (
16
22
class GrackleService:
17
"""A request handler that forwards to server.store."""
23
"""A request handler that forwards to an archive store."""
19
25
def __init__(self, store):
23
29
def __call__(self, environ, start_response):
24
30
self.environ = environ
25
31
self.start_response = start_response
32
self.headers = Headers([('content-type', 'application/json')])
26
33
self.method = environ['REQUEST_METHOD']
27
self.host_port = shift_path_info(environ)
28
self.application = shift_path_info(environ)
29
self.path = environ['PATH_INFO'].split('/')[1:]
34
if '://' in environ['PATH_INFO']:
35
# All the needed information is embedded in PATH_INFO.
36
shift_path_info(environ) # shift the host and or port.
37
self.application = shift_path_info(environ)
38
path = environ['PATH_INFO'].split('/')
39
path.pop(0) # Pop the scheme.
41
elif environ['SCRIPT_NAME'] == '':
42
# Remove the application to set the path.
43
self.application = shift_path_info(environ)
44
self.path = environ['PATH_INFO'].split('/')
46
self.application = environ['SCRIPT_NAME']
47
self.path = environ['PATH_INFO'].split('/')
30
48
self.query_string = environ['QUERY_STRING']
31
49
return self.handle_request()
33
51
def handle_request(self):
34
if self.method == 'PUT':
52
"""Select the method to handle the request and return a response."""
53
if self.application != 'archive':
54
return self.send_response(httplib.NOT_FOUND)
55
elif self.method == 'PUT':
35
56
return self.do_PUT()
36
if self.method == 'POST':
57
elif self.method == 'POST':
37
58
return self.do_POST()
38
if self.method == 'GET':
59
elif self.method == 'GET':
39
60
return self.do_GET()
61
return self.send_response(httplib.METHOD_NOT_ALLOWED)
41
def send_response(self, code, headers={}, reason=None):
63
def send_response(self, code, response='', reason=None):
64
"""Set the status code and reason, then return the response."""
43
reason = httplib.responses[code].upper()
44
response_code = '%s %s' % (code, reason)
45
response_headers = {'content-type': 'application/json'}
46
response_headers.update(headers.items())
47
self.start_response(response_code, response_headers.items())
66
reason = httplib.responses[code]
67
response_status = '%s %s' % (code, reason)
68
self.start_response(response_status, self.headers.items())
50
72
"""Create an archive or message on PUT."""
52
74
# This expected path is /archive/archive_id.
54
76
self.store.put_archive(self.path[0])
55
self.send_response(httplib.CREATED)
77
return self.send_response(httplib.CREATED)
57
78
except Exception, error:
79
return self.send_response(
59
80
httplib.BAD_REQUEST, reason=error.__doc__)
61
if len(self.path) == 2:
81
elif len(self.path) == 2:
62
82
# This expected path is /archive/archive_id/message_id.
64
84
put_input = self.environ['wsgi.input']
65
85
message = put_input.read(int(self.environ['CONTENT_LENGTH']))
66
86
self.store.put_message(self.path[0], self.path[1], message)
67
self.send_response(httplib.CREATED)
70
self.send_response(httplib.BAD_REQUEST)
87
return self.send_response(httplib.CREATED)
88
except Exception, error:
89
return self.send_response(
90
httplib.BAD_REQUEST, reason=error.__doc__)
74
93
"""Change a message on POST."""
78
97
# This expected path is /archive/archive_id/message_id.
79
98
response = self.store.hide_message(
80
99
self.path[0], self.path[1], self.query_string)
81
self.send_response(httplib.OK)
82
return [simplejson.dumps(response)]
84
self.send_response(httplib.BAD_REQUEST)
100
response = simplejson.dumps(response)
101
return self.send_response(httplib.OK, response=response)
102
except Exception, error:
103
return self.send_response(
104
httplib.BAD_REQUEST, reason=error.__doc__)
88
107
"""Retrieve a list of messages on GET."""
90
109
response = self.store.get_messages(
91
110
self.path[0], self.query_string)
92
self.send_response(httplib.OK)
93
return [simplejson.dumps(response)]
111
response = simplejson.dumps(response)
112
return self.send_response(httplib.OK, response=response)
94
113
except Exception, error:
95
self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
114
return self.send_response(
115
httplib.BAD_REQUEST, reason=error.__doc__)
98
117
def log_message(self, format, *args):
99
118
"""Override log_message to use standard Python logging."""
164
183
os.kill(self.pid, SIGKILL)
167
def application(environ, start_response):
168
start_response('200 OK', [('Content-Type', 'text/plain')])
172
186
if __name__ == '__main__':
173
187
app = GrackleService(MemoryStore({}))
174
188
service = make_server('', 8787, app)