##// END OF EJS Templates
CGI compatibility fix for d74fc8dec2b4.
Dirkjan Ochtman -
r5577:e0173902 default
parent child Browse files
Show More
@@ -0,0 +1,89 b''
1 #!/bin/sh
2
3 hg init test
4
5 cat >hgweb.cgi <<HGWEB
6 #!/usr/bin/env python
7 #
8 # An example CGI script to use hgweb, edit as necessary
9
10 import cgitb
11 cgitb.enable()
12
13 from mercurial import demandimport; demandimport.enable()
14 from mercurial.hgweb import hgweb
15 from mercurial.hgweb import wsgicgi
16 from mercurial.hgweb.request import wsgiapplication
17
18 def make_web_app():
19 return hgweb("test", "Empty test repository")
20
21 wsgicgi.launch(wsgiapplication(make_web_app))
22 HGWEB
23 chmod 755 hgweb.cgi
24
25 cat >hgweb.config <<HGWEBDIRCONF
26 [paths]
27 test = test
28 HGWEBDIRCONF
29
30 cat >hgwebdir.cgi <<HGWEBDIR
31 #!/usr/bin/env python
32 #
33 # An example CGI script to export multiple hgweb repos, edit as necessary
34
35 import cgitb
36 cgitb.enable()
37
38 from mercurial import demandimport; demandimport.enable()
39 from mercurial.hgweb import hgwebdir
40 from mercurial.hgweb import wsgicgi
41 from mercurial.hgweb.request import wsgiapplication
42
43 def make_web_app():
44 return hgwebdir("hgweb.config")
45
46 wsgicgi.launch(wsgiapplication(make_web_app))
47 HGWEBDIR
48 chmod 755 hgwebdir.cgi
49
50 DOCUMENT_ROOT="/var/www/hg"; export DOCUMENT_ROOT
51 GATEWAY_INTERFACE="CGI/1.1"; export GATEWAY_INTERFACE
52 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
53 HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"; export HTTP_ACCEPT_CHARSET
54 HTTP_ACCEPT_ENCODING="gzip,deflate"; export HTTP_ACCEPT_ENCODING
55 HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"; export HTTP_ACCEPT_LANGUAGE
56 HTTP_CACHE_CONTROL="max-age=0"; export HTTP_CACHE_CONTROL
57 HTTP_CONNECTION="keep-alive"; export HTTP_CONNECTION
58 HTTP_HOST="hg.omnifarious.org"; export HTTP_HOST
59 HTTP_KEEP_ALIVE="300"; export HTTP_KEEP_ALIVE
60 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
61 PATH_INFO="/"; export PATH_INFO
62 PATH_TRANSLATED="/var/www/hg/index.html"; export PATH_TRANSLATED
63 QUERY_STRING=""; export QUERY_STRING
64 REMOTE_ADDR="127.0.0.2"; export REMOTE_ADDR
65 REMOTE_PORT="44703"; export REMOTE_PORT
66 REQUEST_METHOD="GET"; export REQUEST_METHOD
67 REQUEST_URI="/test/"; export REQUEST_URI
68 SCRIPT_FILENAME="/home/hopper/hg_public/test.cgi"; export SCRIPT_FILENAME
69 SCRIPT_NAME="/test"; export SCRIPT_NAME
70 SCRIPT_URI="http://hg.omnifarious.org/test/"; export SCRIPT_URI
71 SCRIPT_URL="/test/"; export SCRIPT_URL
72 SERVER_ADDR="127.0.0.1"; export SERVER_ADDR
73 SERVER_ADMIN="eric@localhost"; export SERVER_ADMIN
74 SERVER_NAME="hg.omnifarious.org"; export SERVER_NAME
75 SERVER_PORT="80"; export SERVER_PORT
76 SERVER_PROTOCOL="HTTP/1.1"; export SERVER_PROTOCOL
77 SERVER_SIGNATURE="<address>Apache/2.0.53 (Fedora) Server at hg.omnifarious.org Port 80</address>\; export SERVER_SIGNATURE
78 "
79 SERVER_SOFTWARE="Apache/2.0.53 (Fedora)"; export SERVER_SOFTWARE
80 python hgweb.cgi >page1 2>&1 ; echo $?
81 python hgwebdir.cgi >page2 2>&1 ; echo $?
82 PATH_INFO="/test/"
83 PATH_TRANSLATED="/var/something/test.cgi"
84 REQUEST_URI="/test/test/"
85 SCRIPT_URI="http://hg.omnifarious.org/test/test/"
86 SCRIPT_URL="/test/test/"
87 python hgwebdir.cgi >page3 2>&1 ; echo $?
88 fgrep -i error page1 page2 page3 && exit 1
89 exit 0
@@ -0,0 +1,3 b''
1 0
2 0
3 0
@@ -0,0 +1,83 b''
1 #!/bin/sh
2
3 hg init test
4
5 cat >hgweb.cgi <<HGWEB
6 #!/usr/bin/env python
7 #
8 # An example CGI script to use hgweb, edit as necessary
9
10 import cgitb
11 cgitb.enable()
12
13 from mercurial import demandimport; demandimport.enable()
14 from mercurial.hgweb import hgweb
15 from mercurial.hgweb import wsgicgi
16
17 application = hgweb("test", "Empty test repository")
18 wsgicgi.launch(application)
19 HGWEB
20 chmod 755 hgweb.cgi
21
22 cat >hgweb.config <<HGWEBDIRCONF
23 [paths]
24 test = test
25 HGWEBDIRCONF
26
27 cat >hgwebdir.cgi <<HGWEBDIR
28 #!/usr/bin/env python
29 #
30 # An example CGI script to export multiple hgweb repos, edit as necessary
31
32 import cgitb
33 cgitb.enable()
34
35 from mercurial import demandimport; demandimport.enable()
36 from mercurial.hgweb import hgwebdir
37 from mercurial.hgweb import wsgicgi
38
39 application = hgwebdir("hgweb.config")
40 wsgicgi.launch(application)
41 HGWEBDIR
42 chmod 755 hgwebdir.cgi
43
44 DOCUMENT_ROOT="/var/www/hg"; export DOCUMENT_ROOT
45 GATEWAY_INTERFACE="CGI/1.1"; export GATEWAY_INTERFACE
46 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
47 HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7"; export HTTP_ACCEPT_CHARSET
48 HTTP_ACCEPT_ENCODING="gzip,deflate"; export HTTP_ACCEPT_ENCODING
49 HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5"; export HTTP_ACCEPT_LANGUAGE
50 HTTP_CACHE_CONTROL="max-age=0"; export HTTP_CACHE_CONTROL
51 HTTP_CONNECTION="keep-alive"; export HTTP_CONNECTION
52 HTTP_HOST="hg.omnifarious.org"; export HTTP_HOST
53 HTTP_KEEP_ALIVE="300"; export HTTP_KEEP_ALIVE
54 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
55 PATH_INFO="/"; export PATH_INFO
56 PATH_TRANSLATED="/var/www/hg/index.html"; export PATH_TRANSLATED
57 QUERY_STRING=""; export QUERY_STRING
58 REMOTE_ADDR="127.0.0.2"; export REMOTE_ADDR
59 REMOTE_PORT="44703"; export REMOTE_PORT
60 REQUEST_METHOD="GET"; export REQUEST_METHOD
61 REQUEST_URI="/test/"; export REQUEST_URI
62 SCRIPT_FILENAME="/home/hopper/hg_public/test.cgi"; export SCRIPT_FILENAME
63 SCRIPT_NAME="/test"; export SCRIPT_NAME
64 SCRIPT_URI="http://hg.omnifarious.org/test/"; export SCRIPT_URI
65 SCRIPT_URL="/test/"; export SCRIPT_URL
66 SERVER_ADDR="127.0.0.1"; export SERVER_ADDR
67 SERVER_ADMIN="eric@localhost"; export SERVER_ADMIN
68 SERVER_NAME="hg.omnifarious.org"; export SERVER_NAME
69 SERVER_PORT="80"; export SERVER_PORT
70 SERVER_PROTOCOL="HTTP/1.1"; export SERVER_PROTOCOL
71 SERVER_SIGNATURE="<address>Apache/2.0.53 (Fedora) Server at hg.omnifarious.org Port 80</address>\; export SERVER_SIGNATURE
72 "
73 SERVER_SOFTWARE="Apache/2.0.53 (Fedora)"; export SERVER_SOFTWARE
74 python hgweb.cgi >page1 2>&1 ; echo $?
75 python hgwebdir.cgi >page2 2>&1 ; echo $?
76 PATH_INFO="/test/"
77 PATH_TRANSLATED="/var/something/test.cgi"
78 REQUEST_URI="/test/test/"
79 SCRIPT_URI="http://hg.omnifarious.org/test/test/"
80 SCRIPT_URL="/test/test/"
81 python hgwebdir.cgi >page3 2>&1 ; echo $?
82 fgrep -i error page1 page2 page3 && exit 1
83 exit 0
@@ -0,0 +1,3 b''
1 0
2 0
3 0
@@ -1,73 +1,64 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['wsgi.input'] = sys.stdin
19 environ['wsgi.input'] = sys.stdin
20 environ['wsgi.errors'] = sys.stderr
20 environ['wsgi.errors'] = sys.stderr
21 environ['wsgi.version'] = (1, 0)
21 environ['wsgi.version'] = (1, 0)
22 environ['wsgi.multithread'] = False
22 environ['wsgi.multithread'] = False
23 environ['wsgi.multiprocess'] = True
23 environ['wsgi.multiprocess'] = True
24 environ['wsgi.run_once'] = True
24 environ['wsgi.run_once'] = True
25
25
26 if environ.get('HTTPS','off').lower() in ('on','1','yes'):
26 if environ.get('HTTPS','off').lower() in ('on','1','yes'):
27 environ['wsgi.url_scheme'] = 'https'
27 environ['wsgi.url_scheme'] = 'https'
28 else:
28 else:
29 environ['wsgi.url_scheme'] = 'http'
29 environ['wsgi.url_scheme'] = 'http'
30
30
31 headers_set = []
31 headers_set = []
32 headers_sent = []
32 headers_sent = []
33 out = sys.stdout
33 out = sys.stdout
34
34
35 def write(data):
35 def write(data):
36 if not headers_set:
36 if not headers_set:
37 raise AssertionError("write() before start_response()")
37 raise AssertionError("write() before start_response()")
38
38
39 elif not headers_sent:
39 elif not headers_sent:
40 # Before the first output, send the stored headers
40 # Before the first output, send the stored headers
41 status, response_headers = headers_sent[:] = headers_set
41 status, response_headers = headers_sent[:] = headers_set
42 out.write('Status: %s\r\n' % status)
42 out.write('Status: %s\r\n' % status)
43 for header in response_headers:
43 for header in response_headers:
44 out.write('%s: %s\r\n' % header)
44 out.write('%s: %s\r\n' % header)
45 out.write('\r\n')
45 out.write('\r\n')
46
46
47 out.write(data)
47 out.write(data)
48 out.flush()
48 out.flush()
49
49
50 def start_response(status, response_headers, exc_info=None):
50 def start_response(status, response_headers, exc_info=None):
51 if exc_info:
51 if exc_info:
52 try:
52 try:
53 if headers_sent:
53 if headers_sent:
54 # Re-raise original exception if headers sent
54 # Re-raise original exception if headers sent
55 raise exc_info[0], exc_info[1], exc_info[2]
55 raise exc_info[0], exc_info[1], exc_info[2]
56 finally:
56 finally:
57 exc_info = None # avoid dangling circular ref
57 exc_info = None # avoid dangling circular ref
58 elif headers_set:
58 elif headers_set:
59 raise AssertionError("Headers already set!")
59 raise AssertionError("Headers already set!")
60
60
61 headers_set[:] = [status, response_headers]
61 headers_set[:] = [status, response_headers]
62 return write
62 return write
63
63
64 result = application(environ, start_response)
64 application(environ, start_response)
65 try:
66 for data in result:
67 if data: # don't send headers until body appears
68 write(data)
69 if not headers_sent:
70 write('') # send headers now if body was empty
71 finally:
72 if hasattr(result,'close'):
73 result.close()
General Comments 0
You need to be logged in to leave comments. Login now