comparison mercurial/hgweb/common.py @ 45398:dc9fe90bdbd5

hgweb: let staticfile() look up path from default location unless provided This reduces duplication between the two callers. Differential Revision: https://phab.mercurial-scm.org/D8934
author Martin von Zweigbergk <martinvonz@google.com>
date Sat, 22 Aug 2020 16:03:44 -0700
parents 9a5c4875a88c
children ec2fc4d038c2
comparison
equal deleted inserted replaced
45397:da3b7c80aa34 45398:dc9fe90bdbd5
19 open, 19 open,
20 ) 20 )
21 from .. import ( 21 from .. import (
22 encoding, 22 encoding,
23 pycompat, 23 pycompat,
24 templater,
24 util, 25 util,
25 ) 26 )
26 27
27 httpserver = util.httpserver 28 httpserver = util.httpserver
28 29
176 return False 177 return False
177 178
178 return True 179 return True
179 180
180 181
181 def staticfile(directory, fname, res): 182 def staticfile(templatepath, directory, fname, res):
182 """return a file inside directory with guessed Content-Type header 183 """return a file inside directory with guessed Content-Type header
183 184
184 fname always uses '/' as directory separator and isn't allowed to 185 fname always uses '/' as directory separator and isn't allowed to
185 contain unusual path components. 186 contain unusual path components.
186 Content-Type is guessed using the mimetypes module. 187 Content-Type is guessed using the mimetypes module.
187 Return an empty string if fname is illegal or file not found. 188 Return an empty string if fname is illegal or file not found.
188 189
189 """ 190 """
190 if not ispathsafe(fname): 191 if not ispathsafe(fname):
191 return 192 return
193
194 if not directory:
195 tp = templatepath or templater.templatedir()
196 if tp is not None:
197 directory = os.path.join(tp, b'static')
192 198
193 fpath = os.path.join(*fname.split(b'/')) 199 fpath = os.path.join(*fname.split(b'/'))
194 path = os.path.join(directory, fpath) 200 path = os.path.join(directory, fpath)
195 try: 201 try:
196 os.stat(path) 202 os.stat(path)