codekingpro/portable-devtools
114k
1content_types = {2 'css': 'text/css',3 'gif': 'image/gif',4 'html': 'text/html',5 'jpg': 'image/jpeg',6 'js': 'application/javascript',7 'json': 'application/json',8 'png': 'image/png',9 'txt': 'text/plain',10}11 12 13def get_static_file(path, static_files):14 """Return the local filename and content type for the requested static15 file URL.16 17 :param path: the path portion of the requested URL.18 :param static_files: a static file configuration dictionary.19 20 This function returns a dictionary with two keys, "filename" and21 "content_type". If the requested URL does not match any static file, the22 return value is None.23 """24 extra_path = ''25 if path in static_files:26 f = static_files[path]27 else:28 f = None29 while path != '':30 path, last = path.rsplit('/', 1)31 extra_path = '/' + last + extra_path32 if path in static_files:33 f = static_files[path]34 break35 elif path + '/' in static_files:36 f = static_files[path + '/']37 break38 if f:39 if isinstance(f, str):40 f = {'filename': f}41 else:42 f = f.copy() # in case it is mutated below43 if f['filename'].endswith('/') and extra_path.startswith('/'):44 extra_path = extra_path[1:]45 f['filename'] += extra_path46 if f['filename'].endswith('/'):47 if '' in static_files:48 if isinstance(static_files[''], str):49 f['filename'] += static_files['']50 else:51 f['filename'] += static_files['']['filename']52 if 'content_type' in static_files['']:53 f['content_type'] = static_files['']['content_type']54 else:55 f['filename'] += 'index.html'56 if 'content_type' not in f:57 ext = f['filename'].rsplit('.')[-1]58 f['content_type'] = content_types.get(59 ext, 'application/octet-stream')60 return f61 