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