| 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_hash_cache_key(self, rev, path): |
| 27 return '/hashes/%s/%s' % (rev, path) |
| 28 |
| 29 def get_file_hash(self, helper, rev, path): |
| 30 file_hash = cache.get_content(self.get_hash_cache_key(rev, path)) |
| 31 |
| 32 if not file_hash: |
| 33 meta_cache_key = '/meta-parsed/%s' % rev |
| 34 if cache.get_content(meta_cache_key): |
| 35 return None |
| 36 |
| 37 meta_file_name = helper.get_meta_path(rev) |
| 38 meta_content = file_reader.read(helper, meta_file_name) |
| 39 if not meta_content: |
| 40 return None |
| 41 |
| 42 for line in meta_content.split('\n'): |
| 43 hash_and_path = line.split(':') |
| 44 if len(hash_and_path) == 2: |
| 45 (line_hash, line_path) = hash_and_path |
| 46 cache.set_content(self.get_hash_cache_key(rev, line_path), line_hash) |
| 47 if line_path == path: |
| 48 file_hash = line_hash |
| 49 cache.set_content(meta_cache_key, 'parsed') |
| 50 |
| 51 return file_hash |
| 52 |
| 26 def get(self, tag_type, tag, path): | 53 def get(self, tag_type, tag, path): |
| 27 cache_key_name = '/res/%s/%s/%s' % (tag_type, tag, path) | |
| 28 content = cache.get_content(cache_key_name) | |
| 29 helper = config.ConfigHelper(current_config) | 54 helper = config.ConfigHelper(current_config) |
| 55 content = None |
| 30 | 56 |
| 31 if not content: | 57 if tag_type == 'file': |
| 32 if tag_type == 'rev': | 58 file_hash = self.get_file_hash(helper, tag, path) |
| 33 meta_file_name = helper.get_revision_path(tag) | 59 if not file_hash: |
| 34 elif tag_type =='ver': | |
| 35 meta_file_name = helper.get_version_path(tag) | |
| 36 else: | |
| 37 self.abort(404) | 60 self.abort(404) |
| 38 | 61 content = file_reader.read(helper, helper.get_hash_path(file_hash)) |
| 39 meta_content = file_reader.read(helper, meta_file_name) | |
| 40 if meta_content: | |
| 41 zip_file_name = meta_content.strip(' \t\n') | |
| 42 else: | |
| 43 self.abort(404) | |
| 44 | |
| 45 content = zip_proxy.read(helper, zip_file_name, path) | |
| 46 if not content: | 62 if not content: |
| 47 self.abort(404) | 63 self.abort(404) |
| 48 content = content_processor.process(path, content) | 64 content = content_processor.process(path, content) |
| 49 cache.set_content(cache_key_name, content) | 65 |
| 66 else: |
| 67 cache_key_name = '/res/%s/%s/%s' % (tag_type, tag, path) |
| 68 content = cache.get_content(cache_key_name) |
| 69 if not content: |
| 70 if tag_type == 'rev': |
| 71 meta_file_name = helper.get_revision_path(tag) |
| 72 elif tag_type =='ver': |
| 73 meta_file_name = helper.get_version_path(tag) |
| 74 else: |
| 75 self.abort(404) |
| 76 |
| 77 meta_content = file_reader.read(helper, meta_file_name) |
| 78 if meta_content: |
| 79 zip_file_name = meta_content.strip(' \t\n') |
| 80 else: |
| 81 self.abort(404) |
| 82 |
| 83 content = zip_proxy.read(helper, zip_file_name, path) |
| 84 if not content: |
| 85 self.abort(404) |
| 86 content = content_processor.process(path, content) |
| 87 cache.set_content(cache_key_name, content) |
| 50 | 88 |
| 51 self.response.headers['Content-Type'] = content_type.from_path(path) | 89 self.response.headers['Content-Type'] = content_type.from_path(path) |
| 52 self.response.headers['Access-Control-Allow-Origin'] = '*' | 90 self.response.headers['Access-Control-Allow-Origin'] = '*' |
| 53 cache_control = 'public, max-age=%d' % helper.get_max_age() | 91 cache_control = 'public, max-age=%d' % helper.get_max_age() |
| 54 self.response.headers['Cache-Control'] = cache_control | 92 self.response.headers['Cache-Control'] = cache_control |
| 55 self.response.body = content | 93 self.response.body = content |
| 56 | 94 |
| 57 class LegacyStatic(Server): | 95 class LegacyStatic(Server): |
| 58 def get(self, version, path): | 96 def get(self, version, path): |
| 59 # Map x.x.x.x -> x.x.x.0 to deal with patched releases | 97 # Map x.x.x.x -> x.x.x.0 to deal with patched releases |
| 60 match = re.search('(\d+\.\d+\.\d+\.)\d+', version) | 98 match = re.search('(\d+\.\d+\.\d+\.)\d+', version) |
| 61 if match: | 99 if match: |
| 62 return super(LegacyStatic, self).get('ver', match.group(1) + '0', path) | 100 return super(LegacyStatic, self).get('ver', match.group(1) + '0', path) |
| 63 else: | 101 else: |
| 64 self.abort(404) | 102 self.abort(404) |
| 65 | 103 |
| 66 app = webapp2.WSGIApplication( | 104 app = webapp2.WSGIApplication( |
| 67 [('/', MainPage), | 105 [('/', MainPage), |
| 68 ('/precache_url', pre_cacher.PreCacherFromUrl), | 106 ('/precache_url', pre_cacher.PreCacherFromUrl), |
| 69 webapp2.Route('/serve_<tag_type>/<tag>/<path:.*>', Server), | 107 webapp2.Route('/serve_<tag_type>/<tag>/<path:.*>', Server), |
| 70 webapp2.Route('/static/<version>/<path:.*>', LegacyStatic)], | 108 webapp2.Route('/static/<version>/<path:.*>', LegacyStatic)], |
| 71 debug=config.IS_DEV_APP_SERVER) | 109 debug=config.IS_DEV_APP_SERVER) |
| OLD | NEW |