Mercurial > cpython-lru_cache-weakref
annotate Lib/http/server.py @ 60008:0dfd62076ded
Blocked revisions 79402,79406 via svnmerge
........
r79402 | benjamin.peterson | 2010-03-24 20:03:51 -0500 (Wed, 24 Mar 2010) | 1 line
set bsddb eol style
........
r79406 | benjamin.peterson | 2010-03-24 20:05:57 -0500 (Wed, 24 Mar 2010) | 1 line
fix eol properties on capsule files
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Thu, 25 Mar 2010 01:11:10 +0000 |
| parents | f8ddfced2a34 |
| children | e2a9f633d612 |
| rev | line source |
|---|---|
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1 """HTTP server classes. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
2 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
3 Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
4 SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST, |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
5 and CGIHTTPRequestHandler for CGI scripts. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
6 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
7 It does, however, optionally implement HTTP/1.1 persistent connections, |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
8 as of version 0.3. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
9 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
10 Notes on CGIHTTPRequestHandler |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
11 ------------------------------ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
12 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
13 This class implements GET and POST requests to cgi-bin scripts. |
| 2887 | 14 |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
15 If the os.fork() function is not present (e.g. on Windows), |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
16 subprocess.Popen() is used as a fallback, with slightly altered semantics. |
| 2887 | 17 |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
18 In all cases, the implementation is intentionally naive -- all |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
19 requests are executed synchronously. |
| 2887 | 20 |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
21 SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
22 -- it may execute arbitrary Python code or external programs. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
23 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
24 Note that status code 200 is sent prior to execution of a CGI script, so |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
25 scripts cannot send other status codes such as 302 (redirect). |
| 2887 | 26 |
| 27 XXX To do: | |
| 28 | |
| 29 - log requests even later (to capture byte count) | |
| 30 - log user-agent header and other interesting goodies | |
| 31 - send error log to separate file | |
| 32 """ | |
| 33 | |
| 34 | |
| 35 # See also: | |
| 36 # | |
| 37 # HTTP Working Group T. Berners-Lee | |
| 38 # INTERNET-DRAFT R. T. Fielding | |
| 39 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen | |
| 40 # Expires September 8, 1995 March 8, 1995 | |
| 41 # | |
| 42 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
43 # |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
44 # and |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
45 # |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
46 # Network Working Group R. Fielding |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
47 # Request for Comments: 2616 et al |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
48 # Obsoletes: 2068 June 1999 |
|
23075
b7e87732f6ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
22830
diff
changeset
|
49 # Category: Standards Track |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
50 # |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
51 # URL: http://www.faqs.org/rfcs/rfc2616.html |
| 2887 | 52 |
| 53 # Log files | |
| 54 # --------- | |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
55 # |
| 2887 | 56 # Here's a quote from the NCSA httpd docs about log file format. |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
57 # |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
58 # | The logfile format is as follows. Each line consists of: |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
59 # | |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
60 # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
61 # | |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
62 # | host: Either the DNS name or the IP number of the remote client |
| 2887 | 63 # | rfc931: Any information returned by identd for this person, |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
64 # | - otherwise. |
| 2887 | 65 # | authuser: If user sent a userid for authentication, the user name, |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
66 # | - otherwise. |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
67 # | DD: Day |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
68 # | Mon: Month (calendar name) |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
69 # | YYYY: Year |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
70 # | hh: hour (24-hour format, the machine's timezone) |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
71 # | mm: minutes |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
72 # | ss: seconds |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
73 # | request: The first line of the HTTP request as sent by the client. |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
74 # | ddd: the status code returned by the server, - if not available. |
| 2887 | 75 # | bbbb: the total number of bytes sent, |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
76 # | *not including the HTTP/1.0 header*, - if not available |
|
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
77 # | |
| 2887 | 78 # | You can determine the name of the file accessed through request. |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
79 # |
| 2887 | 80 # (Actually, the latter is only true if you know the server configuration |
| 81 # at the time the request was made!) | |
| 82 | |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
83 __version__ = "0.6" |
| 2887 | 84 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15860
diff
changeset
|
85 __all__ = ["HTTPServer", "BaseHTTPRequestHandler"] |
| 2887 | 86 |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
87 import cgi |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
88 import email.message |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
89 import email.parser |
| 52368 | 90 import http.client |
| 91 import io | |
| 92 import mimetypes | |
| 93 import os | |
| 94 import posixpath | |
| 95 import select | |
| 96 import shutil | |
| 97 import socket # For gethostbyaddr() | |
| 98 import socketserver | |
| 99 import sys | |
| 100 import time | |
| 101 import urllib.parse | |
| 2887 | 102 |
|
45460
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
103 # Default error message template |
| 2887 | 104 DEFAULT_ERROR_MESSAGE = """\ |
| 105 <head> | |
| 106 <title>Error response</title> | |
| 107 </head> | |
| 108 <body> | |
| 109 <h1>Error response</h1> | |
| 110 <p>Error code %(code)d. | |
| 111 <p>Message: %(message)s. | |
| 112 <p>Error code explanation: %(code)s = %(explain)s. | |
| 113 </body> | |
| 114 """ | |
| 115 | |
|
45460
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
116 DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8" |
|
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
117 |
|
35151
728a0de88395
backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
118 def _quote_html(html): |
|
728a0de88395
backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
119 return html.replace("&", "&").replace("<", "<").replace(">", ">") |
| 2887 | 120 |
|
46933
aca5c1d1c569
Renamed the SocketServer module to 'socketserver'.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45460
diff
changeset
|
121 class HTTPServer(socketserver.TCPServer): |
| 2887 | 122 |
|
12634
ddf986a971b5
Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents:
11409
diff
changeset
|
123 allow_reuse_address = 1 # Seems to make sense in testing environment |
|
ddf986a971b5
Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents:
11409
diff
changeset
|
124 |
| 2887 | 125 def server_bind(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
126 """Override server_bind to store the server name.""" |
|
46933
aca5c1d1c569
Renamed the SocketServer module to 'socketserver'.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45460
diff
changeset
|
127 socketserver.TCPServer.server_bind(self) |
|
29340
c69ad9227afd
Patch #744041: Use only first two elements of address to support IPv6.
Martin v. Löwis <martin@v.loewis.de>
parents:
28089
diff
changeset
|
128 host, port = self.socket.getsockname()[:2] |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12634
diff
changeset
|
129 self.server_name = socket.getfqdn(host) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
130 self.server_port = port |
| 2887 | 131 |
| 132 | |
|
46933
aca5c1d1c569
Renamed the SocketServer module to 'socketserver'.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45460
diff
changeset
|
133 class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): |
| 2887 | 134 |
| 135 """HTTP request handler base class. | |
| 136 | |
| 137 The following explanation of HTTP serves to guide you through the | |
| 138 code as well as to expose any misunderstandings I may have about | |
| 139 HTTP (so you don't need to read the code to figure out I'm wrong | |
| 140 :-). | |
| 141 | |
| 142 HTTP (HyperText Transfer Protocol) is an extensible protocol on | |
| 143 top of a reliable stream transport (e.g. TCP/IP). The protocol | |
| 144 recognizes three parts to a request: | |
| 145 | |
| 146 1. One line identifying the request type and path | |
| 147 2. An optional set of RFC-822-style headers | |
| 148 3. An optional data part | |
| 149 | |
| 150 The headers and data are separated by a blank line. | |
| 151 | |
| 152 The first line of the request has the form | |
| 153 | |
| 154 <command> <path> <version> | |
| 155 | |
| 156 where <command> is a (case-sensitive) keyword such as GET or POST, | |
| 157 <path> is a string containing path information for the request, | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
158 and <version> should be the string "HTTP/1.0" or "HTTP/1.1". |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
159 <path> is encoded using the URL encoding scheme (using %xx to signify |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
160 the ASCII character with hex code xx). |
| 2887 | 161 |
|
27637
601549c64039
Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents:
23608
diff
changeset
|
162 The specification specifies that lines are separated by CRLF but |
|
601549c64039
Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents:
23608
diff
changeset
|
163 for compatibility with the widest range of clients recommends |
|
601549c64039
Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents:
23608
diff
changeset
|
164 servers also handle LF. Similarly, whitespace in the request line |
|
601549c64039
Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents:
23608
diff
changeset
|
165 is treated sensibly (allowing multiple spaces between components |
|
601549c64039
Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents:
23608
diff
changeset
|
166 and allowing trailing whitespace). |
| 2887 | 167 |
| 168 Similarly, for output, lines ought to be separated by CRLF pairs | |
| 169 but most clients grok LF characters just fine. | |
| 170 | |
| 171 If the first line of the request has the form | |
| 172 | |
| 173 <command> <path> | |
| 174 | |
| 175 (i.e. <version> is left out) then this is assumed to be an HTTP | |
| 176 0.9 request; this form has no optional headers and data part and | |
| 177 the reply consists of just the data. | |
| 178 | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
179 The reply form of the HTTP 1.x protocol again has three parts: |
| 2887 | 180 |
| 181 1. One line giving the response code | |
| 182 2. An optional set of RFC-822-style headers | |
| 183 3. The data | |
| 184 | |
| 185 Again, the headers and data are separated by a blank line. | |
| 186 | |
| 187 The response code line has the form | |
| 188 | |
| 189 <version> <responsecode> <responsestring> | |
| 190 | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
191 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"), |
| 2887 | 192 <responsecode> is a 3-digit response code indicating success or |
| 193 failure of the request, and <responsestring> is an optional | |
| 194 human-readable string explaining what the response code means. | |
| 195 | |
| 196 This server parses the request and the headers, and then calls a | |
| 197 function specific to the request type (<command>). Specifically, | |
|
11287
f67ce152719a
Typo: the method called is do_SPAM, not handle_SPAM.
Guido van Rossum <guido@python.org>
parents:
10939
diff
changeset
|
198 a request SPAM will be handled by a method do_SPAM(). If no |
| 2887 | 199 such method exists the server sends an error response to the |
| 200 client. If it exists, it is called with no arguments: | |
| 201 | |
| 202 do_SPAM() | |
| 203 | |
| 204 Note that the request name is case sensitive (i.e. SPAM and spam | |
| 205 are different requests). | |
| 206 | |
| 207 The various request details are stored in instance variables: | |
| 208 | |
| 209 - client_address is the client IP address in the form (host, | |
| 210 port); | |
| 211 | |
| 212 - command, path and version are the broken-down request line; | |
| 213 | |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
214 - headers is an instance of email.message.Message (or a derived |
| 2887 | 215 class) containing the header information; |
| 216 | |
| 217 - rfile is a file object open for reading positioned at the | |
| 218 start of the optional input data part; | |
| 219 | |
| 220 - wfile is a file object open for writing. | |
| 221 | |
| 222 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING! | |
| 223 | |
| 224 The first thing to be written must be the response line. Then | |
| 225 follow 0 or more header lines, then a blank line, and then the | |
| 226 actual data (if any). The meaning of the header lines depends on | |
| 227 the command executed by the server; in most cases, when data is | |
| 228 returned, there should be at least one header line of the form | |
| 229 | |
| 230 Content-type: <type>/<subtype> | |
| 231 | |
| 232 where <type> and <subtype> should be registered MIME types, | |
| 233 e.g. "text/html" or "text/plain". | |
| 234 | |
| 235 """ | |
| 236 | |
| 237 # The Python system version, truncated to its first component. | |
|
16479
d59663d3e433
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
238 sys_version = "Python/" + sys.version.split()[0] |
| 2887 | 239 |
| 240 # The server software version. You may want to override this. | |
| 241 # The format is multiple whitespace-separated strings, | |
| 242 # where each string is of the form name[/version]. | |
| 243 server_version = "BaseHTTP/" + __version__ | |
| 244 | |
|
45460
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
245 error_message_format = DEFAULT_ERROR_MESSAGE |
|
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
246 error_content_type = DEFAULT_ERROR_CONTENT_TYPE |
|
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
247 |
|
47278
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
248 # The default request version. This only affects responses up until |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
249 # the point where the request line is parsed, so it mainly decides what |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
250 # the client gets back when sending a malformed request line. |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
251 # Most web servers default to HTTP 0.9, i.e. don't send a status line. |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
252 default_request_version = "HTTP/0.9" |
|
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
253 |
|
11409
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
254 def parse_request(self): |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
255 """Parse a request (internal). |
| 2887 | 256 |
|
29357
20894dbc7f6a
Fix SF bug #747348 docstring mistake in BaseHTTPServer.py. \n\nBackport Candidate
Raymond Hettinger <python@rcn.com>
parents:
29340
diff
changeset
|
257 The request should be stored in self.raw_requestline; the results |
|
11409
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
258 are in self.command, self.path, self.request_version and |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
259 self.headers. |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
260 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
261 Return True for success, False for failure; on failure, an |
|
11409
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
262 error is sent back. |
| 2887 | 263 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
264 """ |
|
27648
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
265 self.command = None # set in case of error on the first line |
|
47278
f762a9ab69dd
Merged revisions 63412,63445-63447,63449-63450,63452,63454,63459,63463,63465,63470,63483-63484,63496-63497,63499-63501,63530-63531,63540,63614 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46933
diff
changeset
|
266 self.request_version = version = self.default_request_version |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
267 self.close_connection = 1 |
|
42433
e319d2d285ba
Treat HTTP status line as ISO-8859-1 as in httplib module.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40775
diff
changeset
|
268 requestline = str(self.raw_requestline, 'iso-8859-1') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
269 if requestline[-2:] == '\r\n': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
270 requestline = requestline[:-2] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
271 elif requestline[-1:] == '\n': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
272 requestline = requestline[:-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
273 self.requestline = requestline |
|
16479
d59663d3e433
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16070
diff
changeset
|
274 words = requestline.split() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
275 if len(words) == 3: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
276 [command, path, version] = words |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
277 if version[:5] != 'HTTP/': |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30105
diff
changeset
|
278 self.send_error(400, "Bad request version (%r)" % version) |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
279 return False |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
280 try: |
|
27648
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
281 base_version_number = version.split('/', 1)[1] |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
282 version_number = base_version_number.split(".") |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
283 # RFC 2145 section 3.1 says there can be only one "." and |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
284 # - major and minor numbers MUST be treated as |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
285 # separate integers; |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
286 # - HTTP/2.4 is a lower version than HTTP/2.13, which in |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
287 # turn is lower than HTTP/12.3; |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
288 # - Leading zeros MUST be ignored by recipients. |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
289 if len(version_number) != 2: |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
290 raise ValueError |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
291 version_number = int(version_number[0]), int(version_number[1]) |
|
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
292 except (ValueError, IndexError): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30105
diff
changeset
|
293 self.send_error(400, "Bad request version (%r)" % version) |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
294 return False |
|
27648
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
295 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1": |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
296 self.close_connection = 0 |
|
27648
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
297 if version_number >= (2, 0): |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
298 self.send_error(505, |
|
27648
db784d29f481
[Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents:
27637
diff
changeset
|
299 "Invalid HTTP Version (%s)" % base_version_number) |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
300 return False |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
301 elif len(words) == 2: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
302 [command, path] = words |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
303 self.close_connection = 1 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
304 if command != 'GET': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
305 self.send_error(400, |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30105
diff
changeset
|
306 "Bad HTTP/0.9 request type (%r)" % command) |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
307 return False |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
308 elif not words: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
309 return False |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
310 else: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30105
diff
changeset
|
311 self.send_error(400, "Bad request syntax (%r)" % requestline) |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
312 return False |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
313 self.command, self.path, self.request_version = command, path, version |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
314 |
|
42720
eae10d9c9c4e
Fixes by John Reese and Jacques Frechet that make test_xmlrpc pass.
Guido van Rossum <guido@python.org>
parents:
42528
diff
changeset
|
315 # Examine the headers and look for a Connection directive. |
|
52369
8d6918af232b
Fix compatibility issue with HTTPMessage class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
52368
diff
changeset
|
316 self.headers = http.client.parse_headers(self.rfile, |
|
8d6918af232b
Fix compatibility issue with HTTPMessage class.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
52368
diff
changeset
|
317 _class=self.MessageClass) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
318 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
319 conntype = self.headers.get('Connection', "") |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
320 if conntype.lower() == 'close': |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
321 self.close_connection = 1 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
322 elif (conntype.lower() == 'keep-alive' and |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
323 self.protocol_version >= "HTTP/1.1"): |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
324 self.close_connection = 0 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22346
diff
changeset
|
325 return True |
|
11409
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
326 |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
327 def handle_one_request(self): |
|
11409
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
328 """Handle a single HTTP request. |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
329 |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
330 You normally don't need to override this method; see the class |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
331 __doc__ string for information on how to handle specific HTTP |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
332 commands such as GET and POST. |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
333 |
|
061156451d36
Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents:
11287
diff
changeset
|
334 """ |
|
55028
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
335 try: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
336 self.raw_requestline = self.rfile.readline() |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
337 if not self.raw_requestline: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
338 self.close_connection = 1 |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
339 return |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
340 if not self.parse_request(): |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
341 # An error code has been sent, just exit |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
342 return |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
343 mname = 'do_' + self.command |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
344 if not hasattr(self, mname): |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
345 self.send_error(501, "Unsupported method (%r)" % self.command) |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
346 return |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
347 method = getattr(self, mname) |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
348 method() |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
349 self.wfile.flush() #actually send the response if not already done. |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
350 except socket.timeout as e: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
351 #a read or a write timed out. Discard this connection |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54568
diff
changeset
|
352 self.log_error("Request timed out: %r", e) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
353 self.close_connection = 1 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
354 return |
| 2887 | 355 |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
356 def handle(self): |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
357 """Handle multiple requests if necessary.""" |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
358 self.close_connection = 1 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
359 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
360 self.handle_one_request() |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
361 while not self.close_connection: |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
362 self.handle_one_request() |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
363 |
| 2887 | 364 def send_error(self, code, message=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
365 """Send and log an error reply. |
| 2887 | 366 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
367 Arguments are the error code, and a detailed message. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
368 The detailed message defaults to the short entry matching the |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
369 response code. |
| 2887 | 370 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
371 This sends an error response (so it must be called before any |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
372 output has been generated), logs the error, and finally sends |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
373 a piece of HTML explaining the error to the user. |
| 2887 | 374 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
375 """ |
| 2887 | 376 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
377 try: |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40643
diff
changeset
|
378 shortmsg, longmsg = self.responses[code] |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
379 except KeyError: |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40643
diff
changeset
|
380 shortmsg, longmsg = '???', '???' |
|
23608
ec27ac19f9e3
Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents:
23075
diff
changeset
|
381 if message is None: |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40643
diff
changeset
|
382 message = shortmsg |
|
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40643
diff
changeset
|
383 explain = longmsg |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
384 self.log_error("code %d, message %s", code, message) |
|
35151
728a0de88395
backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
385 # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
386 content = (self.error_message_format % |
|
35151
728a0de88395
backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
387 {'code': code, 'message': _quote_html(message), 'explain': explain}) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
388 self.send_response(code, message) |
|
45460
58c3f3439670
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60964,60966-60967,60971,60977,60979-60989 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43717
diff
changeset
|
389 self.send_header("Content-Type", self.error_content_type) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
390 self.send_header('Connection', 'close') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
391 self.end_headers() |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
392 if self.command != 'HEAD' and code >= 200 and code not in (204, 304): |
|
43717
c1214bd6ed16
make sure to write bytes instead of strings to underlying socket channel
Bill Janssen <janssen@parc.com>
parents:
42720
diff
changeset
|
393 self.wfile.write(content.encode('UTF-8', 'replace')) |
| 2887 | 394 |
| 395 def send_response(self, code, message=None): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
396 """Send the response header and log the response code. |
| 2887 | 397 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
398 Also send two standard headers with the server software |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
399 version and the current date. |
| 2887 | 400 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
401 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
402 self.log_request(code) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
403 if message is None: |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
404 if code in self.responses: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
405 message = self.responses[code][0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
406 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
407 message = '' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
408 if self.request_version != 'HTTP/0.9': |
|
43717
c1214bd6ed16
make sure to write bytes instead of strings to underlying socket channel
Bill Janssen <janssen@parc.com>
parents:
42720
diff
changeset
|
409 self.wfile.write(("%s %d %s\r\n" % |
|
c1214bd6ed16
make sure to write bytes instead of strings to underlying socket channel
Bill Janssen <janssen@parc.com>
parents:
42720
diff
changeset
|
410 (self.protocol_version, code, message)).encode('ASCII', 'strict')) |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
411 # print (self.protocol_version, code, message) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
412 self.send_header('Server', self.version_string()) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
413 self.send_header('Date', self.date_time_string()) |
| 2887 | 414 |
| 415 def send_header(self, keyword, value): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
416 """Send a MIME header.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
417 if self.request_version != 'HTTP/0.9': |
|
43717
c1214bd6ed16
make sure to write bytes instead of strings to underlying socket channel
Bill Janssen <janssen@parc.com>
parents:
42720
diff
changeset
|
418 self.wfile.write(("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict')) |
| 2887 | 419 |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
420 if keyword.lower() == 'connection': |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
421 if value.lower() == 'close': |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
422 self.close_connection = 1 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
423 elif value.lower() == 'keep-alive': |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
424 self.close_connection = 0 |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
425 |
| 2887 | 426 def end_headers(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
427 """Send the blank line ending the MIME headers.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
428 if self.request_version != 'HTTP/0.9': |
|
43717
c1214bd6ed16
make sure to write bytes instead of strings to underlying socket channel
Bill Janssen <janssen@parc.com>
parents:
42720
diff
changeset
|
429 self.wfile.write(b"\r\n") |
| 2887 | 430 |
| 431 def log_request(self, code='-', size='-'): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
432 """Log an accepted request. |
| 2887 | 433 |
| 36879 | 434 This is called by send_response(). |
| 2887 | 435 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
436 """ |
| 2887 | 437 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
438 self.log_message('"%s" %s %s', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
439 self.requestline, str(code), str(size)) |
| 2887 | 440 |
|
40643
47f9efdc0855
Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36879
diff
changeset
|
441 def log_error(self, format, *args): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
442 """Log an error. |
| 2887 | 443 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
444 This is called when a request cannot be fulfilled. By |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
445 default it passes the message on to log_message(). |
| 2887 | 446 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
447 Arguments are the same as for log_message(). |
| 2887 | 448 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
449 XXX This should go to the separate error log. |
| 2887 | 450 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
451 """ |
| 2887 | 452 |
|
40643
47f9efdc0855
Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36879
diff
changeset
|
453 self.log_message(format, *args) |
| 2887 | 454 |
| 455 def log_message(self, format, *args): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
456 """Log an arbitrary message. |
| 2887 | 457 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
458 This is used by all other logging functions. Override |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
459 it if you have specific logging wishes. |
| 2887 | 460 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
461 The first argument, FORMAT, is a format string for the |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
462 message to be logged. If the format string contains |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
463 any % escapes requiring parameters, they should be |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
464 specified as subsequent arguments (it's just like |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
465 printf!). |
| 2887 | 466 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
467 The client host and current date/time are prefixed to |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
468 every message. |
| 2887 | 469 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
470 """ |
| 2887 | 471 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
472 sys.stderr.write("%s - - [%s] %s\n" % |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
473 (self.address_string(), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
474 self.log_date_time_string(), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
475 format%args)) |
| 2887 | 476 |
| 477 def version_string(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
478 """Return the server software version string.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
479 return self.server_version + ' ' + self.sys_version |
| 2887 | 480 |
|
36555
4668df450672
Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents:
35151
diff
changeset
|
481 def date_time_string(self, timestamp=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
482 """Return the current date and time formatted for a message header.""" |
|
36555
4668df450672
Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents:
35151
diff
changeset
|
483 if timestamp is None: |
|
4668df450672
Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents:
35151
diff
changeset
|
484 timestamp = time.time() |
|
4668df450672
Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents:
35151
diff
changeset
|
485 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
486 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
487 self.weekdayname[wd], |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
488 day, self.monthname[month], year, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
489 hh, mm, ss) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
490 return s |
| 2887 | 491 |
| 492 def log_date_time_string(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
493 """Return the current time formatted for logging.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
494 now = time.time() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
495 year, month, day, hh, mm, ss, x, y, z = time.localtime(now) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
496 s = "%02d/%3s/%04d %02d:%02d:%02d" % ( |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
497 day, self.monthname[month], year, hh, mm, ss) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
498 return s |
| 2887 | 499 |
| 500 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] | |
| 501 | |
| 502 monthname = [None, | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
503 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
504 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
| 2887 | 505 |
| 506 def address_string(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
507 """Return the client address formatted for logging. |
| 2887 | 508 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
509 This version looks up the full hostname using gethostbyaddr(), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
510 and tries to find a name that contains at least one dot. |
| 2887 | 511 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
512 """ |
| 2887 | 513 |
|
29340
c69ad9227afd
Patch #744041: Use only first two elements of address to support IPv6.
Martin v. Löwis <martin@v.loewis.de>
parents:
28089
diff
changeset
|
514 host, port = self.client_address[:2] |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
12634
diff
changeset
|
515 return socket.getfqdn(host) |
| 2887 | 516 |
| 517 # Essentially static class variables | |
| 518 | |
| 519 # The version of the HTTP protocol we support. | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
520 # Set this to HTTP/1.1 to enable automatic keepalive |
| 2887 | 521 protocol_version = "HTTP/1.0" |
| 522 | |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
523 # MessageClass used to parse headers |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
524 MessageClass = http.client.HTTPMessage |
| 2887 | 525 |
| 526 # Table mapping response codes to messages; entries have the | |
| 527 # form {code: (shortmessage, longmessage)}. | |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
528 # See RFC 2616. |
| 2887 | 529 responses = { |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
530 100: ('Continue', 'Request received, please continue'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
531 101: ('Switching Protocols', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
532 'Switching to new protocol; obey Upgrade header'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
533 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
534 200: ('OK', 'Request fulfilled, document follows'), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
535 201: ('Created', 'Document created, URL follows'), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
536 202: ('Accepted', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
537 'Request accepted, processing continues off-line'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
538 203: ('Non-Authoritative Information', 'Request fulfilled from cache'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
539 204: ('No Content', 'Request fulfilled, nothing follows'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
540 205: ('Reset Content', 'Clear input form for further input.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
541 206: ('Partial Content', 'Partial content follows.'), |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
542 |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
543 300: ('Multiple Choices', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
544 'Object has several resources -- see URI list'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
545 301: ('Moved Permanently', 'Object moved permanently -- see URI list'), |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
546 302: ('Found', 'Object moved temporarily -- see URI list'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
547 303: ('See Other', 'Object moved -- see Method and URL list'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
548 304: ('Not Modified', |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
549 'Document has not changed since given time'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
550 305: ('Use Proxy', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
551 'You must use proxy specified in Location to access this ' |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
552 'resource.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
553 307: ('Temporary Redirect', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
554 'Object moved temporarily -- see URI list'), |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
555 |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
556 400: ('Bad Request', |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
557 'Bad request syntax or unsupported method'), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
558 401: ('Unauthorized', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
559 'No permission -- see authorization schemes'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
560 402: ('Payment Required', |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
561 'No payment -- see charging schemes'), |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
562 403: ('Forbidden', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
563 'Request forbidden -- authorization will not help'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
564 404: ('Not Found', 'Nothing matches the given URI'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
565 405: ('Method Not Allowed', |
|
58995
f8ddfced2a34
Merged revisions 78299 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
57185
diff
changeset
|
566 'Specified method is invalid for this resource.'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
567 406: ('Not Acceptable', 'URI not available in preferred format.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
568 407: ('Proxy Authentication Required', 'You must authenticate with ' |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
569 'this proxy before proceeding.'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
570 408: ('Request Timeout', 'Request timed out; try again later.'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
571 409: ('Conflict', 'Request conflict.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
572 410: ('Gone', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
573 'URI no longer exists and has been permanently removed.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
574 411: ('Length Required', 'Client must specify Content-Length.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
575 412: ('Precondition Failed', 'Precondition in headers is false.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
576 413: ('Request Entity Too Large', 'Entity is too large.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
577 414: ('Request-URI Too Long', 'URI is too long.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
578 415: ('Unsupported Media Type', 'Entity body in unsupported format.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
579 416: ('Requested Range Not Satisfiable', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
580 'Cannot satisfy request range.'), |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
581 417: ('Expectation Failed', |
|
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
582 'Expect condition could not be satisfied.'), |
|
15860
083a5c75ed90
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14014
diff
changeset
|
583 |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
584 500: ('Internal Server Error', 'Server got itself in trouble'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
585 501: ('Not Implemented', |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
586 'Server does not support this operation'), |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
587 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
588 503: ('Service Unavailable', |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
589 'The server cannot process the request due to a high load'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
590 504: ('Gateway Timeout', |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
591 'The gateway server did not receive a timely response'), |
|
36559
09230357f927
Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents:
36555
diff
changeset
|
592 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'), |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
593 } |
| 2887 | 594 |
| 595 | |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
596 class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
597 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
598 """Simple HTTP request handler with GET and HEAD commands. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
599 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
600 This serves files from the current directory and any of its |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
601 subdirectories. The MIME type for files is determined by |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
602 calling the .guess_type() method. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
603 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
604 The GET and HEAD requests are identical except that the HEAD |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
605 request omits the actual contents of the file. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
606 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
607 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
608 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
609 server_version = "SimpleHTTP/" + __version__ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
610 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
611 def do_GET(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
612 """Serve a GET request.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
613 f = self.send_head() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
614 if f: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
615 self.copyfile(f, self.wfile) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
616 f.close() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
617 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
618 def do_HEAD(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
619 """Serve a HEAD request.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
620 f = self.send_head() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
621 if f: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
622 f.close() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
623 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
624 def send_head(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
625 """Common code for GET and HEAD commands. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
626 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
627 This sends the response code and MIME headers. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
628 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
629 Return value is either a file object (which has to be copied |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
630 to the outputfile by the caller unless the command was HEAD, |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
631 and must be closed by the caller under all circumstances), or |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
632 None, in which case the caller has nothing further to do. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
633 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
634 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
635 path = self.translate_path(self.path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
636 f = None |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
637 if os.path.isdir(path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
638 if not self.path.endswith('/'): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
639 # redirect browser - doing basically what apache does |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
640 self.send_response(301) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
641 self.send_header("Location", self.path + "/") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
642 self.end_headers() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
643 return None |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
644 for index in "index.html", "index.htm": |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
645 index = os.path.join(path, index) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
646 if os.path.exists(index): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
647 path = index |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
648 break |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
649 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
650 return self.list_directory(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
651 ctype = self.guess_type(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
652 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
653 f = open(path, 'rb') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
654 except IOError: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
655 self.send_error(404, "File not found") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
656 return None |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
657 self.send_response(200) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
658 self.send_header("Content-type", ctype) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
659 fs = os.fstat(f.fileno()) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
660 self.send_header("Content-Length", str(fs[6])) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
661 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
662 self.end_headers() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
663 return f |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
664 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
665 def list_directory(self, path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
666 """Helper to produce a directory listing (absent index.html). |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
667 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
668 Return value is either a file object, or None (indicating an |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
669 error). In either case, the headers are sent, making the |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
670 interface the same as for send_head(). |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
671 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
672 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
673 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
674 list = os.listdir(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
675 except os.error: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
676 self.send_error(404, "No permission to list directory") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
677 return None |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
678 list.sort(key=lambda a: a.lower()) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
679 r = [] |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47657
diff
changeset
|
680 displaypath = cgi.escape(urllib.parse.unquote(self.path)) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
681 r.append('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
682 r.append("<html>\n<title>Directory listing for %s</title>\n" % displaypath) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
683 r.append("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
684 r.append("<hr>\n<ul>\n") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
685 for name in list: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
686 fullname = os.path.join(path, name) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
687 displayname = linkname = name |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
688 # Append / for directories or @ for symbolic links |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
689 if os.path.isdir(fullname): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
690 displayname = name + "/" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
691 linkname = name + "/" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
692 if os.path.islink(fullname): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
693 displayname = name + "@" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
694 # Note: a link to a directory displays with @ and links with / |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
695 r.append('<li><a href="%s">%s</a>\n' |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47657
diff
changeset
|
696 % (urllib.parse.quote(linkname), cgi.escape(displayname))) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
697 r.append("</ul>\n<hr>\n</body>\n</html>\n") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
698 enc = sys.getfilesystemencoding() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
699 encoded = ''.join(r).encode(enc) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
700 f = io.BytesIO() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
701 f.write(encoded) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
702 f.seek(0) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
703 self.send_response(200) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
704 self.send_header("Content-type", "text/html; charset=%s" % enc) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
705 self.send_header("Content-Length", str(len(encoded))) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
706 self.end_headers() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
707 return f |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
708 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
709 def translate_path(self, path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
710 """Translate a /-separated PATH to the local filename syntax. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
711 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
712 Components that mean special things to the local file system |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
713 (e.g. drive or directory names) are ignored. (XXX They should |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
714 probably be diagnosed.) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
715 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
716 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
717 # abandon query parameters |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
718 path = path.split('?',1)[0] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
719 path = path.split('#',1)[0] |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47657
diff
changeset
|
720 path = posixpath.normpath(urllib.parse.unquote(path)) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
721 words = path.split('/') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
722 words = filter(None, words) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
723 path = os.getcwd() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
724 for word in words: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
725 drive, word = os.path.splitdrive(word) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
726 head, word = os.path.split(word) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
727 if word in (os.curdir, os.pardir): continue |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
728 path = os.path.join(path, word) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
729 return path |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
730 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
731 def copyfile(self, source, outputfile): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
732 """Copy all data between two file objects. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
733 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
734 The SOURCE argument is a file object open for reading |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
735 (or anything with a read() method) and the DESTINATION |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
736 argument is a file object open for writing (or |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
737 anything with a write() method). |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
738 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
739 The only reason for overriding this would be to change |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
740 the block size or perhaps to replace newlines by CRLF |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
741 -- note however that this the default server uses this |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
742 to copy binary data as well. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
743 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
744 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
745 shutil.copyfileobj(source, outputfile) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
746 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
747 def guess_type(self, path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
748 """Guess the type of a file. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
749 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
750 Argument is a PATH (a filename). |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
751 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
752 Return value is a string of the form type/subtype, |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
753 usable for a MIME Content-type header. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
754 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
755 The default implementation looks the file's extension |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
756 up in the table self.extensions_map, using application/octet-stream |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
757 as a default; however it would be permissible (if |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
758 slow) to look inside the data to make a better guess. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
759 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
760 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
761 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
762 base, ext = posixpath.splitext(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
763 if ext in self.extensions_map: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
764 return self.extensions_map[ext] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
765 ext = ext.lower() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
766 if ext in self.extensions_map: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
767 return self.extensions_map[ext] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
768 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
769 return self.extensions_map[''] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
770 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
771 if not mimetypes.inited: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
772 mimetypes.init() # try to read system mime.types |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
773 extensions_map = mimetypes.types_map.copy() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
774 extensions_map.update({ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
775 '': 'application/octet-stream', # Default |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
776 '.py': 'text/plain', |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
777 '.c': 'text/plain', |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
778 '.h': 'text/plain', |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
779 }) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
780 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
781 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
782 # Utilities for CGIHTTPRequestHandler |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
783 |
|
53073
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
784 # TODO(gregory.p.smith): Move this into an appropriate library. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
785 def _url_collapse_path_split(path): |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
786 """ |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
787 Given a URL path, remove extra '/'s and '.' path elements and collapse |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
788 any '..' references. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
789 |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
790 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
791 |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
792 Returns: A tuple of (head, tail) where tail is everything after the final / |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
793 and head is everything before it. Head will always start with a '/' and, |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
794 if it contains anything else, never have a trailing '/'. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
795 |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
796 Raises: IndexError if too many '..' occur within the path. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
797 """ |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
798 # Similar to os.path.split(os.path.normpath(path)) but specific to URL |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
799 # path semantics rather than local operating system semantics. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
800 path_parts = [] |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
801 for part in path.split('/'): |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
802 if part == '.': |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
803 path_parts.append('') |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
804 else: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
805 path_parts.append(part) |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
806 # Filter out blank non trailing parts before consuming the '..'. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
807 path_parts = [part for part in path_parts[:-1] if part] + path_parts[-1:] |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
808 if path_parts: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
809 tail_part = path_parts.pop() |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
810 else: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
811 tail_part = '' |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
812 head_parts = [] |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
813 for part in path_parts: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
814 if part == '..': |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
815 head_parts.pop() |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
816 else: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
817 head_parts.append(part) |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
818 if tail_part and tail_part == '..': |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
819 head_parts.pop() |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
820 tail_part = '' |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
821 return ('/' + '/'.join(head_parts), tail_part) |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
822 |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
823 |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
824 nobody = None |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
825 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
826 def nobody_uid(): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
827 """Internal routine to get nobody's uid""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
828 global nobody |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
829 if nobody: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
830 return nobody |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
831 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
832 import pwd |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
833 except ImportError: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
834 return -1 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
835 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
836 nobody = pwd.getpwnam('nobody')[2] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
837 except KeyError: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
838 nobody = 1 + max(map(lambda x: x[2], pwd.getpwall())) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
839 return nobody |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
840 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
841 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
842 def executable(path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
843 """Test for executable file.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
844 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
845 st = os.stat(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
846 except os.error: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
847 return False |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
848 return st.st_mode & 0o111 != 0 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
849 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
850 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
851 class CGIHTTPRequestHandler(SimpleHTTPRequestHandler): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
852 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
853 """Complete HTTP server with GET, HEAD and POST commands. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
854 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
855 GET and HEAD also support running CGI scripts. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
856 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
857 The POST command is *only* implemented for CGI scripts. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
858 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
859 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
860 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
861 # Determine platform specifics |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
862 have_fork = hasattr(os, 'fork') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
863 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
864 # Make rfile unbuffered -- we need to read one line and then pass |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
865 # the rest to a subprocess, so we can't use buffered input. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
866 rbufsize = 0 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
867 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
868 def do_POST(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
869 """Serve a POST request. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
870 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
871 This is only implemented for CGI scripts. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
872 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
873 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
874 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
875 if self.is_cgi(): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
876 self.run_cgi() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
877 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
878 self.send_error(501, "Can only POST to CGI scripts") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
879 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
880 def send_head(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
881 """Version of send_head that support CGI scripts""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
882 if self.is_cgi(): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
883 return self.run_cgi() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
884 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
885 return SimpleHTTPRequestHandler.send_head(self) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
886 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
887 def is_cgi(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
888 """Test whether self.path corresponds to a CGI script. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
889 |
|
53073
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
890 Returns True and updates the cgi_info attribute to the tuple |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
891 (dir, rest) if self.path requires running a CGI script. |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
892 Returns False otherwise. |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
893 |
| 53921 | 894 If any exception is raised, the caller should assume that |
| 895 self.path was rejected as invalid and act accordingly. | |
| 896 | |
|
53073
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
897 The default implementation tests whether the normalized url |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
898 path begins with one of the strings in self.cgi_directories |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
899 (and the next character is a '/' or the end of the string). |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
900 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
901 """ |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
902 |
|
53073
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
903 splitpath = _url_collapse_path_split(self.path) |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
904 if splitpath[0] in self.cgi_directories: |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
905 self.cgi_info = splitpath |
|
3b1b6142690a
Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52770
diff
changeset
|
906 return True |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
907 return False |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
908 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
909 cgi_directories = ['/cgi-bin', '/htbin'] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
910 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
911 def is_executable(self, path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
912 """Test whether argument path is an executable file.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
913 return executable(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
914 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
915 def is_python(self, path): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
916 """Test whether argument path is a Python script.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
917 head, tail = os.path.splitext(path) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
918 return tail.lower() in (".py", ".pyw") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
919 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
920 def run_cgi(self): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
921 """Execute a CGI script.""" |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
922 path = self.path |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
923 dir, rest = self.cgi_info |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
924 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
925 i = path.find('/', len(dir) + 1) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
926 while i >= 0: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
927 nextdir = path[:i] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
928 nextrest = path[i+1:] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
929 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
930 scriptdir = self.translate_path(nextdir) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
931 if os.path.isdir(scriptdir): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
932 dir, rest = nextdir, nextrest |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
933 i = path.find('/', len(dir) + 1) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
934 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
935 break |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
936 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
937 # find an explicit query string, if present. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
938 i = rest.rfind('?') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
939 if i >= 0: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
940 rest, query = rest[:i], rest[i+1:] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
941 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
942 query = '' |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
943 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
944 # dissect the part after the directory name into a script name & |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
945 # a possible additional path, to be stored in PATH_INFO. |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
946 i = rest.find('/') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
947 if i >= 0: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
948 script, rest = rest[:i], rest[i:] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
949 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
950 script, rest = rest, '' |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
951 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
952 scriptname = dir + '/' + script |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
953 scriptfile = self.translate_path(scriptname) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
954 if not os.path.exists(scriptfile): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
955 self.send_error(404, "No such CGI script (%r)" % scriptname) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
956 return |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
957 if not os.path.isfile(scriptfile): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
958 self.send_error(403, "CGI script is not a plain file (%r)" % |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
959 scriptname) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
960 return |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
961 ispy = self.is_python(scriptname) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
962 if not ispy: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
963 if not self.is_executable(scriptfile): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
964 self.send_error(403, "CGI script is not executable (%r)" % |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
965 scriptname) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
966 return |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
967 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
968 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
969 # XXX Much of the following could be prepared ahead of time! |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
970 env = {} |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
971 env['SERVER_SOFTWARE'] = self.version_string() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
972 env['SERVER_NAME'] = self.server.server_name |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
973 env['GATEWAY_INTERFACE'] = 'CGI/1.1' |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
974 env['SERVER_PROTOCOL'] = self.protocol_version |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
975 env['SERVER_PORT'] = str(self.server.server_port) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
976 env['REQUEST_METHOD'] = self.command |
|
48334
5a416a6417d3
Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
47657
diff
changeset
|
977 uqrest = urllib.parse.unquote(rest) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
978 env['PATH_INFO'] = uqrest |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
979 env['PATH_TRANSLATED'] = self.translate_path(uqrest) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
980 env['SCRIPT_NAME'] = scriptname |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
981 if query: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
982 env['QUERY_STRING'] = query |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
983 host = self.address_string() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
984 if host != self.client_address[0]: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
985 env['REMOTE_HOST'] = host |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
986 env['REMOTE_ADDR'] = self.client_address[0] |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
987 authorization = self.headers.get("authorization") |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
988 if authorization: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
989 authorization = authorization.split() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
990 if len(authorization) == 2: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
991 import base64, binascii |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
992 env['AUTH_TYPE'] = authorization[0] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
993 if authorization[0].lower() == "basic": |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
994 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
995 authorization = authorization[1].encode('ascii') |
|
54568
031b963f2bc2
More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents:
53921
diff
changeset
|
996 authorization = base64.decodebytes(authorization).\ |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
997 decode('ascii') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
998 except (binascii.Error, UnicodeError): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
999 pass |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1000 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1001 authorization = authorization.split(':') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1002 if len(authorization) == 2: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1003 env['REMOTE_USER'] = authorization[0] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1004 # XXX REMOTE_IDENT |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1005 if self.headers.get('content-type') is None: |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1006 env['CONTENT_TYPE'] = self.headers.get_content_type() |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1007 else: |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1008 env['CONTENT_TYPE'] = self.headers['content-type'] |
|
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1009 length = self.headers.get('content-length') |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1010 if length: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1011 env['CONTENT_LENGTH'] = length |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1012 referer = self.headers.get('referer') |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1013 if referer: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1014 env['HTTP_REFERER'] = referer |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1015 accept = [] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1016 for line in self.headers.getallmatchingheaders('accept'): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1017 if line[:1] in "\t\n\r ": |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1018 accept.append(line.strip()) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1019 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1020 accept = accept + line[7:].split(',') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1021 env['HTTP_ACCEPT'] = ','.join(accept) |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1022 ua = self.headers.get('user-agent') |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1023 if ua: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1024 env['HTTP_USER_AGENT'] = ua |
|
47635
f31e32a2013c
Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents:
47318
diff
changeset
|
1025 co = filter(None, self.headers.get_all('cookie', [])) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1026 if co: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1027 env['HTTP_COOKIE'] = ', '.join(co) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1028 # XXX Other HTTP_* headers |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1029 # Since we're setting the env in the parent, provide empty |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1030 # values to override previously set values |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1031 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH', |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1032 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1033 env.setdefault(k, "") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1034 os.environ.update(env) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1035 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1036 self.send_response(200, "Script output follows") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1037 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1038 decoded_query = query.replace('+', ' ') |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1039 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1040 if self.have_fork: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1041 # Unix -- fork as we should |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1042 args = [script] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1043 if '=' not in decoded_query: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1044 args.append(decoded_query) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1045 nobody = nobody_uid() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1046 self.wfile.flush() # Always flush before forking |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1047 pid = os.fork() |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1048 if pid != 0: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1049 # Parent |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1050 pid, sts = os.waitpid(pid, 0) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1051 # throw away additional data [see bug #427345] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1052 while select.select([self.rfile], [], [], 0)[0]: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1053 if not self.rfile.read(1): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1054 break |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1055 if sts: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1056 self.log_error("CGI script exit status %#x", sts) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1057 return |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1058 # Child |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1059 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1060 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1061 os.setuid(nobody) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1062 except os.error: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1063 pass |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1064 os.dup2(self.rfile.fileno(), 0) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1065 os.dup2(self.wfile.fileno(), 1) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1066 os.execve(scriptfile, args, os.environ) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1067 except: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1068 self.server.handle_error(self.request, self.client_address) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1069 os._exit(127) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1070 |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1071 else: |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1072 # Non-Unix -- use subprocess |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1073 import subprocess |
|
57185
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1074 cmdline = [scriptfile] |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1075 if self.is_python(scriptfile): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1076 interp = sys.executable |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1077 if interp.lower().endswith("w.exe"): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1078 # On Windows, use python.exe, not pythonw.exe |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1079 interp = interp[:-5] + interp[-4:] |
|
57185
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1080 cmdline = [interp, '-u'] + cmdline |
|
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1081 if '=' not in query: |
|
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1082 cmdline.append(query) |
|
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1083 self.log_message("command: %s", subprocess.list2cmdline(cmdline)) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1084 try: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1085 nbytes = int(length) |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1086 except (TypeError, ValueError): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1087 nbytes = 0 |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1088 p = subprocess.Popen(cmdline, |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1089 stdin=subprocess.PIPE, |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1090 stdout=subprocess.PIPE, |
|
57185
12e0c9ea304e
Merged revisions 76208 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
55028
diff
changeset
|
1091 stderr=subprocess.PIPE |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1092 ) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1093 if self.command.lower() == "post" and nbytes > 0: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1094 data = self.rfile.read(nbytes) |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1095 else: |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1096 data = None |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1097 # throw away additional data [see bug #427345] |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1098 while select.select([self.rfile._sock], [], [], 0)[0]: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1099 if not self.rfile._sock.recv(1): |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1100 break |
|
48335
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1101 stdout, stderr = p.communicate(data) |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1102 self.wfile.write(stdout) |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1103 if stderr: |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1104 self.log_error('%s', stderr) |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1105 status = p.returncode |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1106 if status: |
|
69040092b8f8
Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48334
diff
changeset
|
1107 self.log_error("CGI script exit status %#x", status) |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1108 else: |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1109 self.log_message("CGI script exited OK") |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1110 |
|
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1111 |
| 2887 | 1112 def test(HandlerClass = BaseHTTPRequestHandler, |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
1113 ServerClass = HTTPServer, protocol="HTTP/1.0"): |
| 2887 | 1114 """Test the HTTP request handler class. |
| 1115 | |
| 1116 This runs an HTTP server on port 8000 (or the first command line | |
| 1117 argument). | |
| 1118 | |
| 1119 """ | |
| 1120 | |
| 1121 if sys.argv[1:]: | |
|
16482
1fb7befb59f0
Correction to test main.
Eric S. Raymond <esr@thyrsus.com>
parents:
16479
diff
changeset
|
1122 port = int(sys.argv[1]) |
| 2887 | 1123 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5975
diff
changeset
|
1124 port = 8000 |
| 2887 | 1125 server_address = ('', port) |
| 1126 | |
|
22346
27f36f4bf525
Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents:
22182
diff
changeset
|
1127 HandlerClass.protocol_version = protocol |
| 2887 | 1128 httpd = ServerClass(server_address, HandlerClass) |
| 1129 | |
|
18825
40900620293e
Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents:
16482
diff
changeset
|
1130 sa = httpd.socket.getsockname() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
1131 print("Serving HTTP on", sa[0], "port", sa[1], "...") |
|
52770
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1132 try: |
|
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1133 httpd.serve_forever() |
|
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1134 except KeyboardInterrupt: |
|
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1135 print("\nKeyboard interrupt received, exiting.") |
|
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1136 httpd.server_close() |
|
4757ddaa897d
Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
52369
diff
changeset
|
1137 sys.exit(0) |
| 2887 | 1138 |
| 1139 if __name__ == '__main__': | |
|
47318
e1fcc3881cb6
Create http package. #2883.
Georg Brandl <georg@python.org>
parents:
47278
diff
changeset
|
1140 test(HandlerClass=SimpleHTTPRequestHandler) |
