
    6h?X                        S SK Jr  S SKrSSKJr  \R
                  (       a  S SKrSSKJrJrJ	r	  SSK
Jr   " S S5      r\R                  " S	S
9 " S S5      5       rSS jr " S S5      r " S S5      rS rg)    )annotationsN   )attr)errorpycompatutil)urlutilc                  ^    \ rS rSrSrS rS rS rS rS r	S r
SS
 jrS rS rS rS rSrg	)	multidict    zA dict like object that can store multiple values for a key.

Used to store parsed request parameters.

This is inspired by WebOb's class of the same name.
c                    0 U l         g N_itemsselfs    9/usr/lib/python3/dist-packages/mercurial/hgweb/request.py__init__multidict.__init__(   s	        c                &    U R                   U   S   $ )z%Returns the last set value for a key.r   r   keys     r   __getitem__multidict.__getitem__+   s    {{3##r   c                $    U/U R                   U'   g)z,Replace a values for a key with a new value.Nr   r   r   values      r   __setitem__multidict.__setitem__/   s    !7Cr   c                    U R                   U	 g)zDelete all values for a key.Nr   r   s     r   __delitem__multidict.__delitem__3   s    KKr   c                    XR                   ;   $ r   r   r   s     r   __contains__multidict.__contains__7   s    kk!!r   c                ,    [        U R                  5      $ r   )lenr   r   s    r   __len__multidict.__len__:   s    4;;r   Nc                J     U R                  U5      $ ! [         a    Us $ f = fr   )r   KeyError)r   r   defaults      r   getmultidict.get=   s,    	##C(( 	N	s    ""c                Z    U R                   R                  U/ 5      R                  U5        g)z<Add a new value for a key. Does not replace existing values.N)r   
setdefaultappendr   s      r   addmultidict.addC   s!    sB'..u5r   c                :    U R                   R                  U/ 5      $ )zObtains all values for a key.)r   r/   r   s     r   getallmultidict.getallG   s    {{sB''r   c                d    U R                   U   n[        U5      S:  a  [        SU-  5      eUS   $ )zdObtain a single value for a key.

Raises KeyError if key not defined or it has multiple values set.
   s   multiple values for %rr   )r   r)   r-   )r   r   valss      r   getonemultidict.getoneK   s7    
 {{3t9q=4s:;;Awr   c                    U R                   R                  5        VVs0 s H  u  pU[        U5      _M     snn$ s  snnf r   )r   itemslist)r   kvs      r   asdictoflistsmultidict.asdictoflistsW   s3    '+{{'8'8':;':tq47
':;;;s   :r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r    r#   r&   r*   r/   r4   r7   r<   rC   __static_attributes__ r   r   r   r       s>    $#" 6(
<r   r   T)frozenc                  Z   \ rS rSrSr\R                  " 5       r\R                  " 5       r\R                  " 5       r	\R                  " 5       r
\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       r\R                  " 5       rSrg)parsedrequest[   zlRepresents a parsed WSGI request.

Contains both parsed parameters as well as a handle on the input stream.
rK   N)rE   rF   rG   rH   rI   r   ibmethodurlbaseurladvertisedurladvertisedbaseurl	urlscheme
remoteuser
remotehostapppathdispatchpartsdispatchpathreponamequerystringqsparamsheadersbodyfhrawenvrJ   rK   r   r   rN   rN   [   s     WWYF
'')CggiG GGIM		IJJ ggiGGGIM 779LwwyH'')KwwyH ggiGWWYFWWYFr   rN   c                P   S nU R                  5        VVs0 s H  u  pVU" U5      U" U5      _M     n nnU(       d  U R                  S5      nU(       a  [        R                  " U5      nU S   S-   nU(       a!  UR                  (       a  UR                  S-   nOUnS n	U R                  S5      (       a  XpS   -  nOXpS   -  nU	" XpS   5      nU(       a  UR
                  (       a  XR
                  -  nUR                  (       a  UR                  n
OMUR                  S	:X  a  UR                  (       d  S
n
O)UR                  S:X  a  UR                  (       d  Sn
OU S   n
U	" X5      nOXS   -  nU	" XS   5      nUnUnU[        R                  R                  U R                  SS5      5      -  nU[        R                  R                  U R                  SS5      5      -  nU(       aZ  UR                  =(       d    SnU(       a  UR                  S5      (       d  SU-   nU[        R                  R                  U5      -  nO2U[        R                  R                  U R                  SS5      5      -  nU[        R                  R                  U R                  SS5      5      -  nU R                  S5      (       a  USU S   -   -  nUSU S   -   -  nU(       a8  UR                  =(       d    SnU(       a  UR                  S5      (       d  SU-   nOU R                  SS5      nU(       Ga	  SUR                  S5      -   nU R                  S5      (       d  [        R                  " S5      eU S   R                  U5      (       d  [        R                  " SU S   U4-  5      eU S   [        U5      S nU(       a4  UR                  S5      (       d  [        R                  " SU S   U4-  5      eUR!                  S5      U-   nUR                  S5      R#                  S5      nSR%                  U5      nO]SU ;   aS  U S   R                  S5      (       a5  U S   R                  S5      R#                  S5      nSR%                  U5      nO	/ nSnO/ nSnU R                  SS5      n['        5       n[        R                  R)                  USS9 H  u  pVUR+                  XV5        M     / nU R                  5        HL  u  pVUR                  S5      (       d  M  UR-                  U[        S5      S R/                  SS5      U45        MN     SSKJn  UR5                  U5      nSU ;   a  SU ;  a  U S   US '   S!U ;   a  S"U ;  a  U S!   US#'   Uc7  U S$   nS U;   a,  [        R6                  " U[9        US    =(       d    S%5      5      n[;        S:0 S&U S'   _S(U_S)U_S*U_S+U_S,U S   _S-U R                  S.5      _S/U R                  S05      _S1U_S2U_S3U_S4U_S5U_S6U_S7U_S8U_S9U _6$ s  snnf );a  Parse URL components from environment variables.

WSGI defines request attributes via environment variables. This function
parses the environment variables into a data structure.

If ``reponame`` is defined, the leading path components matching that
string are effectively shifted from ``PATH_INFO`` to ``SCRIPT_NAME``.
This simulates the world view of a WSGI application that processes
requests from the base URL of a repo.

If ``altbaseurl`` (typically comes from ``web.baseurl`` config option)
is defined, it is used - instead of the WSGI environment variables - for
constructing URL components up to and including the WSGI application path.
For example, if the current WSGI application is at ``/repo`` and a request
is made to ``/rev/@`` with this argument set to
``http://myserver:9000/prefix``, the URL and path components will resolve as
if the request were to ``http://myserver:9000/prefix/rev/@``. In other
words, ``wsgi.url_scheme``, ``SERVER_NAME``, ``SERVER_PORT``, and
``SCRIPT_NAME`` are all effectively replaced by components from this URL.

``bodyfh`` can be used to specify a file object to read the request body
from. If not defined, ``wsgi.input`` from the environment dict is used.
c                R    [        U [        5      (       d  U $ U R                  S5      $ )Nz	iso8859-1)
isinstancestrencode)ss    r   tobytes$parserequestfromenv.<locals>.tobytes   s#    !S!!Hxx$$r   s	   REPO_NAMEs   wsgi.url_schemes   ://c                n    U R                  S5      (       a  US:w  a  U SU-   -  n U $ US:w  a  U SU-   -  n U $ )Ns   https://   443   :   80)
startswith)rg   ports     r   addport$parserequestfromenv.<locals>.addport   sI    <<$$v~TD[ 
  u}TD[ r   s	   HTTP_HOSTs   SERVER_NAMEs   SERVER_PORTs   httprm   s   httpsrk   s   SCRIPT_NAMEr   s	   PATH_INFO   /s   QUERY_STRING   ?s   reponame requires PATH_INFOs0   PATH_INFO does not begin with repo name: %s (%s)NsD   reponame prefix of PATH_INFO does not end at path delimiter: %s (%s)T)keep_blank_valuess   HTTP_   _   -r:   wsgiheaderss   CONTENT_LENGTHs   HTTP_CONTENT_LENGTH   Content-Lengths   CONTENT_TYPEs   HTTP_CONTENT_TYPEs   Content-Types
   wsgi.input   0rQ   s   REQUEST_METHODrR   rS   rT   rU   rV   rW   s   REMOTE_USERrX   s   REMOTE_HOSTrY   rZ   r[   r\   r]   r^   r_   r`   ra   rK   )r?   r/   r	   rR   schemehostro   r   urlreqquotepathrn   stripr   ProgrammingErrorr)   rstripsplitjoinr   parseqslr4   r3   replace rx   HeaderscappedreaderintrN   )envr\   
altbaseurlr`   rh   rA   rB   fullurladvertisedfullurlrp   ro   rS   rU   r   rY   
repoprefixr[   rZ   r]   r^   r_   rx   s                         r   parserequestfromenvr      s   F%
 /2iik
:kda71:wqz!kC
:
 77<([[,
 $%.Gj''&--6# ww||$$~&&'~#67joo__,????D')*//D(*:??D~&D#$5<00#$5>7JKG)t{{  !=>>Gt{{  s!;<<G%#--$;DT[[..t44T[[..sww~s/KLL**377<+EFF
ww4#o...TC$888 //(S7--d33WnG''.#.HNN400
ww|$$(()GHH< ++J77((!$'$5x#@A 
 <(Z):; 7 7 = =((6|$h/0  ..&3$**4066t<yy/		|""4((-33D9??EM99]3LML''/3/K {H$$[D$IQ J G		<<!!NNAc(mo.66tTBAFG  !!'*G C$:#$E%():%;!"#"6c"A#&#7 ~]#'&&G$56>$?F  $%  (	
 , () 77>* 77>*  $ "       !" #  ;s   X"c                  0    \ rS rSrSrS rS rS rS rSr	g)	offsettrackingwriterih  a  A file object like object that is append only and tracks write count.

Instances are bound to a callable. This callable is called with data
whenever a ``write()`` is attempted.

Instances track the amount of written data so they can answer ``tell()``
requests.

The intent of this class is to wrap the ``write()`` function returned by
a WSGI ``start_response()`` function. Since ``write()`` is a callable and
not a file object, it doesn't implement other file object methods.
c                    Xl         SU l        g )Nr   )_write_offset)r   writefns     r   r   offsettrackingwriter.__init__v  s    r   c                    U R                  U5      nUc  U =R                  [        U5      -  sl        g U =R                  U-  sl        g r   )r   r   r)   )r   rg   ress      r   writeoffsettrackingwriter.writez  s4    kk!n;LLCF"LLLCLr   c                    g r   rK   r   s    r   flushoffsettrackingwriter.flush  s    r   c                    U R                   $ r   )r   r   s    r   telloffsettrackingwriter.tell  s    ||r   )r   r   N)
rE   rF   rG   rH   rI   r   r   r   r   rJ   rK   r   r   r   r   h  s     r   r   c                  B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)wsgiresponsei  a(  Represents a response to a WSGI request.

A response consists of a status line, headers, and a body.

Consumers must populate the ``status`` and ``headers`` fields and
make a call to a ``setbody*()`` method before the response can be
issued.

When it is time to start sending the response over the wire,
``sendresponse()`` is called. It handles emitting the header portion
of the response message. It then yields chunks of body data to be
written to the peer. Typically, the WSGI application itself calls
and returns the value from ``sendresponse()``.
c                    Xl         X l        SU l        SSKJn  UR                  / 5      U l        SU l        SU l        SU l	        SU l
        SU l        g)zCreate an empty response tied to a specific request.

``req`` is a ``parsedrequest``. ``startresponse`` is the
``start_response`` function passed to the WSGI application.
Nr:   rw   F)_req_startresponsestatusr   rx   r   r_   
_bodybytes_bodygen_bodywillwrite_started_bodywritefn)r   reqstartresponserx   s       r   r   wsgiresponse.__init__  sP     	+!"**2.# r   c                    U R                   c  U R                  c  U R                  (       a  [        R                  " S5      eg )Ns!   cannot define body multiple times)r   r   r   r   r   r   s    r   _verifybodywsgiresponse._verifybody  s6    OO'}}(""(()MNN #r   c                f    U R                  5         Xl        S[        U5      -  U R                  S'   g)zdDefine the response body as static bytes.

The empty string signals that there is no response body.
s   %dry   N)r   r   r)   r_   )r   bs     r   setbodybyteswsgiresponse.setbodybytes  s,    
 	*/#a&.&'r   c                0    U R                  5         Xl        g)z1Define the response body as a generator of bytes.N)r   r   )r   gens     r   
setbodygenwsgiresponse.setbodygen  s    r   c                2    U R                  5         SU l        g)a  Signal an intent to use write() to emit the response body.

**This is the least preferred way to send a body.**

It is preferred for WSGI applications to emit a generator of chunks
constituting the response body. However, some consumers can't emit
data this way. So, WSGI provides a way to obtain a ``write(data)``
function that can be used to synchronously perform an unbuffered
write.

Calling this function signals an intent to produce the body in this
manner.
TN)r   r   r   s    r   setbodywillwritewsgiresponse.setbodywillwrite  s     	"r   c              #  
  #    U R                   (       a  [        R                  " S5      eSU l         U R                  (       d  [        R                  " S5      eU R                  cN  U R
                  cA  U R                  (       d0  U R                  R                  S:w  a  [        R                  " S5      eU R                  R                  S5      (       a  U R                  R                  S5      S	:X  a  U R                  S	 U R                  R                  5        Vs1 s H  nUR                  5       S
;  d  M  UiM     nnU(       a1  [        R                  " SSR                  [        U5      5      -  5      eU R
                  c  U R                  (       a  [        R                  " S5      eSnSnU R                  R                  R                  SS5      R                  5       S:X  a  OSU R                  R                  S;  a  O8[!        U R                  R"                  [$        R&                  5      (       d  SnOSnU(       a  SU R                  S'   U(       ae  [!        U R                  R"                  [$        R&                  5      (       d   e U R                  R"                  R)                  S5      nU(       d  OM/  U R                  R+                  5        VVs/ s H1  u  p[,        R.                  " U5      [,        R.                  " U5      4PM3     nnnU R1                  [,        R2                  " U R                  5      U5      nU R                  (       a  U R                  v   gU R
                  (       a8  U R
                   H'  n[!        U[4        5      (       a  [7        U5      nUv   M)     gU R                  (       a  Xl        gU R                  R                  S:X  a  g[        R                  " S5        gs  snf s  snnf 7f)zSend the generated response to the client.

Before this is called, ``status`` must be set and one of
``setbodybytes()`` or ``setbodygen()`` must be called.

Calling this method multiple times is not allowed.
s$   sendresponse() called multiple timesTs   status line not definedNs   HEADs   response body not defineds   304 ry   rz   )s   dates   etags   expiress   cache-controls   content-locations   content-security-policys   varys"   illegal header on 304 response: %ss   , s,   must use setbodybytes('') with 304 responsesFs   Expectr   s   100-continue)s   POSTs   PUTs   Closes
   Connectioni   s   do not know how to send body)r   r   r   r   r   r   r   r   rQ   rn   r_   r/   keyslowerr   sortedrd   r`   r   r   readr?   r   strurlr   sysstr	bytearraybytesr   )	r   rA   
badheadersdrainclosechunkrB   
strheadersr   s	            r   sendresponsewsgiresponse.sendresponse  s,     ==((7  {{(()CDD OO#%''		  G+(()EFF ;;!!'** || 12d:LL!23 **,,A779	 ,   ,,9jj
!345 
 }}(D,?,?,,C  
 99  C0668OK YY%66 dii..0A0ABB *2DLL'dii..0A0ABBBB		((--e4  **,
, __Q!34, 	 
 ##HOODKK$@*M??//!]] eY//!%LE '    %YY(""#BC}P
s&   DPO87O8=FP8O=C1Pc                    U R                   (       d  [        R                  " S5      eU R                  (       d  [        R                  " S5      eU R                  (       d   e[        U R                  5      $ )a  Obtain a file object like object representing the response body.

For this to work, you must call ``setbodywillwrite()`` and then
``sendresponse()`` first. ``sendresponse()`` is a generator and the
function won't run to completion unless the generator is advanced. The
generator yields not items. The easiest way to consume it is with
``list(res.sendresponse())``, which should resolve to an empty list -
``[]``.
s"   must call setbodywillwrite() firstsW   must call sendresponse() first; did you remember to consume it since it is a generator?)r   r   r   r   r   r   r   s    r   getbodyfilewsgiresponse.getbodyfile]  s`     ""(()NOO}}((#      #D$5$566r   )	r   r   r   r   r   r   r   r_   r   N)rE   rF   rG   rH   rI   r   r   r   r   r   r   r   rJ   rK   r   r   r   r     s.    !(O9
#"GDR7r   r   c                "   ^ U " 5       mU4S jnU$ )zwFor compatibility with old CGI scripts. A plain hgweb() or hgwebdir()
can and should now be used as a WSGI application.c                   > T" X5      $ r   rK   )r   respondapplications     r   run_wsgi!wsgiapplication.<locals>.run_wsgiz  s    3((r   rK   )	app_makerr   r   s     @r   wsgiapplicationr   u  s     +K) Or   )NNN)
__future__r   typing
thirdpartyr   TYPE_CHECKINGr   r   r   r   utilsr	   r   rg   rN   r   r   r   r   rK   r   r   <module>r      s    #   
 

8< 8<v t- - -`Yx Bi7 i7Xr   