# HG changeset patch # User Thomas Arendsen Hein # Date 1151696205 -7200 # Node ID 8a8d9ada45280c0024724232c794777952e671b0 # Parent 7a90e0c77f43f9f34232bd0c1493b9b90803a83f# Parent f4b7d71c1c6025d42890a4384fd4a4ed28ec930e Merged WSGI fixes from http://hg.omnifarious.org/~hopper/webmerc/ diff -r 7a90e0c77f43 -r 8a8d9ada4528 mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py Fri Jun 30 21:35:28 2006 +0200 +++ b/mercurial/hgweb/hgweb_mod.py Fri Jun 30 21:36:45 2006 +0200 @@ -650,13 +650,28 @@ raise Exception("suspicious path") return p - def run(self, req): + def run(self): + if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): + raise RuntimeError("This function is only intended to be called while running as a CGI script.") + import mercurial.hgweb.wsgicgi as wsgicgi + from request import wsgiapplication + def make_web_app(): + return self + wsgicgi.launch(wsgiapplication(make_web_app)) + + def run_wsgi(self, req): def header(**map): header_file = cStringIO.StringIO(''.join(self.t("header", **map))) msg = mimetools.Message(header_file, 0) req.header(msg.items()) yield header_file.read() + def rawfileheader(**map): + req.header([('Content-type', map['mimetype']), + ('Content-disposition', 'filename=%s' % map['file']), + ('Content-length', str(len(map['raw'])))]) + yield '' + def footer(**map): yield self.t("footer", motd=self.repo.ui.config("web", "motd", ""), @@ -714,6 +729,7 @@ "repo": self.reponame, "header": header, "footer": footer, + "rawfileheader": rawfileheader, }) if not req.form.has_key('cmd'): diff -r 7a90e0c77f43 -r 8a8d9ada4528 mercurial/hgweb/hgwebdir_mod.py --- a/mercurial/hgweb/hgwebdir_mod.py Fri Jun 30 21:35:28 2006 +0200 +++ b/mercurial/hgweb/hgwebdir_mod.py Fri Jun 30 21:36:45 2006 +0200 @@ -46,7 +46,16 @@ self.repos.append((name.lstrip(os.sep), repo)) self.repos.sort() - def run(self, req): + def run(self): + if not os.environ.get('GATEWAY_INTERFACE', '').startswith("CGI/1."): + raise RuntimeError("This function is only intended to be called while running as a CGI script.") + import mercurial.hgweb.wsgicgi as wsgicgi + from request import wsgiapplication + def make_web_app(): + return self + wsgicgi.launch(wsgiapplication(make_web_app)) + + def run_wsgi(self, req): def header(**map): header_file = cStringIO.StringIO(''.join(tmpl("header", **map))) msg = mimetools.Message(header_file, 0) @@ -124,7 +133,7 @@ real = dict(self.repos).get(virtual) if real: try: - hgweb(real).run(req) + hgweb(real).run_wsgi(req) except IOError, inst: req.write(tmpl("error", error=inst.strerror)) except hg.RepoError, inst: diff -r 7a90e0c77f43 -r 8a8d9ada4528 mercurial/hgweb/request.py --- a/mercurial/hgweb/request.py Fri Jun 30 21:35:28 2006 +0200 +++ b/mercurial/hgweb/request.py Fri Jun 30 21:36:45 2006 +0200 @@ -48,7 +48,7 @@ self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) self.start_response = start_response self.headers = [] - destination.run(self) + destination.run_wsgi(self) def __iter__(self): return iter([]) diff -r 7a90e0c77f43 -r 8a8d9ada4528 mercurial/templater.py --- a/mercurial/templater.py Fri Jun 30 21:35:28 2006 +0200 +++ b/mercurial/templater.py Fri Jun 30 21:36:45 2006 +0200 @@ -202,7 +202,7 @@ if para_re is None: para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) space_re = re.compile(r' +') - + def findparas(): start = 0 while True: diff -r 7a90e0c77f43 -r 8a8d9ada4528 templates/map-raw --- a/templates/map-raw Fri Jun 30 21:35:28 2006 +0200 +++ b/templates/map-raw Fri Jun 30 21:36:45 2006 +0200 @@ -8,7 +8,7 @@ changesetparent = '# Parent #node#' changesetchild = '# Child #node#' filenodelink = '' -filerevision = 'Content-Type: #mimetype#\nContent-Disposition: filename=#file#\n\n#raw#' +filerevision = '#rawfileheader##raw#' fileline = '#line#' diffblock = '#lines#' filediff = filediff-raw.tmpl diff -r 7a90e0c77f43 -r 8a8d9ada4528 tests/get-with-headers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/get-with-headers.py Fri Jun 30 21:36:45 2006 +0200 @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +__doc__ = """This does HTTP get requests given a host:port and path and returns +a subset of the headers plus the body of the result.""" + +import httplib, sys +headers = [h.lower() for h in sys.argv[3:]] +conn = httplib.HTTPConnection(sys.argv[1]) +conn.request("GET", sys.argv[2]) +response = conn.getresponse() +print response.status, response.reason +for h in headers: + if response.getheader(h, None) is not None: + print "%s: %s" % (h, response.getheader(h)) +print +sys.stdout.write(response.read()) diff -r 7a90e0c77f43 -r 8a8d9ada4528 tests/test-oldcgi --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-oldcgi Fri Jun 30 21:36:45 2006 +0200 @@ -0,0 +1,103 @@ +#!/bin/sh + +hg init test + +cat >hgweb.cgi <hgweb.config <hgwebdir.cgi <page1 2>&1 ; echo $? +./hgwebdir.cgi >page2 2>&1 ; echo $? +PATH_INFO="/test/" +PATH_TRANSLATED="/var/something/test.cgi" +REQUEST_URI="/test/test/" +SCRIPT_URI="http://hg.omnifarious.org/test/test/" +SCRIPT_URL="/test/test/" +./hgwebdir.cgi >page3 2>&1 ; echo $? +fgrep -i error page1 page2 page3 && exit 1 +exit 0 diff -r 7a90e0c77f43 -r 8a8d9ada4528 tests/test-oldcgi.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-oldcgi.out Fri Jun 30 21:36:45 2006 +0200 @@ -0,0 +1,3 @@ +0 +0 +0 diff -r 7a90e0c77f43 -r 8a8d9ada4528 tests/test-webraw --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-webraw Fri Jun 30 21:36:45 2006 +0200 @@ -0,0 +1,20 @@ +#!/bin/sh + +hg init test +cd test +cat >sometext.txt <getoutput.txt & + +sleep 5 +kill `cat hg.pid` +sleep 1 # wait for server to scream and die +cat getoutput.txt +cat access.log error.log | sed 's/^\([^[]*\[\)[^]]*\(\].*\)$/\1date\2/g' diff -r 7a90e0c77f43 -r 8a8d9ada4528 tests/test-webraw.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-webraw.out Fri Jun 30 21:36:45 2006 +0200 @@ -0,0 +1,10 @@ +200 Script output follows +content-type: text/plain +content-length: 157 +content-disposition: filename=sometext.txt + +This is just some random text +that will go inside the file and take a few lines. +It is very boring to read, but computers don't +care about things like that. +localhost - - [date] "GET /?f=f165dc289438;file=sometext.txt;style=raw HTTP/1.1" 200 -