Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # This file is part of the Adblock Plus web scripts, | |
Vasily Kuznetsov
2018/07/20 21:00:01
I would just move this into the test folder. Or is
Tudor Avram
2018/07/23 19:29:03
Acknowledged.
Tudor Avram
2018/07/31 09:20:18
Done.
| |
2 # Copyright (C) 2006-present eyeo GmbH | |
3 # | |
4 # Adblock Plus is free software: you can redistribute it and/or modify | |
5 # it under the terms of the GNU General Public License version 3 as | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
13 # You should have received a copy of the GNU General Public License | |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
15 | |
16 from wsgiref.simple_server import make_server | |
17 import json | |
18 import re | |
19 from urlparse import parse_qs | |
20 | |
21 _AUTH_RESPONSE = { | |
22 'access_token': '1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M', | |
23 'token_type': 'Bearer', | |
24 'expires_in': 3600, | |
25 } | |
26 | |
27 _NOT_FOUND_RESPONSE = { | |
28 'error': { | |
29 'errors': [{ | |
30 'domain': 'global', | |
31 'reason': 'notFound', | |
32 'message': 'File not found: {}', | |
33 'locationType': 'parameter', | |
34 'location': 'fileId', | |
35 }], | |
36 'code': '404', | |
37 'message': 'File not found: {}', | |
38 }, | |
39 } | |
40 | |
41 | |
42 def download(environ, start_response): | |
43 """Send a file from a given path to the client.""" | |
44 d = parse_qs(environ['QUERY_STRING']) | |
45 path = d.get('path') | |
46 headers = list() | |
47 try: | |
48 file = open(path[0], 'rb') | |
49 start_response('200 OK', headers) | |
50 if 'wsgi.file_wrapper' in environ: | |
51 return environ['wsgi.file_wrapper'](file, 1024) | |
52 else: | |
53 return iter(lambda: file.read(1024), '') | |
54 except IOError: | |
55 response = _NOT_FOUND_RESPONSE | |
56 response['error']['errors'][0]['message'] = \ | |
57 response['error']['errors'][0]['message'].format(path) | |
58 response['error']['message'] = \ | |
59 response['error']['message'].format(path) | |
60 | |
61 headers = [('content-type', 'text/plain')] | |
62 start_response('404 NOT FOUND', headers) | |
63 return [json.dumps(response, encoding='utf-8')] | |
64 | |
65 | |
66 def auth(environ, start_response): | |
67 """Simulate a successful authentication using Google Oauth2.""" | |
68 headers = [('content-type', 'application/json')] | |
69 response = json.dumps(_AUTH_RESPONSE, encoding='utf-8') | |
70 | |
71 start_response('200 OK', headers) | |
72 | |
73 return [response] | |
74 | |
75 | |
76 _URLS = [ | |
77 (r'oauth2/v4/token/?$', auth), | |
78 (r'download/?$', download), | |
79 ] | |
80 | |
81 | |
82 def main(environ, start_response): | |
83 """Serve as ain entry point for the dummy google API.""" | |
84 path = environ.get('PATH_INFO', '').lstrip('/') | |
85 for regex, callback in _URLS: | |
86 match = re.search(regex, path) | |
87 if match is not None: | |
88 environ['myapp.url_args'] = match.groups() | |
89 return callback(environ, start_response) | |
90 | |
91 | |
92 if __name__ == '__main__': | |
93 app = make_server('localhost', 8080, main) | |
94 app.serve_forever() | |
OLD | NEW |