<s> import cgi <EOL> import copy <EOL> import logging <EOL> import mimetypes <EOL> import os <EOL> from StringIO import StringIO <EOL> import types <EOL> import urlparse <EOL> import uuid <EOL> from restkit . datastructures import MultiDict <EOL> from restkit . errors import AlreadyRead , RequestError <EOL> from restkit . forms import multipart_form_encode , form_encode <EOL> from restkit . tee import ResponseTeeInput <EOL> from restkit . util import to_bytestring <EOL> from restkit . util import parse_cookie <EOL> log = logging . getLogger ( __name__ ) <EOL> class Request ( object ) : <EOL> def __init__ ( self , url , method = '<STR_LIT:GET>' , body = None , headers = None ) : <EOL> headers = headers or [ ] <EOL> self . url = url <EOL> self . initial_url = url <EOL> self . method = method <EOL> self . _headers = None <EOL> self . _body = None <EOL> self . is_proxied = False <EOL> self . headers = headers <EOL> if body is not None : <EOL> self . body = body <EOL> def _headers__get ( self ) : <EOL> if not isinstance ( self . _headers , MultiDict ) : <EOL> self . _headers = MultiDict ( self . _headers or [ ] ) <EOL> return self . _headers <EOL> def _headers__set ( self , value ) : <EOL> self . _headers = MultiDict ( copy . copy ( value ) ) <EOL> headers = property ( _headers__get , _headers__set , doc = _headers__get . __doc__ ) <EOL> def _parsed_url ( self ) : <EOL> if self . url is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return urlparse . urlparse ( self . url ) <EOL> parsed_url = property ( _parsed_url , doc = "<STR_LIT>" ) <EOL> def _path__get ( self ) : <EOL> parsed_url = self . parsed_url <EOL> path = parsed_url . path or '<STR_LIT:/>' <EOL> return urlparse . urlunparse ( ( '<STR_LIT>' , '<STR_LIT>' , path , parsed_url . params , <EOL> parsed_url . query , parsed_url . fragment ) ) <EOL> path = property ( _path__get ) <EOL> def _host__get ( self ) : <EOL> h = to_bytestring ( self . parsed_url . netloc ) <EOL> hdr_host = self . headers . iget ( "<STR_LIT:host>" ) <EOL> if not hdr_host : <EOL> return h <EOL> return hdr_host <EOL> host = property ( _host__get ) <EOL> def is_chunked ( self ) : <EOL> te = self . headers . iget ( "<STR_LIT>" ) <EOL> return ( te is not None and te . lower ( ) == "<STR_LIT>" ) <EOL> def is_ssl ( self ) : <EOL> return self . parsed_url . scheme == "<STR_LIT>" <EOL> def _set_body ( self , body ) : <EOL> ctype = self . headers . ipop ( '<STR_LIT>' , None ) <EOL> clen = self . headers . ipop ( '<STR_LIT>' , None ) <EOL> if isinstance ( body , dict ) : <EOL> if ctype is not None and ctype . startswith ( "<STR_LIT>" ) : <EOL> type_ , opts = cgi . parse_header ( ctype ) <EOL> boundary = opts . get ( '<STR_LIT>' , uuid . uuid4 ( ) . hex ) <EOL> self . _body , self . headers = multipart_form_encode ( body , <EOL> self . headers , boundary ) <EOL> ctype = self . headers . ipop ( '<STR_LIT>' , None ) <EOL> else : <EOL> ctype = "<STR_LIT>" <EOL> self . _body = form_encode ( body ) <EOL> elif hasattr ( body , "<STR_LIT>" ) and hasattr ( body , "<STR_LIT>" ) : <EOL> ctype = "<STR_LIT>" % body . boundary <EOL> clen = body . get_size ( ) <EOL> self . _body = body <EOL> else : <EOL> self . _body = body <EOL> if not ctype : <EOL> ctype = '<STR_LIT>' <EOL> if hasattr ( self . body , '<STR_LIT:name>' ) : <EOL> ctype = mimetypes . guess_type ( body . name ) [ <NUM_LIT:0> ] <EOL> if not clen : <EOL> if hasattr ( self . _body , '<STR_LIT>' ) : <EOL> try : <EOL> self . _body . flush ( ) <EOL> except IOError : <EOL> pass <EOL> try : <EOL> fno = self . _body . fileno ( ) <EOL> clen = str ( os . fstat ( fno ) [ <NUM_LIT:6> ] ) <EOL> except IOError : <EOL> if not self . is_chunked ( ) : <EOL> clen = len ( self . _body . read ( ) ) <EOL> elif hasattr ( self . _body , '<STR_LIT>' ) and not self . is_chunked ( ) : <EOL> clen = len ( self . _body . getvalue ( ) ) <EOL> elif isinstance ( self . _body , types . StringTypes ) : <EOL> self . _body = to_bytestring ( self . _body ) <EOL> clen = len ( self . _body ) <EOL> if clen is not None : <EOL> self . headers [ '<STR_LIT>' ] = clen <EOL> if ctype is not None : <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = ctype <EOL> def _get_body ( self ) : <EOL> return self . _body <EOL> body = property ( _get_body , _set_body , doc = "<STR_LIT>" ) <EOL> def maybe_rewind ( self , msg = "<STR_LIT>" ) : <EOL> if self . body is not None : <EOL> if not hasattr ( self . body , '<STR_LIT>' ) and not isinstance ( self . body , types . StringTypes ) : <EOL> raise RequestError ( "<STR_LIT>" <EOL> % msg ) <EOL> if log . isEnabledFor ( logging . DEBUG ) : <EOL> log . debug ( "<STR_LIT>" % msg ) <EOL> class BodyWrapper ( object ) : <EOL> def __init__ ( self , resp , connection ) : <EOL> self . resp = resp <EOL> self . body = resp . _body <EOL> self . connection = connection <EOL> self . _closed = False <EOL> self . eof = False <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , traceback ) : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _closed : <EOL> return <EOL> if not self . eof : <EOL> self . body . read ( ) <EOL> self . connection . release ( self . resp . should_close ) <EOL> self . _closed = True <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> try : <EOL> return self . body . next ( ) <EOL> except StopIteration : <EOL> self . eof = True <EOL> self . close ( ) <EOL> raise <EOL> def read ( self , n = - <NUM_LIT:1> ) : <EOL> data = self . body . read ( n ) <EOL> if not data : <EOL> self . eof = True <EOL> self . close ( ) <EOL> return data <EOL> def readline ( self , limit = - <NUM_LIT:1> ) : <EOL> line = self . body . readline ( limit ) <EOL> if not line : <EOL> self . eof = True <EOL> self . close ( ) <EOL> return line <EOL> def readlines ( self , hint = None ) : <EOL> lines = self . body . readlines ( hint ) <EOL> if self . body . close : <EOL> self . eof = True <EOL> self . close ( ) <EOL> return lines <EOL> class Response ( object ) : <EOL> charset = "<STR_LIT:utf8>" <EOL> unicode_errors = '<STR_LIT:strict>' <EOL> def __init__ ( self , connection , request , resp ) : <EOL> self . request = request <EOL> self . connection = connection <EOL> self . _resp = resp <EOL> self . headers = resp . headers ( ) <EOL> self . status = resp . status ( ) <EOL> self . status_int = resp . status_code ( ) <EOL> self . version = resp . version ( ) <EOL> self . headerslist = self . headers . items ( ) <EOL> self . location = self . headers . get ( '<STR_LIT:location>' ) <EOL> self . final_url = request . url <EOL> self . should_close = not resp . should_keep_alive ( ) <EOL> if '<STR_LIT>' in self . headers : <EOL> cookie_header = self . headers . get ( '<STR_LIT>' ) <EOL> self . cookies = parse_cookie ( cookie_header , self . final_url ) <EOL> self . _closed = False <EOL> self . _already_read = False <EOL> if request . method == "<STR_LIT>" : <EOL> """<STR_LIT>""" <EOL> self . connection . release ( True ) <EOL> self . _body = StringIO ( "<STR_LIT>" ) <EOL> else : <EOL> self . _body = resp . body_file ( ) <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> return getattr ( self , key ) <EOL> except AttributeError : <EOL> pass <EOL> return self . headers . get ( key ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . headers <EOL> def __iter__ ( self ) : <EOL> return self . headers . iteritems ( ) <EOL> def can_read ( self ) : <EOL> return not self . _already_read <EOL> def close ( self ) : <EOL> self . connection . release ( True ) <EOL> def skip_body ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _already_read : <EOL> self . _body . read ( ) <EOL> self . _already_read = True <EOL> self . connection . release ( self . should_close ) <EOL> def body_string ( self , charset = None , unicode_errors = "<STR_LIT:strict>" ) : <EOL> """<STR_LIT>""" <EOL> if not self . can_read ( ) : <EOL> raise AlreadyRead ( ) <EOL> body = self . _body . read ( ) <EOL> self . _already_read = True <EOL> self . connection . release ( self . should_close ) <EOL> if charset is not None : <EOL> try : <EOL> body = body . decode ( charset , unicode_errors ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> return body <EOL> def body_stream ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . can_read ( ) : <EOL> raise AlreadyRead ( ) <EOL> self . _already_read = True <EOL> return BodyWrapper ( self , self . connection ) <EOL> def tee ( self ) : <EOL> """<STR_LIT>""" <EOL> return ResponseTeeInput ( self , self . connection , <EOL> should_close = self . should_close ) <EOL> ClientResponse = Response </s>
