Merged WSGI fixes from http://hg.omnifarious.org/~hopper/webmerc/
authorThomas Arendsen Hein <thomas@intevation.de>
Fri, 30 Jun 2006 21:36:45 +0200
changeset 2539 8a8d9ada4528
parent 2531 7a90e0c77f43 (current diff)
parent 2538 f4b7d71c1c60 (diff)
child 2540 800a582e2405
Merged WSGI fixes from http://hg.omnifarious.org/~hopper/webmerc/
mercurial/templater.py
templates/map-raw
--- 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'):
--- 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:
--- 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([])
--- 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:
--- 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
--- /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())
--- /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
+#!/usr/bin/env python
+#
+# An example CGI script to use hgweb, edit as necessary
+
+import cgitb, os, sys
+cgitb.enable()
+
+# sys.path.insert(0, "/path/to/python/lib") # if not a system-wide install
+from mercurial import hgweb
+
+h = hgweb.hgweb("test", "Empty test repository")
+h.run()
+HGWEB
+chmod 755 hgweb.cgi
+
+cat >hgweb.config <<HGWEBDIRCONF
+[paths]
+test = test
+HGWEBDIRCONF
+
+cat >hgwebdir.cgi <<HGWEBDIR
+#!/usr/bin/env python
+#
+# An example CGI script to export multiple hgweb repos, edit as necessary
+
+import cgitb, sys
+cgitb.enable()
+
+# sys.path.insert(0, "/path/to/python/lib") # if not a system-wide install
+from mercurial import hgweb
+
+# The config file looks like this.  You can have paths to individual
+# repos, collections of repos in a directory tree, or both.
+#
+# [paths]
+# virtual/path = /real/path
+# virtual/path = /real/path
+#
+# [collections]
+# /prefix/to/strip/off = /root/of/tree/full/of/repos
+#
+# collections example: say directory tree /foo contains repos /foo/bar,
+# /foo/quux/baz.  Give this config section:
+#   [collections]
+#   /foo = /foo
+# Then repos will list as bar and quux/baz.
+
+# Alternatively you can pass a list of ('virtual/path', '/real/path') tuples
+# or use a dictionary with entries like 'virtual/path': '/real/path'
+
+h = hgweb.hgwebdir("hgweb.config")
+h.run()
+HGWEBDIR
+chmod 755 hgwebdir.cgi
+
+declare -x DOCUMENT_ROOT="/var/www/hg"
+declare -x GATEWAY_INTERFACE="CGI/1.1"
+declare -x HTTP_ACCEPT="text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
+declare -x HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
+declare -x HTTP_ACCEPT_ENCODING="gzip,deflate"
+declare -x HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"
+declare -x HTTP_CACHE_CONTROL="max-age=0"
+declare -x HTTP_CONNECTION="keep-alive"
+declare -x HTTP_HOST="hg.omnifarious.org"
+declare -x HTTP_KEEP_ALIVE="300"
+declare -x HTTP_USER_AGENT="Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.0.4) Gecko/20060608 Ubuntu/dapper-security Firefox/1.5.0.4"
+declare -x OLDPWD
+declare -x PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
+declare -x PATH_INFO="/"
+declare -x PATH_TRANSLATED="/var/www/hg/index.html"
+declare -x PWD="/home/hopper/hg_public"
+declare -x QUERY_STRING=""
+declare -x REMOTE_ADDR="127.0.0.2"
+declare -x REMOTE_PORT="44703"
+declare -x REQUEST_METHOD="GET"
+declare -x REQUEST_URI="/test/"
+declare -x SCRIPT_FILENAME="/home/hopper/hg_public/test.cgi"
+declare -x SCRIPT_NAME="/test"
+declare -x SCRIPT_URI="http://hg.omnifarious.org/test/"
+declare -x SCRIPT_URL="/test/"
+declare -x SERVER_ADDR="127.0.0.1"
+declare -x SERVER_ADMIN="eric@localhost"
+declare -x SERVER_NAME="hg.omnifarious.org"
+declare -x SERVER_PORT="80"
+declare -x SERVER_PROTOCOL="HTTP/1.1"
+declare -x SERVER_SIGNATURE="<address>Apache/2.0.53 (Fedora) Server at hg.omnifarious.org Port 80</address>\
+"
+declare -x SERVER_SOFTWARE="Apache/2.0.53 (Fedora)"
+./hgweb.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
--- /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
--- /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 <<ENDSOME
+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.
+ENDSOME
+hg add sometext.txt
+hg commit -d "1 0" -m "Just some text"
+hg serve -p 20059 -A access.log -E error.log -d --pid-file=hg.pid
+("$TESTDIR/get-with-headers.py" localhost:20059 '/?f=f165dc289438;file=sometext.txt;style=raw' content-type content-length content-disposition) >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'
--- /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 -