annotate Lib/BaseHTTPServer.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents f8a9db67fa5b
children 27f36f4bf525
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """HTTP server base class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Note: the class in this module doesn't implement any HTTP request; see
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 SimpleHTTPServer for simple implementations of GET, HEAD and POST
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 (including CGI scripts).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Contents:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 - BaseHTTPRequestHandler: HTTP request handler base class
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 - test: test function
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 XXX To do:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 - send server version
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 - log requests even later (to capture byte count)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 - log user-agent header and other interesting goodies
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 - send error log to separate file
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 - are request names really case sensitive?
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # See also:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # HTTP Working Group T. Berners-Lee
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # INTERNET-DRAFT R. T. Fielding
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Expires September 8, 1995 March 8, 1995
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
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 # Log files
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 # ---------
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
35 #
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # 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
37 #
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
38 # | The logfile format is as follows. Each line consists of:
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
39 # |
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
40 # | 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
41 # |
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
42 # | 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
43 # | rfc931: Any information returned by identd for this person,
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
44 # | - otherwise.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 # | 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
46 # | - otherwise.
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
47 # | DD: Day
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
48 # | Mon: Month (calendar name)
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
49 # | YYYY: Year
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
50 # | hh: hour (24-hour format, the machine's timezone)
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
51 # | mm: minutes
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
52 # | ss: seconds
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
53 # | 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
54 # | ddd: the status code returned by the server, - if not available.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # | bbbb: the total number of bytes sent,
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
56 # | *not including the HTTP/1.0 header*, - if not available
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
57 # |
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # | 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
59 #
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 # (Actually, the latter is only true if you know the server configuration
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # at the time the request was made!)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 __version__ = "0.2"
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
66 __all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 import sys
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 import time
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 import socket # For gethostbyaddr()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 import mimetools
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 import SocketServer
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 # Default error message
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 DEFAULT_ERROR_MESSAGE = """\
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 <head>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 <title>Error response</title>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 </head>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 <body>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 <h1>Error response</h1>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 <p>Error code %(code)d.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 <p>Message: %(message)s.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 <p>Error code explanation: %(code)s = %(explain)s.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 </body>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 class HTTPServer(SocketServer.TCPServer):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
12634
ddf986a971b5 Set HTTPServer class variable allow_reuse_address to 1, so restarting
Guido van Rossum <guido@python.org>
parents: 11409
diff changeset
90 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
91
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 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
93 """Override server_bind to store the server name."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
94 SocketServer.TCPServer.server_bind(self)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
95 host, port = self.socket.getsockname()
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12634
diff changeset
96 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
97 self.server_port = port
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 """HTTP request handler base class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 The following explanation of HTTP serves to guide you through the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 code as well as to expose any misunderstandings I may have about
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 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
107 :-).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 HTTP (HyperText Transfer Protocol) is an extensible protocol on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 top of a reliable stream transport (e.g. TCP/IP). The protocol
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 recognizes three parts to a request:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 1. One line identifying the request type and path
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 3. An optional data part
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 The headers and data are separated by a blank line.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 The first line of the request has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 <command> <path> <version>
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 where <command> is a (case-sensitive) keyword such as GET or POST,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 <path> is a string containing path information for the request,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 and <version> should be the string "HTTP/1.0". <path> is encoded
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 using the URL encoding scheme (using %xx to signify the ASCII
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 character with hex code xx).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 The protocol is vague about whether lines are separated by LF
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 characters or by CRLF pairs -- for compatibility with the widest
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 range of clients, both should be accepted. Similarly, whitespace
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 in the request line should be treated sensibly (allowing multiple
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 spaces between components and allowing trailing whitespace).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 Similarly, for output, lines ought to be separated by CRLF pairs
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 but most clients grok LF characters just fine.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 If the first line of the request has the form
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 <command> <path>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 (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
143 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
144 the reply consists of just the data.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 The reply form of the HTTP 1.0 protocol again has three parts:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 1. One line giving the response code
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 2. An optional set of RFC-822-style headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 3. The data
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 Again, the headers and data are separated by a blank line.
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 The response code line has the form
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 <version> <responsecode> <responsestring>
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 where <version> is the protocol version (always "HTTP/1.0"),
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 <responsecode> is a 3-digit response code indicating success or
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 failure of the request, and <responsestring> is an optional
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 human-readable string explaining what the response code means.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 This server parses the request and the headers, and then calls a
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165 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
166 such method exists the server sends an error response to the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 client. If it exists, it is called with no arguments:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 do_SPAM()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 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
172 are different requests).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 The various request details are stored in instance variables:
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 - client_address is the client IP address in the form (host,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 port);
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 - command, path and version are the broken-down request line;
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 - headers is an instance of mimetools.Message (or a derived
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 class) containing the header information;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 - rfile is a file object open for reading positioned at the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 start of the optional input data part;
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 - wfile is a file object open for writing.
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 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 The first thing to be written must be the response line. Then
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 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
193 actual data (if any). The meaning of the header lines depends on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 the command executed by the server; in most cases, when data is
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 returned, there should be at least one header line of 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 Content-type: <type>/<subtype>
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 where <type> and <subtype> should be registered MIME types,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 e.g. "text/html" or "text/plain".
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 """
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 # The Python system version, truncated to its first component.
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
205 sys_version = "Python/" + sys.version.split()[0]
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 # The server software version. You may want to override this.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 # The format is multiple whitespace-separated strings,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 # where each string is of the form name[/version].
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 server_version = "BaseHTTP/" + __version__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
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
212 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
213 """Parse a request (internal).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
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
215 The request should be stored in self.raw_request; the results
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
216 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
217 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
218
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
219 Return value is 1 for success, 0 for failure; on failure, an
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
220 error is sent back.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
222 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
223 self.request_version = version = "HTTP/0.9" # Default
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
224 requestline = self.raw_requestline
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
225 if requestline[-2:] == '\r\n':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
226 requestline = requestline[:-2]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
227 elif requestline[-1:] == '\n':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
228 requestline = requestline[:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
229 self.requestline = requestline
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
230 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
231 if len(words) == 3:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
232 [command, path, version] = words
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
233 if version[:5] != 'HTTP/':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
234 self.send_error(400, "Bad request version (%s)" % `version`)
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
235 return 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
236 elif len(words) == 2:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
237 [command, path] = words
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
238 if command != 'GET':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
239 self.send_error(400,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
240 "Bad HTTP/0.9 request type (%s)" % `command`)
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
241 return 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
242 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
243 self.send_error(400, "Bad request syntax (%s)" % `requestline`)
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
244 return 0
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
245 self.command, self.path, self.request_version = command, path, version
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
246 self.headers = self.MessageClass(self.rfile, 0)
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
247 return 1
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
248
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
249 def handle(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
250 """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
251
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
252 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
253 __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
254 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
255
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
256 """
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
257
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
258 self.raw_requestline = self.rfile.readline()
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
259 if not self.parse_request(): # An error code has been sent, just exit
061156451d36 Fix by Moshe Zadka (cleaned up and documented by GvR) to break out the
Guido van Rossum <guido@python.org>
parents: 11287
diff changeset
260 return
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
261 mname = 'do_' + self.command
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
262 if not hasattr(self, mname):
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
263 self.send_error(501, "Unsupported method (%s)" % `self.command`)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
264 return
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
265 method = getattr(self, mname)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
266 method()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 def send_error(self, code, message=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
269 """Send and log an error reply.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
271 Arguments are the error code, and a detailed message.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
272 The detailed message defaults to the short entry matching the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
273 response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
275 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
276 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
277 a piece of HTML explaining the error to the user.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
279 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
281 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
282 short, long = self.responses[code]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
283 except KeyError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
284 short, long = '???', '???'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
285 if not message:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
286 message = short
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
287 explain = long
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
288 self.log_error("code %d, message %s", code, message)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
289 self.send_response(code, message)
22182
f8a9db67fa5b backport montanaro's checkin of
Michael W. Hudson <mwh@python.net>
parents: 18826
diff changeset
290 self.send_header("Content-Type", "text/html")
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
291 self.end_headers()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
292 self.wfile.write(self.error_message_format %
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
293 {'code': code,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
294 'message': message,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
295 'explain': explain})
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 error_message_format = DEFAULT_ERROR_MESSAGE
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 def send_response(self, code, message=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
300 """Send the response header and log the response code.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
302 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
303 version and the current date.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
305 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
306 self.log_request(code)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
307 if message is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
308 if self.responses.has_key(code):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
309 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
310 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
311 message = ''
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
312 if self.request_version != 'HTTP/0.9':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
313 self.wfile.write("%s %s %s\r\n" %
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
314 (self.protocol_version, str(code), message))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
315 self.send_header('Server', self.version_string())
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
316 self.send_header('Date', self.date_time_string())
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 def send_header(self, keyword, value):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
319 """Send a MIME header."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
320 if self.request_version != 'HTTP/0.9':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
321 self.wfile.write("%s: %s\r\n" % (keyword, value))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 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
324 """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
325 if self.request_version != 'HTTP/0.9':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
326 self.wfile.write("\r\n")
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 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
329 """Log an accepted request.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
331 This is called by send_reponse().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
333 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
335 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
336 self.requestline, str(code), str(size))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 def log_error(self, *args):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
339 """Log an error.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
341 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
342 default it passes the message on to log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
344 Arguments are the same as for log_message().
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
346 XXX This should go to the separate error log.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
348 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
350 apply(self.log_message, args)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 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
353 """Log an arbitrary message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
355 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
356 it if you have specific logging wishes.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
358 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
359 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
360 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
361 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
362 printf!).
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
364 The client host and current date/time are prefixed to
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
365 every message.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
367 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
369 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
370 (self.address_string(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
371 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
372 format%args))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 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
375 """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
376 return self.server_version + ' ' + self.sys_version
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 def 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
379 """Return the current date and time formatted for a message header."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
380 now = time.time()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
381 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
382 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
383 self.weekdayname[wd],
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
384 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
385 hh, mm, ss)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
386 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 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
389 """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
390 now = time.time()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
391 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
392 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
393 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
394 return s
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 monthname = [None,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
399 '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
400 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 def address_string(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
403 """Return the client address formatted for logging.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
405 This version looks up the full hostname using gethostbyaddr(),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
406 and tries to find a name that contains at least one dot.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
408 """
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12634
diff changeset
410 host, port = self.client_address
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 12634
diff changeset
411 return socket.getfqdn(host)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 # Essentially static class variables
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 # The version of the HTTP protocol we support.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 # Don't override unless you know what you're doing (hint: incoming
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 # requests are required to have exactly this version string).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 protocol_version = "HTTP/1.0"
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 # The Message-like class used to parse headers
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 MessageClass = mimetools.Message
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 # Table mapping response codes to messages; entries have the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 # form {code: (shortmessage, longmessage)}.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 responses = {
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
427 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
428 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
429 202: ('Accepted',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
430 'Request accepted, processing continues off-line'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
431 203: ('Partial information', 'Request fulfilled from cache'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
432 204: ('No response', 'Request fulfilled, nothing follows'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
433
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
434 301: ('Moved', 'Object moved permanently -- see URI list'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
435 302: ('Found', 'Object moved temporarily -- see URI list'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
436 303: ('Method', 'Object moved -- see Method and URL list'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
437 304: ('Not modified',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
438 'Document has not changed singe given time'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
439
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
440 400: ('Bad request',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
441 '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
442 401: ('Unauthorized',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
443 'No permission -- see authorization schemes'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
444 402: ('Payment required',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
445 '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
446 403: ('Forbidden',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
447 'Request forbidden -- authorization will not help'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
448 404: ('Not found', 'Nothing matches the given URI'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
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 500: ('Internal error', 'Server got itself in trouble'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
451 501: ('Not implemented',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
452 'Server does not support this operation'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
453 502: ('Service temporarily overloaded',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
454 'The server cannot process the request due to a high load'),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
455 503: ('Gateway timeout',
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
456 'The gateway server did not receive a timely response'),
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 14014
diff changeset
457
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
458 }
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 def test(HandlerClass = BaseHTTPRequestHandler,
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
462 ServerClass = HTTPServer):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 """Test the HTTP request handler class.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 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
466 argument).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 """
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 if sys.argv[1:]:
16482
1fb7befb59f0 Correction to test main.
Eric S. Raymond <esr@thyrsus.com>
parents: 16479
diff changeset
471 port = int(sys.argv[1])
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5975
diff changeset
473 port = 8000
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 server_address = ('', port)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
475
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 httpd = ServerClass(server_address, HandlerClass)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
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
478 sa = httpd.socket.getsockname()
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
479 print "Serving HTTP on", sa[0], "port", sa[1], "..."
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 httpd.serve_forever()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 if __name__ == '__main__':
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 test()