author | Vadim Gelfer <vadim.gelfer@gmail.com> |
Tue, 20 Jun 2006 18:39:52 -0700 | |
changeset 2469 | 2e91ba371c4c |
parent 2466 | e10665147d26 |
child 2506 | d0db3462d568 |
permissions | -rw-r--r-- |
2391
d351a3be3371
Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents:
2355
diff
changeset
|
1 |
# hgweb/request.py - An http request from either CGI or the standalone server. |
131 | 2 |
# |
238
3b92f8fe47ae
hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents:
222
diff
changeset
|
3 |
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
575 | 4 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
131 | 5 |
# |
6 |
# This software may be used and distributed according to the terms |
|
7 |
# of the GNU General Public License, incorporated herein by reference. |
|
8 |
||
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
9 |
from mercurial.demandload import demandload |
2394
a8f1049d1d2d
hgweb: fix errors and warnings found by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2391
diff
changeset
|
10 |
demandload(globals(), "socket sys cgi os errno") |
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
11 |
from mercurial.i18n import gettext as _ |
138 | 12 |
|
1559
59b3639df0a9
Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents:
1554
diff
changeset
|
13 |
class hgrequest(object): |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
14 |
def __init__(self, inp=None, out=None, env=None): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
15 |
self.inp = inp or sys.stdin |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
16 |
self.out = out or sys.stdout |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
17 |
self.env = env or os.environ |
1407
db571bcaa35d
allow empty values for url so we can have /?tip
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1406
diff
changeset
|
18 |
self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
19 |
self.will_close = True |
131 | 20 |
|
2464
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
21 |
def read(self, count=-1): |
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
22 |
return self.inp.read(count) |
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
23 |
|
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
24 |
def write(self, *things): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
25 |
for thing in things: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
26 |
if hasattr(thing, "__iter__"): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
27 |
for part in thing: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
28 |
self.write(part) |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
29 |
else: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
30 |
try: |
1160
0da98529a476
Fix TypeError
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1159
diff
changeset
|
31 |
self.out.write(str(thing)) |
1174
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
32 |
except socket.error, inst: |
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
33 |
if inst[0] != errno.ECONNRESET: |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
34 |
raise |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
35 |
|
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
36 |
def done(self): |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
37 |
if self.will_close: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
38 |
self.inp.close() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
39 |
self.out.close() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
40 |
else: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
41 |
self.out.flush() |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
42 |
|
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
43 |
def header(self, headers=[('Content-type','text/html')]): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
44 |
for header in headers: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
45 |
self.out.write("%s: %s\r\n" % header) |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
46 |
self.out.write("\r\n") |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
47 |
|
2466
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
48 |
def httphdr(self, type, filename=None, length=0, headers={}): |
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
49 |
headers = headers.items() |
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
50 |
headers.append(('Content-type', type)) |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
51 |
if filename: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
52 |
headers.append(('Content-disposition', 'attachment; filename=%s' % |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
53 |
filename)) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
54 |
# we do not yet support http 1.1 chunked transfer, so we have |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
55 |
# to force connection to close if content-length not known |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
56 |
if length: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
57 |
headers.append(('Content-length', str(length))) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
58 |
self.will_close = False |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
59 |
else: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
60 |
headers.append(('Connection', 'close')) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
61 |
self.will_close = True |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
62 |
self.header(headers) |