Viewing File: /opt/alt/python35/lib64/python3.5/site-packages/aiohttp/__pycache__/multipart.cpython-35.opt-1.pyc



Zs@sddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
ddlmZm
Z
mZddlmZddlmZmZmZmZmZddlmZmZmZmZddlmZdd	lmZm Z m!Z!m"Z"m#Z#m$Z$dZ%Gdd
d
e&Z'Gddde&Z(ddZ)dddZ*GdddZ+GdddZ,e$e,Gddde!Z-Gdd
d
Z.Gddde!Z/GdddZ0dS) N)MappingSequencedeque)	parse_qslunquote	urlencode)CIMultiDict)CONTENT_DISPOSITIONCONTENT_ENCODINGCONTENT_LENGTHCONTENT_TRANSFER_ENCODINGCONTENT_TYPE)CHARTOKENparse_mimetypereify)
HttpParser)JsonPayloadLookupErrorPayload
StringPayloadget_payloadpayload_typeMultipartReaderMultipartWriterBodyPartReaderBadContentDispositionHeaderBadContentDispositionParamparse_content_dispositioncontent_disposition_filenamec@seZdZdS)rN)__name__
__module____qualname__r$r$
/multipart.pyrsc@seZdZdS)rN)r!r"r#r$r$r$r%rscs\dddd}fdd}dd}d	d
}ddjttjtd
d}|sydifS|jd^}}|stjt|difSi}x|rK|j	d}	d|	krtjt|difS|	jdd\}
}|
j
j}
|j}|
|kr]tjt|difS|
stjt
|	qn||
r||r||dd}q>|s>tjt
|	qnf||
rt||r|jdd\}}
}|pd}ntjt
|	qyt||d}Wq>tk
rptjt
|	wYq>Xnd}||rd}||ddjd}nm|rd}nX|rd||df}||r|j	d||ddjd}d}|r>tjt|difS|||
<qW|j
|fS)NcSs|ott|kS)N)rset)stringr$r$r%is_token$sz+parse_content_disposition.<locals>.is_tokencSs |d|dkodkSS)Nrr	"r$)r'r$r$r%	is_quoted'sz,parse_content_disposition.<locals>.is_quotedcs|o|jddkS)N')count)r')r(r$r%
is_rfc5987*sz-parse_content_disposition.<locals>.is_rfc5987cSs
|jdS)N*)endswith)r'r$r$r%is_extended_param-sz4parse_content_disposition.<locals>.is_extended_paramcSsV|jdd}|sdS|jdr<||dn
||d}|jS)Nr0r	Fr*)findr1isdigit)r'posZ	substringr$r$r%is_continuous_param0s
/z6parse_content_disposition.<locals>.is_continuous_paramcharscSstjdj|d|S)Nz\\([{}])z\1)resubformat)textr7r$r$r%unescape7sz+parse_content_disposition.<locals>.unescape;r=r	r,r-zutf-8strictTFz\/z%s;%sr*r*r*)joinmapr9escapersplitwarningswarnrpoplowerstriplstriprrUnicodeDecodeError)headerr+r/r2r6r=Zdisptypepartsparamsitemkeyvalueencoding_Zfailed_valuer$)r(r%r"sz'

	


"	

filenamec
sHd||sdS|kr(|S||kr<||Sg}tfdd|jD}xzt|D]l\}\}}|jdd\}}|jdr|dd}|t|kr|j|qtPqtW|sdSdj|}d|kr@|jdd\}	}}|	p-d	}	t||	d
S|SdS)Nz%s*c3s0|]&\}}|jr||fVqdS)N)
startswith).0rPrQ)name_sufr$r%	<genexpr>s	z/content_disposition_filename.<locals>.<genexpr>r0r	r8r,r-zutf-8r@r*)	sorteditems	enumeraterDr1strappendrAr)
rNnamerMZfnparamsnumrPrQrStailrRr$)rXr%r s2
c@s^eZdZdZddZddZddZdd	Zd
dZdd
Z	dS)MultipartResponseWrapperz~Wrapper around the MultipartBodyReader.

    It takes care about
    underlying connection and close it when it needs in.
    cCs||_||_dS)N)respstream)selfrcrdr$r$r%__init__s	z!MultipartResponseWrapper.__init__cCs|S)Nr$)rer$r$r%	__aiter__sz"MultipartResponseWrapper.__aiter__cs'|jIdH}|dkr#t|S)N)nextStopAsyncIteration)repartr$r$r%	__anext__sz"MultipartResponseWrapper.__anext__cCs|jjjS)z2Returns True when all response data had been read.)rccontentat_eof)rer$r$r%rmszMultipartResponseWrapper.at_eofcs6|jjIdH}|jjr2|jIdH|S)z#Emits next multipart reader object.N)rdrhrmrelease)rerOr$r$r%rhszMultipartResponseWrapper.nextcs|jjIdHdS)zPReleases the connection gracefully, reading all the content
        to the void.N)rcrn)rer$r$r%rnsz MultipartResponseWrapper.releaseN)
r!r"r#__doc__rfrgrkrmrhrnr$r$r$r%rbsrbc@s6eZdZdZdZddZddZddZd	d
Zddd
dZ	eddZ
ddZddZddZ
ddZddddZddddZdddd Zd!d"Zd#d$Zd%d&Zd'd(Zdd)d*Zed+d,Zed-d.ZdS)/rz&Multipart reader for single body part.i cCs||_||_||_d|_|jjtd}|dk	rQt|nd|_d|_t	|_
d|_d|_i|_
dS)NFr)headers	_boundary_content_at_eofgetrint_length_read_bytesr_unread_prev_chunk_content_eof_cache)reboundaryrprllengthr$r$r%rfs				!			zBodyPartReader.__init__cCs|S)Nr$)rer$r$r%rgszBodyPartReader.__aiter__cs'|jIdH}|dkr#t|S)N)rhri)rerjr$r$r%rkszBodyPartReader.__anext__cs|jIdH}|sdS|S)N)read)rerOr$r$r%rhszBodyPartReader.nextdecodeFcs[|jr
dSt}x+|jsC|j|j|jIdHqW|rW|j|S|S)zReads body part data.

        decode: Decodes data following by encoding
                method from Content-Encoding header. If it missed
                data remains untouched
        N)rs	bytearrayextend
read_chunk
chunk_sizer)rerdatar$r$r%r~s		"
zBodyPartReader.readcs|jr
dS|jr-|j|IdH}n|j|IdH}|jt|7_|j|jkrqd|_|jr|jjIdH}|S)zWReads body part content chunk of the specified size.

        size: chunk size
        rNT)rsrv_read_chunk_from_length_read_chunk_from_streamrwlenrrreadline)resizechunkZclrfr$r$r%rs				zBodyPartReader.read_chunkcs4t||j|j}|jj|IdH}|S)N)minrvrwrrr~)rerrrr$r$r%rsz&BodyPartReader._read_chunk_from_lengthcsJ|jdk}|r/|jj|IdH|_|jj|IdH}|jt|jj7_|j|}d|j}|r|j|}n.|j|tdt	|jt	|}|dkr4|jj
||d||kr|jd||_|t	|j|}|s4d|_|j}||_|S)Ns
rT)ryrrr~rzrurmrqr3maxrZunread_datars)rerZfirst_chunkrZwindowr:idxresultr$r$r%rs(

.			z&BodyPartReader._read_chunk_from_streamcs|jr
dS|jr(|jj}n|jjIdH}|j|jr|jd}|j}|jd}||ks||krd|_|jj|dSnF|jjIdH}|j|jr|dd}|jj||S)z Reads body part by line by line.rNs
s--Tr-)	rsrxpopleftrrrrVrqrstripr^)relineZsliner|Z
last_boundary	next_liner$r$r%r5s$			
	zBodyPartReader.readlinecs6|jr
dSx"|js1|j|jIdHqWdS)z0Like read(), but reads all the data to the void.N)rsrr)rer$r$r%rnRs	zBodyPartReader.releaserRNcs<|jddIdH}|p,|jdd}|j|S)z;Like read(), but assumes that body part contains text data.rTNdefaultzutf-8)r~get_charsetr)rerRrr$r$r%r<YszBodyPartReader.textcsO|jddIdH}|s!dS|p6|jdd}tj|j|S)z<Like read(), but assumes that body parts contains JSON data.rTNrzutf-8)r~rjsonloadsr)rerRrr$r$r%ras
zBodyPartReader.jsoncs^|jddIdH}|s!dS|p6|jdd}t|jj|ddd|S)zXLike read(), but assumes that body parts contains form
        urlencoded data.
        rTNrzutf-8Zkeep_blank_valuesrR)r~rrrr)rerRrr$r$r%formiszBodyPartReader.formcCs|jS)z<Returns True if the boundary was reached or False otherwise.)rs)rer$r$r%rmuszBodyPartReader.at_eofcCs>t|jkr|j|}t|jkr:|j|S|S)zrDecodes data according the specified Content-Encoding
        or Content-Transfer-Encoding headers value.
        )r
rp_decode_content_transferr_decode_content)rerr$r$r%rys

zBodyPartReader.decodecCs|jtj}|dkr3tj|tjS|dkrVtj|dtjS|dkrf|Stdj|dS)Ndeflategzipidentityzunknown content encoding: {})rprrHzlib
decompress	MAX_WBITSRuntimeErrorr;)rerrRr$r$r%rszBodyPartReader._decode_contentcCsn|jtj}|dkr,tj|S|dkrEtj|S|dkrU|Stdj|dS)Nbase64zquoted-printablebinary8bit7bitz%unknown content transfer encoding: {})rrr)	rpr
rHrZ	b64decodebinasciiZa2b_qprr;)rerrRr$r$r%rs

	z'BodyPartReader._decode_content_transfercCs4|jjtd}t|}|jjd|S)z>Returns charset parameter from Content-Type header or default.r8charset)rprtrr
parameters)rerctypemimetyper$r$r%rszBodyPartReader.get_charsetcCs+t|jjt\}}t|dS)zoReturns name specified in Content-Disposition header or None
        if missed or header is malformed.
        r_)rrprtr
r )rerSrNr$r$r%r_szBodyPartReader.namecCs+t|jjt\}}t|dS)zsReturns filename specified in Content-Disposition header or None
        if missed or header is malformed.
        rU)rrprtr
r )rerSrNr$r$r%rUszBodyPartReader.filename)r!r"r#rorrfrgrkrhr~rrrrrnr<rrrmrrrrrr_rUr$r$r$r%rs,
	

	cs.eZdZfddZddZS)BodyPartReaderPayloadcsntj|||i}|jdk	r8|j|d<|jdk	rT|j|d<|rj|jd|dS)Nr_rUZ
attachment)superrfr_rUZset_content_disposition)rerQargskwargsrN)	__class__r$r%rfs

zBodyPartReaderPayload.__init__cs^|j}|jddIdH}x7|rY|j|j||jddIdH}q#WdS)Nrr-rii)rTrwriter)rewriterZfieldrr$r$r%rs
		zBodyPartReaderPayload.write)r!r"r#rfrr$r$)rr%rsrc@seZdZdZeZdZeZddZ	ddZ
ddZed	d
Z
ddZd
dZddZddZddZddZddZddZddZddZdd ZdS)!rzMultipart body reader.NcCsS||_d|jj|_||_d|_d|_d|_g|_dS)Nz--FT)	rp
_get_boundaryencoderqrr
_last_partrs_at_bofrx)rerprlr$r$r%rfs					zMultipartReader.__init__cCs|S)Nr$)rer$r$r%rgszMultipartReader.__aiter__cs'|jIdH}|dkr#t|S)N)rhri)rerjr$r$r%rkszMultipartReader.__anext__cCs%|j|||j|j}|S)zConstructs reader instance from HTTP response.

        :param response: :class:`~aiohttp.client.ClientResponse` instance
        )response_wrapper_clsrprl)clsresponseobjr$r$r%
from_responseszMultipartReader.from_responsecCs|jS)zSReturns True if the final boundary was reached or
        False otherwise.
        )rs)rer$r$r%rmszMultipartReader.at_eofcsw|jr
dS|jIdH|jr@|jIdHd|_n|jIdH|jr\dS|jIdH|_|jS)z#Emits the next multipart body part.NF)rs_maybe_release_last_partr_read_until_first_boundary_read_boundaryfetch_next_partr)rer$r$r%rhs			zMultipartReader.nextcsAx:|js<|jIdH}|dkr*P|jIdHqWdS)z=Reads all the body parts to the void till the final boundary.N)rsrhrn)rerOr$r$r%rn	s
zMultipartReader.releasecs|jIdH}|j|S)z"Returns the next body part reader.N)
_read_headers_get_part_reader)rerpr$r$r%rszMultipartReader.fetch_next_partcCs|jtd}t|}|jdkre|jdkrRt|||jS|j||jS|j|j||jSdS)zDispatches the response by the `Content-Type` header, returning
        suitable reader instance.

        :param dict headers: Response headers
        r8Z	multipartN)rtrrtypemultipart_reader_clsrrpart_reader_clsrq)rerprrr$r$r%rsz MultipartReader._get_part_readercCslt|jt}d|jkr9td|jt|jd}t|dkrhtd||S)Nr|z$boundary missed for Content-Type: %sFz&boundary %r is too long (70 chars max))rrprr
ValueErrorr)rerr|r$r$r%r&s

zMultipartReader._get_boundarycs(|jr|jjS|jjIdHS)N)rxrGrrr)rer$r$r%	_readline8s	
zMultipartReader._readlinecszxs|jIdH}|dkr3td|j|j}||jkrRdS||jdkrd|_dSqWdS)Nrz#Could not find starting boundary %rs--T)rrrqrrs)rerr$r$r%r=s
	z*MultipartReader._read_until_first_boundarycs|jIdHj}||jkr)n||jdkrd|_|jIdH}|jIdH}|dddkr|jj|q|jj||gntd||jfdS)Ns--Tr-z Invalid boundary %r, expected %r)rrrqrsrxr^rr)rerZepiloguerr$r$r%rJs	zMultipartReader._read_boundarycsfdg}x8|jjIdH}|j}|j||sPqWt}|j|^}}|S)Nr)rrrrIr^rZ
parse_headers)relinesrparserrprSr$r$r%rbs	
	zMultipartReader._read_headerscsS|jdk	rO|jjs0|jjIdH|jj|jjd|_dS)z8Ensures that the last read body part is read completely.N)rrmrnrxr)rer$r$r%rns
z(MultipartReader._maybe_release_last_part)r!r"r#rorbrrrrrfrgrkclassmethodrrmrhrnrrrrrrrrr$r$r$r%rs&	

cseZdZdZddfddZddZdd	Zd
dZdd
Ze	j
dZe	j
dZe
ddZe
ddZdddZddZdddZdddZe
ddZddZS) rzMultipart body writer.ZmixedNcs|dk	r|ntjj}y|jd|_Wn!tk
rZtddYnXdj||j}t	j
dd|g|_t|_
|j|j
t<dS)Nasciiz(boundary should contain ASCII only charszmultipart/{}; boundary={}content_type)uuidZuuid4hexrrqUnicodeEncodeErrorrr;_boundary_valuerrf_partsr_headersrr)resubtyper|r)rr$r%rfzs!
		zMultipartWriter.__init__cCs|S)Nr$)rer$r$r%	__enter__szMultipartWriter.__enter__cCsdS)Nr$)reexc_typeexc_valexc_tbr$r$r%__exit__szMultipartWriter.__exit__cCs
t|jS)N)iterr)rer$r$r%__iter__szMultipartWriter.__iter__cCs
t|jS)N)rr)rer$r$r%__len__szMultipartWriter.__len__s\A[!#$%&'*+\-.^_`|~\w]+\Zs[\x00-\x08\x0A-\x1F\x7F]cCs|j}tj|j|r+|jdStj|j|rLtd|jdd}|jdd}d|jddS)zyWrap boundary parameter value in quotes, if necessary.

        Reads self.boundary and returns a unicode sting.
        rz*boundary value contains invalid characterss\s\\"s\"r))	rqr9match_valid_tchar_regexrsearch_invalid_qdtext_char_regexrreplace)rerQZquoted_value_contentr$r$r%rs	
zMultipartWriter._boundary_valuecCs|jjdS)Nr)rqr)rer$r$r%r|szMultipartWriter.boundarycCs|dkrt}t|tr\|jdk	rF|jj|n	||_|j|Sy|jt|d|SWntk
rt	YnXdS)Nrp)
r
isinstancerrpupdaterappend_payloadrr	TypeError)rerrpr$r$r%r^s		

zMultipartWriter.appendcCsHt|jkr|j|jt<|jjtdj}|ra|dkratdj||dkrsd}|jjtdj}|dkrtd
j||d	krd}|j	}|dk	r|p|rt
||jt<djdd|jj
Djd
d}|jj||||f|S)z)Adds a new body part to multipart writer.r8rrrzunknown content encoding: {}Nrquoted-printablerz%unknown content transfer encoding: {}cSs(g|]\}}|d|dqS)z: z
r$)rWkvr$r$r%
<listcomp>s	z2MultipartWriter.append_payload.<locals>.<listcomp>zutf-8s
)rrr)r8rrr)rrprrtrrHrr;r
rr]rrAr[rrr^)repayloadrRte_encodingrrpr$r$r%rs,			
zMultipartWriter.append_payloadcCs.|dkrt}|jt|d|S)zHelper to append JSON part.Nrp)rrr)rerrpr$r$r%append_jsons	zMultipartWriter.append_jsoncCsg|dkrt}t|tr6t|j}t|dd}|jt|d|ddS)z&Helper to append form urlencoded part.NZdoseqTrprz!application/x-www-form-urlencoded)rrrlistr[rrr)rerrprr$r$r%append_forms	zMultipartWriter.append_formcCs|js
dSd}xl|jD]a\}}}}|sJ|sJ|jdkrNdS|dt|jd|jt|d7}qW|dt|jd7}|S)zSize of the payload.rNr-)rrrrq)retotalrjrprRrr$r$r%rs	%zMultipartWriter.sizecs|js
dSx|jD]\}}}}|jd|jdIdH|j|IdH|sd|rt|}|r|j||r|j||j|IdH|jIdHn|j|IdH|jdIdHqW|jd|jdIdHdS)zWrite body.Ns--s
s--
)rrrqMultipartPayloadWriterenable_compressionenable_encoding	write_eof)rerrjrprRrwr$r$r%rs 	

zMultipartWriter.write)r!r"r#rorfrrrrr9compilerrpropertyrr|r^rrrrrr$r$)rr%rws  #c@sOeZdZddZddZdddZdd	Zd
dZdS)
rcCs||_d|_d|_dS)N)_writer	_encoding	_compress)rerr$r$r%rf:s		zMultipartPayloadWriter.__init__cCs=|dkr$||_t|_n|dkr9d|_dS)Nrzquoted-printable)rr_encoding_buffer)rerRr$r$r%r?s
	z&MultipartPayloadWriter.enable_encodingrcCs<|dkrdtjntj}tjd||_dS)NrrZwbits)rrcompressobjr)rerRZ	zlib_moder$r$r%rFs#z)MultipartPayloadWriter.enable_compressioncs||jdk	r?|jj}|r?d|_|j|IdH|jdkrx|jrx|jjtj|jIdHdS)Nr)rflushrrrrr	b64encode)rerr$r$r%rKs		z MultipartPayloadWriter.write_eofcs|jdk	r1|r1|jj|}|s1dS|jdkr|jj||jr|j}tt|d\}}|d|d||dd}|_|rtj|}|j	j
|IdHnE|jdkr|j	j
tj|IdHn|j	j
|IdHdS)Nrzquoted-printable)
rcompressrrrdivmodrrrrrrZb2a_qp)rerbufferZdivmodZ	enc_chunkr$r$r%rWs"		,!zMultipartPayloadWriter.writeN)r!r"r#rfrrrrr$r$r$r%r8s
r)rrrrrrr )1rrrr9rrErcollectionsrrrZurllib.parserrrZ	multidictrZhdrsr
rrr
rZhelpersrrrrZhttprrrrrrrr__all__RuntimeWarningrrrr rbrrrrrr$r$r$r%<module>s6(".b%
Back to Directory File Manager