
    L^i                     |   d Z ddlZddlZddlmZmZmZ g dZ eed          re	                    ddg            G d d	          Z
 G d
 de
ej                  Z G d dej                  Z G d dej                  Z eed          r' G d de
ej                  Z G d dej                  Zd Zd ZdS )a  Streaming HTTP uploads module.

This module extends the standard httplib and urllib2 objects so that
iterable objects can be used in the body of HTTP requests.

In most cases all one should have to do is call :func:`register_openers()`
to register the new streaming http handlers which will take priority over
the default handlers, and then you can use iterable objects in the body
of HTTP requests.

**N.B.** You must specify a Content-Length header if using an iterable object
since there is no way to determine in advance the total size that will be
yielded, and there is no way to reset an interator.

Example usage:

>>> from StringIO import StringIO
>>> import urllib2, poster.streaminghttp

>>> opener = poster.streaminghttp.register_openers()

>>> s = "Test file data"
>>> f = StringIO(s)

>>> req = urllib2.Request("http://localhost:5000", f,
...                       {'Content-Length': str(len(s))})
    N)NotConnectedhttpliburllib2)StreamingHTTPConnectionStreamingHTTPRedirectHandlerStreamingHTTPHandlerregister_openersHTTPSStreamingHTTPSHandlerStreamingHTTPSConnectionc                       e Zd ZdZd ZdS )_StreamingHTTPMixinzWMixin class for HTTP and HTTPS connections that implements a streaming
    send method.c                    | j         *| j        r|                                  nt                      | j        dk    rt          dt          |                     	 d}t          |d          rt          |d          r|                    d           | j        dk    rt          d           |	                    |          }|r3| j         
                    |           |	                    |          }|1dS dS t          |d          r_t          |d	          r|                                 | j        dk    rt          d
           |D ]}| j         
                    |           dS | j         
                    |           dS # t          j        $ r; t          j                    d         }|d         dk    r|                                   w xY w)zSend ``value`` to the server.

        ``value`` can be a string object, a file-like object that supports
        a .read() method, or an iterable object that supports a .next()
        method.
        Nr   zsend:i    readseekzsendIng a read()ablenextresetzsendIng an iterable       )sock	auto_openconnectr   
debuglevelprintreprhasattrr   r   sendallr   socketerrorsysexc_infoclose)selfvalue	blocksizedataes        BD:\Nbitz\venv\Lib\site-packages\cloudinary/poster/streaminghttp.pysendz_StreamingHTTPMixin.send-   s    9~ %"nn$ ?Q'4;;'''	Iuf%% )5&)) "JJqMMM?Q&&0111zz),, 1I%%d+++ ::i00D  1 1 1 1 1 '' )5'** "KKMMM?Q&&/000! , ,DI%%d++++, , 	!!%(((((| 	 	 	q!Atrzz

		s   BF 8A-F 'F A
GN)__name__
__module____qualname____doc__r)        r(   r   r   *   s-         - - - - -r/   r   c                       e Zd ZdZdS )r   zlSubclass of `httplib.HTTPConnection` that overrides the `send()` method
    to support iterable body objectsNr*   r+   r,   r-   r.   r/   r(   r   r   ]   s        ( ( ( (r/   r   c                   6    e Zd ZdZej        j        dz
  Zd ZdS )r   al  Subclass of `urllib2.HTTPRedirectHandler` that overrides the
    `redirect_request` method to properly handle redirected POST requests

    This class is required because python 2.5's HTTPRedirectHandler does
    not remove the Content-Type or Content-Length headers when requesting
    the new resource, but the body of the original request is not preserved.
    r   c                    |                                 }|dv r|dv s
|dv rv|dk    rp|                    dd          }t          d |j                                        D                       }t          j        |||                                d	          S t          j        |	                                ||||          )
a  Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods when a
        redirection response is received.  If a redirection should
        take place, return a new Request to allow http_error_30x to
        perform the redirect.  Otherwise, raise HTTPError if no-one
        else should try to handle this url.  Return None if you can't
        but another Handler might.
        )-  .  /  i3  )GETHEAD)r4   r5   r6   POST z%20c              3   P   K   | ]!\  }}|                                 d v||fV  "dS ))zcontent-lengthzcontent-typeN)lower).0kvs      r(   	<genexpr>z@StreamingHTTPRedirectHandler.redirect_request.<locals>.<genexpr>   s^          A!" 3D "D "D  !!f"D "D "D "D   r/   T)headersorigin_req_hostunverifiable)

get_methodreplacedictrA   itemsr   Requestget_origin_req_host	HTTPErrorget_full_url)	r#   reqfpcodemsgrA   newurlm
newheaderss	            r(   redirect_requestz-StreamingHTTPRedirectHandler.redirect_requestn   s     NN(((Q/-A-A?**qF{{ ^^C//F    1B1B1D1D          J ?" # 7 7 9 9!	# # # # #C$4$4$6$6c7BOOOr/   N)r*   r+   r,   r-   r   HTTPRedirectHandlerhandler_orderrS   r.   r/   r(   r   r   b   sD          /=AMP P P P Pr/   r   c                   <    e Zd ZdZej        j        dz
  Zd Zd ZdS )r   zeSubclass of `urllib2.HTTPHandler` that uses
    StreamingHTTPConnection as its http connection class.r   c                 8    |                      t          |          S )z4Open a StreamingHTTPConnection for the given request)do_openr   r#   rL   s     r(   	http_openzStreamingHTTPHandler.http_open   s    ||3S999r/   c                    |                                 rX|                                }t          |d          st          |d          r$|                    d          st	          d          t
          j                            | |          S )zlHandle a HTTP request.  Make sure that Content-Length is specified
        if we're using an interable valuer   r   Content-length-No Content-Length specified for iterable body)has_dataget_datar   
has_header
ValueErrorr   HTTPHandlerdo_request_r#   rL   r&   s      r(   http_requestz!StreamingHTTPHandler.http_request   s    
 <<>> 	M<<>>DtV$$ Mf(=(= M~~&677 M$KM M M"..tS999r/   N)	r*   r+   r,   r-   r   rb   rU   rZ   re   r.   r/   r(   r   r      sN        = = '59M: : :: : : : :r/   r   c                       e Zd ZdZdS )r   zpSubclass of `httplib.HTTSConnection` that overrides the `send()`
        method to support iterable body objectsNr1   r.   r/   r(   r   r      s        	3 	3 	3 	3r/   c                   <    e Zd ZdZej        j        dz
  Zd Zd ZdS )r   zkSubclass of `urllib2.HTTPSHandler` that uses
        StreamingHTTPSConnection as its http connection class.r   c                 8    |                      t          |          S )N)rX   r   rY   s     r(   
https_openz StreamingHTTPSHandler.https_open   s    << 8#>>>r/   c                    |                                 rX|                                }t          |d          st          |d          r$|                    d          st	          d          t
          j                            | |          S )Nr   r   r\   r]   )r^   r_   r   r`   ra   r   HTTPSHandlerrc   rd   s      r(   https_requestz#StreamingHTTPSHandler.https_request   s     ||~~ M||~~4(( MGD&,A,A M>>*:;; M(KM M M'33D#>>>r/   N)	r*   r+   r,   r-   r   rk   rU   ri   rl   r.   r/   r(   r   r      sP        	B 	B  ,:Q>	? 	? 	?		? 		? 		? 		? 		?r/   c                      t           t          g} t          t          d          r|                     t
                     | S )Nr
   )r   r   r   r   appendr   )handlerss    r(   get_handlersrp      s7    $&BCHw   /-...Or/   c                  b    t          j        t                       } t          j        |            | S )zRegister the streaming http handlers in the global urllib2 default
    opener object.

    Returns the created OpenerDirector object.)r   build_openerrp   install_opener)openers    r(   r	   r	      s+    
 !<>>2F6"""Mr/   )r-   r   r    cloudinary.compatr   r   r   __all__r   extendr   HTTPConnectionr   rT   r   rb   r   HTTPSConnectionr   rk   r   rp   r	   r.   r/   r(   <module>rz      s   8  



 < < < < < < < < < <7 7 7 77G JNN+-GHIII0 0 0 0 0 0 0 0f( ( ( ( (173I ( ( (
*P *P *P *P *P7#> *P *P *PZ: : : : :7. : : :0 77G ?3 3 3 3 3#68O 3 3 3? ? ? ? ? 4 ? ? ?*  	 	 	 	 	r/   