comparison mercurial/hgweb/hgweb_mod.py @ 36808:0031e972ded2

hgweb: use the parsed application path directly Previously, we assigned a custom system string with a trailing slash to wsgirequest.url. The addition of the trailing slash felt arbitrary and seems to go against how things typically work in WSGI. We also want our URLs to be bytes, not system strings. And, assigning a custom attribute to wsgirequest felt wrong. This commit fixes all those things by removing the trailing slash from the app path, changing consumers to use that variable and to use it without a trailing slash, and removing the custom attribute from wsgirequest. We preserve the trailing slash on {url}. Also, makebreadcrumb strips the trailing slash. So no change to it was needed. Differential Revision: https://phab.mercurial-scm.org/D2736
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 08 Mar 2018 15:08:20 -0800
parents 1e2194e0ef62
children 886fba199022
comparison
equal deleted inserted replaced
36807:1e2194e0ef62 36808:0031e972ded2
146 # determine scheme, port and server name 146 # determine scheme, port and server name
147 # this is needed to create absolute urls 147 # this is needed to create absolute urls
148 logourl = self.config('web', 'logourl') 148 logourl = self.config('web', 'logourl')
149 logoimg = self.config('web', 'logoimg') 149 logoimg = self.config('web', 'logoimg')
150 staticurl = (self.config('web', 'staticurl') 150 staticurl = (self.config('web', 'staticurl')
151 or pycompat.sysbytes(wsgireq.url) + 'static/') 151 or req.apppath + '/static/')
152 if not staticurl.endswith('/'): 152 if not staticurl.endswith('/'):
153 staticurl += '/' 153 staticurl += '/'
154 154
155 # some functions for the templater 155 # some functions for the templater
156 156
168 sessionvars = webutil.sessionvars(vars, '?') 168 sessionvars = webutil.sessionvars(vars, '?')
169 169
170 if not self.reponame: 170 if not self.reponame:
171 self.reponame = (self.config('web', 'name', '') 171 self.reponame = (self.config('web', 'name', '')
172 or wsgireq.env.get('REPO_NAME') 172 or wsgireq.env.get('REPO_NAME')
173 or wsgireq.url.strip(r'/') or self.repo.root) 173 or req.apppath or self.repo.root)
174 174
175 def websubfilter(text): 175 def websubfilter(text):
176 return templatefilters.websub(text, self.websubtable) 176 return templatefilters.websub(text, self.websubtable)
177 177
178 # create the templater 178 # create the templater
179 # TODO: export all keywords: defaults = templatekw.keywords.copy() 179 # TODO: export all keywords: defaults = templatekw.keywords.copy()
180 defaults = { 180 defaults = {
181 'url': pycompat.sysbytes(wsgireq.url), 181 'url': req.apppath + '/',
182 'logourl': logourl, 182 'logourl': logourl,
183 'logoimg': logoimg, 183 'logoimg': logoimg,
184 'staticurl': staticurl, 184 'staticurl': staticurl,
185 'urlbase': req.advertisedbaseurl, 185 'urlbase': req.advertisedbaseurl,
186 'repo': self.reponame, 186 'repo': self.reponame,
187 'encoding': encoding.encoding, 187 'encoding': encoding.encoding,
188 'motd': motd, 188 'motd': motd,
189 'sessionvars': sessionvars, 189 'sessionvars': sessionvars,
190 'pathdef': makebreadcrumb(pycompat.sysbytes(wsgireq.url)), 190 'pathdef': makebreadcrumb(req.apppath),
191 'style': style, 191 'style': style,
192 'nonce': self.nonce, 192 'nonce': self.nonce,
193 } 193 }
194 tres = formatter.templateresources(self.repo.ui, self.repo) 194 tres = formatter.templateresources(self.repo.ui, self.repo)
195 tmpl = templater.templater.frommapfile(mapfile, 195 tmpl = templater.templater.frommapfile(mapfile,
315 # hgwebdir may have added CSP header. Since we generate our own, 315 # hgwebdir may have added CSP header. Since we generate our own,
316 # replace it. 316 # replace it.
317 wsgireq.headers = [h for h in wsgireq.headers 317 wsgireq.headers = [h for h in wsgireq.headers
318 if h[0] != 'Content-Security-Policy'] 318 if h[0] != 'Content-Security-Policy']
319 wsgireq.headers.append(('Content-Security-Policy', rctx.csp)) 319 wsgireq.headers.append(('Content-Security-Policy', rctx.csp))
320
321 wsgireq.url = pycompat.sysstr(req.apppath)
322 320
323 if r'PATH_INFO' in wsgireq.env: 321 if r'PATH_INFO' in wsgireq.env:
324 parts = wsgireq.env[r'PATH_INFO'].strip(r'/').split(r'/') 322 parts = wsgireq.env[r'PATH_INFO'].strip(r'/').split(r'/')
325 repo_parts = wsgireq.env.get(r'REPO_NAME', r'').split(r'/') 323 repo_parts = wsgireq.env.get(r'REPO_NAME', r'').split(r'/')
326 if parts[:len(repo_parts)] == repo_parts: 324 if parts[:len(repo_parts)] == repo_parts: