Skip to content

Commit

Permalink
issue viewvc#218: Move WSGI path envvar reinterpretation to sapi.Wsgi…
Browse files Browse the repository at this point in the history
…Server
  • Loading branch information
cmpilato committed Apr 15, 2020
1 parent b579bf1 commit 66348cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
13 changes: 0 additions & 13 deletions bin/wsgi/viewvc.wsgi
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,8 @@ else:
import sapi
import viewvc

def recode_latin1_path(path):
return path.encode('latin-1').decode('utf-8')

def application(environ, start_response):
server = sapi.WsgiServer(environ, start_response)

# PEP 3333 demands that PATH_INFO et al carry only latin-1 strings,
# so multibyte versioned path names arrive munged, with each byte
# being a character. But ViewVC generates it's own URLs from
# Unicode strings, where UTF-8 is used during URI-encoding. So we
# need to reinterpret path-carrying CGI environment variables as
# UTF-8 instead of as latin-1.
environ['PATH_INFO'] = recode_latin1_path(environ['PATH_INFO'])
environ['SCRIPT_NAME'] = recode_latin1_path(environ['SCRIPT_NAME'])

cfg = viewvc.load_config(CONF_PATHNAME, server)
viewvc.main(server, cfg)
return []
13 changes: 11 additions & 2 deletions lib/sapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,17 @@ def redirect(self, url):
self.start_response(status='301 Moved')
self._wsgi_write(redirect_notice(url))

def getenv(self, name, value=None):
return self._environ.get(name, value)
def getenv(self, name, default_value=None):
value = self._environ.get(name, default_value)
# PEP 3333 demands that PATH_INFO et al carry only latin-1
# strings, so multibyte versioned path names arrive munged, with
# each byte being a character. But ViewVC generates it's own URLs
# from Unicode strings, where UTF-8 is used during URI-encoding.
# So we need to reinterpret path-carrying CGI environment
# variables as UTF-8 instead of as latin-1.
if name in ['PATH_INFO', 'SCRIPT_NAME']:
value = value.encode('latin-1').decode('utf-8', errors='replace')
return value

def params(self):
return cgi.parse(environ=self._environ, fp=self._environ["wsgi.input"])
Expand Down

0 comments on commit 66348cf

Please sign in to comment.