~didrocks/unity/altf10

« back to all changes in this revision

Viewing changes to grackle/service.py

  • Committer: Curtis Hovey
  • Date: 2012-03-17 21:21:37 UTC
  • Revision ID: curtis.hovey@canonical.com-20120317212137-r7zonl5ksenpa5ci
Save the scheme.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
__metaclass__ = type
2
 
__all__ = [
3
 
    'ForkedFakeService',
4
 
    'GrackleService',
5
 
    ]
6
2
 
7
3
import httplib
8
4
import logging
18
14
 
19
15
 
20
16
class GrackleService:
21
 
    """A request handler that forwards to an archive store."""
 
17
    """A request handler that forwards to server.store."""
22
18
 
23
19
    def __init__(self, store):
24
20
        self.store = store
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.
37
 
            self.path = path
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('/')
42
 
        else:
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)
 
31
        self.path = path
45
32
        self.query_string = environ['QUERY_STRING']
46
33
        return self.handle_request()
47
34
 
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)
59
42
 
60
43
    def send_response(self, code, response='', reason=None, headers={}):
61
 
        """Set the status code and reason, then return the response."""
62
44
        if reason is None:
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())
68
50
        return [response]
69
51
 
70
52
    def do_PUT(self):
182
164
        os.kill(self.pid, SIGKILL)
183
165
 
184
166
 
 
167
def application(environ, start_response):
 
168
    start_response('200 OK', [('Content-Type', 'text/plain')])
 
169
    return "Hello World"
 
170
 
 
171
 
185
172
if __name__ == '__main__':
186
173
    app = GrackleService(MemoryStore({}))
187
174
    service = make_server('', 8787, app)