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
|
|
59 ### methods dealing with HTTP
|
|
60 def __call__(self, environ, start_response):
|
|
61 request = Request(environ)
|
|
62 for h in self.handlers:
|
|
63 if h.match(request):
|
|
64 handler = h(request)
|
|
65 break
|
|
66 else:
|
|
67 handler = exc.HTTPNotFound
|
|
68 res = handler()
|
|
69 return res(environ, start_response)
|
|
70
|
|
71 def main(args=sys.argv[1:]):
|
|
72 """CLI entry point"""
|
|
73
|
|
74 import optparse
|
|
75 from wsgiref import simple_server
|
|
76
|
|
77 parser = optparse.OptionParser()
|
|
78
|
|
79 parser.add_option('-p', '--port', dest='port',
|
|
80 default=8080, type='int',
|
|
81 help="port to serve on")
|
|
82 options, args = parser.parse_args()
|
|
83
|
6
|
84 app = Dispatcher()
|
0
|
85
|
|
86 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
|
|
87 server.serve_forever()
|
|
88
|
|
89 if __name__ == '__main__':
|
|
90 main()
|