
    ;                        S SK r S SKrS SKJrJr  S SKrS SKJr  SSKJ	r	  S r
S rS rS	 rS
 rS rS rS rS rSS/4SS/4SSS/4/r/ r\ H%  u  rr\ H  r\R1                  \ S3\45        M     M'     \\ V Vs/ s H  u  pU R3                  5       U4PM     snn -  r\R4                  R7                  S\5      S 5       rS rS rS rS r gs  snn f )    N)Serverconsume_socket_content)JSONDecodeError   )override_environc                 Z    [        U SS9nS[        U5      U4-  nU R                  U5        g)zDSimple handler that will take request and echo it back to requester.      ?timeouts)   HTTP/1.1 200 OK
Content-Length: %d

%sN)r   lensend)sockrequest_contenttext_200s      ;platform/gsutil/third_party/requests/tests/test_lowlevel.pyecho_response_handlerr      s7    ,T3?O	 
_	/	0H
 	IIh    c                  j   [         R                  " 5       n [        R                  " U S9n[	        / SQ5      nU u  p4SU SU S3n[
        R                  " XRSS9nU R                  5         SSS5        WR                  S	:X  d   eUR                  R                  S
   S:X  d   eg! , (       d  f       N@= f)zcan safely send generatorswait_to_close_event   a   b   chttp://:/TdatastreamN   zTransfer-Encodingchunked)	threadingEventr   basic_response_serveriterrequestspostsetstatus_coderequestheaders)close_serverserverr   hostporturlrs          r   test_chunked_uploadr3      s    ??$L))lKF"#D	<DvQtfA&MM#6 

 ==C9901Y>>> 
s   1B$$
B2c                  t   S n [         R                  " 5       n[        U 5      nU u  p4SU SU S3n[        R                  " [
        R                  R                  5         [
        R                  " U5        SSS5        UR                  5         SSS5        g! , (       d  f       N'= f! , (       d  f       g= f)z?get a ChunkedEncodingError if the server returns a bad responsec                 <    [        U SS9nU R                  S5        U$ )Nr	   r
   s-   HTTP/1.1 200 OK
Transfer-Encoding: chunked
r   r   )r   r   s     r   #incomplete_chunked_response_handlerHtest_chunked_encoding_error.<locals>.incomplete_chunked_response_handler*   s)    0sC 			.	

 r   r   r   r   N)
r#   r$   r   pytestraisesr'   
exceptionsChunkedEncodingErrorgetr)   )r7   r-   r.   r/   r0   r1   s         r   test_chunked_encoding_errorr>   '   s    	 ??$L78F	<DvQtfA&]]8..CCDLL E	 
DD 
#   :B) B7B)
B&	"B))
B7c                     [         R                  " 5       n [        [        U S9n[	        / SQ5      nSnU u  pESU SU S3n[
        R                  " XbSU0SS	9nU R                  5         S
S
S
5        SUR                  S5      -  nUWR                  ;   d   eUR                  R                  S5      S:X  d   eg
! , (       d  f       NV= f)zBEnsure we use only the specified Host header for chunked requests.r   r   zsample-hostr   r   r   HostT)r   r,   r    N
   Host: %s
utf-8   Host: r   r#   r$   r   r   r&   r'   r(   r)   encodecontentcount)	r-   r.   r   custom_hostr/   r0   r1   r2   expected_headers	            r   3test_chunked_upload_uses_only_specified_host_headerrK   ?   s    ??$L)|LF"#DK	<DvQtfA&MM#6;2GPTU 

 &(:(:7(CCOaii'''99??9%*** 
s   4B99
Cc                     [         R                  " 5       n [        [        U S9n[	        / SQ5      nU u  p4U SU 3nSU SU S3n[
        R                  " XbSS9nU R                  5         SSS5        S	WR                  S
5      -  nUWR                  ;   d   eUR                  R                  S5      S:X  d   eg! , (       d  f       NV= f)z<Ensure we don't omit all Host headers with chunked requests.r   r   r   r   r   Tr   NrB   rC   rD   r   rE   )	r-   r.   r   r/   r0   expected_hostr1   r2   rJ   s	            r   +test_chunked_upload_doesnt_skip_host_headerrN   Q   s    ??$L)|LF"#D	<D&$(vQtfA&MM#6	 
 &(<(<W(EEOaii'''99??9%*** 
s   8B;;
C	c                  t   S n [         R                  " 5       n[        U 5      nU u  p4SU SU S3n[        R                  " [
        R                  R                  5         [
        R                  " U5        SSS5        UR                  5         SSS5        g! , (       d  f       N'= f! , (       d  f       g= f)znEnsure we correctly throw an InvalidHeader error if multiple
conflicting Content-Length headers are returned.
c                 @    [        U SS9nSnU R                  U5        U$ )Nr	   r
   sw   HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 16
Content-Length: 32

-- Bad Actor -- Original Content
r6   )r   r   responses      r   (multiple_content_length_response_handlerRtest_conflicting_content_lengths.<locals>.multiple_content_length_response_handlerh   s,    0sC4 	 			(r   r   r   r   N)
r#   r$   r   r9   r:   r'   r;   InvalidHeaderr=   r)   )rR   r-   r.   r/   r0   r1   s         r    test_conflicting_content_lengthsrU   c   s    
 ??$L<=F	<DvQtfA&]]8..<<=LL >	 
== 
r?   c                  8  ^^	^
^ SmSm
Sm	Sm[         R                  R                  SS5      n UU	U
U4S jn[        R                  " 5       n[        XS9nU u  pES	U S
U S3n[         R                  " X`S9nUR                  S:X  d   eSUR                  R                  ;   d   eUR                  R                  S   R                  S5      (       d   eUR                  S   R                  S:X  d   eUR                  5         SSS5        g! , (       d  f       g= f)zEnsure we correctly reset num_401_calls after a successful digest auth,
followed by a 302 redirect to another digest auth prompt.

See https://github.com/psf/requests/issues/1979.
   HTTP/1.1 401 UNAUTHORIZED
Content-Length: 0
WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d", opaque="372825293d1c26955496c80ed6426e9e", realm="me@kennethreitz.com", qop=auth

s6   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /

s&   HTTP/1.1 200 OK
Content-Length: 0

u   Authorization: Digest username="user", realm="me@kennethreitz.com", nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"userpassc                 @  > [        U SS9nUR                  S5      (       d   eU R                  T5        [        U SS9nTU;   d   eU R                  T5        [        U SS9nSU;  d   eU R                  T5        [        U SS9nTU;   d   eU R                  T5        U$ )Nr	   r
      GET / HTTP/1.1s   Authorization:r   
startswithr   )r   r   expected_digestr   text_302text_401s     r   digest_response_handlerLtest_digestauth_401_count_reset_on_redirect.<locals>.digest_response_handler   s    0sC))*;<<<<		( 1sC/111		( 1sC 777		( 1sC/111		(r   r   r   r   r   authr!   AuthorizationzDigest r   i.  N)r'   re   HTTPDigestAuthr#   r$   r   r=   r*   r+   r,   r^   historyr)   )re   rb   r-   r.   r/   r0   r1   r2   r_   r   r`   ra   s           @@@@r   +test_digestauth_401_count_reset_on_redirectri      s   AH'H-HLO ==''7D 0 ??$L+NF	<DvQtfA&LL(}}###!))"3"3333yy  1<<YGGGGyy|''3... 
s   B,D
Dc                    ^^	 Sm	Sm[         R                  R                  SS5      n UU	4S jn[        R                  " 5       n[        XS9nU u  pESU SU S	3n[         R                  " X`S
9nUR                  S:X  d   eUR                  S   R                  S:X  d   eUR                  5         SSS5        g! , (       d  f       g= f)zcEnsure we correctly respond to a 401 challenge once, and then
stop responding if challenged again.
rW   rX   rY   rZ   c                    > [        U SS9nUR                  S5      (       d   eU R                  T5        [        U SS9nTU;   d   eU R                  T5        [        U SS9nUS:X  d   eU$ Nr	   r
   r\   r   r]   )r   r   r_   ra   s     r   digest_failed_response_handlerJtest_digestauth_401_only_sent_once.<locals>.digest_failed_response_handler   s}    0sC))*;<<<<		( 1sC/111		( 1sC#%%%r   r   r   r   r   rd   i  r   N)
r'   re   rg   r#   r$   r   r=   r*   rh   r)   )
re   rm   r-   r.   r/   r0   r1   r2   r_   ra   s
           @@r   "test_digestauth_401_only_sent_oncero      s    AHLO ==''7D$ ??$L2UF	<DvQtfA&LL(}}###yy|''3... 
s   A!B88
Cc                    ^ Sm[         R                  R                  SS5      n U4S jn[        R                  " 5       n[        XS9nU u  pESU SU S3n[         R                  " X`S	9nUR                  S
:X  d   e[        UR                  5      S:X  d   eUR                  5         SSS5        g! , (       d  f       g= f)zdEnsure we only send digestauth on 4xx challenges.

See https://github.com/psf/requests/issues/3772.
s   HTTP/1.1 200 OK
Content-Length: 0
WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d", opaque="372825293d1c26955496c80ed6426e9e", realm="me@kennethreitz.com", qop=auth

rY   rZ   c                    > [        U SS9nUR                  S5      (       d   eU R                  T5        [        U SS9nUS:X  d   eU$ rl   r]   )r   r   text_200_chals     r   rb   <test_digestauth_only_on_4xx.<locals>.digest_response_handler   sU    0sC))*;<<<<		-  1sC#%%%r   r   r   r   r   rd   r!   r   N)r'   re   rg   r#   r$   r   r=   r*   r   rh   r)   )	re   rb   r-   r.   r/   r0   r1   r2   rr   s	           @r   test_digestauth_only_on_4xxrt      s    
FM ==''7D
 ??$L+NF	<DvQtfA&LL(}}###199~""" 
s   
AB00
B>httphttpsall_proxyz
var,schemec                    U S3n[        5       nU u  pVSU SU 3nX0n[        S0 UD6   [        R                  " [        R
                  R                  5         [        R                  " U5        S S S 5        S S S 5        [        UR                  5      S:X  d   e[        UR                  S   5      S:  d   e S S S 5        g ! , (       d  f       NY= f! , (       d  f       Nb= f! , (       d  f       g = f)Nz://httpbin.orgz	socks5://r   r   r    )
r   r   r9   r:   r'   r;   ConnectionErrorr=   r   handler_results)	httpbinvarschemer1   
fake_proxyr/   r0   	proxy_urlkwargss	            r   test_use_proxy_from_environmentr   !  s    HN
#CJ	|vQtf-	!''x22BBCS! D ( :--.!333 :--a01A555 

 DC (' 
s;   C&/CC1C9AC&
CC
C#	C&&
C4c                  8  ^^^^	 SmSn / m	UUUU	4S jn[         R                  " 5       n[        XS9nU u  mmST ST 3n[        R                  " USS9nUR
                  S	:X  d   e[        UR                  5      S
:X  d   eUR                  S   R
                  S:X  d   eT	S   R                  SU -   S-   5      (       d   eUR                  SR                  X@R                  S5      5      :X  d   eUR                  5         S S S 5        g ! , (       d  f       g = f)Nu   šs   %C5%A1c                    > [        U SS9  ST ST ST 3nU R                  SUR                  S5      -  5        TR                  [        U SS95        U R                  S5        g )	Nr	   r
   z//r   r   sC   HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: %s

utf8   HTTP/1.1 200 OK

)r   r   rF   append)r   locationr/   pathr0   redirect_requests     r   redirect_resp_handlerJtest_redirect_rfc1808_to_non_ascii_location.<locals>.redirect_resp_handler9  sn    tS1vQtfAdV,		 '(	
 	 6tS IJ		,-r   r   r   r   T)r1   allow_redirectsr!   r   r   i-  s   GET /s	    HTTP/1.1z{}/{}ascii)r#   r$   r   r'   r=   r*   r   rh   r^   r1   formatdecoder)   )
expected_pathr   r-   r.   r1   r2   r/   r   r0   r   s
         @@@@r   +test_redirect_rfc1808_to_non_ascii_locationr   4  s   DM. . ??$L)LF	<D$vQtf%LLS$7}}###199~"""yy|''3..."--h.F.UVVVVuus,@,@,IJJJJ 
s   CD
Dc                     [         R                  " 5       n [        [        U S9nU u  p#SU SU S3n[        R
                  " U5      nUR                  nUR                  S:X  d   eUR                  SS5      u  pxUR                  SS5      u  pU	S	:X  d   eS
 H  n
X;  d   eX;  a  M   e   U R                  5         SSS5        g! , (       d  f       g= f)zCVerify that the fragment portion of a URI isn't sent to the server.r   r   r   '/path/to/thing/#view=edit&token=hunter2r!   s   

r   s   
s   GET /path/to/thing/ HTTP/1.1)s   views   edits   tokens   hunter2N)
r#   r$   r   r   r'   r=   rG   r*   splitr)   )r-   r.   r/   r0   r1   r2   raw_requestr,   bodystatus_linefrags              r   #test_fragment_not_sent_with_requestr   V  s    ??$L)|LF	<DvQtf$KLLLii}}####))+q9&}}Wa8====<D&&&### = 	 
s   A?C)C
Cc                     S n [         R                  " 5       n[        XS9nU u  p4SU SU S3n[        R                  " U5      nUR
                  S:X  d   e[        UR                  5      S:X  d   eUR                  S   R                  R                  U:X  d   eUR                  S	   R                  R                  SU SU S
3:X  d   eUR                  SU SU S3:X  d   eUR                  5         SSS5        g! , (       d  f       g= f)zVerify we only append previous fragment if one doesn't exist on new
location. If a new fragment is encountered in a Location header, it should
be added to all subsequent requests.
c                     [        U SS9  U R                  S5        [        U SS9  U R                  S5        [        U SS9  U R                  S5        g )Nr	   r
   sJ   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /get#relevant-section

s@   HTTP/1.1 302 FOUND
Content-Length: 0
Location: /final-url/

r   r6   r   s    r   response_handler:test_fragment_update_on_redirect.<locals>.response_handlerr  sU    tS1		7	

 	tS1		-	

 	tS1		&	
r   r   r   r   r   r!      r   r   z/get#relevant-sectionz/final-url/#relevant-sectionN)r#   r$   r   r'   r=   r*   r   rh   r+   r1   r)   )r   r-   r.   r/   r0   r1   r2   s          r    test_fragment_update_on_redirectr   l  s    
$ ??$L$GF	<DvQtf$KLLL}}###199~"""yy|##''3... yy|##''WTF!D6AV+WWWWuu'$q.JKKKK 
s   CC>>
Dc                  t   S n [         R                  " 5       n[        XS9nU u  p4SU SU S3n[        R                  " U5      nS S S 5        S Wl        [        R                  " [        R                  R                  5       nUR                  5         S S S 5        [        WR                  [        R                  R                  5      (       d   e[        UR                  [        5      (       d   eUR                  [        UR                  5      ;  d   eg ! , (       d  f       N= f! , (       d  f       N= f)Nc                 :    [        U SS9  U R                  S5        g )Nr	   r
   s;   HTTP/1.1 200 OK
Content-Length: 18

{ " K0" = " 0" 
r6   r   s    r   r   Ntest_json_decode_compatibility_for_alt_utf_encodings.<locals>.response_handler  s    tS1		A	
r   r   r   r   r   )r#   r$   r   r'   r=   encodingr9   r:   r;   r   json
isinstancevalueRequestExceptiontextstr)r   r-   r.   r/   r0   r1   r2   excinfos           r   4test_json_decode_compatibility_for_alt_utf_encodingsr     s    
 ??$L$GF	<DvQtfA&LL 
 AJ	x**::	;w	 
<gmmX%8%8%I%IJJJJgmm_555566W]]++++ 
 
<	;s   "DD)
D&)
D7)!r#   r9   tests.testserver.serverr   r   r'   requests.compatr   utilsr   r   r3   r>   rK   rN   rU   ri   ro   rt   _schemes_by_var_prefix_proxy_combosprefixschemesr   r   uppermarkparametrizer   r   r   r   r   )r~   r   s   00r   <module>r      s     B  + #	?0+$+$8>B+\"L fXwi
VW  -OFG/89  . =I=KC399;'=I I }56 66$D,(V,s Js   >C