changeset 6945:2cfdabe235fb

hgweb: return content iterator instead of using write() callable This is a new version of 4879468fa28f (which was backed out in 943f066c0d58), with an extra line removed to fix problems with hg serve. hg's internal web server contains checking if the app isn't trying to write more bytes than specified by the Content-Length header. The first try still contained an old line that wrote the response, so the response was sent twice.
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Sat, 30 Aug 2008 17:13:23 +0200
parents 7e5f3480c45b
children 0061498f8ab8
files mercurial/hgweb/hgweb_mod.py mercurial/hgweb/hgwebdir_mod.py tests/test-hgweb-no-path-info tests/test-hgweb-no-path-info.out tests/test-hgweb-no-request-uri tests/test-hgweb-no-request-uri.out
diffstat 6 files changed, 48 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/hgweb/hgweb_mod.py	Fri Aug 29 16:50:11 2008 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Sat Aug 30 17:13:23 2008 +0200
@@ -174,24 +174,20 @@
                 content = getattr(webcommands, cmd)(self, req, tmpl)
                 req.respond(HTTP_OK, ctype)
 
-            req.write(content)
-            return []
+            return ''.join(content),
 
         except revlog.LookupError, err:
             req.respond(HTTP_NOT_FOUND, ctype)
             msg = str(err)
             if 'manifest' not in msg:
                 msg = 'revision not found: %s' % err.name
-            req.write(tmpl('error', error=msg))
-            return []
+            return ''.join(tmpl('error', error=msg)),
         except (RepoError, revlog.RevlogError), inst:
             req.respond(HTTP_SERVER_ERROR, ctype)
-            req.write(tmpl('error', error=str(inst)))
-            return []
+            return ''.join(tmpl('error', error=str(inst))),
         except ErrorResponse, inst:
             req.respond(inst.code, ctype)
-            req.write(tmpl('error', error=inst.message))
-            return []
+            return ''.join(tmpl('error', error=inst.message)),
 
     def templater(self, req):
 
--- a/mercurial/hgweb/hgwebdir_mod.py	Fri Aug 29 16:50:11 2008 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Sat Aug 30 17:13:23 2008 +0200
@@ -89,14 +89,12 @@
                         fname = virtual[7:]
                     else:
                         fname = req.form['static'][0]
-                    req.write(staticfile(static, fname, req))
-                    return []
+                    return staticfile(static, fname, req)
 
                 # top-level index
                 elif not virtual:
                     req.respond(HTTP_OK, ctype)
-                    req.write(self.makeindex(req, tmpl))
-                    return []
+                    return ''.join(self.makeindex(req, tmpl)),
 
                 # nested indexes and hgwebs
 
@@ -118,8 +116,7 @@
                     subdir = virtual + '/'
                     if [r for r in repos if r.startswith(subdir)]:
                         req.respond(HTTP_OK, ctype)
-                        req.write(self.makeindex(req, tmpl, subdir))
-                        return []
+                        return ''.join(self.makeindex(req, tmpl, subdir)),
 
                     up = virtual.rfind('/')
                     if up < 0:
@@ -128,13 +125,11 @@
 
                 # prefixes not found
                 req.respond(HTTP_NOT_FOUND, ctype)
-                req.write(tmpl("notfound", repo=virtual))
-                return []
+                return ''.join(tmpl("notfound", repo=virtual)),
 
             except ErrorResponse, err:
                 req.respond(err.code, ctype)
-                req.write(tmpl('error', error=err.message or ''))
-                return []
+                return ''.join(tmpl('error', error=err.message or '')),
         finally:
             tmpl = None
 
--- a/tests/test-hgweb-no-path-info	Fri Aug 29 16:50:11 2008 +0200
+++ b/tests/test-hgweb-no-path-info	Sat Aug 30 17:13:23 2008 +0200
@@ -41,19 +41,20 @@
 	'SERVER_PROTOCOL': 'HTTP/1.0'
 }
 
+def process(app):
+    content = app(env, startrsp)
+    sys.stdout.write(output.getvalue())
+    sys.stdout.write(''.join(content))
+    print '---- ERRORS'
+    print errors.getvalue()
+
 output = StringIO()
 env['QUERY_STRING'] = 'style=atom'
-hgweb('.', name = 'repo')(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgweb('.', name='repo'))
 
 output = StringIO()
 env['QUERY_STRING'] = 'style=raw'
-hgwebdir({'repo': '.'})(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgwebdir({'repo': '.'}))
 EOF
 
 python request.py | sed "s/http:\/\/127\.0\.0\.1:[0-9]*\//http:\/\/127.0.0.1\//"
--- a/tests/test-hgweb-no-path-info.out	Fri Aug 29 16:50:11 2008 +0200
+++ b/tests/test-hgweb-no-path-info.out	Sat Aug 30 17:13:23 2008 +0200
@@ -35,7 +35,6 @@
  </entry>
 
 </feed>
-
 ---- ERRORS
 
 ---- HEADERS
@@ -45,6 +44,5 @@
 
 repo/
 
-
 ---- ERRORS
 
--- a/tests/test-hgweb-no-request-uri	Fri Aug 29 16:50:11 2008 +0200
+++ b/tests/test-hgweb-no-request-uri	Sat Aug 30 17:13:23 2008 +0200
@@ -41,37 +41,33 @@
 	'SERVER_PROTOCOL': 'HTTP/1.0'
 }
 
+def process(app):
+	content = app(env, startrsp)
+	sys.stdout.write(output.getvalue())
+	sys.stdout.write(''.join(content))
+	print '---- ERRORS'
+	print errors.getvalue()
+	
+
 output = StringIO()
 env['PATH_INFO'] = '/'
 env['QUERY_STRING'] = 'style=atom'
-hgweb('.', name = 'repo')(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgweb('.', name = 'repo'))
 
 output = StringIO()
 env['PATH_INFO'] = '/file/tip/'
 env['QUERY_STRING'] = 'style=raw'
-hgweb('.', name = 'repo')(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgweb('.', name = 'repo'))
 
 output = StringIO()
 env['PATH_INFO'] = '/'
 env['QUERY_STRING'] = 'style=raw'
-hgwebdir({'repo': '.'})(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgwebdir({'repo': '.'}))
 
 output = StringIO()
 env['PATH_INFO'] = '/repo/file/tip/'
 env['QUERY_STRING'] = 'style=raw'
-hgwebdir({'repo': '.'})(env, startrsp)
-print output.getvalue()
-print '---- ERRORS'
-print errors.getvalue()
+process(hgwebdir({'repo': '.'}))
 EOF
 
 python request.py | sed "s/http:\/\/127\.0\.0\.1:[0-9]*\//http:\/\/127.0.0.1\//"
--- a/tests/test-hgweb-no-request-uri.out	Fri Aug 29 16:50:11 2008 +0200
+++ b/tests/test-hgweb-no-request-uri.out	Sat Aug 30 17:13:23 2008 +0200
@@ -35,6 +35,24 @@
  </entry>
 
 </feed>
+---- ERRORS
+
+---- HEADERS
+200 Script output follows
+---- DATA
+[('Content-Type', 'text/plain; charset=ascii')]
+
+-rw-r--r-- 4 bar
+
+
+---- ERRORS
+
+---- HEADERS
+200 Script output follows
+---- DATA
+[('Content-Type', 'text/plain; charset=ascii')]
+
+/repo/
 
 ---- ERRORS
 
@@ -46,27 +64,5 @@
 -rw-r--r-- 4 bar
 
 
-
 ---- ERRORS
 
----- HEADERS
-200 Script output follows
----- DATA
-[('Content-Type', 'text/plain; charset=ascii')]
-
-/repo/
-
-
----- ERRORS
-
----- HEADERS
-200 Script output follows
----- DATA
-[('Content-Type', 'text/plain; charset=ascii')]
-
--rw-r--r-- 4 bar
-
-
-
----- ERRORS
-