D2742: hgweb: parse and store HTTP request headers

2018-03-09 Thread indygreg (Gregory Szorc)
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG29b477d7f334: hgweb: parse and store HTTP request headers 
(authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2742?vs=6748=6786

REVISION DETAIL
  https://phab.mercurial-scm.org/D2742

AFFECTED FILES
  mercurial/hgweb/hgweb_mod.py
  mercurial/hgweb/request.py

CHANGE DETAILS

diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py
--- a/mercurial/hgweb/request.py
+++ b/mercurial/hgweb/request.py
@@ -11,6 +11,7 @@
 import cgi
 import errno
 import socket
+import wsgiref.headers as wsgiheaders
 #import wsgiref.validate
 
 from .common import (
@@ -85,6 +86,9 @@
 querystringlist = attr.ib()
 # Dict of query string arguments. Values are lists with at least 1 item.
 querystringdict = attr.ib()
+# wsgiref.headers.Headers instance. Operates like a dict with case
+# insensitive keys.
+headers = attr.ib()
 
 def parserequestfromenv(env):
 """Parse URL components from environment variables.
@@ -186,15 +190,26 @@
 else:
 querystringdict[k] = [v]
 
+# HTTP_* keys contain HTTP request headers. The Headers structure should
+# perform case normalization for us. We just rewrite underscore to dash
+# so keys match what likely went over the wire.
+headers = []
+for k, v in env.iteritems():
+if k.startswith('HTTP_'):
+headers.append((k[len('HTTP_'):].replace('_', '-'), v))
+
+headers = wsgiheaders.Headers(headers)
+
 return parsedrequest(url=fullurl, baseurl=baseurl,
  advertisedurl=advertisedfullurl,
  advertisedbaseurl=advertisedbaseurl,
  apppath=apppath,
  dispatchparts=dispatchparts, 
dispatchpath=dispatchpath,
  havepathinfo='PATH_INFO' in env,
  querystring=querystring,
  querystringlist=querystringlist,
- querystringdict=querystringdict)
+ querystringdict=querystringdict,
+ headers=headers)
 
 class wsgirequest(object):
 """Higher-level API for a WSGI request.
diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -351,7 +351,7 @@
 if args:
 wsgireq.form['file'] = args
 
-ua = wsgireq.env.get('HTTP_USER_AGENT', '')
+ua = req.headers.get('User-Agent', '')
 if cmd == 'rev' and 'mercurial' in ua:
 wsgireq.form['style'] = ['raw']
 



To: indygreg, #hg-reviewers, durin42
Cc: mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


D2742: hgweb: parse and store HTTP request headers

2018-03-08 Thread indygreg (Gregory Szorc)
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  WSGI transmits HTTP request headers as HTTP_* environment variables.
  We teach our parser about these and hook up a dict-like data
  structure that supports case insensitive header manipulation.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2742

AFFECTED FILES
  mercurial/hgweb/hgweb_mod.py
  mercurial/hgweb/request.py

CHANGE DETAILS

diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py
--- a/mercurial/hgweb/request.py
+++ b/mercurial/hgweb/request.py
@@ -11,6 +11,7 @@
 import cgi
 import errno
 import socket
+import wsgiref.headers as wsgiheaders
 #import wsgiref.validate
 
 from .common import (
@@ -85,6 +86,9 @@
 querystringlist = attr.ib()
 # Dict of query string arguments. Values are lists with at least 1 item.
 querystringdict = attr.ib()
+# wsgiref.headers.Headers instance. Operates like a dict with case
+# insensitive keys.
+headers = attr.ib()
 
 def parserequestfromenv(env):
 """Parse URL components from environment variables.
@@ -186,15 +190,26 @@
 else:
 querystringdict[k] = [v]
 
+# HTTP_* keys contain HTTP request headers. The Headers structure should
+# perform case normalization for us. We just rewrite underscore to dash
+# so keys match what likely went over the wire.
+headers = []
+for k, v in env.iteritems():
+if k.startswith('HTTP_'):
+headers.append((k[len('HTTP_'):].replace('_', '-'), v))
+
+headers = wsgiheaders.Headers(headers)
+
 return parsedrequest(url=fullurl, baseurl=baseurl,
  advertisedurl=advertisedfullurl,
  advertisedbaseurl=advertisedbaseurl,
  apppath=apppath,
  dispatchparts=dispatchparts, 
dispatchpath=dispatchpath,
  havepathinfo='PATH_INFO' in env,
  querystring=querystring,
  querystringlist=querystringlist,
- querystringdict=querystringdict)
+ querystringdict=querystringdict,
+ headers=headers)
 
 class wsgirequest(object):
 """Higher-level API for a WSGI request.
diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -351,7 +351,7 @@
 if args:
 wsgireq.form['file'] = args
 
-ua = wsgireq.env.get('HTTP_USER_AGENT', '')
+ua = req.headers.get('User-Agent', '')
 if cmd == 'rev' and 'mercurial' in ua:
 wsgireq.form['style'] = ['raw']
 



To: indygreg, #hg-reviewers
Cc: mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel