1995-08-04 01:00:20 -03:00
|
|
|
"""HTTP server base class.
|
|
|
|
|
|
|
|
Note: the class in this module doesn't implement any HTTP request; see
|
|
|
|
SimpleHTTPServer for simple implementations of GET, HEAD and POST
|
2002-03-17 14:37:22 -04:00
|
|
|
(including CGI scripts). It does, however, optionally implement HTTP/1.1
|
|
|
|
persistent connections, as of version 0.3.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
Contents:
|
|
|
|
|
|
|
|
- BaseHTTPRequestHandler: HTTP request handler base class
|
|
|
|
- test: test function
|
|
|
|
|
|
|
|
XXX To do:
|
|
|
|
|
|
|
|
- log requests even later (to capture byte count)
|
|
|
|
- log user-agent header and other interesting goodies
|
|
|
|
- send error log to separate file
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
# See also:
|
|
|
|
#
|
|
|
|
# HTTP Working Group T. Berners-Lee
|
|
|
|
# INTERNET-DRAFT R. T. Fielding
|
|
|
|
# <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
|
|
|
|
# Expires September 8, 1995 March 8, 1995
|
|
|
|
#
|
|
|
|
# URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
|
2002-03-17 14:37:22 -04:00
|
|
|
#
|
|
|
|
# and
|
|
|
|
#
|
|
|
|
# Network Working Group R. Fielding
|
|
|
|
# Request for Comments: 2616 et al
|
|
|
|
# Obsoletes: 2068 June 1999
|
2002-04-15 22:38:40 -03:00
|
|
|
# Category: Standards Track
|
2002-03-17 14:37:22 -04:00
|
|
|
#
|
|
|
|
# URL: http://www.faqs.org/rfcs/rfc2616.html
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
# Log files
|
|
|
|
# ---------
|
2001-01-14 17:54:20 -04:00
|
|
|
#
|
1995-08-04 01:00:20 -03:00
|
|
|
# Here's a quote from the NCSA httpd docs about log file format.
|
2001-01-14 17:54:20 -04:00
|
|
|
#
|
|
|
|
# | The logfile format is as follows. Each line consists of:
|
|
|
|
# |
|
|
|
|
# | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
|
|
|
|
# |
|
|
|
|
# | host: Either the DNS name or the IP number of the remote client
|
1995-08-04 01:00:20 -03:00
|
|
|
# | rfc931: Any information returned by identd for this person,
|
2001-01-14 17:54:20 -04:00
|
|
|
# | - otherwise.
|
1995-08-04 01:00:20 -03:00
|
|
|
# | authuser: If user sent a userid for authentication, the user name,
|
2001-01-14 17:54:20 -04:00
|
|
|
# | - otherwise.
|
|
|
|
# | DD: Day
|
|
|
|
# | Mon: Month (calendar name)
|
|
|
|
# | YYYY: Year
|
|
|
|
# | hh: hour (24-hour format, the machine's timezone)
|
|
|
|
# | mm: minutes
|
|
|
|
# | ss: seconds
|
|
|
|
# | request: The first line of the HTTP request as sent by the client.
|
|
|
|
# | ddd: the status code returned by the server, - if not available.
|
1995-08-04 01:00:20 -03:00
|
|
|
# | bbbb: the total number of bytes sent,
|
2001-01-14 17:54:20 -04:00
|
|
|
# | *not including the HTTP/1.0 header*, - if not available
|
|
|
|
# |
|
1995-08-04 01:00:20 -03:00
|
|
|
# | You can determine the name of the file accessed through request.
|
2001-01-14 17:54:20 -04:00
|
|
|
#
|
1995-08-04 01:00:20 -03:00
|
|
|
# (Actually, the latter is only true if you know the server configuration
|
|
|
|
# at the time the request was made!)
|
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
__version__ = "0.3"
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2001-01-20 15:54:20 -04:00
|
|
|
__all__ = ["HTTPServer", "BaseHTTPRequestHandler"]
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2007-08-10 15:49:32 -03:00
|
|
|
import io
|
1995-08-04 01:00:20 -03:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import socket # For gethostbyaddr()
|
|
|
|
import mimetools
|
|
|
|
import SocketServer
|
|
|
|
|
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
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60980 | georg.brandl | 2008-02-23 16:02:28 +0100 (Sat, 23 Feb 2008) | 2 lines
#1492: allow overriding BaseHTTPServer's content type for error messages.
........
r60982 | georg.brandl | 2008-02-23 16:06:25 +0100 (Sat, 23 Feb 2008) | 2 lines
#2165: fix test_logging failure on some machines.
........
r60983 | facundo.batista | 2008-02-23 16:07:35 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1089358. Adds the siginterrupt() function, that is just a
wrapper around the system call with the same name. Also added
test cases, doc changes and NEWS entry. Thanks Jason and Ralf
Schmitt.
........
r60984 | georg.brandl | 2008-02-23 16:11:18 +0100 (Sat, 23 Feb 2008) | 2 lines
#2067: file.__exit__() now calls subclasses' close() method.
........
r60985 | georg.brandl | 2008-02-23 16:19:54 +0100 (Sat, 23 Feb 2008) | 2 lines
More difflib examples. Written for GHOP by Josip Dzolonga.
........
r60987 | andrew.kuchling | 2008-02-23 16:41:51 +0100 (Sat, 23 Feb 2008) | 1 line
#2072: correct documentation for .rpc_paths
........
r60988 | georg.brandl | 2008-02-23 16:43:48 +0100 (Sat, 23 Feb 2008) | 2 lines
#2161: Fix opcode name.
........
r60989 | andrew.kuchling | 2008-02-23 16:49:35 +0100 (Sat, 23 Feb 2008) | 2 lines
#1119331: ncurses will just call exit() if the terminal name isn't found.
Call setupterm() first so that we get a Python exception instead of just existing.
........
2008-02-23 12:23:06 -04:00
|
|
|
# Default error message template
|
1995-08-04 01:00:20 -03:00
|
|
|
DEFAULT_ERROR_MESSAGE = """\
|
|
|
|
<head>
|
|
|
|
<title>Error response</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Error response</h1>
|
|
|
|
<p>Error code %(code)d.
|
|
|
|
<p>Message: %(message)s.
|
|
|
|
<p>Error code explanation: %(code)s = %(explain)s.
|
|
|
|
</body>
|
|
|
|
"""
|
|
|
|
|
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
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60980 | georg.brandl | 2008-02-23 16:02:28 +0100 (Sat, 23 Feb 2008) | 2 lines
#1492: allow overriding BaseHTTPServer's content type for error messages.
........
r60982 | georg.brandl | 2008-02-23 16:06:25 +0100 (Sat, 23 Feb 2008) | 2 lines
#2165: fix test_logging failure on some machines.
........
r60983 | facundo.batista | 2008-02-23 16:07:35 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1089358. Adds the siginterrupt() function, that is just a
wrapper around the system call with the same name. Also added
test cases, doc changes and NEWS entry. Thanks Jason and Ralf
Schmitt.
........
r60984 | georg.brandl | 2008-02-23 16:11:18 +0100 (Sat, 23 Feb 2008) | 2 lines
#2067: file.__exit__() now calls subclasses' close() method.
........
r60985 | georg.brandl | 2008-02-23 16:19:54 +0100 (Sat, 23 Feb 2008) | 2 lines
More difflib examples. Written for GHOP by Josip Dzolonga.
........
r60987 | andrew.kuchling | 2008-02-23 16:41:51 +0100 (Sat, 23 Feb 2008) | 1 line
#2072: correct documentation for .rpc_paths
........
r60988 | georg.brandl | 2008-02-23 16:43:48 +0100 (Sat, 23 Feb 2008) | 2 lines
#2161: Fix opcode name.
........
r60989 | andrew.kuchling | 2008-02-23 16:49:35 +0100 (Sat, 23 Feb 2008) | 2 lines
#1119331: ncurses will just call exit() if the terminal name isn't found.
Call setupterm() first so that we get a Python exception instead of just existing.
........
2008-02-23 12:23:06 -04:00
|
|
|
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
|
|
|
|
|
2005-06-26 18:33:14 -03:00
|
|
|
def _quote_html(html):
|
|
|
|
return html.replace("&", "&").replace("<", "<").replace(">", ">")
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
class HTTPServer(SocketServer.TCPServer):
|
|
|
|
|
2000-05-09 11:54:13 -03:00
|
|
|
allow_reuse_address = 1 # Seems to make sense in testing environment
|
|
|
|
|
1995-08-04 01:00:20 -03:00
|
|
|
def server_bind(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Override server_bind to store the server name."""
|
|
|
|
SocketServer.TCPServer.server_bind(self)
|
2003-05-31 04:55:43 -03:00
|
|
|
host, port = self.socket.getsockname()[:2]
|
2000-08-16 17:30:21 -03:00
|
|
|
self.server_name = socket.getfqdn(host)
|
1998-03-26 17:13:24 -04:00
|
|
|
self.server_port = port
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):
|
|
|
|
|
|
|
|
"""HTTP request handler base class.
|
|
|
|
|
|
|
|
The following explanation of HTTP serves to guide you through the
|
|
|
|
code as well as to expose any misunderstandings I may have about
|
|
|
|
HTTP (so you don't need to read the code to figure out I'm wrong
|
|
|
|
:-).
|
|
|
|
|
|
|
|
HTTP (HyperText Transfer Protocol) is an extensible protocol on
|
|
|
|
top of a reliable stream transport (e.g. TCP/IP). The protocol
|
|
|
|
recognizes three parts to a request:
|
|
|
|
|
|
|
|
1. One line identifying the request type and path
|
|
|
|
2. An optional set of RFC-822-style headers
|
|
|
|
3. An optional data part
|
|
|
|
|
|
|
|
The headers and data are separated by a blank line.
|
|
|
|
|
|
|
|
The first line of the request has the form
|
|
|
|
|
|
|
|
<command> <path> <version>
|
|
|
|
|
|
|
|
where <command> is a (case-sensitive) keyword such as GET or POST,
|
|
|
|
<path> is a string containing path information for the request,
|
2002-03-17 14:37:22 -04:00
|
|
|
and <version> should be the string "HTTP/1.0" or "HTTP/1.1".
|
|
|
|
<path> is encoded using the URL encoding scheme (using %xx to signify
|
|
|
|
the ASCII character with hex code xx).
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2003-02-03 11:21:15 -04:00
|
|
|
The specification specifies that lines are separated by CRLF but
|
|
|
|
for compatibility with the widest range of clients recommends
|
|
|
|
servers also handle LF. Similarly, whitespace in the request line
|
|
|
|
is treated sensibly (allowing multiple spaces between components
|
|
|
|
and allowing trailing whitespace).
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
Similarly, for output, lines ought to be separated by CRLF pairs
|
|
|
|
but most clients grok LF characters just fine.
|
|
|
|
|
|
|
|
If the first line of the request has the form
|
|
|
|
|
|
|
|
<command> <path>
|
|
|
|
|
|
|
|
(i.e. <version> is left out) then this is assumed to be an HTTP
|
|
|
|
0.9 request; this form has no optional headers and data part and
|
|
|
|
the reply consists of just the data.
|
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
The reply form of the HTTP 1.x protocol again has three parts:
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
1. One line giving the response code
|
|
|
|
2. An optional set of RFC-822-style headers
|
|
|
|
3. The data
|
|
|
|
|
|
|
|
Again, the headers and data are separated by a blank line.
|
|
|
|
|
|
|
|
The response code line has the form
|
|
|
|
|
|
|
|
<version> <responsecode> <responsestring>
|
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
|
1995-08-04 01:00:20 -03:00
|
|
|
<responsecode> is a 3-digit response code indicating success or
|
|
|
|
failure of the request, and <responsestring> is an optional
|
|
|
|
human-readable string explaining what the response code means.
|
|
|
|
|
|
|
|
This server parses the request and the headers, and then calls a
|
|
|
|
function specific to the request type (<command>). Specifically,
|
1999-09-15 12:28:25 -03:00
|
|
|
a request SPAM will be handled by a method do_SPAM(). If no
|
1995-08-04 01:00:20 -03:00
|
|
|
such method exists the server sends an error response to the
|
|
|
|
client. If it exists, it is called with no arguments:
|
|
|
|
|
|
|
|
do_SPAM()
|
|
|
|
|
|
|
|
Note that the request name is case sensitive (i.e. SPAM and spam
|
|
|
|
are different requests).
|
|
|
|
|
|
|
|
The various request details are stored in instance variables:
|
|
|
|
|
|
|
|
- client_address is the client IP address in the form (host,
|
|
|
|
port);
|
|
|
|
|
|
|
|
- command, path and version are the broken-down request line;
|
|
|
|
|
|
|
|
- headers is an instance of mimetools.Message (or a derived
|
|
|
|
class) containing the header information;
|
|
|
|
|
|
|
|
- rfile is a file object open for reading positioned at the
|
|
|
|
start of the optional input data part;
|
|
|
|
|
|
|
|
- wfile is a file object open for writing.
|
|
|
|
|
|
|
|
IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
|
|
|
|
|
|
|
|
The first thing to be written must be the response line. Then
|
|
|
|
follow 0 or more header lines, then a blank line, and then the
|
|
|
|
actual data (if any). The meaning of the header lines depends on
|
|
|
|
the command executed by the server; in most cases, when data is
|
|
|
|
returned, there should be at least one header line of the form
|
|
|
|
|
|
|
|
Content-type: <type>/<subtype>
|
|
|
|
|
|
|
|
where <type> and <subtype> should be registered MIME types,
|
|
|
|
e.g. "text/html" or "text/plain".
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The Python system version, truncated to its first component.
|
2001-02-09 01:07:04 -04:00
|
|
|
sys_version = "Python/" + sys.version.split()[0]
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
# The server software version. You may want to override this.
|
|
|
|
# The format is multiple whitespace-separated strings,
|
|
|
|
# where each string is of the form name[/version].
|
|
|
|
server_version = "BaseHTTP/" + __version__
|
|
|
|
|
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
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60980 | georg.brandl | 2008-02-23 16:02:28 +0100 (Sat, 23 Feb 2008) | 2 lines
#1492: allow overriding BaseHTTPServer's content type for error messages.
........
r60982 | georg.brandl | 2008-02-23 16:06:25 +0100 (Sat, 23 Feb 2008) | 2 lines
#2165: fix test_logging failure on some machines.
........
r60983 | facundo.batista | 2008-02-23 16:07:35 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1089358. Adds the siginterrupt() function, that is just a
wrapper around the system call with the same name. Also added
test cases, doc changes and NEWS entry. Thanks Jason and Ralf
Schmitt.
........
r60984 | georg.brandl | 2008-02-23 16:11:18 +0100 (Sat, 23 Feb 2008) | 2 lines
#2067: file.__exit__() now calls subclasses' close() method.
........
r60985 | georg.brandl | 2008-02-23 16:19:54 +0100 (Sat, 23 Feb 2008) | 2 lines
More difflib examples. Written for GHOP by Josip Dzolonga.
........
r60987 | andrew.kuchling | 2008-02-23 16:41:51 +0100 (Sat, 23 Feb 2008) | 1 line
#2072: correct documentation for .rpc_paths
........
r60988 | georg.brandl | 2008-02-23 16:43:48 +0100 (Sat, 23 Feb 2008) | 2 lines
#2161: Fix opcode name.
........
r60989 | andrew.kuchling | 2008-02-23 16:49:35 +0100 (Sat, 23 Feb 2008) | 2 lines
#1119331: ncurses will just call exit() if the terminal name isn't found.
Call setupterm() first so that we get a Python exception instead of just existing.
........
2008-02-23 12:23:06 -04:00
|
|
|
error_message_format = DEFAULT_ERROR_MESSAGE
|
|
|
|
error_content_type = DEFAULT_ERROR_CONTENT_TYPE
|
|
|
|
|
1999-10-26 10:01:36 -03:00
|
|
|
def parse_request(self):
|
|
|
|
"""Parse a request (internal).
|
1998-03-26 17:13:24 -04:00
|
|
|
|
2003-06-02 11:25:43 -03:00
|
|
|
The request should be stored in self.raw_requestline; the results
|
1999-10-26 10:01:36 -03:00
|
|
|
are in self.command, self.path, self.request_version and
|
|
|
|
self.headers.
|
1998-03-26 17:13:24 -04:00
|
|
|
|
2002-04-04 18:55:58 -04:00
|
|
|
Return True for success, False for failure; on failure, an
|
1999-10-26 10:01:36 -03:00
|
|
|
error is sent back.
|
1998-03-26 17:13:24 -04:00
|
|
|
|
1999-10-26 10:01:36 -03:00
|
|
|
"""
|
2003-02-03 15:11:18 -04:00
|
|
|
self.command = None # set in case of error on the first line
|
1998-03-26 17:13:24 -04:00
|
|
|
self.request_version = version = "HTTP/0.9" # Default
|
2002-03-17 14:37:22 -04:00
|
|
|
self.close_connection = 1
|
2007-08-03 17:32:27 -03:00
|
|
|
requestline = str(self.raw_requestline, 'iso-8859-1')
|
1998-03-26 17:13:24 -04:00
|
|
|
if requestline[-2:] == '\r\n':
|
|
|
|
requestline = requestline[:-2]
|
|
|
|
elif requestline[-1:] == '\n':
|
|
|
|
requestline = requestline[:-1]
|
|
|
|
self.requestline = requestline
|
2001-02-09 01:07:04 -04:00
|
|
|
words = requestline.split()
|
1998-03-26 17:13:24 -04:00
|
|
|
if len(words) == 3:
|
|
|
|
[command, path, version] = words
|
|
|
|
if version[:5] != 'HTTP/':
|
2004-02-12 13:35:32 -04:00
|
|
|
self.send_error(400, "Bad request version (%r)" % version)
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
2002-03-17 14:37:22 -04:00
|
|
|
try:
|
2003-02-03 15:11:18 -04:00
|
|
|
base_version_number = version.split('/', 1)[1]
|
|
|
|
version_number = base_version_number.split(".")
|
|
|
|
# RFC 2145 section 3.1 says there can be only one "." and
|
|
|
|
# - major and minor numbers MUST be treated as
|
|
|
|
# separate integers;
|
|
|
|
# - HTTP/2.4 is a lower version than HTTP/2.13, which in
|
|
|
|
# turn is lower than HTTP/12.3;
|
|
|
|
# - Leading zeros MUST be ignored by recipients.
|
|
|
|
if len(version_number) != 2:
|
|
|
|
raise ValueError
|
|
|
|
version_number = int(version_number[0]), int(version_number[1])
|
|
|
|
except (ValueError, IndexError):
|
2004-02-12 13:35:32 -04:00
|
|
|
self.send_error(400, "Bad request version (%r)" % version)
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
2003-02-03 15:11:18 -04:00
|
|
|
if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
|
2002-03-17 14:37:22 -04:00
|
|
|
self.close_connection = 0
|
2003-02-03 15:11:18 -04:00
|
|
|
if version_number >= (2, 0):
|
2002-03-17 14:37:22 -04:00
|
|
|
self.send_error(505,
|
2003-02-03 15:11:18 -04:00
|
|
|
"Invalid HTTP Version (%s)" % base_version_number)
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
1998-03-26 17:13:24 -04:00
|
|
|
elif len(words) == 2:
|
|
|
|
[command, path] = words
|
2002-03-17 14:37:22 -04:00
|
|
|
self.close_connection = 1
|
1998-03-26 17:13:24 -04:00
|
|
|
if command != 'GET':
|
|
|
|
self.send_error(400,
|
2004-02-12 13:35:32 -04:00
|
|
|
"Bad HTTP/0.9 request type (%r)" % command)
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
2002-03-17 14:37:22 -04:00
|
|
|
elif not words:
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
1998-03-26 17:13:24 -04:00
|
|
|
else:
|
2004-02-12 13:35:32 -04:00
|
|
|
self.send_error(400, "Bad request syntax (%r)" % requestline)
|
2002-04-04 18:55:58 -04:00
|
|
|
return False
|
1998-03-26 17:13:24 -04:00
|
|
|
self.command, self.path, self.request_version = command, path, version
|
2002-03-17 14:37:22 -04:00
|
|
|
|
2007-08-22 20:45:42 -03:00
|
|
|
# Examine the headers and look for a Connection directive.
|
|
|
|
|
|
|
|
# MessageClass (rfc822) wants to see strings rather than bytes.
|
|
|
|
# But a TextIOWrapper around self.rfile would buffer too many bytes
|
|
|
|
# from the stream, bytes which we later need to read as bytes.
|
|
|
|
# So we read the correct bytes here, as bytes, then use StringIO
|
|
|
|
# to make them look like strings for MessageClass to parse.
|
|
|
|
headers = []
|
|
|
|
while True:
|
|
|
|
line = self.rfile.readline()
|
|
|
|
headers.append(line)
|
|
|
|
if line in (b'\r\n', b'\n', b''):
|
|
|
|
break
|
|
|
|
hfile = io.StringIO(b''.join(headers).decode('iso-8859-1'))
|
|
|
|
self.headers = self.MessageClass(hfile)
|
2002-03-17 14:37:22 -04:00
|
|
|
|
|
|
|
conntype = self.headers.get('Connection', "")
|
|
|
|
if conntype.lower() == 'close':
|
|
|
|
self.close_connection = 1
|
|
|
|
elif (conntype.lower() == 'keep-alive' and
|
|
|
|
self.protocol_version >= "HTTP/1.1"):
|
|
|
|
self.close_connection = 0
|
2002-04-04 18:55:58 -04:00
|
|
|
return True
|
1999-10-26 10:01:36 -03:00
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
def handle_one_request(self):
|
1999-10-26 10:01:36 -03:00
|
|
|
"""Handle a single HTTP request.
|
|
|
|
|
|
|
|
You normally don't need to override this method; see the class
|
|
|
|
__doc__ string for information on how to handle specific HTTP
|
|
|
|
commands such as GET and POST.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.raw_requestline = self.rfile.readline()
|
2002-03-17 14:37:22 -04:00
|
|
|
if not self.raw_requestline:
|
|
|
|
self.close_connection = 1
|
|
|
|
return
|
1999-10-26 10:01:36 -03:00
|
|
|
if not self.parse_request(): # An error code has been sent, just exit
|
|
|
|
return
|
|
|
|
mname = 'do_' + self.command
|
1998-03-26 17:13:24 -04:00
|
|
|
if not hasattr(self, mname):
|
2004-02-12 13:35:32 -04:00
|
|
|
self.send_error(501, "Unsupported method (%r)" % self.command)
|
1998-03-26 17:13:24 -04:00
|
|
|
return
|
|
|
|
method = getattr(self, mname)
|
|
|
|
method()
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
def handle(self):
|
|
|
|
"""Handle multiple requests if necessary."""
|
|
|
|
self.close_connection = 1
|
|
|
|
|
|
|
|
self.handle_one_request()
|
|
|
|
while not self.close_connection:
|
|
|
|
self.handle_one_request()
|
|
|
|
|
1995-08-04 01:00:20 -03:00
|
|
|
def send_error(self, code, message=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Send and log an error reply.
|
|
|
|
|
|
|
|
Arguments are the error code, and a detailed message.
|
|
|
|
The detailed message defaults to the short entry matching the
|
|
|
|
response code.
|
|
|
|
|
|
|
|
This sends an error response (so it must be called before any
|
|
|
|
output has been generated), logs the error, and finally sends
|
|
|
|
a piece of HTML explaining the error to the user.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2007-01-15 12:59:06 -04:00
|
|
|
shortmsg, longmsg = self.responses[code]
|
1998-03-26 17:13:24 -04:00
|
|
|
except KeyError:
|
2007-01-15 12:59:06 -04:00
|
|
|
shortmsg, longmsg = '???', '???'
|
2002-05-31 20:03:33 -03:00
|
|
|
if message is None:
|
2007-01-15 12:59:06 -04:00
|
|
|
message = shortmsg
|
|
|
|
explain = longmsg
|
1998-03-26 17:13:24 -04:00
|
|
|
self.log_error("code %d, message %s", code, message)
|
2005-06-26 18:33:14 -03:00
|
|
|
# using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
|
2002-03-17 14:37:22 -04:00
|
|
|
content = (self.error_message_format %
|
2005-06-26 18:33:14 -03:00
|
|
|
{'code': code, 'message': _quote_html(message), 'explain': explain})
|
1998-03-26 17:13:24 -04:00
|
|
|
self.send_response(code, message)
|
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
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60980 | georg.brandl | 2008-02-23 16:02:28 +0100 (Sat, 23 Feb 2008) | 2 lines
#1492: allow overriding BaseHTTPServer's content type for error messages.
........
r60982 | georg.brandl | 2008-02-23 16:06:25 +0100 (Sat, 23 Feb 2008) | 2 lines
#2165: fix test_logging failure on some machines.
........
r60983 | facundo.batista | 2008-02-23 16:07:35 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1089358. Adds the siginterrupt() function, that is just a
wrapper around the system call with the same name. Also added
test cases, doc changes and NEWS entry. Thanks Jason and Ralf
Schmitt.
........
r60984 | georg.brandl | 2008-02-23 16:11:18 +0100 (Sat, 23 Feb 2008) | 2 lines
#2067: file.__exit__() now calls subclasses' close() method.
........
r60985 | georg.brandl | 2008-02-23 16:19:54 +0100 (Sat, 23 Feb 2008) | 2 lines
More difflib examples. Written for GHOP by Josip Dzolonga.
........
r60987 | andrew.kuchling | 2008-02-23 16:41:51 +0100 (Sat, 23 Feb 2008) | 1 line
#2072: correct documentation for .rpc_paths
........
r60988 | georg.brandl | 2008-02-23 16:43:48 +0100 (Sat, 23 Feb 2008) | 2 lines
#2161: Fix opcode name.
........
r60989 | andrew.kuchling | 2008-02-23 16:49:35 +0100 (Sat, 23 Feb 2008) | 2 lines
#1119331: ncurses will just call exit() if the terminal name isn't found.
Call setupterm() first so that we get a Python exception instead of just existing.
........
2008-02-23 12:23:06 -04:00
|
|
|
self.send_header("Content-Type", self.error_content_type)
|
2002-03-17 14:37:22 -04:00
|
|
|
self.send_header('Connection', 'close')
|
1998-03-26 17:13:24 -04:00
|
|
|
self.end_headers()
|
2002-03-17 14:37:22 -04:00
|
|
|
if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
|
2007-10-30 15:13:17 -03:00
|
|
|
self.wfile.write(content.encode('UTF-8', 'replace'))
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def send_response(self, code, message=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Send the response header and log the response code.
|
|
|
|
|
|
|
|
Also send two standard headers with the server software
|
|
|
|
version and the current date.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.log_request(code)
|
|
|
|
if message is None:
|
2002-03-17 14:37:22 -04:00
|
|
|
if code in self.responses:
|
1998-03-26 17:13:24 -04:00
|
|
|
message = self.responses[code][0]
|
|
|
|
else:
|
|
|
|
message = ''
|
|
|
|
if self.request_version != 'HTTP/0.9':
|
2007-10-30 15:13:17 -03:00
|
|
|
self.wfile.write(("%s %d %s\r\n" %
|
|
|
|
(self.protocol_version, code, message)).encode('ASCII', 'strict'))
|
2002-03-17 14:37:22 -04:00
|
|
|
# print (self.protocol_version, code, message)
|
1998-03-26 17:13:24 -04:00
|
|
|
self.send_header('Server', self.version_string())
|
|
|
|
self.send_header('Date', self.date_time_string())
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def send_header(self, keyword, value):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Send a MIME header."""
|
|
|
|
if self.request_version != 'HTTP/0.9':
|
2007-10-30 15:13:17 -03:00
|
|
|
self.wfile.write(("%s: %s\r\n" % (keyword, value)).encode('ASCII', 'strict'))
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
if keyword.lower() == 'connection':
|
|
|
|
if value.lower() == 'close':
|
|
|
|
self.close_connection = 1
|
|
|
|
elif value.lower() == 'keep-alive':
|
|
|
|
self.close_connection = 0
|
|
|
|
|
1995-08-04 01:00:20 -03:00
|
|
|
def end_headers(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Send the blank line ending the MIME headers."""
|
|
|
|
if self.request_version != 'HTTP/0.9':
|
2007-10-30 15:13:17 -03:00
|
|
|
self.wfile.write(b"\r\n")
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def log_request(self, code='-', size='-'):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Log an accepted request.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2006-03-07 12:16:07 -04:00
|
|
|
This is called by send_response().
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
self.log_message('"%s" %s %s',
|
|
|
|
self.requestline, str(code), str(size))
|
1995-08-04 01:00:20 -03:00
|
|
|
|
Merged revisions 53304-53433,53435-53450 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r53304 | vinay.sajip | 2007-01-09 15:50:28 +0100 (Tue, 09 Jan 2007) | 1 line
Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
........
r53305 | vinay.sajip | 2007-01-09 15:51:36 +0100 (Tue, 09 Jan 2007) | 1 line
Added entry about addition of _open() method to logging.FileHandler.
........
r53306 | vinay.sajip | 2007-01-09 15:54:56 +0100 (Tue, 09 Jan 2007) | 1 line
Added a docstring
........
r53316 | thomas.heller | 2007-01-09 20:19:33 +0100 (Tue, 09 Jan 2007) | 4 lines
Verify the sizes of the basic ctypes data types against the struct
module.
Will backport to release25-maint.
........
r53340 | gustavo.niemeyer | 2007-01-10 17:13:40 +0100 (Wed, 10 Jan 2007) | 3 lines
Mention in the int() docstring that a base zero has meaning, as
stated in http://docs.python.org/lib/built-in-funcs.html as well.
........
r53341 | gustavo.niemeyer | 2007-01-10 17:15:48 +0100 (Wed, 10 Jan 2007) | 2 lines
Minor change in int() docstring for proper spacing.
........
r53358 | thomas.heller | 2007-01-10 21:12:13 +0100 (Wed, 10 Jan 2007) | 1 line
Change the ctypes version number to "1.1.0".
........
r53361 | thomas.heller | 2007-01-10 21:51:19 +0100 (Wed, 10 Jan 2007) | 1 line
Must change the version number in the _ctypes extension as well.
........
r53362 | guido.van.rossum | 2007-01-11 00:12:56 +0100 (Thu, 11 Jan 2007) | 3 lines
Fix the signature of log_error(). (A subclass that did the right thing
was getting complaints from pychecker.)
........
r53370 | matthias.klose | 2007-01-11 11:26:31 +0100 (Thu, 11 Jan 2007) | 2 lines
- Make the documentation match the code and the docstring
........
r53375 | matthias.klose | 2007-01-11 12:44:04 +0100 (Thu, 11 Jan 2007) | 2 lines
- idle: Honor the "Cancel" action in the save dialog (Debian bug #299092).
........
r53381 | raymond.hettinger | 2007-01-11 19:22:55 +0100 (Thu, 11 Jan 2007) | 1 line
SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
........
r53388 | thomas.heller | 2007-01-11 22:18:56 +0100 (Thu, 11 Jan 2007) | 4 lines
Fixes for 64-bit Windows: In ctypes.wintypes, correct the definitions
of HANDLE, WPARAM, LPARAM data types. Make parameterless foreign
function calls work.
........
r53390 | thomas.heller | 2007-01-11 22:23:12 +0100 (Thu, 11 Jan 2007) | 2 lines
Correct the comments: the code is right.
........
r53393 | brett.cannon | 2007-01-12 08:27:52 +0100 (Fri, 12 Jan 2007) | 3 lines
Fix error where the end of a funcdesc environment was accidentally moved too
far down.
........
r53397 | anthony.baxter | 2007-01-12 10:35:56 +0100 (Fri, 12 Jan 2007) | 3 lines
add parsetok.h as a dependency - previously, changing this file doesn't
cause the right files to be rebuilt.
........
r53401 | thomas.heller | 2007-01-12 21:08:19 +0100 (Fri, 12 Jan 2007) | 3 lines
Avoid warnings in the test suite because ctypes.wintypes cannot be
imported on non-windows systems.
........
r53402 | thomas.heller | 2007-01-12 21:17:34 +0100 (Fri, 12 Jan 2007) | 6 lines
patch #1610795: BSD version of ctypes.util.find_library, by Martin
Kammerhofer.
release25-maint backport candidate, but the release manager has to
decide.
........
r53403 | thomas.heller | 2007-01-12 21:21:53 +0100 (Fri, 12 Jan 2007) | 3 lines
patch #1610795: BSD version of ctypes.util.find_library, by Martin
Kammerhofer.
........
r53406 | brett.cannon | 2007-01-13 01:29:49 +0100 (Sat, 13 Jan 2007) | 2 lines
Deprecate the sets module.
........
r53407 | georg.brandl | 2007-01-13 13:31:51 +0100 (Sat, 13 Jan 2007) | 3 lines
Fix typo.
........
r53409 | marc-andre.lemburg | 2007-01-13 22:00:08 +0100 (Sat, 13 Jan 2007) | 16 lines
Bump version number and change copyright year.
Add new API linux_distribution() which supports reading the full distribution
name and also knows how to parse LSB-style release files.
Redirect the old dist() API to the new API (using the short distribution name
taken from the release file filename).
Add branch and revision to _sys_version().
Add work-around for Cygwin to libc_ver().
Add support for IronPython (thanks for Anthony Baxter) and make
Jython support more robust.
........
r53410 | neal.norwitz | 2007-01-13 22:22:37 +0100 (Sat, 13 Jan 2007) | 1 line
Fix grammar in docstrings
........
r53411 | marc-andre.lemburg | 2007-01-13 23:32:21 +0100 (Sat, 13 Jan 2007) | 9 lines
Add parameter sys_version to _sys_version().
Change the cache for _sys_version() to take the parameter into account.
Add support for parsing the IronPython 1.0.1 sys.version value - even
though it still returns '1.0.0'; the version string no longer includes
the patch level.
........
r53412 | peter.astrand | 2007-01-13 23:35:35 +0100 (Sat, 13 Jan 2007) | 1 line
Fix for bug #1634343: allow specifying empty arguments on Windows
........
r53414 | marc-andre.lemburg | 2007-01-13 23:59:36 +0100 (Sat, 13 Jan 2007) | 14 lines
Add Python implementation to the machine details.
Pretty-print the Python version used for running PyBench.
Let the user know when calibration has finished.
[ 1563844 ] pybench support for IronPython:
Simplify Unicode version detection.
Make garbage collection and check interval settings optional if
the Python implementation doesn't support thess (e.g. IronPython).
........
r53415 | marc-andre.lemburg | 2007-01-14 00:13:54 +0100 (Sun, 14 Jan 2007) | 5 lines
Use defaults if sys.executable isn't set (e.g. on Jython).
This change allows running PyBench under Jython.
........
r53416 | marc-andre.lemburg | 2007-01-14 00:15:33 +0100 (Sun, 14 Jan 2007) | 3 lines
Jython doesn't have sys.setcheckinterval() - ignore it in that case.
........
r53420 | gerhard.haering | 2007-01-14 02:43:50 +0100 (Sun, 14 Jan 2007) | 29 lines
Merged changes from standalone version 2.3.3. This should probably all be
merged into the 2.5 maintenance branch:
- self->statement was not checked while fetching data, which could
lead to crashes if you used the pysqlite API in unusual ways.
Closing the cursor and continuing to fetch data was enough.
- Converters are stored in a converters dictionary. The converter name
is uppercased first. The old upper-casing algorithm was wrong and
was replaced by a simple call to the Python string's upper() method
instead.
-Applied patch by Glyph Lefkowitz that fixes the problem with
subsequent SQLITE_SCHEMA errors.
- Improvement to the row type: rows can now be iterated over and have a keys()
method. This improves compatibility with both tuple and dict a lot.
- A bugfix for the subsecond resolution in timestamps.
- Corrected the way the flags PARSE_DECLTYPES and PARSE_COLNAMES are
checked for. Now they work as documented.
- gcc on Linux sucks. It exports all symbols by default in shared
libraries, so if symbols are not unique it can lead to problems with
symbol lookup. pysqlite used to crash under Apache when mod_cache
was enabled because both modules had the symbol cache_init. I fixed
this by applying the prefix pysqlite_ almost everywhere. Sigh.
........
r53423 | guido.van.rossum | 2007-01-14 04:46:33 +0100 (Sun, 14 Jan 2007) | 2 lines
Remove a dependency of this test on $COLUMNS.
........
r53425 | ka-ping.yee | 2007-01-14 05:25:15 +0100 (Sun, 14 Jan 2007) | 3 lines
Handle old-style instances more gracefully (display documentation on
the relevant class instead of documentation on <type 'instance'>).
........
r53440 | vinay.sajip | 2007-01-14 22:49:59 +0100 (Sun, 14 Jan 2007) | 1 line
Added WatchedFileHandler (based on SF patch #1598415)
........
r53441 | vinay.sajip | 2007-01-14 22:50:50 +0100 (Sun, 14 Jan 2007) | 1 line
Added documentation for WatchedFileHandler (based on SF patch #1598415)
........
r53442 | guido.van.rossum | 2007-01-15 01:02:35 +0100 (Mon, 15 Jan 2007) | 2 lines
Doc patch matching r53434 (htonl etc. now always take/return positive ints).
........
2007-01-15 11:49:28 -04:00
|
|
|
def log_error(self, format, *args):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Log an error.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
This is called when a request cannot be fulfilled. By
|
|
|
|
default it passes the message on to log_message().
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
Arguments are the same as for log_message().
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
XXX This should go to the separate error log.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
1995-08-04 01:00:20 -03:00
|
|
|
|
Merged revisions 53304-53433,53435-53450 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r53304 | vinay.sajip | 2007-01-09 15:50:28 +0100 (Tue, 09 Jan 2007) | 1 line
Bug #1627575: Added _open() method to FileHandler which can be used to reopen files. The FileHandler instance now saves the encoding (which can be None) in an attribute called "encoding".
........
r53305 | vinay.sajip | 2007-01-09 15:51:36 +0100 (Tue, 09 Jan 2007) | 1 line
Added entry about addition of _open() method to logging.FileHandler.
........
r53306 | vinay.sajip | 2007-01-09 15:54:56 +0100 (Tue, 09 Jan 2007) | 1 line
Added a docstring
........
r53316 | thomas.heller | 2007-01-09 20:19:33 +0100 (Tue, 09 Jan 2007) | 4 lines
Verify the sizes of the basic ctypes data types against the struct
module.
Will backport to release25-maint.
........
r53340 | gustavo.niemeyer | 2007-01-10 17:13:40 +0100 (Wed, 10 Jan 2007) | 3 lines
Mention in the int() docstring that a base zero has meaning, as
stated in http://docs.python.org/lib/built-in-funcs.html as well.
........
r53341 | gustavo.niemeyer | 2007-01-10 17:15:48 +0100 (Wed, 10 Jan 2007) | 2 lines
Minor change in int() docstring for proper spacing.
........
r53358 | thomas.heller | 2007-01-10 21:12:13 +0100 (Wed, 10 Jan 2007) | 1 line
Change the ctypes version number to "1.1.0".
........
r53361 | thomas.heller | 2007-01-10 21:51:19 +0100 (Wed, 10 Jan 2007) | 1 line
Must change the version number in the _ctypes extension as well.
........
r53362 | guido.van.rossum | 2007-01-11 00:12:56 +0100 (Thu, 11 Jan 2007) | 3 lines
Fix the signature of log_error(). (A subclass that did the right thing
was getting complaints from pychecker.)
........
r53370 | matthias.klose | 2007-01-11 11:26:31 +0100 (Thu, 11 Jan 2007) | 2 lines
- Make the documentation match the code and the docstring
........
r53375 | matthias.klose | 2007-01-11 12:44:04 +0100 (Thu, 11 Jan 2007) | 2 lines
- idle: Honor the "Cancel" action in the save dialog (Debian bug #299092).
........
r53381 | raymond.hettinger | 2007-01-11 19:22:55 +0100 (Thu, 11 Jan 2007) | 1 line
SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
........
r53388 | thomas.heller | 2007-01-11 22:18:56 +0100 (Thu, 11 Jan 2007) | 4 lines
Fixes for 64-bit Windows: In ctypes.wintypes, correct the definitions
of HANDLE, WPARAM, LPARAM data types. Make parameterless foreign
function calls work.
........
r53390 | thomas.heller | 2007-01-11 22:23:12 +0100 (Thu, 11 Jan 2007) | 2 lines
Correct the comments: the code is right.
........
r53393 | brett.cannon | 2007-01-12 08:27:52 +0100 (Fri, 12 Jan 2007) | 3 lines
Fix error where the end of a funcdesc environment was accidentally moved too
far down.
........
r53397 | anthony.baxter | 2007-01-12 10:35:56 +0100 (Fri, 12 Jan 2007) | 3 lines
add parsetok.h as a dependency - previously, changing this file doesn't
cause the right files to be rebuilt.
........
r53401 | thomas.heller | 2007-01-12 21:08:19 +0100 (Fri, 12 Jan 2007) | 3 lines
Avoid warnings in the test suite because ctypes.wintypes cannot be
imported on non-windows systems.
........
r53402 | thomas.heller | 2007-01-12 21:17:34 +0100 (Fri, 12 Jan 2007) | 6 lines
patch #1610795: BSD version of ctypes.util.find_library, by Martin
Kammerhofer.
release25-maint backport candidate, but the release manager has to
decide.
........
r53403 | thomas.heller | 2007-01-12 21:21:53 +0100 (Fri, 12 Jan 2007) | 3 lines
patch #1610795: BSD version of ctypes.util.find_library, by Martin
Kammerhofer.
........
r53406 | brett.cannon | 2007-01-13 01:29:49 +0100 (Sat, 13 Jan 2007) | 2 lines
Deprecate the sets module.
........
r53407 | georg.brandl | 2007-01-13 13:31:51 +0100 (Sat, 13 Jan 2007) | 3 lines
Fix typo.
........
r53409 | marc-andre.lemburg | 2007-01-13 22:00:08 +0100 (Sat, 13 Jan 2007) | 16 lines
Bump version number and change copyright year.
Add new API linux_distribution() which supports reading the full distribution
name and also knows how to parse LSB-style release files.
Redirect the old dist() API to the new API (using the short distribution name
taken from the release file filename).
Add branch and revision to _sys_version().
Add work-around for Cygwin to libc_ver().
Add support for IronPython (thanks for Anthony Baxter) and make
Jython support more robust.
........
r53410 | neal.norwitz | 2007-01-13 22:22:37 +0100 (Sat, 13 Jan 2007) | 1 line
Fix grammar in docstrings
........
r53411 | marc-andre.lemburg | 2007-01-13 23:32:21 +0100 (Sat, 13 Jan 2007) | 9 lines
Add parameter sys_version to _sys_version().
Change the cache for _sys_version() to take the parameter into account.
Add support for parsing the IronPython 1.0.1 sys.version value - even
though it still returns '1.0.0'; the version string no longer includes
the patch level.
........
r53412 | peter.astrand | 2007-01-13 23:35:35 +0100 (Sat, 13 Jan 2007) | 1 line
Fix for bug #1634343: allow specifying empty arguments on Windows
........
r53414 | marc-andre.lemburg | 2007-01-13 23:59:36 +0100 (Sat, 13 Jan 2007) | 14 lines
Add Python implementation to the machine details.
Pretty-print the Python version used for running PyBench.
Let the user know when calibration has finished.
[ 1563844 ] pybench support for IronPython:
Simplify Unicode version detection.
Make garbage collection and check interval settings optional if
the Python implementation doesn't support thess (e.g. IronPython).
........
r53415 | marc-andre.lemburg | 2007-01-14 00:13:54 +0100 (Sun, 14 Jan 2007) | 5 lines
Use defaults if sys.executable isn't set (e.g. on Jython).
This change allows running PyBench under Jython.
........
r53416 | marc-andre.lemburg | 2007-01-14 00:15:33 +0100 (Sun, 14 Jan 2007) | 3 lines
Jython doesn't have sys.setcheckinterval() - ignore it in that case.
........
r53420 | gerhard.haering | 2007-01-14 02:43:50 +0100 (Sun, 14 Jan 2007) | 29 lines
Merged changes from standalone version 2.3.3. This should probably all be
merged into the 2.5 maintenance branch:
- self->statement was not checked while fetching data, which could
lead to crashes if you used the pysqlite API in unusual ways.
Closing the cursor and continuing to fetch data was enough.
- Converters are stored in a converters dictionary. The converter name
is uppercased first. The old upper-casing algorithm was wrong and
was replaced by a simple call to the Python string's upper() method
instead.
-Applied patch by Glyph Lefkowitz that fixes the problem with
subsequent SQLITE_SCHEMA errors.
- Improvement to the row type: rows can now be iterated over and have a keys()
method. This improves compatibility with both tuple and dict a lot.
- A bugfix for the subsecond resolution in timestamps.
- Corrected the way the flags PARSE_DECLTYPES and PARSE_COLNAMES are
checked for. Now they work as documented.
- gcc on Linux sucks. It exports all symbols by default in shared
libraries, so if symbols are not unique it can lead to problems with
symbol lookup. pysqlite used to crash under Apache when mod_cache
was enabled because both modules had the symbol cache_init. I fixed
this by applying the prefix pysqlite_ almost everywhere. Sigh.
........
r53423 | guido.van.rossum | 2007-01-14 04:46:33 +0100 (Sun, 14 Jan 2007) | 2 lines
Remove a dependency of this test on $COLUMNS.
........
r53425 | ka-ping.yee | 2007-01-14 05:25:15 +0100 (Sun, 14 Jan 2007) | 3 lines
Handle old-style instances more gracefully (display documentation on
the relevant class instead of documentation on <type 'instance'>).
........
r53440 | vinay.sajip | 2007-01-14 22:49:59 +0100 (Sun, 14 Jan 2007) | 1 line
Added WatchedFileHandler (based on SF patch #1598415)
........
r53441 | vinay.sajip | 2007-01-14 22:50:50 +0100 (Sun, 14 Jan 2007) | 1 line
Added documentation for WatchedFileHandler (based on SF patch #1598415)
........
r53442 | guido.van.rossum | 2007-01-15 01:02:35 +0100 (Mon, 15 Jan 2007) | 2 lines
Doc patch matching r53434 (htonl etc. now always take/return positive ints).
........
2007-01-15 11:49:28 -04:00
|
|
|
self.log_message(format, *args)
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def log_message(self, format, *args):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Log an arbitrary message.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
This is used by all other logging functions. Override
|
|
|
|
it if you have specific logging wishes.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
The first argument, FORMAT, is a format string for the
|
|
|
|
message to be logged. If the format string contains
|
|
|
|
any % escapes requiring parameters, they should be
|
|
|
|
specified as subsequent arguments (it's just like
|
|
|
|
printf!).
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
The client host and current date/time are prefixed to
|
|
|
|
every message.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
sys.stderr.write("%s - - [%s] %s\n" %
|
|
|
|
(self.address_string(),
|
|
|
|
self.log_date_time_string(),
|
|
|
|
format%args))
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def version_string(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return the server software version string."""
|
|
|
|
return self.server_version + ' ' + self.sys_version
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2006-02-17 09:34:16 -04:00
|
|
|
def date_time_string(self, timestamp=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return the current date and time formatted for a message header."""
|
2006-02-17 09:34:16 -04:00
|
|
|
if timestamp is None:
|
|
|
|
timestamp = time.time()
|
|
|
|
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
|
1998-03-26 17:13:24 -04:00
|
|
|
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
|
|
|
|
self.weekdayname[wd],
|
|
|
|
day, self.monthname[month], year,
|
|
|
|
hh, mm, ss)
|
|
|
|
return s
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def log_date_time_string(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return the current time formatted for logging."""
|
|
|
|
now = time.time()
|
|
|
|
year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
|
|
|
|
s = "%02d/%3s/%04d %02d:%02d:%02d" % (
|
|
|
|
day, self.monthname[month], year, hh, mm, ss)
|
|
|
|
return s
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|
|
|
|
|
|
|
monthname = [None,
|
1998-03-26 17:13:24 -04:00
|
|
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
def address_string(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return the client address formatted for logging.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
This version looks up the full hostname using gethostbyaddr(),
|
|
|
|
and tries to find a name that contains at least one dot.
|
1995-08-04 01:00:20 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
"""
|
1995-08-04 01:00:20 -03:00
|
|
|
|
2003-05-31 04:55:43 -03:00
|
|
|
host, port = self.client_address[:2]
|
2000-08-16 17:30:21 -03:00
|
|
|
return socket.getfqdn(host)
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
# Essentially static class variables
|
|
|
|
|
|
|
|
# The version of the HTTP protocol we support.
|
2002-03-17 14:37:22 -04:00
|
|
|
# Set this to HTTP/1.1 to enable automatic keepalive
|
1995-08-04 01:00:20 -03:00
|
|
|
protocol_version = "HTTP/1.0"
|
|
|
|
|
|
|
|
# The Message-like class used to parse headers
|
|
|
|
MessageClass = mimetools.Message
|
|
|
|
|
|
|
|
# Table mapping response codes to messages; entries have the
|
|
|
|
# form {code: (shortmessage, longmessage)}.
|
2006-02-17 15:17:25 -04:00
|
|
|
# See RFC 2616.
|
1995-08-04 01:00:20 -03:00
|
|
|
responses = {
|
2002-03-17 14:37:22 -04:00
|
|
|
100: ('Continue', 'Request received, please continue'),
|
|
|
|
101: ('Switching Protocols',
|
|
|
|
'Switching to new protocol; obey Upgrade header'),
|
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
200: ('OK', 'Request fulfilled, document follows'),
|
|
|
|
201: ('Created', 'Document created, URL follows'),
|
|
|
|
202: ('Accepted',
|
|
|
|
'Request accepted, processing continues off-line'),
|
2002-03-17 14:37:22 -04:00
|
|
|
203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
|
2006-02-17 15:17:25 -04:00
|
|
|
204: ('No Content', 'Request fulfilled, nothing follows'),
|
2002-03-17 14:37:22 -04:00
|
|
|
205: ('Reset Content', 'Clear input form for further input.'),
|
|
|
|
206: ('Partial Content', 'Partial content follows.'),
|
2001-01-14 17:54:20 -04:00
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
300: ('Multiple Choices',
|
|
|
|
'Object has several resources -- see URI list'),
|
|
|
|
301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
|
1998-03-26 17:13:24 -04:00
|
|
|
302: ('Found', 'Object moved temporarily -- see URI list'),
|
2002-03-17 14:37:22 -04:00
|
|
|
303: ('See Other', 'Object moved -- see Method and URL list'),
|
2006-02-17 15:17:25 -04:00
|
|
|
304: ('Not Modified',
|
2002-03-17 14:37:22 -04:00
|
|
|
'Document has not changed since given time'),
|
|
|
|
305: ('Use Proxy',
|
|
|
|
'You must use proxy specified in Location to access this '
|
|
|
|
'resource.'),
|
|
|
|
307: ('Temporary Redirect',
|
|
|
|
'Object moved temporarily -- see URI list'),
|
2001-01-14 17:54:20 -04:00
|
|
|
|
2006-02-17 15:17:25 -04:00
|
|
|
400: ('Bad Request',
|
1998-03-26 17:13:24 -04:00
|
|
|
'Bad request syntax or unsupported method'),
|
|
|
|
401: ('Unauthorized',
|
|
|
|
'No permission -- see authorization schemes'),
|
2006-02-17 15:17:25 -04:00
|
|
|
402: ('Payment Required',
|
1998-03-26 17:13:24 -04:00
|
|
|
'No payment -- see charging schemes'),
|
|
|
|
403: ('Forbidden',
|
|
|
|
'Request forbidden -- authorization will not help'),
|
2002-03-17 14:37:22 -04:00
|
|
|
404: ('Not Found', 'Nothing matches the given URI'),
|
|
|
|
405: ('Method Not Allowed',
|
|
|
|
'Specified method is invalid for this server.'),
|
|
|
|
406: ('Not Acceptable', 'URI not available in preferred format.'),
|
|
|
|
407: ('Proxy Authentication Required', 'You must authenticate with '
|
|
|
|
'this proxy before proceeding.'),
|
2006-02-17 15:17:25 -04:00
|
|
|
408: ('Request Timeout', 'Request timed out; try again later.'),
|
2002-03-17 14:37:22 -04:00
|
|
|
409: ('Conflict', 'Request conflict.'),
|
|
|
|
410: ('Gone',
|
|
|
|
'URI no longer exists and has been permanently removed.'),
|
|
|
|
411: ('Length Required', 'Client must specify Content-Length.'),
|
|
|
|
412: ('Precondition Failed', 'Precondition in headers is false.'),
|
|
|
|
413: ('Request Entity Too Large', 'Entity is too large.'),
|
|
|
|
414: ('Request-URI Too Long', 'URI is too long.'),
|
|
|
|
415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
|
|
|
|
416: ('Requested Range Not Satisfiable',
|
|
|
|
'Cannot satisfy request range.'),
|
|
|
|
417: ('Expectation Failed',
|
|
|
|
'Expect condition could not be satisfied.'),
|
2001-01-14 17:54:20 -04:00
|
|
|
|
2006-02-17 15:17:25 -04:00
|
|
|
500: ('Internal Server Error', 'Server got itself in trouble'),
|
2002-03-17 14:37:22 -04:00
|
|
|
501: ('Not Implemented',
|
1998-03-26 17:13:24 -04:00
|
|
|
'Server does not support this operation'),
|
2002-03-17 14:37:22 -04:00
|
|
|
502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
|
2006-02-17 15:17:25 -04:00
|
|
|
503: ('Service Unavailable',
|
1998-03-26 17:13:24 -04:00
|
|
|
'The server cannot process the request due to a high load'),
|
2006-02-17 15:17:25 -04:00
|
|
|
504: ('Gateway Timeout',
|
1998-03-26 17:13:24 -04:00
|
|
|
'The gateway server did not receive a timely response'),
|
2006-02-17 15:17:25 -04:00
|
|
|
505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
|
1998-03-26 17:13:24 -04:00
|
|
|
}
|
1995-08-04 01:00:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test(HandlerClass = BaseHTTPRequestHandler,
|
2002-03-17 14:37:22 -04:00
|
|
|
ServerClass = HTTPServer, protocol="HTTP/1.0"):
|
1995-08-04 01:00:20 -03:00
|
|
|
"""Test the HTTP request handler class.
|
|
|
|
|
|
|
|
This runs an HTTP server on port 8000 (or the first command line
|
|
|
|
argument).
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.argv[1:]:
|
2001-02-09 01:38:46 -04:00
|
|
|
port = int(sys.argv[1])
|
1995-08-04 01:00:20 -03:00
|
|
|
else:
|
1998-03-26 17:13:24 -04:00
|
|
|
port = 8000
|
1995-08-04 01:00:20 -03:00
|
|
|
server_address = ('', port)
|
|
|
|
|
2002-03-17 14:37:22 -04:00
|
|
|
HandlerClass.protocol_version = protocol
|
1995-08-04 01:00:20 -03:00
|
|
|
httpd = ServerClass(server_address, HandlerClass)
|
|
|
|
|
2001-07-24 17:34:08 -03:00
|
|
|
sa = httpd.socket.getsockname()
|
2007-02-09 01:37:30 -04:00
|
|
|
print("Serving HTTP on", sa[0], "port", sa[1], "...")
|
1995-08-04 01:00:20 -03:00
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|