OLD | NEW |
---|---|
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import re | 5 import re |
6 # F0401: 7,0: Unable to import 'webapp2' | 6 # F0401: 7,0: Unable to import 'webapp2' |
7 # pylint: disable=F0401 | 7 # pylint: disable=F0401 |
8 import webapp2 | 8 import webapp2 |
9 | 9 |
10 import cache | 10 import cache |
11 import config | 11 import config |
12 import content_processor | 12 import content_processor |
13 import content_type | 13 import content_type |
14 import pre_cacher | 14 import pre_cacher |
15 import file_reader | 15 import file_reader |
16 import zip_proxy | 16 import zip_proxy |
17 | 17 |
18 current_config = config.DEFAULT | 18 current_config = config.DEFAULT |
19 | 19 |
20 class MainPage(webapp2.RequestHandler): | 20 class MainPage(webapp2.RequestHandler): |
21 def get(self): | 21 def get(self): |
22 self.response.headers['Content-Type'] = 'text/plain' | 22 self.response.headers['Content-Type'] = 'text/plain' |
23 self.response.write(str(current_config)) | 23 self.response.write(str(current_config)) |
24 | 24 |
25 class Server(webapp2.RequestHandler): | 25 class Server(webapp2.RequestHandler): |
26 def get(self, tag_type, tag, path): | 26 def get_hash_cache_key(self, rev, path): |
27 cache_key_name = '/res/%s/%s/%s' % (tag_type, tag, path) | 27 return '/hashes/%s/%s' % (rev, path) |
28 content = cache.get_content(cache_key_name) | |
29 helper = config.ConfigHelper(current_config) | |
30 | 28 |
31 if not content: | 29 def get_file_hash(self, helper, rev, path): |
32 if tag_type == 'rev': | 30 file_hash = cache.get_content(self.get_hash_cache_key(rev, path)) |
33 meta_file_name = helper.get_revision_path(tag) | 31 |
34 elif tag_type =='ver': | 32 if not file_hash: |
35 meta_file_name = helper.get_version_path(tag) | 33 meta_file_name = helper.get_meta_path(rev) |
36 else: | 34 meta_content = file_reader.read(helper, meta_file_name) |
35 if not meta_content: | |
37 self.abort(404) | 36 self.abort(404) |
mnaganov (inactive)
2015/02/25 12:56:35
Let's just return null here to make it clear that
dgozman
2015/02/25 14:22:08
Nice catch! Done.
| |
38 | 37 |
39 meta_content = file_reader.read(helper, meta_file_name) | 38 for line in meta_content.split('\n'): |
mnaganov (inactive)
2015/02/25 12:56:35
In case when the path isn't listed in the meta fil
dgozman
2015/02/25 14:22:08
Done.
| |
40 if meta_content: | 39 index = line.find(':') |
mnaganov (inactive)
2015/02/25 12:56:35
You just do another split here, so you can just wr
dgozman
2015/02/25 14:22:08
Done.
| |
41 zip_file_name = meta_content.strip(' \t\n') | 40 if index != -1: |
42 else: | 41 line_path = line[(index + 1):] |
42 line_hash = line[:index] | |
43 cache.set_content(self.get_hash_cache_key(rev, line_path), line_hash) | |
44 if line_path == path: | |
45 file_hash = line_hash | |
46 | |
47 return file_hash | |
48 | |
49 def get(self, tag_type, tag, path): | |
50 helper = config.ConfigHelper(current_config) | |
51 content = None | |
52 | |
53 if tag_type == 'file': | |
54 file_hash = self.get_file_hash(helper, tag, path) | |
55 if not file_hash: | |
43 self.abort(404) | 56 self.abort(404) |
44 | 57 content = file_reader.read(helper, helper.get_hash_path(file_hash)) |
45 content = zip_proxy.read(helper, zip_file_name, path) | |
46 if not content: | 58 if not content: |
47 self.abort(404) | 59 self.abort(404) |
48 content = content_processor.process(path, content) | 60 content = content_processor.process(path, content) |
49 cache.set_content(cache_key_name, content) | 61 |
62 else: | |
63 cache_key_name = '/res/%s/%s/%s' % (tag_type, tag, path) | |
64 content = cache.get_content(cache_key_name) | |
65 if not content: | |
66 if tag_type == 'rev': | |
67 meta_file_name = helper.get_revision_path(tag) | |
68 elif tag_type =='ver': | |
69 meta_file_name = helper.get_version_path(tag) | |
70 else: | |
71 self.abort(404) | |
72 | |
73 meta_content = file_reader.read(helper, meta_file_name) | |
74 if meta_content: | |
75 zip_file_name = meta_content.strip(' \t\n') | |
76 else: | |
77 self.abort(404) | |
78 | |
79 content = zip_proxy.read(helper, zip_file_name, path) | |
80 if not content: | |
81 self.abort(404) | |
82 content = content_processor.process(path, content) | |
83 cache.set_content(cache_key_name, content) | |
50 | 84 |
51 self.response.headers['Content-Type'] = content_type.from_path(path) | 85 self.response.headers['Content-Type'] = content_type.from_path(path) |
52 self.response.headers['Access-Control-Allow-Origin'] = '*' | 86 self.response.headers['Access-Control-Allow-Origin'] = '*' |
53 cache_control = 'public, max-age=%d' % helper.get_max_age() | 87 cache_control = 'public, max-age=%d' % helper.get_max_age() |
54 self.response.headers['Cache-Control'] = cache_control | 88 self.response.headers['Cache-Control'] = cache_control |
55 self.response.body = content | 89 self.response.body = content |
56 | 90 |
57 class LegacyStatic(Server): | 91 class LegacyStatic(Server): |
58 def get(self, version, path): | 92 def get(self, version, path): |
59 # Map x.x.x.x -> x.x.x.0 to deal with patched releases | 93 # Map x.x.x.x -> x.x.x.0 to deal with patched releases |
60 match = re.search('(\d+\.\d+\.\d+\.)\d+', version) | 94 match = re.search('(\d+\.\d+\.\d+\.)\d+', version) |
61 if match: | 95 if match: |
62 return super(LegacyStatic, self).get('ver', match.group(1) + '0', path) | 96 return super(LegacyStatic, self).get('ver', match.group(1) + '0', path) |
63 else: | 97 else: |
64 self.abort(404) | 98 self.abort(404) |
65 | 99 |
66 app = webapp2.WSGIApplication( | 100 app = webapp2.WSGIApplication( |
67 [('/', MainPage), | 101 [('/', MainPage), |
68 ('/precache_url', pre_cacher.PreCacherFromUrl), | 102 ('/precache_url', pre_cacher.PreCacherFromUrl), |
69 webapp2.Route('/serve_<tag_type>/<tag>/<path:.*>', Server), | 103 webapp2.Route('/serve_<tag_type>/<tag>/<path:.*>', Server), |
70 webapp2.Route('/static/<version>/<path:.*>', LegacyStatic)], | 104 webapp2.Route('/static/<version>/<path:.*>', LegacyStatic)], |
71 debug=config.IS_DEV_APP_SERVER) | 105 debug=config.IS_DEV_APP_SERVER) |
OLD | NEW |