N
u9c       s     d  Z   @ d Z C k Z D k Z E k Z F k Z G k Z H k Z I k Z L d Z	 Y d e i
 f d     YZ k d e i f d     YZ e e d  Z e d j o e   n d	 S(
   s  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
(including CGI scripts).

Contents:

- BaseHTTPRequestHandler: HTTP request handler base class
- test: test function

XXX To do:

- send server version
- log requests even later (to capture byte count)
- log user-agent header and other interesting goodies
- send error log to separate file
- are request names really case sensitive?

s   0.2s   <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>
s
   HTTPServerc      s   Y [ d   Z  RS(   Nc    s   [ \ ] t  i i |   ^ |  i i   \ } } _ | p
 | d j o ` t i   } n a t i	 |  \ } } } b d | j o> c x4 | d c r' } d d | j o e | } f Pn q Wn g | |  _ h | |  _ d S(   s.   Override server_bind to store the server name.s   0.0.0.0s   .i    N(   s   SocketServers	   TCPServers   server_binds   selfs   sockets   getsocknames   hosts   ports   gethostnames   gethostbyaddrs   hostnames	   hostnamess	   hostaddrss   server_names   server_port(   s   selfs   hosts   ports   hostnames	   hostnamess	   hostaddrss2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   server_bind[ s    		(   s   server_bind(    s2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys
   HTTPServerY s   s   BaseHTTPRequestHandlerc      sW  k d  Z    d e i e i  d Z  d e Z  d   Z e	 d  Z
  e Z "e	 d  Z 5d   Z :d   Z ?d
 d
 d	  Z Id   Z Wd   Z md   Z qd   Z {d   Z d d d d d d d g Z e	 d d d d d d d d d d  d! d" g Z d#   Z d$ Z e i Z h  d% d& f d' <d( d) f d* <d+ d, f d- <d. d/ f d0 <d1 d2 f d3 <d4 d5 f d6 <d7 d8 f d9 <d: d; f d< <d= d> f d? <d@ dA f dB <dC dD f dE <dF dG f dH <dI dJ f dK <dL dM f dN <dO dP f dQ <dR dS f dT <dU dV f dW <dX dY f dZ <Z RS([   s  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,
    and <version> should be the string "HTTP/1.0".  <path> is encoded
    using the URL encoding scheme (using %xx to signify the ASCII
    character with hex code xx).

    The protocol is vague about whether lines are separated by LF
    characters or by CRLF pairs -- for compatibility with the widest
    range of clients, both should be accepted.  Similarly, whitespace
    in the request line should be 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

    <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.

    The reply form of the HTTP 1.0 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

    <version> <responsecode> <responsestring>

    where <version> is the protocol version (always "HTTP/1.0"),
    <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,
    a request SPAM will be handled by a method handle_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 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".

    s   Python/i    s	   BaseHTTP/c    s'     |  i i   |  _  d |  _ }  |  i }  | d d j o  | d  } n(  | d d j o  | d  } n  | |  _  t i |  }  t
 |  d j oM  | ] } } }  | d  d j o#  |  i d	 d
 |   d Sn n  t
 |  d j oI  | ] } }  | d j o&  |  i d	  d |   d Sn n   |  i d	 d |   d S | | | f \ |  _ |  _ |  _  |  i |  i d  |  _  d | }  t |  |  o#  |  i d d |   d Sn t |  |  } |   d S(   s   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.

        s   HTTP/0.9i   s   
i   s   
i   i   s   HTTP/i  s   Bad request version (%s)Ns   GETs   Bad HTTP/0.9 request type (%s)s   Bad request syntax (%s)i    s   do_i  s   Unsupported method (%s)(   s   selfs   rfiles   readlines   raw_requestlines   request_versions   versions   requestlines   strings   splits   wordss   lens   commands   paths
   send_errors   MessageClasss   headerss   mnames   hasattrs   getattrs   method(   s   selfs   versions   requestlines   wordss   commands   paths   mnames   methods2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   handle s>   $c    s   y |  i | \ } } Wn+ t j
 o d d f \ } } n X| o | } n | } |  i d | |  |  i	 | |  |  i
   |  i i |  i h  | d <| d <| d < d S(   s  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.

        s   ???s   code %d, message %ss   codes   messages   explainN(   s   selfs	   responsess   codes   shorts   longs   KeyErrors   messages   explains	   log_errors   send_responses   end_headerss   wfiles   writes   error_message_format(   s   selfs   codes   messages   shorts   longs   explains2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys
   send_errors   	c    s   "()|  i |  *| t j o; +|  i i |  o ,|  i | d } n
 .d } n /|  i d j o- 0|  i i	 d |  i
 t |  | f  n 2|  i d |  i    3|  i d |  i    d S(   s   Send the response header and log the response code.

        Also send two standard headers with the server software
        version and the current date.

        i    s    s   HTTP/0.9s
   %s %s %s
s   Servers   DateN(   s   selfs   log_requests   codes   messages   Nones	   responsess   has_keys   request_versions   wfiles   writes   protocol_versions   strs   send_headers   version_strings   date_time_string(   s   selfs   codes   messages2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   send_response"s   -c    s>   567|  i d j o! 8|  i i d | | f  n d S(   s   Send a MIME header.s   HTTP/0.9s   %s: %s
N(   s   selfs   request_versions   wfiles   writes   keywords   value(   s   selfs   keywords   values2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   send_header5s   c    s4   :;<|  i d j o =|  i i d  n d S(   s,   Send the blank line ending the MIME headers.s   HTTP/0.9s   
N(   s   selfs   request_versions   wfiles   write(   s   selfs2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   end_headers:s   c    s5   ?DF|  i d G|  i t |  t |   d S(   sM   Log an accepted request.

        This is called by send_reponse().

        s
   "%s" %s %sN(   s   selfs   log_messages   requestlines   strs   codes   size(   s   selfs   codes   sizes2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   log_request?s   s   -c    s   ISUt  |  i |  d S(   s   Log an error.

        This is called when a request cannot be fulfilled.  By
        default it passes the message on to log_message().

        Arguments are the same as for log_message().

        XXX This should go to the separate error log.

        N(   s   applys   selfs   log_messages   args(   s   selfs   argss2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys	   log_errorIs   
c    s:   Wfht  i i d |  i   |  i   | | f  d S(   s  Log an arbitrary message.

        This is used by all other logging functions.  Override
        it if you have specific logging wishes.

        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!).

        The client host and current date/time are prefixed to
        every message.

        s   %s - - [%s] %s
N(   s   syss   stderrs   writes   selfs   address_strings   log_date_time_strings   formats   args(   s   selfs   formats   argss2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   log_messageWs   c    s   mno|  i d |  i Sd S(   s*   Return the server software version string.s    N(   s   selfs   server_versions   sys_version(   s   selfs2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   version_stringms   c  	  s}   qrst  i    } tt  i |  \	 } } } } } } } }	 }
 ud |  i | | |  i | | | | | f } y| Sd S(   s@   Return the current date and time formatted for a message header.s#   %s, %02d %3s %4d %02d:%02d:%02d GMTN(   s   times   nows   gmtimes   years   months   days   hhs   mms   sss   wds   ys   zs   selfs   weekdaynames	   monthnames   s(   s   selfs   nows   years   months   days   hhs   mms   sss   wds   ys   zs   ss2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   date_time_stringqs
   -0c  	  ss   {|}t  i    } ~t  i |  \	 } } } } } } } }	 }
 d | |  i | | | | | f } | Sd S(   s.   Return the current time formatted for logging.s   %02d/%3s/%04d %02d:%02d:%02dN(   s   times   nows	   localtimes   years   months   days   hhs   mms   sss   xs   ys   zs   selfs	   monthnames   s(   s   selfs   nows   years   months   days   hhs   mms   sss   xs   ys   zs   ss2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   log_date_time_string{s
   -&s   Mons   Tues   Weds   Thus   Fris   Sats   Suns   Jans   Febs   Mars   Aprs   Mays   Juns   Juls   Augs   Seps   Octs   Novs   Decc    s   |  i \ } } y t i |  \ } } } Wn" t i	 j
 o } | Sn X| i d |  x. | d r! } d | j o | Sn q~ W| d Sd S(   s   Return the client address formatted for logging.

        This version looks up the full hostname using gethostbyaddr(),
        and tries to find a name that contains at least one dot.

        i    s   .N(   s   selfs   client_addresss   hosts   ports   sockets   gethostbyaddrs   names   namess	   addressess   errors   msgs   insert(   s   selfs   hosts   ports   names   namess	   addressess   msgs2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   address_strings    	 s   HTTP/1.0s   OKs#   Request fulfilled, document followsi   s   Createds   Document created, URL followsi   s   Accepteds/   Request accepted, processing continues off-linei   s   Partial informations   Request fulfilled from cachei   s   No responses"   Request fulfilled, nothing followsi   s   Moveds(   Object moved permanently -- see URI listi-  s   Founds(   Object moved temporarily -- see URI listi.  s   Methods'   Object moved -- see Method and URL listi/  s   Not modifieds)   Document has not changed singe given timei0  s   Bad requests(   Bad request syntax or unsupported methodi  s   Unauthorizeds*   No permission -- see authorization schemesi  s   Payment requireds"   No payment -- see charging schemesi  s	   Forbiddens0   Request forbidden -- authorization will not helpi  s	   Not founds   Nothing matches the given URIi  s   Internal errors   Server got itself in troublei  s   Not implementeds&   Server does not support this operationi  s   Service temporarily overloadeds8   The server cannot process the request due to a high loadi  s   Gateway timeouts4   The gateway server did not receive a timely responsei  (   s   __doc__s   strings   splits   syss   versions   sys_versions   __version__s   server_versions   handles   Nones
   send_errors   DEFAULT_ERROR_MESSAGEs   error_message_formats   send_responses   send_headers   end_headerss   log_requests	   log_errors   log_messages   version_strings   date_time_strings   log_date_time_strings   weekdaynames	   monthnames   address_strings   protocol_versions	   mimetoolss   Messages   MessageClasss	   responses(    s2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   BaseHTTPRequestHandlerk s*   	f)	

0	c    s   t  i d o t i t  i d  } n
 d } d | f } | | |   } d G| Gd GH| i	   d S(   s   Test the HTTP request handler class.

    This runs an HTTP server on port 8000 (or the first command line
    argument).

    i   i@  s    s   Serving HTTP on ports   ...N(
   s   syss   argvs   strings   atois   ports   server_addresss   ServerClasss   HandlerClasss   httpds   serve_forever(   s   HandlerClasss   ServerClasss   ports   server_addresss   httpds2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   tests   	s   __main__N(   s   __doc__s   __version__s   syss   times   sockets   strings   rfc822s	   mimetoolss   SocketServers   DEFAULT_ERROR_MESSAGEs	   TCPServers
   HTTPServers   StreamRequestHandlers   BaseHTTPRequestHandlers   tests   __name__(    s2   /usr/lib/python1.5/site-packages/BaseHTTPServer.pys   ? s   ,									 b