a
97aӱ @ s d Z ddlmZmZmZmZ ddlmZ ddlT dZ ddgZ
ddlmZ dd l
mZ dd
lmZ ddlmZ ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZd
ZdZ dd Z!G dd dej"Z#G dd dej$Z%G dd de%Z&dd Z'da(dd Z)dd Z*G dd de&Z+e%e#ddfdd Z,e-d!kre. Z/e/j0d"d#d$d% e/j0d&d'de1d(d)d* e/2 Z3e3j4re,e+e3j5d+ ne,e&e3j5d+ dS ),aQ HTTP server classes.
From Python 3.3
Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
and CGIHTTPRequestHandler for CGI scripts.
It does, however, optionally implement HTTP/1.1 persistent connections,
as of version 0.3.
Notes on CGIHTTPRequestHandler
------------------------------
This class implements GET and POST requests to cgi-bin scripts.
If the os.fork() function is not present (e.g. on Windows),
subprocess.Popen() is used as a fallback, with slightly altered semantics.
In all cases, the implementation is intentionally naive -- all
requests are executed synchronously.
SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
-- it may execute arbitrary Python code or external programs.
Note that status code 200 is sent prior to execution of a CGI script, so
scripts cannot send other status codes such as 302 (redirect).
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
)absolute_importdivisionprint_functionunicode_literals)utils)*z0.6
HTTPServerBaseHTTPRequestHandlerhtml)client)parse)socketserverNa
Error response
Error response
Error code: %(code)d
Message: %(message)s.
Error code explanation: %(code)s - %(explain)s.
ztext/html;charset=utf-8c C s | dd dd ddS )N&z&z>)replacer
r w/private/var/folders/s6/9n5zrl012gv99k63s4q6ccsd4s6mqz/T/pip-target-f5cq3f2q/lib/python/future/backports/http/server.py_quote_html s r c @ s e Zd ZdZdd ZdS )r c C s8 t j| | j dd \}}t|| _|| _dS )z.Override server_bind to store the server name.N )r TCPServerserver_bindsocketgetsocknamegetfqdnserver_nameserver_port)selfhostportr r r r s zHTTPServer.server_bindN)__name__
__module____qualname__allow_reuse_addressr r r r r r s c - @ s@ e Zd ZdZdej d Zde Z e
ZeZ
dZdd Zdd Zd
d Zdd
ZdZddZd[ddZd\ddZdd Zdd Zdd Zd]ddZdd Zd d! Zd"d# Zd^d$d%Zd&d' Zg d(Zg d)Z d*d+ Z!d,Z"e#j$Z%d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdY,Z&dS )_r a 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
where is a (case-sensitive) keyword such as GET or POST,
is a string containing path information for the request,
and should be the string "HTTP/1.0" or "HTTP/1.1".
is encoded using the URL encoding scheme (using %xx to signify
the ASCII character with hex code xx).
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).
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
(i.e. 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.
The reply form of the HTTP 1.x protocol again has three parts:
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
where is the protocol version ("HTTP/1.0" or "HTTP/1.1"),
is a 3-digit response code indicating success or
failure of the request, and 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 (). Specifically,
a request SPAM will be handled by a method do_SPAM(). If no
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 email.message.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: /
where and should be registered MIME types,
e.g. "text/html" or "text/plain".
zPython/r z BaseHTTP/HTTP/0.9c
C s\ d| _ | j | _}d| _t| jd}|d}|| _| }t |dkr$|\}}}|dd dkrx|
dd | d
S zF|ddd }|d}t |d
krtt|d t|d f}W n( tt
fy |
dd | Y d
S 0 |dkr| jdkrd| _|dkr~|
dd| d
S nZt |d
kr`|\}}d| _|dkr~|
dd| d
S n|sjd
S |
dd| d
S ||| | _ | _| _ztj| j| jd| _W n$ tjy |
dd Y d
S 0 | jdd}| dkrd| _n | dkr| jdkrd| _| jdd} | dkrX| jdkrX| jdkrX| sXd
S dS ) a' Parse a request (internal).
The request should be stored in self.raw_requestline; the results
are in self.command, self.path, self.request_version and
self.headers.
Return True for success, False for failure; on failure, an
error is sent back.
Nr z
iso-8859-1z
zHTTP/ zBad request version (%r)F/.r r )r r zHTTP/1.1)r r zInvalid HTTP Version (%s)GETzBad HTTP/0.9 request type (%r)zBad request syntax (%r))_classz
Line too long
Connection close
keep-aliveZExpectz100-continueT)commanddefault_request_versionrequest_versionclose_connectionstrraw_requestlinerstriprequestlinesplitlen
send_error
ValueErrorint
IndexErrorprotocol_versionpathhttp_client
parse_headersrfileMessageClassheadersLineTooLonggetlowerhandle_expect_100)
r versionr: wordsr3 rB Zbase_version_numberZversion_numberZconntypeexpectr r r
parse_request s
z$BaseHTTPRequestHandler.parse_requestc C s | d | dS )a7 Decide what to do with an "Expect: 100-continue" header.
If the client is expecting a 100 Continue response, we must
respond with either a 100 Continue or a final response before
waiting for the request body. The default is to always respond
with a 100 Continue. You can behave differently (for example,
reject unauthorized requests) by overriding this method.
This method should either return True (possibly after sending
a 100 Continue response) or send an error response and return
False.
d T)send_response_only
flush_headersr r r r rK ] s
z(BaseHTTPRequestHandler.handle_expect_100c
C s z| j d| _t| jdkr@d| _d| _d| _| d W dS | jsRd| _W dS | s`W dS d| j }t
| |s| dd | j W dS t| |}| | j
W n: tjy } z | d
| d| _W Y d}~dS d}~0 0 dS )zHandle 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.
i i r0 Nr Zdo_ zUnsupported method (%r)zRequest timed out: %r)rE readliner8 r<