
    6h.                    x   S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SK	J
r
JrJr  SSKJr  \R                  r\R                   r\R"                  r\R$                  r\R&                  r\R(                  r\R*                  r\
R                  " 5       r\R/                  \S5      S 5       rSS	 jrS
 rS rS rS rg)    )annotationsN)common)	exthelperpycompatwireprotoserver   )	blobstorehandlewsgirequestc                   ^^^ U " TTUT5      (       a  gTR                   R                  R                  SS5      (       d  g[        TR                   R                  S5      (       d  gTR
                  (       d  g TR
                  S:X  a!  T" TTS5        [        TR                   TU5      $ TR
                  R                  S5      (       a  [        TR                   TUUUU4S	 j5      $ g! [        R                   a  nUR                   H  u  pgXsR                  U'   M     [        R                  " UR                  [        R                  " U5      5      Ul        UR#                  S
[        R                  " U5      -  5         SnAgSnAff = f)z}Wrap wireprotoserver.handlewsgirequest() to possibly process an LFS
request if it is left unprocessed by the wrapped method.
Ts   experimentals	   lfs.serveFlfslocalblobstores   .git/info/lfs/objects/batch   pulls   .hg/lfs/objectsc                   > T" TTU 5      $ N )perm	checkpermrctxreqs    >/usr/lib/python3/dist-packages/hgext/lfs/wireprotolfsserver.py<lambda>#handlewsgirequest.<locals>.<lambda>=   s    )D#t2L    s   0
%s
N)repoui
configboolhasattrsvfsdispatchpath_processbatchrequest
startswith_processbasictransferhgwebcommonErrorResponseheadersstatusmessagecoder   bytestrstatussetbodybytes)origr   r   resr   ekvs    `` `   r   r
   r
   $   s@   
 D#sI&&99<<""?LAA499>>#677==dC)'		3<< (();<<(		3%L  $$  IIDAKKN  ..qvvx7G7G7JK
h&6&6q&99:s   :0C* +=C* *F>BFFc                v    [         R                  " XS9U l        SU R                  S'   U R	                  S5        g )Nmessage   text/plain; charset=utf-8   Content-Typer   )r"   r%   r(   r$   r)   )r+   r&   r1   s      r   _sethttperrorr4   K   s0    **4ACJ#?CKK Sr   c                   [         R                  " [        R                  " 5       5      nU R                  S   nSnU R
                  (       a  X0R
                  -  nUSU R                  -   -  nUR                  SX14-  5        g)z=Write information about the current exception to wsgi.errors.s   wsgi.errorsr      /s4   Exception happened while processing request '%s':
%sN)r   sysbytes	traceback
format_excrawenvapppathr   write)r   tberrorloguris       r   _logexceptionr@   Q   sl    			9//1	2Bzz.)H
C
{{{{4#""""CNN@C9Lr   c                n   UR                   S:w  a  [        U[        5        gUR                  S   S:w  a  [        U[        5        gUR                  S   S:w  a  [        U[
        5        g[        R                  " UR                  R                  5       5      nSUR                  SS/5      ;  a  [        U[        S5        gUR                  S	5      n[        R                  " U5      nUS
;  a  [        U[        SU-  5        gU R                  R                  n[        XR                  S/ 5      XE5       Vs/ s H  nUPM     nnSUS.n[         R"                  " [$        5      Ul        SUR                  S'   UR)                  [        R                  " [*        R,                  " U5      5      5        gs  snf )zHandle a request for the Batch API, which is the gateway to granting file
access.

https://github.com/git-lfs/git-lfs/blob/master/docs/api/batch.md
s   POSTTr3   s   application/vnd.git-lfs+jsons   Acceptbasic	transferss0   Only the basic LFS transfer handler is supported	operation)   upload   downloads&   Unsupported LFS transfer operation: %sobjects)transferrG   )methodr4   HTTP_METHOD_NOT_ALLOWEDr$   HTTP_UNSUPPORTED_MEDIA_TYPEHTTP_NOT_ACCEPTABLEr   
json_loadsbodyfhreadgetHTTP_BAD_REQUESTr'   r   r   _batchresponseobjectsr"   r%   HTTP_OKr(   r)   jsondumps)	r   r   r+   lfsreqrD   
localstoreprG   rsps	            r   r   r   `   s   4 zzWc23
{{?#'FFc67
{{9!@@c./   !23F fjjwi88?	

 

;'I  +I005	A	

 ,,J 'Ir*I

A 	

   C
 **73CJ#BCKK X%%djjo67!s   7F2c              #    ^ #    U GHM  nUR                  S5      nUR                  S5      nUUR                  S5      S.nSnSn	 UR                  U5      n	U	(       a  US:X  a  UR                  U5        US:X  a+  U(       d  SSS
.US'   Uv   M  U	(       d  SSS
.US'   Uv   M  OU	(       a  Uv   M  [        R                  R                  5       [        R                  " SS9-   nU 4S jnS[        R                  " U5      -  [        R                  " ST R                  T R                  U4-  5      UR!                  S5      U" 5       S.0US'   Uv   GMP     g! [         a\  n
U
R
                  [
        R                  :w  a1  [        T 5        SU
R                  =(       d    S	S
.US'   Uv    Sn
A
GM  Sn Sn
A
GNFSn
A
ff = f7f)a   Yield one dictionary of attributes for the Batch API response for each
object in the list.

req: The parsedrequest for the Batch API request
objects: The list of objects in the Batch API object request list
action: 'upload' or 'download'
store: The local blob store for servicing requestsoidasciisize)r[   r]   TFrE   i  zInternal Server Server)r&   r1   errorNrF   i  zThe object does not exist  zThe object is corrupt
   )minutesc                    > SS0n TR                   R                  SS5      nUR                  S5      (       a  [        R                  " U5      U S'   U $ )NAcceptzapplication/vnd.git-lfss   Authorizationr   s   Basic Authorization)r$   rP   r    r   strurl)hdrauthr   s     r   _buildheader+_batchresponseobjects.<locals>._buildheader  sL     67C;;??#3S9Dy))'/t'<O$Jr   z%ss   %s%s/.hg/lfs/objects/%sz%Y-%m-%dT%H:%M:%SZ)href
expires_atheaderactions)rP   encodeverifylinkfromusercacheOSErrorerrnoENOENTr@   strerrordatetimenow	timedeltar   re   baseurlr;   strftime)r   rG   actionstoreobjsoidr[   rY   existsverifiesinst	expiresatrh   s   `            r   rR   rR      s    8 wwu~kk'"GGFO
 	||C(HFi/ '',  [ : G 	6 G 	  I%%))+h.@.@.LL	
	 oof%& .#++s{{C1PP (001EF&.(

I 	q 4  	zzU\\)c"  #}}H0H G 	F	s>   AG/E4C%G
F?#AF:+G2F:4G:F??Gc                   UR                   nUR                  S   nU R                  R                  n[	        UR                  5      S:w  a  [        U[        5        gUS:X  a  U" S5        UR                  U5      n[        R                  n UR                  XQR                  UR                  S   5        U" U(       a  [        O[        5      Ul        S	UR                  S
'   UR'                  S5        gUS:X  aX  U" S5        [        R                  " [        5      Ul        SUR                  S
'    UR'                  UR)                  U5      5        g[        U[*        SU-  S9  g! [         R"                   a    [%        U5        U" SS5      Ul         Nf = f! [         R"                   a;    [%        U5        [        R                  " SS5      Ul        UR'                  S5         gf = f)aX  Handle a single file upload (PUT) or download (GET) action for the Basic
Transfer Adapter.

After determining if the request is for an upload or download, the access
must be checked by calling ``checkperm()`` with either 'pull' or 'upload'
before accessing the files.

https://github.com/git-lfs/git-lfs/blob/master/docs/api/basic-transfers.md
   Ts   PUTrE   s   Content-Lengthr_   s   corrupt blobr2   r3   r   s   GETr   s   application/octet-streams#   Unsupported LFS transfer method: %sr0   )rI   dispatchpartsr   r   lenr4   HTTP_NOT_FOUNDhasr"   r%   downloadrN   r$   rS   HTTP_CREATEDr(   r	   LfsCorruptionErrorr@   r)   rO   rJ   )	r   r   r+   r   rI   r[   rW   existedr%   s	            r   r!   r!   )  s    ZZF


B
C,,J
3"c>*) ..% $11	=ZZ=N1OP&'w|LCJ (DO$	6	' ..w7
'BO$	"
 Z__S12 #:VC	

 M ++ 	=# 'sO<CJ		=0 ++ 	"# %223HCJS!	"s&   AE% 1 F %-FFAG'&G'r   )
__future__r   ru   rr   rT   r8   mercurial.hgwebr   r"   	mercurialr   r   r    r	   rS   r   rQ   r   rJ   rL   rK   ehwrapfunctionr
   r4   r@   r   rR   r!   r   r   r   <module>r      s    #     1  


''// ++%== !55 )EE  "56# 7#LOdtnHr   