1
|
1 #!/usr/bin/env python
|
|
2
|
0
|
3 """
|
1
|
4 dogdish
|
0
|
5 """
|
|
6
|
4
|
7 import sys
|
0
|
8 from urlparse import urlparse
|
4
|
9 from webob import Request
|
0
|
10 from webob import Response, exc
|
|
11
|
|
12 class Handler(object):
|
1
|
13
|
0
|
14 def __init__(self, request):
|
|
15 self.request = request
|
|
16 self.application_path = urlparse(request.application_url)[2]
|
|
17
|
|
18 def link(self, path=(), permanant=False):
|
|
19 if isinstance(path, basestring):
|
|
20 path = [ path ]
|
|
21 path = [ i.strip('/') for i in path ]
|
|
22 if permanant:
|
|
23 application_url = [ self.request.application_url ]
|
|
24 else:
|
|
25 application_url = [ self.application_path ]
|
|
26 path = application_url + path
|
|
27 return '/'.join(path)
|
|
28
|
|
29 def redirect(self, location):
|
|
30 raise exc.HTTPSeeOther(location=location)
|
|
31
|
|
32 class Get(Handler):
|
|
33
|
|
34 body = """<?xml version="1.0"?>
|
|
35 <updates>
|
|
36 <update type="minor" appVersion="19.0a1" version="19.0a1" extensionVersion="19.0a1" buildID="20121010114416" licenseURL="http://www.mozilla.com/test/sample-eula.html" detailsURL="http://www.mozilla.com/test/sample-details.html">
|
|
37 <patch type="complete" URL="http://update.boot2gecko.org/nightly/b2g_update_2012-10-10_114416.mar" hashFunction="SHA512" hashValue="84edb1f53891cf983bc0f6066d31492f43e2d063aaceb05e1c51876f4fde81635afeb7ce3203dee6f65dd59be0cae5c73c49093b625c99acd4118000ad72dda8" size="42924805"/>
|
|
38 </update>
|
|
39 </updates>"""
|
|
40
|
|
41 @classmethod
|
|
42 def match(cls, request):
|
|
43 return request.method == 'GET'
|
|
44
|
|
45 def __call__(self):
|
1
|
46 return Response(content_type='text/xml',
|
3
|
47 body=self.body)
|
0
|
48
|
|
49 class Dispatcher(object):
|
|
50
|
|
51 ### class level variables
|
|
52 defaults = {}
|
|
53
|
|
54 def __init__(self, **kw):
|
|
55 for key in self.defaults:
|
|
56 setattr(self, key, kw.get(key, self.defaults[key]))
|
|
57 self.handlers = [ Get ]
|
|
58 if self.app:
|
|
59 assert hasattr(self.app, '__call__')
|
|
60
|
|
61 ### methods dealing with HTTP
|
|
62 def __call__(self, environ, start_response):
|
|
63 request = Request(environ)
|
|
64 for h in self.handlers:
|
|
65 if h.match(request):
|
|
66 handler = h(request)
|
|
67 break
|
|
68 else:
|
|
69 if self.app:
|
|
70 return self.app(environ, start_response)
|
|
71 handler = exc.HTTPNotFound
|
|
72 res = handler()
|
|
73 return res(environ, start_response)
|
|
74
|
|
75 def main(args=sys.argv[1:]):
|
|
76 """CLI entry point"""
|
|
77
|
|
78 import optparse
|
|
79 from wsgiref import simple_server
|
|
80
|
|
81 parser = optparse.OptionParser()
|
|
82
|
|
83 parser.add_option('-p', '--port', dest='port',
|
|
84 default=8080, type='int',
|
|
85 help="port to serve on")
|
|
86 options, args = parser.parse_args()
|
|
87
|
|
88 dispatcher = Dispatcher()
|
|
89
|
|
90 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
|
|
91 server.serve_forever()
|
|
92
|
|
93 if __name__ == '__main__':
|
|
94 main()
|