hgweb: simplify wsgirequest header handling
authorMads Kiilerich <mads@kiilerich.com>
Tue, 15 Jan 2013 01:05:12 +0100
changeset 18348 764a758780b6
parent 18347 853221386f48
child 18349 c007e5c54b16
hgweb: simplify wsgirequest header handling Remove leaky header abstraction and prepare for other encodings.
mercurial/hgweb/request.py
mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/request.py	Tue Jan 15 01:05:12 2013 +0100
+++ b/mercurial/hgweb/request.py	Tue Jan 15 01:05:12 2013 +0100
@@ -72,15 +72,21 @@
 
     def respond(self, status, type, filename=None, length=None):
         if self._start_response is not None:
-
-            self.httphdr(type, filename, length)
+            self.headers.append(('Content-Type', type))
+            if filename:
+                filename = (filename.split('/')[-1]
+                            .replace('\\', '\\\\').replace('"', '\\"'))
+                self.headers.append(('Content-Disposition',
+                                     'inline; filename="%s"' % filename))
+            if length is not None:
+                self.headers.append(('Content-Length', str(length)))
 
             for k, v in self.headers:
                 if not isinstance(v, str):
-                    raise TypeError('header value must be string: %r' % v)
+                    raise TypeError('header value must be string: %r' % (v,))
 
             if isinstance(status, ErrorResponse):
-                self.header(status.headers)
+                self.headers.extend(status.headers)
                 if status.code == HTTP_NOT_MODIFIED:
                     # RFC 2616 Section 10.3.5: 304 Not Modified has cases where
                     # it MUST NOT include any headers other than these and no
@@ -120,21 +126,6 @@
     def close(self):
         return None
 
-    def header(self, headers=[('Content-Type','text/html')]):
-        self.headers.extend(headers)
-
-    def httphdr(self, type, filename=None, length=None, headers={}):
-        headers = headers.items()
-        headers.append(('Content-Type', type))
-        if filename:
-            filename = (filename.split('/')[-1]
-                        .replace('\\', '\\\\').replace('"', '\\"'))
-            headers.append(('Content-Disposition',
-                            'inline; filename="%s"' % filename))
-        if length is not None:
-            headers.append(('Content-Length', str(length)))
-        self.header(headers)
-
 def wsgiapplication(app_maker):
     '''For compatibility with old CGI scripts. A plain hgweb() or hgwebdir()
     can and should now be used as a WSGI application.'''
--- a/mercurial/hgweb/webcommands.py	Tue Jan 15 01:05:12 2013 +0100
+++ b/mercurial/hgweb/webcommands.py	Tue Jan 15 01:05:12 2013 +0100
@@ -804,7 +804,7 @@
         ]
     if encoding:
         headers.append(('Content-Encoding', encoding))
-    req.header(headers)
+    req.headers.extend(headers)
     req.respond(HTTP_OK, mimetype)
 
     ctx = webutil.changectx(web.repo, req)