hgweb: return content iterator instead of using write() callable

This is a new version of b6b16fec2a60 (which was backed out in faf83e1e40f5),
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.
This commit is contained in:
Dirkjan Ochtman 2008-08-30 17:13:23 +02:00
parent 5562ddda56
commit 0c1845ffb9
6 changed files with 30 additions and 48 deletions

View File

@ -174,24 +174,20 @@ class hgweb(object):
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):

View File

@ -89,14 +89,12 @@ class hgwebdir(object):
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 @@ class hgwebdir(object):
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 @@ class hgwebdir(object):
# 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

View File

@ -41,19 +41,20 @@ env = {
'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\//"

View File

@ -35,7 +35,6 @@ summary: test
</entry>
</feed>
---- ERRORS
---- HEADERS
@ -45,6 +44,5 @@ summary: test
repo/
---- ERRORS

View File

@ -41,37 +41,33 @@ env = {
'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\//"

View File

@ -35,7 +35,6 @@ summary: test
</entry>
</feed>
---- ERRORS
---- HEADERS
@ -46,7 +45,6 @@ summary: test
-rw-r--r-- 4 bar
---- ERRORS
---- HEADERS
@ -56,7 +54,6 @@ summary: test
/repo/
---- ERRORS
---- HEADERS
@ -67,6 +64,5 @@ summary: test
-rw-r--r-- 4 bar
---- ERRORS