
\[\                 @   s@  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d d d d d	 d
 d d d d d d d d d g Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d  Z d! Z d" Z d# Z d$ Z d% Z d& Z d' Z d( Z  d) Z! d* Z" d+ Z# d, Z$ d- Z% d. Z& d/ Z' d0 Z( d1 Z) d2 Z* d3 Z+ d4 Z, d5 Z- d6 Z. d7 Z/ d8 Z0 d9 Z1 d: Z2 d; Z3 d< Z4 d= Z5 d> Z6 d? Z7 d@ Z8 dA Z9 dB Z: dC Z; dD Z< dE Z= dF Z> dG Z? dH Z@ dI ZA dJ ZB dK ZC dL ZD dM ZE dN ZF i- dO d 6dP d 6dQ d 6dR d 6dS d 6dT d  6dU d! 6dV d" 6dW d# 6dX d& 6dY d' 6dZ d( 6d[ d) 6d\ d* 6d] d+ 6d^ d_ 6d` d, 6da d- 6db d. 6dc d/ 6dd d0 6de d1 6df d2 6dg d3 6dh d4 6di d5 6dj d6 6dk d7 6dl d8 6dm d9 6dn d: 6do d; 6dp d< 6dq d= 6dr d> 6ds dC 6dt dD 6du dE 6dv dF 6dw dG 6dx dH 6dy dI 6dz dJ 6d{ dK 6d| dN 6ZG d} ZH d~ ZI d ZJ e jK d  jL ZM e jK d  jN ZO Gd d   d e jP jQ  ZR eR d d  ZS Gd d   d e jT  ZU Gd d   d  ZV y d d lW ZW Wn eX k
 rYn$ XGd d   d eV  ZY e jZ d  Gd d   d e[  Z\ Gd d   d e\  Z] Gd d   d e\  Z^ Gd d   d e\  Z_ Gd d	   d	 e\  Z` Gd d
   d
 e\  Za Gd d   d e\  Zb Gd d   d e\  Zc Gd d   d ec  Zd Gd d   d ec  Ze Gd d   d ec  Zf Gd d   d e\  Zg Gd d   d e\  Zh e\ Zi d S)a	  HTTP/1.1 client library

<intro stuff goes here>
<other stuff, too>

HTTPConnection goes through a number of "states", which define when a client
may legally make another request or fetch the response for a particular
request. This diagram details these state transitions:

    (null)
      |
      | HTTPConnection()
      v
    Idle
      |
      | putrequest()
      v
    Request-started
      |
      | ( putheader() )*  endheaders()
      v
    Request-sent
      |
      | response = getresponse()
      v
    Unread-response   [Response-headers-read]
      |\____________________
      |                     |
      | response.read()     | putrequest()
      v                     v
    Idle                  Req-started-unread-response
                     ______/|
                   /        |
   response.read() |        | ( putheader() )*  endheaders()
                   v        v
       Request-started    Req-sent-unread-response
                            |
                            | response.read()
                            v
                          Request-sent

This diagram presents the following rules:
  -- a second request may not be started until {response-headers-read}
  -- a response [object] cannot be retrieved until {request-sent}
  -- there is no differentiation between an unread response body and a
     partially read response body

Note: this enforcement is applied by the HTTPConnection class. The
      HTTPResponse class does not enforce this state machine, which
      implies sophisticated clients may accelerate the request/response
      pipeline. Caution should be taken, though: accelerating the states
      beyond the above pattern may imply knowledge of the server's
      connection-close behavior for certain requests. For example, it
      is impossible to tell whether the server will close the connection
      UNTIL the response headers have been read; this means that further
      requests cannot be placed into the pipeline until it is known that
      the server will NOT be closing the connection.

Logical State                  __state            __response
-------------                  -------            ----------
Idle                           _CS_IDLE           None
Request-started                _CS_REQ_STARTED    None
Request-sent                   _CS_REQ_SENT       None
Unread-response                _CS_IDLE           <response_class>
Req-started-unread-response    _CS_REQ_STARTED    <response_class>
Req-sent-unread-response       _CS_REQ_SENT       <response_class>
    N)urlsplitHTTPResponseHTTPConnectionHTTPExceptionNotConnectedUnknownProtocolUnknownTransferEncodingUnimplementedFileModeIncompleteRead
InvalidURLImproperConnectionStateCannotSendRequestCannotSendHeaderResponseNotReadyBadStatusLineerror	responsesP   i  ZUNKNOWNZIdlezRequest-startedzRequest-sentd   e   f                              i,  i-  i.  i/  i0  i1  i3  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  ZContinuezSwitching ProtocolsOKZCreatedZAcceptedzNon-Authoritative Informationz
No ContentzReset ContentzPartial ContentzMultiple ChoiceszMoved PermanentlyZFoundz	See OtherzNot Modifiedz	Use Proxyz(Unused)i2  zTemporary RedirectzBad RequestZUnauthorizedzPayment RequiredZ	Forbiddenz	Not FoundzMethod Not AllowedzNot AcceptablezProxy Authentication RequiredzRequest TimeoutZConflictZGonezLength RequiredzPrecondition FailedzRequest Entity Too LargezRequest-URI Too LongzUnsupported Media TypezRequested Range Not SatisfiablezExpectation FailedzPrecondition RequiredzToo Many RequestszRequest Header Fields Too LargezInternal Server ErrorzNot ImplementedzBad GatewayzService UnavailablezGateway TimeoutzHTTP Version Not SupportedzNetwork Authentication Requiredi   i   s   [^:\s][^:\r\n]*s   \n(?![ \t])|\r(?![ \t\n])c               @   s   e  Z d  Z d d   Z d S)HTTPMessagec             C   s   | j    d } t |  } g  } d } xn |  j   D]` } | d |  j    | k r` d } n | d d  j   s d } n  | r5 | j |  q5 q5 W| S)a  Find all header lines matching a given header name.

        Look through the list of headers and find all lines matching a given
        header name (and their continuation lines).  A list of the lines is
        returned, without interpretation.  If the header does not occur, an
        empty list is returned.  If the header occurs multiple times, all
        occurrences are returned.  Case is not important in the header name.

        :r   N   )lowerlenkeysisspaceappend)selfnamenZlstZhitline r-   !/usr/lib/python3.4/http/client.pygetallmatchingheaders   s    
		z!HTTPMessage.getallmatchingheadersN)__name__
__module____qualname__r/   r-   r-   r-   r.   r!      s   r!   c             C   s   g  } xy |  j  t d  } t |  t k r= t d   n  | j |  t |  t k ro t d t   n  | d	 k r	 Pq	 q	 d j |  j d  } t	 j
 j d |  j |  S)
aG  Parses only RFC2822 headers from a file pointer.

    email Parser 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, for email Parser
    to parse.

    r#   zheader linezgot more than %d headers   
   
    z
iso-8859-1_class)r3   r4   r5   )readline_MAXLINEr%   LineTooLongr(   _MAXHEADERSr   joindecodeemailparserZParserZparsestr)fpr6   headersr,   Zhstringr-   r-   r.   parse_headers  s    
rA   c                   sQ  e  Z d  Z d d d d d  Z d d   Z d d   Z d	 d
   Z d d   Z   f d d   Z   f d d   Z	 d d   Z
 d d   Z d   f d d  Z d d   Z d d   Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d d' d(  Z d) d*   Z d+ d,   Z d- d.   Z d/ d0   Z d1 d2   Z   S)3r   r   Nc             C   sw   | j  d  |  _ | |  _ | |  _ d  |  _ |  _ t |  _ t |  _ t |  _	 t |  _
 t |  _ t |  _ t |  _ d  S)Nrb)Zmakefiler?   
debuglevel_methodr@   msg_UNKNOWNversionstatusreasonchunked
chunk_leftlength
will_close)r)   sockrC   methodurlr-   r-   r.   __init__7  s    								zHTTPResponse.__init__c             C   sx  t  |  j j t d  d  } t |  t k r@ t d   n  |  j d k re t d t |   n  | sz t	 |   n  y | j
 d  d  \ } } } WnO t k
 r y" | j
 d  d  \ } } d } Wn t k
 r d } Yn XYn X| j d  s|  j   t	 |   n  y7 t |  } | d	 k  s:| d
 k rIt	 |   n  Wn t k
 rjt	 |   Yn X| | | f S)Nr#   z
iso-8859-1zstatus liner   zreply:    zHTTP/r   i  )strr?   r7   r8   r%   r9   rC   printreprr   split
ValueError
startswith_close_connint)r)   r,   rG   rH   rI   r-   r-   r.   _read_statusU  s2    

zHTTPResponse._read_statusc             C   s  |  j  d  k	 r d  Sx |  j   \ } } } | t k r; Pn  xo |  j j t d  } t |  t k ru t d   n  | j   } | s Pn  |  j	 d k r> t
 d |  q> q> q | |  _ |  _ | j   |  _ | d k r d |  _ n' | j d  rd	 |  _ n t |   t |  j  |  _  |  _ |  j	 d k r`x' |  j  D] } t
 d | d
 d q@Wn  |  j  j d  } | r| j   d k rd |  _ d  |  _ n	 d |  _ |  j   |  _ d  |  _ |  j  j d  } |  j  j d  } | rC|  j rCy t |  |  _ Wn t k
 r$d  |  _ YqLX|  j d k  rLd  |  _ qLn	 d  |  _ | t k s| t k sd | k o{d k  n s|  j d k rd |  _ n  |  j r|  j r|  j d  k rd |  _ n  d  S)Nr#   zheader liner   zheader:HTTP/1.0HTTP/0.9
   zHTTP/1.   end ztransfer-encodingrJ   TFzcontent-lengthr   r   HEAD)r]   r^   )r@   r\   CONTINUEr?   r7   r8   r%   r9   striprC   rU   coderH   rI   rG   rY   r   rA   rE   getr$   rJ   rK   _check_closerM   rL   r[   rX   
NO_CONTENTNOT_MODIFIEDrD   )r)   rG   rH   rI   skiphdrZtr_encrL   r-   r-   r.   beginu  sf    				

zHTTPResponse.beginc             C   s   |  j  j d  } |  j d k rS |  j  j d  } | rO d | j   k rO d Sd S|  j  j d  ri d S| r d | j   k r d S|  j  j d  } | r d | j   k r d Sd S)NZ
connectionr`   closeTFz
keep-alivezproxy-connection)r@   rg   rG   r$   )r)   ZconnZpconnr-   r-   r.   rh     s    zHTTPResponse._check_closec             C   s    |  j  } d  |  _  | j   d  S)N)r?   rn   )r)   r?   r-   r-   r.   rZ     s    		zHTTPResponse._close_connc                s'   t    j   |  j r# |  j   n  d  S)N)superrn   r?   rZ   )r)   )	__class__r-   r.   rn     s    	zHTTPResponse.closec                s*   t    j   |  j r& |  j j   n  d  S)N)ro   flushr?   )r)   )rp   r-   r.   rq     s    	zHTTPResponse.flushc             C   s   d S)NTr-   )r)   r-   r-   r.   readable  s    zHTTPResponse.readablec             C   s   |  j  d k S)z!True if the connection is closed.N)r?   )r)   r-   r-   r.   isclosed  s    zHTTPResponse.isclosedc                s   |  j  d  k r d S|  j d k r0 |  j   d S| d  k	 rR t t |   j |  S|  j re |  j   S|  j d  k r |  j  j   } nA y |  j	 |  j  } Wn t
 k
 r |  j     Yn Xd |  _ |  j   | Sd  S)Nr5   rc   r   )r?   rD   rZ   ro   r   readrJ   _readall_chunkedrL   
_safe_readr
   )r)   amts)rp   r-   r.   rt     s&    
	

	
zHTTPResponse.readc             C   s   |  j  d  k r d S|  j d k r0 |  j   d S|  j rF |  j |  S|  j d  k	 r t |  |  j k r t |  d |  j  } q n  |  j  j |  } | r | r |  j   n7 |  j d  k	 r |  j | 8_ |  j s |  j   q n  | S)Nr   rc   )	r?   rD   rZ   rJ   _readinto_chunkedrL   r%   
memoryviewreadinto)r)   br+   r-   r-   r.   r{   %  s$    
		zHTTPResponse.readintoc             C   s   |  j  j t d  } t |  t k r7 t d   n  | j d  } | d k re | d  |  } n  y t | d  SWn t k
 r |  j     Yn Xd  S)Nr#   z
chunk size   ;r      )	r?   r7   r8   r%   r9   findr[   rX   rZ   )r)   r,   ir-   r-   r.   _read_next_chunk_sizeC  s    
z"HTTPResponse._read_next_chunk_sizec             C   s[   xT |  j  j t d  } t |  t k r: t d   n  | sD Pn  | d k r Pq q d  S)Nr#   ztrailer line   
   
r5   )r   r   r5   )r?   r7   r8   r%   r9   )r)   r,   r-   r-   r.   _read_and_discard_trailerS  s    z&HTTPResponse._read_and_discard_trailerc             C   s   |  j  t k s t  |  j } g  } x | d  k r y  |  j   } | d k rR Pn  Wq t k
 r| t d j |    Yq Xn  | j |  j	 |   |  j	 d  d  } q' |  j
   |  j   d j |  S)Nr   r5   rR   )rJ   rF   AssertionErrorrK   r   rX   r
   r;   r(   rv   r   rZ   )r)   rK   valuer-   r-   r.   ru   a  s"    		

zHTTPResponse._readall_chunkedc             C   so  |  j  t k s t  |  j } d } t |  } x$| d  k r y  |  j   } | d k r^ Pn  Wq t k
 r t t | d |     Yq Xn  t	 |  | k  r |  j
 |  } | | |  _ | | St	 |  | k r|  j
 |  } |  j d  d  |  _ | | S| d |  } |  j
 |  } | | d   } | | 7} |  j d  d  } q3 |  j   |  j   | S)Nr   rR   )rJ   rF   r   rK   rz   r   rX   r
   bytesr%   _safe_readintorv   r   rZ   )r)   r|   rK   total_bytesmvbr+   temp_mvbr-   r-   r.   ry   z  s<    	$	
	

zHTTPResponse._readinto_chunkedc             C   s   g  } xi | d k rq |  j  j t | t   } | sQ t d j |  |   n  | j |  | t |  8} q	 Wd j |  S)aV  Read the number of bytes requested, compensating for partial reads.

        Normally, we have a blocking socket, but a read() can be interrupted
        by a signal (resulting in a partial read).

        Note that we cannot distinguish between EOF and an interrupt when zero
        bytes have been read. IncompleteRead() will be raised in this
        situation.

        This function should be used when <amt> bytes "should" be present for
        reading. If the bytes are truly not available (due to EOF), then the
        IncompleteRead exception can be used to detect the problem.
        r   r5   )r?   rt   min	MAXAMOUNTr
   r;   r(   r%   )r)   rw   rx   chunkr-   r-   r.   rv     s    zHTTPResponse._safe_readc             C   s   d } t  |  } x | t |  k  r t t |  k  r^ | d t  } |  j j |  } n |  j j |  } | s t t | d |   t |    n  | | d  } | | 7} q W| S)z2Same as _safe_read, but for reading into a buffer.r   N)rz   r%   r   r?   r{   r
   r   )r)   r|   r   r   r   r+   r-   r-   r.   r     s    (zHTTPResponse._safe_readintoc             C   s   |  j  j   S)N)r?   fileno)r)   r-   r-   r.   r     s    zHTTPResponse.filenoc             C   sg   |  j  d  k r t    n  |  j  j |  p0 | } t | t  sR t | d  rV | Sd j |  Sd  S)N__iter__z, )r@   r   Zget_all
isinstancerT   hasattrr;   )r)   r*   defaultr@   r-   r-   r.   	getheader  s    zHTTPResponse.getheaderc             C   s.   |  j  d k r t    n  t |  j  j    S)z&Return list of (header, value) tuples.N)r@   r   listitems)r)   r-   r-   r.   
getheaders  s    zHTTPResponse.getheadersc             C   s   |  S)Nr-   )r)   r-   r-   r.   r     s    zHTTPResponse.__iter__c             C   s   |  j  S)N)r@   )r)   r-   r-   r.   info  s    zHTTPResponse.infoc             C   s   |  j  S)N)rP   )r)   r-   r-   r.   geturl  s    zHTTPResponse.geturlc             C   s   |  j  S)N)rH   )r)   r-   r-   r.   getcode  s    zHTTPResponse.getcode)r0   r1   r2   rQ   r\   rm   rh   rZ   rn   rq   rr   rs   rt   r{   r   r   ru   ry   rv   r   r   r   r   r   r   r   r   r-   r-   )rp   r.   r   .  s0   	 P

)	c               @   s*  e  Z d  Z d Z d Z e Z e Z d Z	 d Z
 d Z d e j d d d  Z d d d	 d
  Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d d  Z d d d d  Z d d   Z d d d   Z d i  d! d"  Z d# d$   Z d% d&   Z d' d(   Z d S))r   r`   zHTTP/1.1r#   r   i @  Nc             C   s   | |  _  | |  _ d  |  _ g  |  _ d  |  _ t |  _ d  |  _ d  |  _ d  |  _	 i  |  _
 |  j | |  \ |  _ |  _ t j |  _ d  S)N)timeoutsource_addressrN   _buffer_HTTPConnection__response_CS_IDLE_HTTPConnection__staterD   _tunnel_host_tunnel_port_tunnel_headers_get_hostporthostportsocketZcreate_connection_create_connection)r)   r   r   r   r   r-   r-   r.   rQ     s    										zHTTPConnection.__init__c             C   sM   |  j  r t d   n  | |  _ | |  _ | r< | |  _ n |  j j   d S)aD  Set up host and port for HTTP CONNECT tunnelling.

        In a connection that uses HTTP CONNECT tunneling, the host passed to the
        constructor is used as a proxy server that relays all communication to
        the endpoint passed to `set_tunnel`. This done by sending an HTTP
        CONNECT request to the proxy server when the connection is established.

        This method must be called before the HTML connection has been
        established.

        The headers argument should be a mapping of extra HTTP headers to send
        with the CONNECT request.
        z.Can't set up tunnel for established connectionN)rN   RuntimeErrorr   r   r   clear)r)   r   r   r@   r-   r-   r.   
set_tunnel  s    			zHTTPConnection.set_tunnelc             C   s  | d  k r| j  d  } | j  d  } | | k r y t | | d d    } WnV t k
 r | | d d   d k r |  j } n t d | | d d     Yn X| d  |  } n	 |  j } | r| d d k r| d d k r| d d	  } qn  | | f S)
Nr"   ]r#   rS   znonnumeric port: '%s'r   [r   )rfindr[   rX   default_portr   )r)   r   r   r   jr-   r-   r.   r   *  s    #	&zHTTPConnection._get_hostportc             C   s   | |  _  d  S)N)rC   )r)   levelr-   r-   r.   set_debuglevel>  s    zHTTPConnection.set_debuglevelc             C   sc  |  j  |  j |  j  \ } } d | | f } | j d  } |  j |  xI |  j j   D]8 \ } } d | | f } | j d  } |  j |  qZ W|  j d  |  j |  j d |  j	 }	 |	 j
   \ }
 } } | d k r|  j   t d | | j   f   n  xT |	 j j t d	  } t |  t k rBt d
   n  | sLPn  | d k rPqqd  S)NzCONNECT %s:%d HTTP/1.0
asciiz%s: %s
zlatin-1   
rO   r   zTunnel connection failed: %d %sr#   zheader line   
r5   )r   r   r5   )r   r   r   encodesendr   r   response_classrN   rD   r\   rn   OSErrorre   r?   r7   r8   r%   r9   )r)   r   r   Zconnect_strZconnect_bytesheaderr   Z
header_strZheader_bytesresponserG   rf   messager,   r-   r-   r.   _tunnelA  s0    
	zHTTPConnection._tunnelc             C   sD   |  j  |  j |  j f |  j |  j  |  _ |  j r@ |  j   n  d S)z3Connect to the host and port specified in __init__.N)r   r   r   r   r   rN   r   r   )r)   r-   r-   r.   connect^  s    	zHTTPConnection.connectc             C   sQ   |  j  r" |  j  j   d |  _  n  |  j rD |  j j   d |  _ n  t |  _ d S)z(Close the connection to the HTTP server.N)rN   rn   r   r   r   )r)   r-   r-   r.   rn   f  s    		zHTTPConnection.closec             C   s  |  j  d k r1 |  j r% |  j   q1 t    n  |  j d k rV t d t |   n  d } t | d  r+|  j d k r t d  n  d } y | j } Wn t	 k
 r Yn2 Xd | k r d	 } |  j d k r t d
  q n  xD | j
 |  } | s Pn  | r| j d  } n  |  j  j |  q d Sy |  j  j |  Wn^ t k
 rt | t j  rx7 | D] } |  j  j |  qhWn t d t |    Yn Xd S)zSend `data' to the server.
        ``data`` can be a string object, a bytes object, an array object, a
        file-like object that supports a .read() method, or an iterable object.
        Nr   zsend:i    rt   zsendIng a read()ableFr|   Tzencoding file using iso-8859-1z
iso-8859-1z9data should be a bytes-like object or an iterable, got %r)rN   	auto_openr   r   rC   rU   rV   r   modeAttributeErrorrt   r   Zsendall	TypeErrorr   collectionsIterabletype)r)   dataZ	blocksizer   r   Z	datablockdr-   r-   r.   r   p  sF    	zHTTPConnection.sendc             C   s   |  j  j |  d S)zuAdd a line of output to the current request buffer.

        Assumes that the line does *not* end with \r\n.
        N)r   r(   )r)   rx   r-   r-   r.   _output  s    zHTTPConnection._outputc             C   s   |  j  j d  d j |  j   } |  j  d d  =t | t  ri t |  |  j k  ri | | 7} d } n  |  j |  | d k	 r |  j |  n  d S)zSend the currently buffered request and clear the buffer.

        Appends an extra \r\n to the buffer.
        A message_body may be specified, to be appended to the request.
        r5   s   
N)r5   r5   )r   extendr;   r   r   r%   mssr   )r)   message_bodyrE   r-   r-   r.   _send_output  s    $
	zHTTPConnection._send_outputc             C   s%  |  j  r$ |  j  j   r$ d |  _  n  |  j t k r? t |  _ n t |  j   | |  _ | sf d } n  d | | |  j f } |  j | j	 d   |  j
 d k r!| sd } | j d  r t |  \ } } } } } n  | r*y | j	 d  } Wn! t k
 r| j	 d  } Yn X|  j d	 |  q|  j rH|  j }	 |  j }
 n |  j }	 |  j }
 y |	 j	 d  } Wn! t k
 r|	 j	 d  } Yn X|	 j d
  d k rd | d } n  |
 |  j k r|  j d	 |  q| j d  } |  j d	 d | |
 f  n  | s!|  j d d  q!n  d S)a`  Send a request to the server.

        `method' specifies an HTTP request method, e.g. 'GET'.
        `url' specifies the object being requested, e.g. '/index.html'.
        `skip_host' if True does not add automatically a 'Host:' header
        `skip_accept_encoding' if True does not add automatically an
           'Accept-Encoding:' header
        N/z%s %s %sr   r`   rS   ZhttpZidnaZHostr"   r      [   ]z%s:%szAccept-EncodingZidentity)r   rs   r   r   _CS_REQ_STARTEDr   rD   _http_vsn_strr   r   	_http_vsnrY   r   UnicodeEncodeError	putheaderr   r   r   r   r   r   r<   )r)   rO   rP   	skip_hostskip_accept_encodingrequestZnetlocZnilZ
netloc_encr   r   Zhost_encr-   r-   r.   
putrequest  sN    						
zHTTPConnection.putrequestc             G   s/  |  j  t k r t    n  t | d  r< | j d  } n  t |  s^ t d | f   n  t |  } x t |  D] \ } } t | d  r | j d  | | <n+ t	 | t
  r t |  j d  | | <n  t | |  rw t d | | f   qw qw Wd j |  } | d | } |  j |  d S)	zkSend a request header line to the server.

        For example: h.putheader('Accept', 'text/html')
        r   r   zInvalid header name %rzlatin-1zInvalid header value %rs   
	s   : N)r   r   r   r   r   _is_legal_header_namerX   r   	enumerater   r[   rT   _is_illegal_header_valuer;   r   )r)   r   valuesr   Z	one_valuer   r-   r-   r.   r   :  s"    zHTTPConnection.putheaderc             C   s5   |  j  t k r t |  _  n	 t    |  j |  d S)a  Indicate that the last header line has been sent to the server.

        This method sends the request to the server.  The optional message_body
        argument can be used to pass a message body associated with the
        request.  The message body will be sent in the same packet as the
        message headers if it is a string, otherwise it is sent as a separate
        packet.
        N)r   r   _CS_REQ_SENTr   r   )r)   r   r-   r-   r.   
endheadersV  s    		zHTTPConnection.endheadersc             C   s   |  j  | | | |  d S)z&Send a complete request to the server.N)_send_request)r)   rO   rP   bodyr@   r-   r-   r.   r   e  s    zHTTPConnection.requestc             C   s   d  } y t  t |   } Wn~ t k
 r } z^ y" t  t j | j    j  } Wn4 t t f k
 r |  j	 d k r t
 d  n  Yn XWYd  d  } ~ Xn X| d  k	 r |  j d |  n  d  S)Nr   zCannot stat!!zContent-Length)rT   r%   r   osfstatr   st_sizer   r   rC   rU   r   )r)   r   ZthelenZter-   r-   r.   _set_content_lengthi  s    " %z"HTTPConnection._set_content_lengthc       	      C   s   t  j d d   | D  } i  } d | k r; d | d <n  d | k rT d | d <n  |  j | | |  | d  k	 r d | k r |  j |  n  x* | j   D] \ } } |  j | |  q Wt | t  r | j d	  } n  |  j	 |  d  S)
Nc             S   s   g  |  ] } | j     q Sr-   )r$   ).0kr-   r-   r.   
<listcomp>|  s   	 z0HTTPConnection._send_request.<locals>.<listcomp>r   r#   r   zaccept-encodingr   zcontent-lengthz
iso-8859-1)
dictfromkeysr   r   r   r   r   rT   r   r   )	r)   rO   rP   r   r@   Zheader_namesZskipsrl   r   r-   r-   r.   r   z  s    zHTTPConnection._send_requestc             C   s   |  j  r$ |  j  j   r$ d |  _  n  |  j t k s< |  j  rN t |  j   n  |  j d k r |  j |  j |  j d |  j } n |  j |  j d |  j } | j	   | j
 t k s t  t |  _ | j
 r |  j   n	 | |  _  | S)a/  Get the response from the server.

        If the HTTPConnection is in the correct state, returns an
        instance of HTTPResponse or of whatever object is returned by
        class the response_class variable.

        If a request has not been sent or if a previous response has
        not be handled, ResponseNotReady is raised.  If the HTTP
        response indicates that the connection should be closed, then
        it will be closed before the response is returned.  When the
        connection is closed, the underlying socket is closed.
        Nr   rO   )r   rs   r   r   r   rC   r   rN   rD   rm   rM   rF   r   r   rn   )r)   r   r-   r-   r.   getresponse  s    
			zHTTPConnection.getresponse)r0   r1   r2   r   r   r   r   	HTTP_PORTr   r   rC   r   r   _GLOBAL_DEFAULT_TIMEOUTrQ   r   r   r   r   r   rn   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   r     s2   	
0{c                   s^   e  Z d  Z d Z e Z d d d e j d d d d d   f d d Z   f d d   Z	   S)	HTTPSConnectionz(This class allows communication via SSL.Ncontextcheck_hostnamec      
         s   t  t |   j | | | |  | |  _ | |  _ | d  k rL t j   } n  | j t j k }	 | d  k rs |	 } n | r |	 r t	 d   n  | s | r | j
 | |  n  | |  _ | |  _ d  S)NzMcheck_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED)ro   r   rQ   key_file	cert_filesslZ_create_stdlib_contextZverify_modeZ	CERT_NONErX   Zload_cert_chain_context_check_hostname)
r)   r   r   r   r   r   r   r   r   Zwill_verify)rp   r-   r.   rQ     s    				zHTTPSConnection.__init__c                s   t    j   |  j r" |  j } n	 |  j } t j r: | n d } |  j j |  j d | |  _ |  j j	 r |  j
 r y t j |  j j   |  Wq t k
 r |  j j t j  |  j j     Yq Xn  d S)z(Connect to a host on a given (SSL) port.Nserver_hostname)ro   r   r   r   r   ZHAS_SNIr   Zwrap_socketrN   r   r   Zmatch_hostnameZgetpeercert	ExceptionZshutdownr   Z	SHUT_RDWRrn   )r)   r   Zsni_hostname)rp   r-   r.   r     s    		zHTTPSConnection.connect)
r0   r1   r2   __doc__
HTTPS_PORTr   r   r   rQ   r   r-   r-   )rp   r.   r     s   	r   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r     s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r     s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r     s   c               @   s   e  Z d  Z d d   Z d S)r   c             C   s   | f |  _  | |  _ d  S)N)argsrG   )r)   rG   r-   r-   r.   rQ   
  s    zUnknownProtocol.__init__N)r0   r1   r2   rQ   r-   r-   r-   r.   r   	  s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r     s   c               @   s   e  Z d  Z d S)r	   N)r0   r1   r2   r-   r-   r-   r.   r	     s   c               @   s7   e  Z d  Z d d d  Z d d   Z d d   Z d S)r
   Nc             C   s"   | f |  _  | |  _ | |  _ d  S)N)r   partialexpected)r)   r   r   r-   r-   r.   rQ     s    	zIncompleteRead.__init__c             C   s<   |  j  d  k	 r d |  j  } n d } d t |  j  | f S)Nz, %i more expectedrS   zIncompleteRead(%i bytes read%s))r   r%   r   )r)   er-   r-   r.   __repr__  s    zIncompleteRead.__repr__c             C   s
   t  |   S)N)rV   )r)   r-   r-   r.   __str__  s    zIncompleteRead.__str__)r0   r1   r2   rQ   r   r   r-   r-   r-   r.   r
     s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r   "  s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r   %  s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r   (  s   c               @   s   e  Z d  Z d S)r   N)r0   r1   r2   r-   r-   r-   r.   r   +  s   c               @   s   e  Z d  Z d d   Z d S)r   c             C   s.   | s t  |  } n  | f |  _ | |  _ d  S)N)rV   r   r,   )r)   r,   r-   r-   r.   rQ   /  s    zBadStatusLine.__init__N)r0   r1   r2   rQ   r-   r-   r-   r.   r   .  s   c               @   s   e  Z d  Z d d   Z d S)r9   c             C   s   t  j |  d t | f  d  S)Nz&got more than %d bytes when reading %s)r   rQ   r8   )r)   Z	line_typer-   r-   r.   rQ   6  s    zLineTooLong.__init__N)r0   r1   r2   rQ   r-   r-   r-   r.   r9   5  s   r9   )jr   Zemail.parserr=   Zemail.messageior   rer   r   Zurllib.parser   __all__r   r   rF   r   r   r   rd   ZSWITCHING_PROTOCOLSZ
PROCESSINGr    ZCREATEDZACCEPTEDZNON_AUTHORITATIVE_INFORMATIONri   ZRESET_CONTENTZPARTIAL_CONTENTZMULTI_STATUSZIM_USEDZMULTIPLE_CHOICESZMOVED_PERMANENTLYZFOUNDZ	SEE_OTHERrj   Z	USE_PROXYZTEMPORARY_REDIRECTZBAD_REQUESTZUNAUTHORIZEDZPAYMENT_REQUIREDZ	FORBIDDENZ	NOT_FOUNDZMETHOD_NOT_ALLOWEDZNOT_ACCEPTABLEZPROXY_AUTHENTICATION_REQUIREDZREQUEST_TIMEOUTZCONFLICTZGONEZLENGTH_REQUIREDZPRECONDITION_FAILEDZREQUEST_ENTITY_TOO_LARGEZREQUEST_URI_TOO_LONGZUNSUPPORTED_MEDIA_TYPEZREQUESTED_RANGE_NOT_SATISFIABLEZEXPECTATION_FAILEDZUNPROCESSABLE_ENTITYZLOCKEDZFAILED_DEPENDENCYZUPGRADE_REQUIREDZPRECONDITION_REQUIREDZTOO_MANY_REQUESTSZREQUEST_HEADER_FIELDS_TOO_LARGEZINTERNAL_SERVER_ERRORZNOT_IMPLEMENTEDZBAD_GATEWAYZSERVICE_UNAVAILABLEZGATEWAY_TIMEOUTZHTTP_VERSION_NOT_SUPPORTEDZINSUFFICIENT_STORAGEZNOT_EXTENDEDZNETWORK_AUTHENTICATION_REQUIREDr   r   r8   r:   compile	fullmatchr   searchr   r   ZMessager!   rA   	RawIOBaser   r   r   ImportErrorr   r(   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r9   r   r-   r-   r-   r.   <module>C   s,  			
  1