##// END OF EJS Templates
hgweb: fix WSGI iterators handling in CGI adapter (issue1254)
Dirkjan Ochtman -
r6922:1ec2d227 default
parent child Browse files
Show More
@@ -0,0 +1,62 b''
1 #!/bin/sh
2 # This is a test of the wire protocol over CGI-based hgweb.
3
4 echo % initialize repository
5 hg init test
6 cd test
7 echo a > a
8 hg ci -Ama
9 cd ..
10
11 cat >hgweb.cgi <<HGWEB
12 #!/usr/bin/env python
13 #
14 # An example CGI script to use hgweb, edit as necessary
15
16 import cgitb
17 cgitb.enable()
18
19 from mercurial import demandimport; demandimport.enable()
20 from mercurial.hgweb import hgweb
21 from mercurial.hgweb import wsgicgi
22
23 application = hgweb("test", "Empty test repository")
24 wsgicgi.launch(application)
25 HGWEB
26 chmod 755 hgweb.cgi
27
28 DOCUMENT_ROOT="/var/www/hg"; export DOCUMENT_ROOT
29 GATEWAY_INTERFACE="CGI/1.1"; export GATEWAY_INTERFACE
30 HTTP_ACCEPT="text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; export HTTP_ACCEPT
31 HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"; export HTTP_ACCEPT_CHARSET
32 HTTP_ACCEPT_ENCODING="gzip,deflate"; export HTTP_ACCEPT_ENCODING
33 HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"; export HTTP_ACCEPT_LANGUAGE
34 HTTP_CACHE_CONTROL="max-age=0"; export HTTP_CACHE_CONTROL
35 HTTP_CONNECTION="keep-alive"; export HTTP_CONNECTION
36 HTTP_HOST="hg.omnifarious.org"; export HTTP_HOST
37 HTTP_KEEP_ALIVE="300"; export HTTP_KEEP_ALIVE
38 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"; export HTTP_USER_AGENT
39 PATH_INFO="/"; export PATH_INFO
40 PATH_TRANSLATED="/var/www/hg/index.html"; export PATH_TRANSLATED
41 REMOTE_ADDR="127.0.0.2"; export REMOTE_ADDR
42 REMOTE_PORT="44703"; export REMOTE_PORT
43 REQUEST_METHOD="GET"; export REQUEST_METHOD
44 REQUEST_URI="/test/"; export REQUEST_URI
45 SCRIPT_FILENAME="/home/hopper/hg_public/test.cgi"; export SCRIPT_FILENAME
46 SCRIPT_NAME="/test"; export SCRIPT_NAME
47 SCRIPT_URI="http://hg.omnifarious.org/test/"; export SCRIPT_URI
48 SCRIPT_URL="/test/"; export SCRIPT_URL
49 SERVER_ADDR="127.0.0.1"; export SERVER_ADDR
50 SERVER_ADMIN="eric@localhost"; export SERVER_ADMIN
51 SERVER_NAME="hg.omnifarious.org"; export SERVER_NAME
52 SERVER_PORT="80"; export SERVER_PORT
53 SERVER_PROTOCOL="HTTP/1.1"; export SERVER_PROTOCOL
54 SERVER_SIGNATURE="<address>Apache/2.0.53 (Fedora) Server at hg.omnifarious.org Port 80</address>"; export SERVER_SIGNATURE
55 SERVER_SOFTWARE="Apache/2.0.53 (Fedora)"; export SERVER_SOFTWARE
56
57 echo % try hgweb request
58 QUERY_STRING="cmd=changegroup"; export QUERY_STRING
59 python hgweb.cgi >page1 2>&1 ; echo $?
60 python "$TESTDIR/md5sum.py" page1
61
62 exit 0
@@ -0,0 +1,5 b''
1 % initialize repository
2 adding a
3 % try hgweb request
4 0
5 54086fe9a47b47d83204f38bda0b90c2 page1
@@ -1,65 +1,67 b''
1 # hgweb/wsgicgi.py - CGI->WSGI translator
1 # hgweb/wsgicgi.py - CGI->WSGI translator
2 #
2 #
3 # Copyright 2006 Eric Hopper <hopper@omnifarious.org>
3 # Copyright 2006 Eric Hopper <hopper@omnifarious.org>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7 #
7 #
8 # This was originally copied from the public domain code at
8 # This was originally copied from the public domain code at
9 # http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
9 # http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
10
10
11 import os, sys
11 import os, sys
12 from mercurial import util
12 from mercurial import util
13
13
14 def launch(application):
14 def launch(application):
15 util.set_binary(sys.stdin)
15 util.set_binary(sys.stdin)
16 util.set_binary(sys.stdout)
16 util.set_binary(sys.stdout)
17
17
18 environ = dict(os.environ.items())
18 environ = dict(os.environ.items())
19 environ.setdefault('PATH_INFO', '')
19 environ.setdefault('PATH_INFO', '')
20 environ['wsgi.input'] = sys.stdin
20 environ['wsgi.input'] = sys.stdin
21 environ['wsgi.errors'] = sys.stderr
21 environ['wsgi.errors'] = sys.stderr
22 environ['wsgi.version'] = (1, 0)
22 environ['wsgi.version'] = (1, 0)
23 environ['wsgi.multithread'] = False
23 environ['wsgi.multithread'] = False
24 environ['wsgi.multiprocess'] = True
24 environ['wsgi.multiprocess'] = True
25 environ['wsgi.run_once'] = True
25 environ['wsgi.run_once'] = True
26
26
27 if environ.get('HTTPS','off').lower() in ('on','1','yes'):
27 if environ.get('HTTPS','off').lower() in ('on','1','yes'):
28 environ['wsgi.url_scheme'] = 'https'
28 environ['wsgi.url_scheme'] = 'https'
29 else:
29 else:
30 environ['wsgi.url_scheme'] = 'http'
30 environ['wsgi.url_scheme'] = 'http'
31
31
32 headers_set = []
32 headers_set = []
33 headers_sent = []
33 headers_sent = []
34 out = sys.stdout
34 out = sys.stdout
35
35
36 def write(data):
36 def write(data):
37 if not headers_set:
37 if not headers_set:
38 raise AssertionError("write() before start_response()")
38 raise AssertionError("write() before start_response()")
39
39
40 elif not headers_sent:
40 elif not headers_sent:
41 # Before the first output, send the stored headers
41 # Before the first output, send the stored headers
42 status, response_headers = headers_sent[:] = headers_set
42 status, response_headers = headers_sent[:] = headers_set
43 out.write('Status: %s\r\n' % status)
43 out.write('Status: %s\r\n' % status)
44 for header in response_headers:
44 for header in response_headers:
45 out.write('%s: %s\r\n' % header)
45 out.write('%s: %s\r\n' % header)
46 out.write('\r\n')
46 out.write('\r\n')
47
47
48 out.write(data)
48 out.write(data)
49 out.flush()
49 out.flush()
50
50
51 def start_response(status, response_headers, exc_info=None):
51 def start_response(status, response_headers, exc_info=None):
52 if exc_info:
52 if exc_info:
53 try:
53 try:
54 if headers_sent:
54 if headers_sent:
55 # Re-raise original exception if headers sent
55 # Re-raise original exception if headers sent
56 raise exc_info[0], exc_info[1], exc_info[2]
56 raise exc_info[0], exc_info[1], exc_info[2]
57 finally:
57 finally:
58 exc_info = None # avoid dangling circular ref
58 exc_info = None # avoid dangling circular ref
59 elif headers_set:
59 elif headers_set:
60 raise AssertionError("Headers already set!")
60 raise AssertionError("Headers already set!")
61
61
62 headers_set[:] = [status, response_headers]
62 headers_set[:] = [status, response_headers]
63 return write
63 return write
64
64
65 application(environ, start_response)
65 content = application(environ, start_response)
66 for chunk in content:
67 write(chunk)
General Comments 0
You need to be logged in to leave comments. Login now