annotate Lib/http/server.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents d6ad412dee40
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 XXX To do:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 - log requests even later (to capture byte count)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 - log user-agent header and other interesting goodies
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 - send error log to separate file
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # See also:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # HTTP Working Group T. Berners-Lee
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # INTERNET-DRAFT R. T. Fielding
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # Expires September 8, 1995 March 8, 1995
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 # Log files
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # ---------
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
55 #
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # (Actually, the latter is only true if you know the server configuration
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # at the time the request was made!)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
83 __version__ = "0.6"
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
65362
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents: 65075
diff changeset
87 import html
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
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
90 import http.client
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
91 import io
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
92 import mimetypes
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
93 import os
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
94 import posixpath
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
95 import select
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
96 import shutil
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
97 import socket # For gethostbyaddr()
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
98 import socketserver
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
99 import sys
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
100 import time
10a84c638eda Sort import list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 52367
diff changeset
101 import urllib.parse
65076
945a55d4e73e Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63443
diff changeset
102 import copy
77320
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
103 import argparse
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
104
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
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
106 # Default error message template
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 DEFAULT_ERROR_MESSAGE = """\
68705
135a0d7bc4db Issue #11567 - Let's have the DOCTYPE as HTML instead of XHTML.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68646
diff changeset
108 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
135a0d7bc4db Issue #11567 - Let's have the DOCTYPE as HTML instead of XHTML.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68646
diff changeset
109 "http://www.w3.org/TR/html4/strict.dtd">
73303
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
110 <html>
68646
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
111 <head>
68705
135a0d7bc4db Issue #11567 - Let's have the DOCTYPE as HTML instead of XHTML.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68646
diff changeset
112 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
68646
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
113 <title>Error response</title>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
114 </head>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
115 <body>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
116 <h1>Error response</h1>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
117 <p>Error code: %(code)d</p>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
118 <p>Message: %(message)s.</p>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
119 <p>Error code explanation: %(code)s - %(explain)s.</p>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
120 </body>
2f222360a774 Error message in http.server code. Missed to update in 3.1 branch initially.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67029
diff changeset
121 </html>
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
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
124 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
125
35151
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
126 def _quote_html(html):
728a0de88395 backport bug [ 1100201 ] Cross-site scripting on BaseHTTPServer
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
127 return html.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
46933
aca5c1d1c569 Renamed the SocketServer module to 'socketserver'.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45460
diff changeset
129 class HTTPServer(socketserver.TCPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
12634
ddf986a971b5 Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents: 11409
diff changeset
131 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
132
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 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
134 """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
135 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
136 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
137 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
138 self.server_port = port
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
46933
aca5c1d1c569 Renamed the SocketServer module to 'socketserver'.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 45460
diff changeset
141 class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 """HTTP request handler base class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 The following explanation of HTTP serves to guide you through the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 code as well as to expose any misunderstandings I may have about
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 HTTP (so you don't need to read the code to figure out I'm wrong
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 :-).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 HTTP (HyperText Transfer Protocol) is an extensible protocol on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 top of a reliable stream transport (e.g. TCP/IP). The protocol
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 recognizes three parts to a request:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 1. One line identifying the request type and path
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 3. An optional data part
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 The headers and data are separated by a blank line.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 The first line of the request has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 <command> <path> <version>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 where <command> is a (case-sensitive) keyword such as GET or POST,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 <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
166 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
167 <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
168 the ASCII character with hex code xx).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
27637
601549c64039 Bug #676273: Rewrite paragraph in module docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 23608
diff changeset
170 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
171 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
172 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
173 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
174 and allowing trailing whitespace).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 Similarly, for output, lines ought to be separated by CRLF pairs
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 but most clients grok LF characters just fine.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 If the first line of the request has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 <command> <path>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 (i.e. <version> is left out) then this is assumed to be an HTTP
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 0.9 request; this form has no optional headers and data part and
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 the reply consists of just the data.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
187 The reply form of the HTTP 1.x protocol again has three parts:
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 1. One line giving the response code
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 3. The data
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 Again, the headers and data are separated by a blank line.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 The response code line has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 <version> <responsecode> <responsestring>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
199 where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 <responsecode> is a 3-digit response code indicating success or
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 failure of the request, and <responsestring> is an optional
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 human-readable string explaining what the response code means.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 This server parses the request and the headers, and then calls a
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 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
206 a request SPAM will be handled by a method do_SPAM(). If no
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 such method exists the server sends an error response to the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 client. If it exists, it is called with no arguments:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 do_SPAM()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 Note that the request name is case sensitive (i.e. SPAM and spam
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 are different requests).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 The various request details are stored in instance variables:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 - client_address is the client IP address in the form (host,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 port);
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 - command, path and version are the broken-down request line;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47318
diff changeset
222 - headers is an instance of email.message.Message (or a derived
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 class) containing the header information;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 - rfile is a file object open for reading positioned at the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 start of the optional input data part;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 - wfile is a file object open for writing.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 The first thing to be written must be the response line. Then
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 follow 0 or more header lines, then a blank line, and then the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 actual data (if any). The meaning of the header lines depends on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 the command executed by the server; in most cases, when data is
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 returned, there should be at least one header line of the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 Content-type: <type>/<subtype>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 where <type> and <subtype> should be registered MIME types,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 e.g. "text/html" or "text/plain".
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 # The Python system version, truncated to its first component.
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
246 sys_version = "Python/" + sys.version.split()[0]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 # The server software version. You may want to override this.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 # The format is multiple whitespace-separated strings,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 # where each string is of the form name[/version].
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 server_version = "BaseHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
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
253 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
254 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
255
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
256 # 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
257 # 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
258 # 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
259 # 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
260 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
261
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 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
263 """Parse a request (internal).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
29357
20894dbc7f6a Fix SF bug #747348 docstring mistake in BaseHTTPServer.py. \n\nBackport Candidate
Raymond Hettinger <python@rcn.com>
parents: 29340
diff changeset
265 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
266 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
267 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
268
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
269 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
270 error is sent back.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
272 """
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
273 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
274 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
275 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
276 requestline = str(self.raw_requestline, 'iso-8859-1')
74138
2c1720468dbb Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68706
diff changeset
277 requestline = requestline.rstrip('\r\n')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
278 self.requestline = requestline
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
279 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
280 if len(words) == 3:
74138
2c1720468dbb Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68706
diff changeset
281 command, path, version = words
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
282 if version[:5] != 'HTTP/':
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
283 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
284 return False
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
285 try:
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
286 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
287 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
288 # 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
289 # - 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
290 # separate integers;
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
291 # - 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
292 # 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
293 # - 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
294 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
295 raise ValueError
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
296 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
297 except (ValueError, IndexError):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
298 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
299 return False
27648
db784d29f481 [Bug #676292] BaseHTTPServer incorrectly parses protocol; fix by Andrew Dalke
Andrew M. Kuchling <amk@amk.ca>
parents: 27637
diff changeset
300 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
301 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
302 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
303 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
304 "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
305 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
306 elif len(words) == 2:
74138
2c1720468dbb Minor code style improvements in http.server suggested in Issue13294.
Senthil Kumaran <senthil@uthcode.com>
parents: 68706
diff changeset
307 command, path = words
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
308 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
309 if command != 'GET':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
310 self.send_error(400,
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
311 "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
312 return False
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
313 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
314 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
315 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30105
diff changeset
316 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
317 return False
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
318 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
319
42720
eae10d9c9c4e Fixes by John Reese and Jacques Frechet that make test_xmlrpc pass.
Guido van Rossum <guido@python.org>
parents: 42528
diff changeset
320 # Examine the headers and look for a Connection directive.
67029
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
321 try:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
322 self.headers = http.client.parse_headers(self.rfile,
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
323 _class=self.MessageClass)
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
324 except http.client.LineTooLong:
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
325 self.send_error(400, "Line too long")
3c182f71d4c9 Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 66971
diff changeset
326 return False
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
327
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
328 conntype = self.headers.get('Connection', "")
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
329 if conntype.lower() == 'close':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
330 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
331 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
332 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
333 self.close_connection = 0
65028
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
334 # Examine the headers and look for an Expect directive
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
335 expect = self.headers.get('Expect', "")
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
336 if (expect.lower() == "100-continue" and
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
337 self.protocol_version >= "HTTP/1.1" and
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
338 self.request_version >= "HTTP/1.1"):
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
339 if not self.handle_expect_100():
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
340 return False
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
341 return True
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
342
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
343 def handle_expect_100(self):
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
344 """Decide what to do with an "Expect: 100-continue" header.
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
345
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
346 If the client is expecting a 100 Continue response, we must
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
347 respond with either a 100 Continue or a final response before
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
348 waiting for the request body. The default is to always respond
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
349 with a 100 Continue. You can behave differently (for example,
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
350 reject unauthorized requests) by overriding this method.
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
351
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
352 This method should either return True (possibly after sending
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
353 a 100 Continue response) or send an error response and return
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
354 False.
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
355
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
356 """
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
357 self.send_response_only(100)
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
358 self.flush_headers()
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22346
diff changeset
359 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
360
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
361 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
362 """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
363
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
364 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
365 __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
366 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
367
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
368 """
55028
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
369 try:
66970
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
370 self.raw_requestline = self.rfile.readline(65537)
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
371 if len(self.raw_requestline) > 65536:
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
372 self.requestline = ''
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
373 self.request_version = ''
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
374 self.command = ''
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
375 self.send_error(414)
436bbc7f0aa6 Issue #10714: Limit length of incoming request in http.server to 65536 bytes
Antoine Pitrou <solipsis@pitrou.net>
parents: 66686
diff changeset
376 return
55028
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
377 if not self.raw_requestline:
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
378 self.close_connection = 1
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
379 return
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
380 if not self.parse_request():
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
381 # 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
382 return
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
383 mname = 'do_' + self.command
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
384 if not hasattr(self, mname):
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
385 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
386 return
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
387 method = getattr(self, mname)
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
388 method()
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
389 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
390 except socket.timeout as e:
366c8a4e5a46 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54568
diff changeset
391 #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
392 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
393 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
394 return
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
396 def handle(self):
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
397 """Handle multiple requests if necessary."""
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
398 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
399
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
400 self.handle_one_request()
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
401 while not self.close_connection:
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
402 self.handle_one_request()
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
403
82669
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
404 def send_error(self, code, message=None, explain=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
405 """Send and log an error reply.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
406
82669
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
407 Arguments are
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
408 * code: an HTTP error code
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
409 3 digits
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
410 * message: a simple optional 1 line reason phrase.
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
411 *( HTAB / SP / VCHAR / %x80-FF )
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
412 defaults to short entry matching the response code
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
413 * explain: a detailed message defaults to the long entry
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
414 matching the response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
416 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
417 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
418 a piece of HTML explaining the error to the user.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
420 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
422 try:
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40593
diff changeset
423 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
424 except KeyError:
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40593
diff changeset
425 shortmsg, longmsg = '???', '???'
23608
ec27ac19f9e3 Use is None rather than general boolean
Raymond Hettinger <python@rcn.com>
parents: 23075
diff changeset
426 if message is None:
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40593
diff changeset
427 message = shortmsg
82669
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
428 if explain is None:
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
429 explain = longmsg
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
430 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
431 # 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
432 content = (self.error_message_format %
82669
59292f366b53 #1291 http.server's send_error takes an optional explain argument
Senthil Kumaran <senthil@uthcode.com>
parents: 82497
diff changeset
433 {'code': code, 'message': _quote_html(message), 'explain': _quote_html(explain)})
79664
ee34cb049a10 Issue #16088: BaseHTTPRequestHandler's send_error method includes a
Senthil Kumaran <senthil@uthcode.com>
parents: 77320
diff changeset
434 body = content.encode('UTF-8', 'replace')
82495
637d7c953b10 Reverting the changeset 5126e62c60af made for Issue #12921
Senthil Kumaran <senthil@uthcode.com>
parents: 82491
diff changeset
435 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
436 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
437 self.send_header('Connection', 'close')
79664
ee34cb049a10 Issue #16088: BaseHTTPRequestHandler's send_error method includes a
Senthil Kumaran <senthil@uthcode.com>
parents: 77320
diff changeset
438 self.send_header('Content-Length', int(len(body)))
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
439 self.end_headers()
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
440 if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
79664
ee34cb049a10 Issue #16088: BaseHTTPRequestHandler's send_error method includes a
Senthil Kumaran <senthil@uthcode.com>
parents: 77320
diff changeset
441 self.wfile.write(body)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 def send_response(self, code, message=None):
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
444 """Add the response header to the headers buffer and log the
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
445 response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
447 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
448 version and the current date.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
450 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
451 self.log_request(code)
65028
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
452 self.send_response_only(code, message)
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
453 self.send_header('Server', self.version_string())
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
454 self.send_header('Date', self.date_time_string())
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
455
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
456 def send_response_only(self, code, message=None):
7add45bcc9c6 Issue1491 - BaseHTTPServer incorrectly implements response code 100
Senthil Kumaran <orsenthil@gmail.com>
parents: 63388
diff changeset
457 """Send the response header only."""
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
458 if message is None:
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
459 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
460 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
461 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
462 message = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
463 if self.request_version != 'HTTP/0.9':
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
464 if not hasattr(self, '_headers_buffer'):
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
465 self._headers_buffer = []
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
466 self._headers_buffer.append(("%s %d %s\r\n" %
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
467 (self.protocol_version, code, message)).encode(
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
468 'latin-1', 'strict'))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 def send_header(self, keyword, value):
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
471 """Send a MIME header to the headers buffer."""
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
472 if self.request_version != 'HTTP/0.9':
66351
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
473 if not hasattr(self, '_headers_buffer'):
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
474 self._headers_buffer = []
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
475 self._headers_buffer.append(
68142
be6c38d1817b Normalize the encoding names for Latin-1 and UTF-8 to
Marc-André Lemburg <mal@egenix.com>
parents: 67719
diff changeset
476 ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict'))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
478 if keyword.lower() == 'connection':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
479 if value.lower() == 'close':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
480 self.close_connection = 1
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
481 elif value.lower() == 'keep-alive':
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
482 self.close_connection = 0
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
483
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 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
485 """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
486 if self.request_version != 'HTTP/0.9':
66351
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
487 self._headers_buffer.append(b"\r\n")
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
488 self.flush_headers()
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
489
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
490 def flush_headers(self):
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
491 if hasattr(self, '_headers_buffer'):
66351
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
492 self.wfile.write(b"".join(self._headers_buffer))
9717153d558d Fix issue3709 - BaseHTTPRequestHandler will buffer the headers and write only on end_headers call.
Senthil Kumaran <orsenthil@gmail.com>
parents: 65965
diff changeset
493 self._headers_buffer = []
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 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
496 """Log an accepted request.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
497
36879
8d41ddc31e61 Typo fix
Andrew M. Kuchling <amk@amk.ca>
parents: 36559
diff changeset
498 This is called by send_response().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
500 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
502 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
503 self.requestline, str(code), str(size))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
40593
5f130271f386 Fix the signature of log_error(). (A subclass that did the right thing
Guido van Rossum <guido@python.org>
parents: 36879
diff changeset
505 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
506 """Log an error.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
508 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
509 default it passes the message on to log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
511 Arguments are the same as for log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
513 XXX This should go to the separate error log.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
515 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516
40593
5f130271f386 Fix the signature of log_error(). (A subclass that did the right thing
Guido van Rossum <guido@python.org>
parents: 36879
diff changeset
517 self.log_message(format, *args)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 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
520 """Log an arbitrary message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
522 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
523 it if you have specific logging wishes.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
525 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
526 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
527 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
528 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
529 printf!).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
76618
6adad27fcc8c Fix issue6085 - Remove the delay caused by fqdn lookup while logging in BaseHTTPRequestHandler
Senthil Kumaran <senthil@uthcode.com>
parents: 76248
diff changeset
531 The client ip and current date/time are prefixed to
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
532 every message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
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 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
536 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
537 (self.address_string(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
538 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
539 format%args))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 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
542 """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
543 return self.server_version + ' ' + self.sys_version
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
545 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
546 """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
547 if timestamp is None:
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
548 timestamp = time.time()
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 35151
diff changeset
549 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
550 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
551 self.weekdayname[wd],
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
552 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
553 hh, mm, ss)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
554 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 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
557 """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
558 now = time.time()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
559 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
560 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
561 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
562 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 monthname = [None,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
567 '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
568 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 def address_string(self):
76614
64a6824d129d Fix Issue6085 - SimpleHTTPServer address_string to return client ip instead of client hostname
Senthil Kumaran <senthil@uthcode.com>
parents: 76249
diff changeset
571 """Return the client address."""
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
76614
64a6824d129d Fix Issue6085 - SimpleHTTPServer address_string to return client ip instead of client hostname
Senthil Kumaran <senthil@uthcode.com>
parents: 76249
diff changeset
573 return self.client_address[0]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 # Essentially static class variables
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 # 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
578 # Set this to HTTP/1.1 to enable automatic keepalive
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 protocol_version = "HTTP/1.0"
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47318
diff changeset
581 # 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
582 MessageClass = http.client.HTTPMessage
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
583
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 # Table mapping response codes to messages; entries have the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 # form {code: (shortmessage, longmessage)}.
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
586 # See RFC 2616 and 6585.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 responses = {
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
588 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
589 101: ('Switching Protocols',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
590 '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
591
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
592 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
593 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
594 202: ('Accepted',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
595 '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
596 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
597 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
598 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
599 206: ('Partial Content', 'Partial content follows.'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
600
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
601 300: ('Multiple Choices',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
602 '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
603 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
604 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
605 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
606 304: ('Not Modified',
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
607 '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
608 305: ('Use Proxy',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
609 '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
610 'resource.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
611 307: ('Temporary Redirect',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
612 'Object moved temporarily -- see URI list'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
613
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
614 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
615 '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
616 401: ('Unauthorized',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
617 '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
618 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
619 '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
620 403: ('Forbidden',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
621 '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
622 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
623 405: ('Method Not Allowed',
58996
0369bbaa5d2d Merged revisions 78301 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 57186
diff changeset
624 '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
625 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
626 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
627 '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
628 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
629 409: ('Conflict', 'Request conflict.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
630 410: ('Gone',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
631 '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
632 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
633 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
634 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
635 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
636 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
637 416: ('Requested Range Not Satisfiable',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
638 'Cannot satisfy request range.'),
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
639 417: ('Expectation Failed',
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
640 'Expect condition could not be satisfied.'),
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
641 428: ('Precondition Required',
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
642 'The origin server requires the request to be conditional.'),
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
643 429: ('Too Many Requests', 'The user has sent too many requests '
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
644 'in a given amount of time ("rate limiting").'),
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
645 431: ('Request Header Fields Too Large', 'The server is unwilling to '
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
646 'process the request because its header fields are too large.'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
647
36559
09230357f927 Move "httpresponses" dict from urllib2 to httplib where it belongs.
Georg Brandl <georg@python.org>
parents: 36555
diff changeset
648 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
649 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
650 '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
651 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
652 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
653 '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
654 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
655 '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
656 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
76985
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
657 511: ('Network Authentication Required',
981aabe6ea2f #14809: Add HTTP status codes from RFC 6585 to http.server and http.client
Hynek Schlawack <hs@ox.cx>
parents: 76619
diff changeset
658 'The client needs to authenticate to gain network access.'),
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
659 }
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
662 class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
663
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
664 """Simple HTTP request handler with GET and HEAD commands.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
665
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
666 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
667 subdirectories. The MIME type for files is determined by
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
668 calling the .guess_type() method.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
669
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
670 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
671 request omits the actual contents of the file.
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 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
674
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
675 server_version = "SimpleHTTP/" + __version__
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
676
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
677 def do_GET(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
678 """Serve a GET request."""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
679 f = self.send_head()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
680 if f:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
681 self.copyfile(f, self.wfile)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
682 f.close()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
683
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
684 def do_HEAD(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
685 """Serve a HEAD request."""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
686 f = self.send_head()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
687 if f:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
688 f.close()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
689
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
690 def send_head(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
691 """Common code for GET and HEAD commands.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
692
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
693 This sends the response code and MIME headers.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
694
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
695 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
696 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
697 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
698 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
699
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
700 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
701 path = self.translate_path(self.path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
702 f = None
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
703 if os.path.isdir(path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
704 if not self.path.endswith('/'):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
705 # redirect browser - doing basically what apache does
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
706 self.send_response(301)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
707 self.send_header("Location", self.path + "/")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
708 self.end_headers()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
709 return None
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
710 for index in "index.html", "index.htm":
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
711 index = os.path.join(path, index)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
712 if os.path.exists(index):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
713 path = index
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
714 break
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
715 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
716 return self.list_directory(path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
717 ctype = self.guess_type(path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
718 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
719 f = open(path, 'rb')
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
720 except OSError:
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
721 self.send_error(404, "File not found")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
722 return None
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
723 self.send_response(200)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
724 self.send_header("Content-type", ctype)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
725 fs = os.fstat(f.fileno())
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
726 self.send_header("Content-Length", str(fs[6]))
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
727 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
728 self.end_headers()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
729 return f
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 list_directory(self, path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
732 """Helper to produce a directory listing (absent index.html).
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 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
735 error). In either case, the headers are sent, making the
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
736 interface the same as for send_head().
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
737
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 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
740 list = os.listdir(path)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79664
diff changeset
741 except OSError:
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
742 self.send_error(404, "No permission to list directory")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
743 return None
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
744 list.sort(key=lambda a: a.lower())
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
745 r = []
65362
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents: 65075
diff changeset
746 displaypath = html.escape(urllib.parse.unquote(self.path))
73303
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
747 enc = sys.getfilesystemencoding()
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
748 title = 'Directory listing for %s' % displaypath
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
749 r.append('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
750 '"http://www.w3.org/TR/html4/strict.dtd">')
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
751 r.append('<html>\n<head>')
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
752 r.append('<meta http-equiv="Content-Type" '
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
753 'content="text/html; charset=%s">' % enc)
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
754 r.append('<title>%s</title>\n</head>' % title)
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
755 r.append('<body>\n<h1>%s</h1>' % title)
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
756 r.append('<hr>\n<ul>')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
757 for name in list:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
758 fullname = os.path.join(path, name)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
759 displayname = linkname = name
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
760 # Append / for directories or @ for symbolic links
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
761 if os.path.isdir(fullname):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
762 displayname = name + "/"
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
763 linkname = name + "/"
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
764 if os.path.islink(fullname):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
765 displayname = name + "@"
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
766 # Note: a link to a directory displays with @ and links with /
73303
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
767 r.append('<li><a href="%s">%s</a></li>'
65362
5633af590057 #2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents: 65075
diff changeset
768 % (urllib.parse.quote(linkname), html.escape(displayname)))
73303
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
769 r.append('</ul>\n<hr>\n</body>\n</html>\n')
10823e7be085 #13295: http.server now produces valid HTML 4.01 strict.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70908
diff changeset
770 encoded = '\n'.join(r).encode(enc)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
771 f = io.BytesIO()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
772 f.write(encoded)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
773 f.seek(0)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
774 self.send_response(200)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
775 self.send_header("Content-type", "text/html; charset=%s" % enc)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
776 self.send_header("Content-Length", str(len(encoded)))
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
777 self.end_headers()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
778 return f
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 def translate_path(self, path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
781 """Translate a /-separated PATH to the local filename syntax.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
782
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
783 Components that mean special things to the local file system
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
784 (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
785 probably be diagnosed.)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
786
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
787 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
788 # abandon query parameters
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
789 path = path.split('?',1)[0]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
790 path = path.split('#',1)[0]
85671
1fcccbbe15e2 Fix http.server's request handling case on trailing '/'.
Senthil Kumaran <senthil@uthcode.com>
parents: 82496
diff changeset
791 # Don't forget explicit trailing slash when normalizing. Issue17324
85878
e27148c51f05 Minor code improvement. Review comment by Eric V. Smith
Senthil Kumaran <senthil@uthcode.com>
parents: 85671
diff changeset
792 trailing_slash = path.rstrip().endswith('/')
48334
5a416a6417d3 Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 47657
diff changeset
793 path = posixpath.normpath(urllib.parse.unquote(path))
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
794 words = path.split('/')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
795 words = filter(None, words)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
796 path = os.getcwd()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
797 for word in words:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
798 drive, word = os.path.splitdrive(word)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
799 head, word = os.path.split(word)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
800 if word in (os.curdir, os.pardir): continue
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
801 path = os.path.join(path, word)
85671
1fcccbbe15e2 Fix http.server's request handling case on trailing '/'.
Senthil Kumaran <senthil@uthcode.com>
parents: 82496
diff changeset
802 if trailing_slash:
1fcccbbe15e2 Fix http.server's request handling case on trailing '/'.
Senthil Kumaran <senthil@uthcode.com>
parents: 82496
diff changeset
803 path += '/'
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
804 return path
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
805
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
806 def copyfile(self, source, outputfile):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
807 """Copy all data between two file objects.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
808
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
809 The SOURCE argument is a file object open for reading
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
810 (or anything with a read() method) and the DESTINATION
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
811 argument is a file object open for writing (or
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
812 anything with a write() method).
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
813
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
814 The only reason for overriding this would be to change
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
815 the block size or perhaps to replace newlines by CRLF
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
816 -- note however that this the default server uses this
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
817 to copy binary data as well.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
818
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
819 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
820 shutil.copyfileobj(source, outputfile)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
821
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
822 def guess_type(self, path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
823 """Guess the type of a file.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
824
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
825 Argument is a PATH (a filename).
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
826
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
827 Return value is a string of the form type/subtype,
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
828 usable for a MIME Content-type header.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
829
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
830 The default implementation looks the file's extension
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
831 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
832 as a default; however it would be permissible (if
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
833 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
834
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
835 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
836
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
837 base, ext = posixpath.splitext(path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
838 if ext in self.extensions_map:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
839 return self.extensions_map[ext]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
840 ext = ext.lower()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
841 if ext in self.extensions_map:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
842 return self.extensions_map[ext]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
843 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
844 return self.extensions_map['']
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
845
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
846 if not mimetypes.inited:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
847 mimetypes.init() # try to read system mime.types
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
848 extensions_map = mimetypes.types_map.copy()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
849 extensions_map.update({
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
850 '': 'application/octet-stream', # Default
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
851 '.py': 'text/plain',
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
852 '.c': 'text/plain',
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
853 '.h': 'text/plain',
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
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 # Utilities for CGIHTTPRequestHandler
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
858
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
859 def _url_collapse_path(path):
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
860 """
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
861 Given a URL path, remove extra '/'s and '.' path elements and collapse
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
862 any '..' references and returns a colllapsed path.
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
863
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
864 Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
865 The utility of this function is limited to is_cgi method and helps
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
866 preventing some security attacks.
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
867
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
868 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
869 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
870 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
871
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
872 Raises: IndexError if too many '..' occur within the path.
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
873
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
874 """
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
875 # 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
876 # path semantics rather than local operating system semantics.
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
877 path_parts = path.split('/')
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
878 head_parts = []
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
879 for part in path_parts[:-1]:
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
880 if part == '..':
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
881 head_parts.pop() # IndexError if more '..' than prior parts
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
882 elif part and part != '.':
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
883 head_parts.append( part )
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
884 if path_parts:
76220
827a4062b1d6 3.2- fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75727
diff changeset
885 tail_part = path_parts.pop()
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
886 if tail_part:
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
887 if tail_part == '..':
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
888 head_parts.pop()
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
889 tail_part = ''
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
890 elif tail_part == '.':
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
891 tail_part = ''
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
892 else:
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
893 tail_part = ''
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
894
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
895 splitpath = ('/' + '/'.join(head_parts), tail_part)
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
896 collapsed_path = "/".join(splitpath)
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
897
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
898 return collapsed_path
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
899
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
900
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
901
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
902 nobody = None
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
903
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
904 def nobody_uid():
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
905 """Internal routine to get nobody's uid"""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
906 global nobody
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
907 if nobody:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
908 return nobody
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
909 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
910 import pwd
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
911 except ImportError:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
912 return -1
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
913 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
914 nobody = pwd.getpwnam('nobody')[2]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
915 except KeyError:
66686
8b02f7f9475b #1513299: cleanup some map() uses where a comprehension works better.
Georg Brandl <georg@python.org>
parents: 66351
diff changeset
916 nobody = 1 + max(x[2] for x in pwd.getpwall())
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
917 return nobody
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
918
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 executable(path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
921 """Test for executable file."""
70908
ecef74419d55 Close #12289: Fix "is executable?" test in the CGI server
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69981
diff changeset
922 return os.access(path, os.X_OK)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
923
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 class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
926
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
927 """Complete HTTP server with GET, HEAD and POST commands.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
928
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
929 GET and HEAD also support running CGI scripts.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
930
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
931 The POST command is *only* implemented for CGI scripts.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
932
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
933 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
934
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
935 # Determine platform specifics
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
936 have_fork = hasattr(os, 'fork')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
937
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
938 # 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
939 # 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
940 rbufsize = 0
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
941
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
942 def do_POST(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
943 """Serve a POST request.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
944
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
945 This is only implemented for CGI scripts.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
946
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
947 """
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
948
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
949 if self.is_cgi():
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
950 self.run_cgi()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
951 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
952 self.send_error(501, "Can only POST to CGI scripts")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
953
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
954 def send_head(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
955 """Version of send_head that support CGI scripts"""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
956 if self.is_cgi():
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
957 return self.run_cgi()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
958 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
959 return SimpleHTTPRequestHandler.send_head(self)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
960
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
961 def is_cgi(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
962 """Test whether self.path corresponds to a CGI script.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
963
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
964 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
965 (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
966 Returns False otherwise.
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
967
53921
cb7be63b91d8 port r72246
Benjamin Peterson <benjamin@python.org>
parents: 53073
diff changeset
968 If any exception is raised, the caller should assume that
cb7be63b91d8 port r72246
Benjamin Peterson <benjamin@python.org>
parents: 53073
diff changeset
969 self.path was rejected as invalid and act accordingly.
cb7be63b91d8 port r72246
Benjamin Peterson <benjamin@python.org>
parents: 53073
diff changeset
970
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
971 The default implementation tests whether the normalized url
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
972 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
973 (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
974
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
975 """
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
976 collapsed_path = _url_collapse_path(self.path)
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
977 dir_sep = collapsed_path.find('/', 1)
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
978 head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
76220
827a4062b1d6 3.2- fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75727
diff changeset
979 if head in self.cgi_directories:
827a4062b1d6 3.2- fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75727
diff changeset
980 self.cgi_info = head, tail
53073
3b1b6142690a Merged revisions 71303 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52770
diff changeset
981 return True
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
982 return False
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
983
76248
fc001124a3ee 3.2 - Issue 10484 - Incorporate improvements to CGI module - Suggested by Glenn Linderman. Refactor code and tests
Senthil Kumaran <senthil@uthcode.com>
parents: 76220
diff changeset
984
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
985 cgi_directories = ['/cgi-bin', '/htbin']
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
986
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
987 def is_executable(self, path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
988 """Test whether argument path is an executable file."""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
989 return executable(path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
990
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
991 def is_python(self, path):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
992 """Test whether argument path is a Python script."""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
993 head, tail = os.path.splitext(path)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
994 return tail.lower() in (".py", ".pyw")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
995
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
996 def run_cgi(self):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
997 """Execute a CGI script."""
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
998 dir, rest = self.cgi_info
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
999
87169
691b3df5036b Transplant of rev 544b654d000c: directory traversal attack in CGIHttpRequestHandler.
Georg Brandl <georg@python.org>
parents: 85878
diff changeset
1000 i = rest.find('/')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1001 while i >= 0:
87169
691b3df5036b Transplant of rev 544b654d000c: directory traversal attack in CGIHttpRequestHandler.
Georg Brandl <georg@python.org>
parents: 85878
diff changeset
1002 nextdir = rest[:i]
691b3df5036b Transplant of rev 544b654d000c: directory traversal attack in CGIHttpRequestHandler.
Georg Brandl <georg@python.org>
parents: 85878
diff changeset
1003 nextrest = rest[i+1:]
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1004
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1005 scriptdir = self.translate_path(nextdir)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1006 if os.path.isdir(scriptdir):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1007 dir, rest = nextdir, nextrest
87169
691b3df5036b Transplant of rev 544b654d000c: directory traversal attack in CGIHttpRequestHandler.
Georg Brandl <georg@python.org>
parents: 85878
diff changeset
1008 i = rest.find('/')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1009 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1010 break
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1011
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1012 # find an explicit query string, if present.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1013 i = rest.rfind('?')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1014 if i >= 0:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1015 rest, query = rest[:i], rest[i+1:]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1016 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1017 query = ''
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1018
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1019 # 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
1020 # a possible additional path, to be stored in PATH_INFO.
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1021 i = rest.find('/')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1022 if i >= 0:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1023 script, rest = rest[:i], rest[i:]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1024 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1025 script, rest = rest, ''
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1026
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1027 scriptname = dir + '/' + script
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1028 scriptfile = self.translate_path(scriptname)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1029 if not os.path.exists(scriptfile):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1030 self.send_error(404, "No such CGI script (%r)" % scriptname)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1031 return
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1032 if not os.path.isfile(scriptfile):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1033 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
1034 scriptname)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1035 return
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1036 ispy = self.is_python(scriptname)
70908
ecef74419d55 Close #12289: Fix "is executable?" test in the CGI server
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69981
diff changeset
1037 if self.have_fork or not ispy:
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1038 if not self.is_executable(scriptfile):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1039 self.send_error(403, "CGI script is not executable (%r)" %
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1040 scriptname)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1041 return
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1042
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1043 # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1044 # XXX Much of the following could be prepared ahead of time!
65076
945a55d4e73e Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63443
diff changeset
1045 env = copy.deepcopy(os.environ)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1046 env['SERVER_SOFTWARE'] = self.version_string()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1047 env['SERVER_NAME'] = self.server.server_name
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1048 env['GATEWAY_INTERFACE'] = 'CGI/1.1'
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1049 env['SERVER_PROTOCOL'] = self.protocol_version
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1050 env['SERVER_PORT'] = str(self.server.server_port)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1051 env['REQUEST_METHOD'] = self.command
48334
5a416a6417d3 Make a new urllib package .
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 47657
diff changeset
1052 uqrest = urllib.parse.unquote(rest)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1053 env['PATH_INFO'] = uqrest
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1054 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1055 env['SCRIPT_NAME'] = scriptname
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1056 if query:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1057 env['QUERY_STRING'] = query
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1058 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
1059 authorization = self.headers.get("authorization")
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1060 if authorization:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1061 authorization = authorization.split()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1062 if len(authorization) == 2:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1063 import base64, binascii
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1064 env['AUTH_TYPE'] = authorization[0]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1065 if authorization[0].lower() == "basic":
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1066 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1067 authorization = authorization[1].encode('ascii')
54568
031b963f2bc2 More codestring -> codebytes.
Georg Brandl <georg@python.org>
parents: 53921
diff changeset
1068 authorization = base64.decodebytes(authorization).\
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1069 decode('ascii')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1070 except (binascii.Error, UnicodeError):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1071 pass
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1072 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1073 authorization = authorization.split(':')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1074 if len(authorization) == 2:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1075 env['REMOTE_USER'] = authorization[0]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1076 # 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
1077 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
1078 env['CONTENT_TYPE'] = self.headers.get_content_type()
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1079 else:
47635
f31e32a2013c Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
Barry Warsaw <barry@python.org>
parents: 47318
diff changeset
1080 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
1081 length = self.headers.get('content-length')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1082 if length:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1083 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
1084 referer = self.headers.get('referer')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1085 if referer:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1086 env['HTTP_REFERER'] = referer
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1087 accept = []
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1088 for line in self.headers.getallmatchingheaders('accept'):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1089 if line[:1] in "\t\n\r ":
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1090 accept.append(line.strip())
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1091 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1092 accept = accept + line[7:].split(',')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1093 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
1094 ua = self.headers.get('user-agent')
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1095 if ua:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1096 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
1097 co = filter(None, self.headers.get_all('cookie', []))
63443
93b3ae63d2f3 Merged revisions 83371,83390 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58996
diff changeset
1098 cookie_str = ', '.join(co)
93b3ae63d2f3 Merged revisions 83371,83390 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58996
diff changeset
1099 if cookie_str:
93b3ae63d2f3 Merged revisions 83371,83390 via svnmerge from
Georg Brandl <georg@python.org>
parents: 58996
diff changeset
1100 env['HTTP_COOKIE'] = cookie_str
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1101 # XXX Other HTTP_* headers
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1102 # 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
1103 # values to override previously set values
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1104 for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1105 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1106 env.setdefault(k, "")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1107
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1108 self.send_response(200, "Script output follows")
69981
25298224cb25 Issue #3709: a flush_headers method to BaseHTTPRequestHandler which manages the
Senthil Kumaran <senthil@uthcode.com>
parents: 68707
diff changeset
1109 self.flush_headers()
47318
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 decoded_query = query.replace('+', ' ')
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1112
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1113 if self.have_fork:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1114 # Unix -- fork as we should
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1115 args = [script]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1116 if '=' not in decoded_query:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1117 args.append(decoded_query)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1118 nobody = nobody_uid()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1119 self.wfile.flush() # Always flush before forking
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1120 pid = os.fork()
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1121 if pid != 0:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1122 # Parent
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1123 pid, sts = os.waitpid(pid, 0)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1124 # throw away additional data [see bug #427345]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1125 while select.select([self.rfile], [], [], 0)[0]:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1126 if not self.rfile.read(1):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1127 break
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1128 if sts:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1129 self.log_error("CGI script exit status %#x", sts)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1130 return
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1131 # Child
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1132 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1133 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1134 os.setuid(nobody)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79664
diff changeset
1135 except OSError:
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1136 pass
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1137 os.dup2(self.rfile.fileno(), 0)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1138 os.dup2(self.wfile.fileno(), 1)
65076
945a55d4e73e Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63443
diff changeset
1139 os.execve(scriptfile, args, env)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1140 except:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1141 self.server.handle_error(self.request, self.client_address)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1142 os._exit(127)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1143
48335
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1144 else:
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1145 # Non-Unix -- use subprocess
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1146 import subprocess
57186
c85eb5946614 Merged revisions 76210 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 54568
diff changeset
1147 cmdline = [scriptfile]
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1148 if self.is_python(scriptfile):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1149 interp = sys.executable
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1150 if interp.lower().endswith("w.exe"):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1151 # On Windows, use python.exe, not pythonw.exe
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1152 interp = interp[:-5] + interp[-4:]
57186
c85eb5946614 Merged revisions 76210 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 54568
diff changeset
1153 cmdline = [interp, '-u'] + cmdline
c85eb5946614 Merged revisions 76210 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 54568
diff changeset
1154 if '=' not in query:
c85eb5946614 Merged revisions 76210 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 54568
diff changeset
1155 cmdline.append(query)
c85eb5946614 Merged revisions 76210 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 54568
diff changeset
1156 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1157 try:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1158 nbytes = int(length)
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1159 except (TypeError, ValueError):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1160 nbytes = 0
48335
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1161 p = subprocess.Popen(cmdline,
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1162 stdin=subprocess.PIPE,
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1163 stdout=subprocess.PIPE,
65076
945a55d4e73e Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63443
diff changeset
1164 stderr=subprocess.PIPE,
945a55d4e73e Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 63443
diff changeset
1165 env = env
48335
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1166 )
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1167 if self.command.lower() == "post" and nbytes > 0:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1168 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
1169 else:
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1170 data = None
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1171 # throw away additional data [see bug #427345]
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1172 while select.select([self.rfile._sock], [], [], 0)[0]:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1173 if not self.rfile._sock.recv(1):
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1174 break
48335
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1175 stdout, stderr = p.communicate(data)
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1176 self.wfile.write(stdout)
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1177 if stderr:
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1178 self.log_error('%s', stderr)
65966
1243ec679c29 Merged revisions 86195 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65076
diff changeset
1179 p.stderr.close()
1243ec679c29 Merged revisions 86195 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65076
diff changeset
1180 p.stdout.close()
48335
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1181 status = p.returncode
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1182 if status:
69040092b8f8 Issue3113: tests for CGIHTTPRequestHandler failed on windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48334
diff changeset
1183 self.log_error("CGI script exit status %#x", status)
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1184 else:
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1185 self.log_message("CGI script exited OK")
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1186
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47278
diff changeset
1187
85713
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1188 def test(HandlerClass=BaseHTTPRequestHandler,
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1189 ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 """Test the HTTP request handler class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 This runs an HTTP server on port 8000 (or the first command line
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 argument).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 """
85713
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1196 server_address = (bind, port)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197
22346
27f36f4bf525 Patch #430706: Persistent connections in BaseHTTPServer.
Martin v. Löwis <martin@v.loewis.de>
parents: 22182
diff changeset
1198 HandlerClass.protocol_version = protocol
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 httpd = ServerClass(server_address, HandlerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200
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
1201 sa = httpd.socket.getsockname()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1202 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
1203 try:
4757ddaa897d Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52369
diff changeset
1204 httpd.serve_forever()
4757ddaa897d Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52369
diff changeset
1205 except KeyboardInterrupt:
4757ddaa897d Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52369
diff changeset
1206 print("\nKeyboard interrupt received, exiting.")
4757ddaa897d Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52369
diff changeset
1207 httpd.server_close()
4757ddaa897d Make http.server main program nicer for interactive use.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 52369
diff changeset
1208 sys.exit(0)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 if __name__ == '__main__':
77320
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1211 parser = argparse.ArgumentParser()
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1212 parser.add_argument('--cgi', action='store_true',
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1213 help='Run as CGI Server')
85713
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1214 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1215 help='Specify alternate bind address '
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1216 '[default: all interfaces]')
77320
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1217 parser.add_argument('port', action='store',
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1218 default=8000, type=int,
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1219 nargs='?',
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1220 help='Specify alternate port [default: 8000]')
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1221 args = parser.parse_args()
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1222 if args.cgi:
85713
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1223 handler_class = CGIHTTPRequestHandler
77320
935a656359ae Issue 14989: http.server --cgi option can enable the CGI http server.
Senthil Kumaran <senthil@uthcode.com>
parents: 76985
diff changeset
1224 else:
85713
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1225 handler_class = SimpleHTTPRequestHandler
a248655c8261 Expose --bind argument for http.server, enable http.server to bind to a user
Senthil Kumaran <senthil@uthcode.com>
parents: 85672
diff changeset
1226 test(HandlerClass=handler_class, port=args.port, bind=args.bind)