
    q&f&                     2   d dl Z d dlZd dlZd dlZd dlZddlmZ ddlmZ ddl	m
Z
mZmZmZmZ ej                  ZdZdZdZd	Zd
ZdZdZdZdZdZdZej4                  Zd Zd ZegZ G d de      Z G d d      Z d Z!d dZ"d Z#d Z$d Z%d Z&d!dZ'd Z(d Z)y)"    N   )_)open)encodingpycompatscmutil	templaterutil      i0  i  i  i  i  i  i  i  i  c                     t        |j                  j                  d            rY| j                  }|j	                  dd      }|j
                  }|rt        |||      ryt        d      |z  }|j                  |       y)Ns   access-hiddens   experimentals   server.allow-hidden-accessTsE   ignoring request to access hidden changeset by unauthorized user: %r
F)	boolqsparamsgetui
configlist
remoteuserismemberr   warn)reporeqr   allowusermsgs         8/usr/lib/python3/dist-packages/mercurial/hgweb/common.pyhashiddenaccessr   -   s~    CLL-./ WWo/LM~~Xb$. / 	  GGCL    c                    |j                   }| j                  dd      }|r3|r!t        | j                  j                  ||      rt        t        d      | j                  dd      }|r1t        | j                  j                  ||      st        t        d      |dk(  r| j                  st        t        d      |dk(  s|y|dk(  r!|j                  d	k7  r3d
}t        t        |      |j                  dk7  rd}t        t        |      | j                  dd      r|j                  dk7  rt        t        d      | j                  dd      }|r3|r!t        | j                  j                  ||      rt        t        d      | j                  dd      }|r!t        | j                  j                  ||      st        t        d      y)zCheck permission for operation based on request data (including
    authentication info). Return if op allowed, else raise an ErrorResponse
    exception.   webs	   deny_reads   read not authorizeds
   allow_reads   pulls   pull not authorizedNs   uploads   PUTs   upload requires PUT requests   POSTs   push requires POST requests   push_ssls   httpss   ssl requireds	   deny_pushs   push not authorizeds
   allow-push)r   r   r   r   r   ErrorResponseHTTP_UNAUTHORIZED	allowpullmethodHTTP_METHOD_NOT_ALLOWED
configbool	urlschemeHTTP_FORBIDDEN)	hgwebr   opr   	deny_read
allow_readr   denyr   s	            r   
checkauthzr-   G   s   
 >>D  6I$(5::==$	"J-/EFF!!&-8J8EJJMM4D-/EFF	W}U__-/EFF	w"* 
Y::0C 7==	w	+3S99 ,(1JNO<<FL1DTXejjmmT4@-/EFFV]3Ehuzz}}dE:-/EFF ;r   c                       e Zd ZddZy)r    Nc                     |t        |      }t        j                  | t        j                  |             || _        |g }|| _        || _        y N)_statusmessage	Exception__init__r   sysstrcodeheadersmessage)selfr5   r7   r6   s       r   r3   zErrorResponse.__init__~   sI    ?$T*G4!9:	?Gr   )NN)__name__
__module____qualname__r3    r   r   r    r    }   s    r   r    c                   $    e Zd ZdZd ZddZd Zy)continuereadera.  File object wrapper to handle HTTP 100-continue.

    This is used by servers so they automatically handle Expect: 100-continue
    request headers. On first read of the request body, the 100 Continue
    response is sent. This should trigger the client into actually sending
    the request body.
    c                 .    || _         || _        d| _        y )NF)f_write	continued)r8   r@   writes      r   r3   zcontinuereader.__init__   s    r   c                     | j                   sd| _         | j                  d       | j                  j                  |      S )NTs   HTTP/1.1 100 Continue

)rB   rA   r@   read)r8   amts     r   rE   zcontinuereader.read   s0    ~~!DNKK89vv{{3r   c                 B    |dv rt        | j                  |      S t        )N)s   closes   readlines	   readliness   __iter__)getattrr@   AttributeError)r8   attrs     r   __getattr__zcontinuereader.__getattr__   s"    EE4664((r   N))r9   r:   r;   __doc__r3   rE   rK   r<   r   r   r>   r>      s    
 r   r>   c                     t         j                  j                  }t        j                  |j                  | d      d         S )N)ErrorzUnknown errorr   )
httpserverbasehttprequesthandler	responsesr   bytesurlr   )r5   rR   s     r   r1   r1      s5    11;;IY]]41KLQOPPr   c                 *    d| |xs t        |       fz  S )Ns   %d %s)r1   )r5   r7   s     r   statusmessagerU      s    tW<t(<===r   c                     t         j                  j                  | |      }t         j                  j                  |      rt        j                  |      S t        j                  |       S )z%stat fn if it exists, spath otherwise)ospathjoinexistsstat)spathfncl_paths      r   get_statr_      sC    ggll5"%G	ww~~gwwwwwu~r   c                 <    t        | d      t        j                     S )Ns   00changelog.i)r_   r[   ST_MTIME)r\   s    r   	get_mtimerb      s    E+,T]];;r   c                     | j                  d      }|D ]\  }|dt        j                  t        j                  fv s6t        j                  |v s$t        j
                  It        j
                  |v s\ y y)z9Determine if a path is safe to use for filesystem access.   /r   FT)splitr   oscurdirospardirosseposaltsep)rX   partsparts      r   
ispathsaferl      sh    JJtE S(++X->->??~~%  ,!!T) r   c                    t        |      sy|s:| xs t        j                         }| t        j                  j                  |d      }t        j                  j
                  |j                  d       }t        j                  t        j                  t        j                  |            d   xs d      }t        j                  j                  ||      }	 t        j                  |       t        |d      5 }|j                         }	ddd       ||j2                  d<   |j5                  	       |S # 1 sw Y   +xY w# t        $ r t!        t"        d      t$        $ r^}
|
j&                  t&        j(                  k(  rt!        t*              t!        t"        t-        j.                  |
j0                              d}
~
ww xY w)	a+  return a file inside directory with guessed Content-Type header

    fname always uses '/' as directory separator and isn't allowed to
    contain unusual path components.
    Content-Type is guessed using the mimetypes module.
    Return an empty string if fname is illegal or file not found.

    Ns   staticrd   r   z
text/plains   rbs   illegal filenames   Content-Type)rl   r	   templatedirrW   rX   rY   re   r   sysbytes	mimetypes
guess_typefsdecoder[   r   rE   	TypeErrorr    HTTP_SERVER_ERROROSErrorerrnoENOENTHTTP_NOT_FOUNDr   
strtolocalstrerrorr6   setbodybytes)templatepath	directoryfnamerestpfpathctrX   fhdataerrs              r   
staticfiler      sd    e4Y224>R3IGGLL%++d+,E			X..u56q9J]
B 77<<	5)D
$ 	"779D	 $&CKK TJ	 	 D-/BCC 99$//!8#6#6s||#D 	s1   !D? 8D3	D? 3D<8D? ?!F> AF99F>c              #   t   K   | r|r| |z   | z  }| |z   | z  dz  }nd}d}	 | |dz  }| r|| k\  rd|z
  }d}w)z5count parity of horizontal stripes for easier reading   r   r<   )stripecountoffsetcountparitys       r   	paritygenr      sf     vv%4&;6:

5K/ZFE s   68c                 t     | dd      xs.  | dd      xs# t         j                  j                  d      xs dS )zReturn repo contact information or empty string.

    web.contact is the primary source, but if that is not set, try
    ui.username or $EMAIL as a fallback to display something useful.
    r   s   contacts   uis   usernames   EMAILr   )r   environr   )configs    r   get_contactr     sE     	vz" 	%%	)	 	r   c                     ddl }| j                  ddd      }d}|rRd|v rNt        j                  |j	                         j
                        j                  d      }|j                  d|      }||fS )	a  Obtain the Content-Security-Policy header and nonce value.

    Returns a 2-tuple of the CSP header value and the nonce value.

    First value is ``None`` if CSP isn't enabled. Second value is ``None``
    if CSP isn't enabled or if the CSP header doesn't need a nonce.
    r   Nr   s   cspF)	untrusteds   %nonce%   =)uuidr   base64urlsafe_b64encodeuuid4bytesrstripreplace)r   r   cspnonces       r   	cspvaluesr     sm    .  ))FFe)
4CE
zS ((););<CCDIkk*e,:r   r0   )r   )*r   rv   rp   rW   r[   i18nr   r   r    r   r   r	   r
   rP   HTTP_OKHTTP_CREATEDHTTP_NOT_MODIFIEDHTTP_BAD_REQUESTr!   r'   rx   r$   HTTP_NOT_ACCEPTABLEHTTP_UNSUPPORTED_MEDIA_TYPErt   r   r   r-   	permhooksr2   r    r>   r1   rU   r_   rb   rl   r   r   r   r   r<   r   r   <module>r      s       	    __

     !  4)Gf L		I 	 4Q
><&R""r   