
    DfLF                        d 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mZ 	 ddlmZmZ ddlmZmZmZ n# e$ r ddlmZmZ ddlmZmZmZ Y nw xY wg d	Z e            Zd
e_          G d de          ZdGdZ edd          xZZ ed          xZ Z! ed          xZ"Z# ed          xZ$Z% G d de          Z&e&Z' G d de&          Z(e(Z) G d de&          Z*e*Z+ G d de          Z,e,Z- G d de&          Z.e.Z/ G d de          Z0e0Z1 G d  d!e          Z2e2Z3 G d" d#e          Z4e4Z5 G d$ d%e          Z6dHd&Z7e7Z8 G d' d(e          Z9e9Z: G d) d*e          Z;e;Z< G d+ d,e          Z=e=Z> G d- d.e          Z?e?Z@ G d/ d0e          ZAeAZB G d1 d2e          ZCeCZD G d3 d4e          ZEdHd5ZFeFZG G d6 d7e          ZHdHd8ZIeIZJ G d9 d:ejK                  ZLdId<ZMdHd=ZNd> ZOd? ZP	 	 	 	 	 	 dJd@ZQdA ZRdB ZSdC ZTdD ZUeUeT_V         e            xZWZXdEeW_         eYdFk    rddlZZZ eZj[                     dS dS )Kz-
Web API (wrapper around WSGI)
(from web.py)
    N)BytesIO)urljoin   )dictaddintgetsafestrstoragestorifythreadeddict)unquotequote)CookieErrorMorselSimpleCookie)3configheaderdebuginputdata	setcookiecookiesctx	HTTPErrorOKCreatedAccepted	NoContentokcreatedaccepted	nocontentRedirectFoundSeeOtherNotModifiedTempRedirectredirectfoundseeothernotmodifiedtempredirect
BadRequestUnauthorized	ForbiddenNotFoundNoMethodNotAcceptableConflictGonePreconditionFailedUnsupportedMediaTypeUnavailableForLegalReasons
badrequestunauthorized	forbiddennotfoundnomethodnotacceptableconflictgonepreconditionfailedunsupportedmediatypeunavailableforlegalreasonsInternalErrorinternalerrorz
A configuration object for various aspects of web.py.

`debug`
   : when True, enables reloading, disabled template caching and sets internalerror to debugerror.
c                       e Zd Zi dfdZdS )r    c                     |t           _        |                                D ]\  }}t          ||           || _        t
                              | |           d S N)r   statusitemsr   r   	Exception__init__)selfrH   headersr   kvs         9/var/www/media/lib/python3.11/site-packages/web/webapi.pyrK   zHTTPError.__init__Z   sV    
MMOO 	 	DAq1aLLLL	4(((((    N)__name__
__module____qualname__rK    rQ   rP   r   r   Y   s-        ') ) ) ) ) ) )rQ   r   c                      |                      dd          d         }                      dd          d                             dd          }|pd z  }|i f fd	}t          |t          t          f||d          S )N r   rE   z`%s` statusc                 B    t                               | ||           d S rG   r   rK   )rL   r   rM   rH   s      rP   rK   z_status_code.<locals>.__init__j   s#    4$77777rQ   )__doc__rK   )splitreplacetyper   object)rH   r   	classname	docstringrK   s   `    rP   _status_codera   b   s    |||C##A&S!$$Q'//R I 3]V3I " 8 8 8 8 8 8 Iv&I8(T(T  rQ   z200 OKrE   )r   z201 Createdz202 Acceptedz204 No Contentc                       e Zd ZdZddZdS )r"   z#A `301 Moved Permanently` redirect.301 Moved PermanentlyFc                     t          t          j        |          }|                    d          r |rt          j        }nt          j        }||z   }d|d}t                              | ||d           dS )z
        Returns a `status` redirect to the new URL.
        `url` is joined with the base URL so that things like
        `redirect("about") will work properly.
        /	text/html)Content-TypeLocationrE   N)r   r   path
startswithrealhomehomer   rK   )rL   urlrH   absolutenewlocrl   rM   s          rP   rK   zRedirect.__init__|   sy     3''S!! 	#  |xF]F#.FCC4"55555rQ   N)rc   FrR   rS   rT   rZ   rK   rU   rQ   rP   r"   r"   y   s.        --6 6 6 6 6 6rQ   r"   c                       e Zd ZdZddZdS )r#   zA `302 Found` redirect.Fc                 B    t                               | |d|           d S )Nz	302 Foundrn   r"   rK   rL   rm   rn   s      rP   rK   zFound.__init__   s%    $[8DDDDDrQ   NFrp   rU   rQ   rP   r#   r#      s4        !!E E E E E ErQ   r#   c                       e Zd ZdZddZdS )r$   zA `303 See Other` redirect.Fc                 B    t                               | |d|           d S )Nz303 See Otherrs   rt   ru   s      rP   rK   zSeeOther.__init__   s%    $_xHHHHHrQ   Nrv   rp   rU   rQ   rP   r$   r$      s4        %%I I I I I IrQ   r$   c                       e Zd ZdZd ZdS )r%   zA `304 Not Modified` status.c                 <    t                               | d           d S )Nz304 Not ModifiedrY   )rL   s    rP   rK   zNotModified.__init__   s    4!344444rQ   Nrp   rU   rQ   rP   r%   r%      s)        &&5 5 5 5 5rQ   r%   c                       e Zd ZdZddZdS )r&   z$A `307 Temporary Redirect` redirect.Fc                 B    t                               | |d|           d S )Nz307 Temporary Redirectrs   rt   ru   s      rP   rK   zTempRedirect.__init__   s&    $%=QQQQQrQ   Nrv   rp   rU   rQ   rP   r&   r&      s4        ..R R R R R RrQ   r&   c                       e Zd ZdZdZddZdS )r,   z`400 Bad Request` error.zbad requestNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz400 Bad Requestrg   rf   r   rK   messagerL   r   rH   rM   s       rP   rK   zBadRequest.__init__   s8    "!;/4'2IT\JJJJJrQ   rG   rR   rS   rT   rZ   r   rK   rU   rQ   rP   r,   r,      s9        ""GK K K K K KrQ   r,   c                       e Zd ZdZdZddZdS )r-   z`401 Unauthorized` error.r8   Nc                 Z    d}ddi}t                               | |||p| j                   d S )Nz401 Unauthorizedrg   rf   r   r   s       rP   rK   zUnauthorized.__init__   s8    #!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r-   r-      s9        ##GK K K K K KrQ   r-   c                       e Zd ZdZdZddZdS )r.   z`403 Forbidden` error.r9   Nc                 Z    d}ddi}t                               | |||p| j                   d S )Nz403 Forbiddenrg   rf   r   r   s       rP   rK   zForbidden.__init__   s8     !;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r.   r.      9          GK K K K K KrQ   r.   c                       e Zd ZdZdZddZdS )	_NotFoundz`404 Not Found` error.z	not foundNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz404 Not Foundrg   ztext/html; charset=utf-8r   r   s       rP   rK   z_NotFound.__init__   s9     !#=>4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r   r      r   rQ   r   c                     | rt          |           S t                              d          r$t          j        d                                         S t                      S )zIReturns HTTPError with '404 Not Found' error from the active application.	app_stack)r   r   getr   r:   r   s    rP   r/   r/      sO     !!!			 }R ))+++{{rQ   c                       e Zd ZdZdZddZdS )r0   z!A `405 Method Not Allowed` error.zmethod not allowedNc                     d}i }d|d<   g d}rfd|D             }d                     |          |d<   t                              | ||| j                   d S )Nz405 Method Not Allowedrf   rg   )GETHEADPOSTPUTDELETEc                 4    g | ]}t          |          |S rU   )hasattr).0methodclss     rP   
<listcomp>z%NoMethod.__init__.<locals>.<listcomp>  s(    LLL&wsF7K7KLvLLLrQ   z, Allow)joinr   rK   r   )rL   r   rH   rM   methodss    `   rP   rK   zNoMethod.__init__  s|    )"-::: 	MLLLLGLLLG99W--4$,?????rQ   rG   r   rU   rQ   rP   r0   r0      s9        ++"G
@ 
@ 
@ 
@ 
@ 
@rQ   r0   c                       e Zd ZdZdZddZdS )r1   z`406 Not Acceptable` error.znot acceptableNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz406 Not Acceptablerg   rf   r   r   s       rP   rK   zNotAcceptable.__init__  s8    %!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r1   r1     s9        %%GK K K K K KrQ   r1   c                       e Zd ZdZdZddZdS )r2   z`409 Conflict` error.r=   Nc                 Z    d}ddi}t                               | |||p| j                   d S )Nz409 Conflictrg   rf   r   r   s       rP   rK   zConflict.__init__$  s8    !;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r2   r2     s9        GK K K K K KrQ   r2   c                       e Zd ZdZdZddZdS )r3   z`410 Gone` error.r>   Nc                 Z    d}ddi}t                               | |||p| j                   d S )Nz410 Gonerg   rf   r   r   s       rP   rK   zGone.__init__2  s8    !;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r3   r3   -  s9        GK K K K K KrQ   r3   c                       e Zd ZdZdZddZdS )r4   z `412 Precondition Failed` error.zprecondition failedNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz412 Precondition Failedrg   rf   r   r   s       rP   rK   zPreconditionFailed.__init__@  s8    *!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r4   r4   ;  s9        **#GK K K K K KrQ   r4   c                       e Zd ZdZdZddZdS )r5   z#`415 Unsupported Media Type` error.zunsupported media typeNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz415 Unsupported Media Typerg   rf   r   r   s       rP   rK   zUnsupportedMediaType.__init__N  s8    -!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r5   r5   I  s9        --&GK K K K K KrQ   r5   c                       e Zd ZdZdZddZdS )_UnavailableForLegalReasonsz*`451 Unavailable For Legal Reasons` error.zunavailable for legal reasonsNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz!451 Unavailable For Legal Reasonsrg   rf   r   r   s       rP   rK   z$_UnavailableForLegalReasons.__init__\  s8    4!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r   r   W  s9        44-GK K K K K KrQ   r   c                     | rt          |           S t                              d          r$t          j        d                                         S t                      S )z]Returns HTTPError with '415 Unavailable For Legal Reasons' error from the active application.r   r   )r   r   r   r   rA   r   s    rP   r6   r6   b  sQ     -*7333			 -}R ;;===*,,,rQ   c                       e Zd ZdZdZddZdS )_InternalErrorz500 Internal Server Error`.zinternal server errorNc                 Z    d}ddi}t                               | |||p| j                   d S )Nz500 Internal Server Errorrg   rf   r   r   s       rP   rK   z_InternalError.__init__t  s8    ,!;/4'2IT\JJJJJrQ   rG   r   rU   rQ   rP   r   r   o  s9        %%%GK K K K K KrQ   r   c                     | rt          |           S t                              d          r$t          j        d                                         S t                      S )zNReturns HTTPError with '500 internal error' error from the active application.r   r   )r   r   r   r   rC   r   s    rP   rB   rB   z  sQ      g&&&			  }R ..000rQ   c                       e Zd ZdZddZdS )cgiFieldStorageaA  
    Subclass cgi.FieldStorage, as read_binary expects fp to return
    bytes. If the headers do not contain a content-disposition with a
    filename, cgi.FieldStorage's make_file will create a TemporaryFile
    with `w+` flags. The write to that temporary file will fail, due
    to incorrect encoding in Python 3.
    Nc                 *    t          j        d          S )z
        For backwards compatibility with Python 2, make_file accepted
        a binary flag. This was unused, and removed in Python 3.
        zwb+)tempfileTemporaryFile)rL   binarys     rP   	make_filezcgiFieldStorage.make_file  s    
 %e,,,rQ   rG   )rR   rS   rT   rZ   r   rU   rQ   rP   r   r     s2         - - - - - -rQ   r   Fc                 H   t          |           t          |          }} d| v sd| v sd|v sd|v rt          d          |du r?t          j        D ]2\  }}|                                |                                 k    r dS 3t          j                            | |f           dS )z
    Adds the header `hdr: value` with the response.

    If `unique` is True and a header with that name already exists,
    it doesn't add a new one.
    
zinvalid characters in headerTN)r   
ValueErrorr   rM   lowerappend)hdrvalueuniquehrO   s        rP   r   r     s     wu~~Cs{{dckkTU]]demm7888~~K 	 	DAqwwyyCIIKK'' ( KU|$$$$$rQ   c                 D   | pd} d }t           j                                        }i x}}|                                 dv r|d         dv r|                    dd                                                              d          rCt                               d	          }|s&|d
         }t          ||d          }|t           _        nYt                      }t          |t                    r|                    d          }t          |          }t          ||d          } ||          }|                                 dv rd|d<    |t          |d                    }fdt          fdt          ||                                          D                       S )z2Returns storage object with GET or POST arguments.bothc                 V      j         g  _         t           fd D                       S )Nc                 $    g | ]}||         fS rU   rU   )r   rN   fss     rP   r   z-rawinput.<locals>.dictify.<locals>.<listcomp>  s!    ,,,AaAZ,,,rQ   )listdict)r   s   `rP   dictifyzrawinput.<locals>.dictify  s4    7?BG,,,,,,,---rQ   )r   postputpatchREQUEST_METHOD)r   r   PATCHCONTENT_TYPErE   z
multipart/_fieldstorage
wsgi.inputr   )fpenvironkeep_blank_valueszutf-8)r   r   r   )r   r   c                 j    t          | t                    rfd| D             S | j        | j        S | S )Nc                 &    g | ]} |          S rU   rU   )r   xprocess_fieldstorages     rP   r   z:rawinput.<locals>.process_fieldstorage.<locals>.<listcomp>  s%    888((++888rQ   )
isinstancer   filenamer   )r   r   s    rP   r   z&rawinput.<locals>.process_fieldstorage  sC    b$ 	8888R8888[ 8OIrQ   c                 0    g | ]\  }}| |          fS rU   rU   )r   rN   rO   r   s      rP   r   zrawinput.<locals>.<listcomp>  s.    SSSTQQ,,Q//0SSSrQ   )r   envcopyr   r   rj   r   r   r   r   strencoder   r	   r   rI   )r   r   eabr   dr   s          @rP   rawinputr     s   vF. . . 	AJA||~~999":::uu^R((..00;;LII K GGO,, *<B'2qANNNA()C%FFa%% *))AQZZ#r1JJJ

A||~~((#
GOACCCDD     SSSSWQ]]=P=P=R=RSSSTTTrQ   c                      |                     dd          }t          |          }	 |                    dd           t          |g| R i |S # t          $ r t                      w xY w)z
    Returns a `storage` object with the GET and POST arguments.
    See `storify` for how `requireds` and `defaults` work.
    _methodr   _unicodeT)popr   
setdefaultr
   KeyErrorr7   )	requiredsdefaultsr   outs       rP   r   r     s    
 ll9f--G
7

CJ---s3Y333(333   lls   %A A&c                     dt           vrt           j                            d          dk    r/t           j        d                                         t           _        n\t          t           j                            d          d          } t           j        d                             |           t           _        t           j        S )z'Returns the data sent with the request.r   HTTP_TRANSFER_ENCODINGchunkedr   CONTENT_LENGTHr   )r   r   r   readr   r   )cls    rP   r   r     s    S7;;/00I==w|,1133CHH$455q99Bw|,11"55CH8OrQ   c                    t                      }t          |           t          |          }} |                    | |t          |                     t	          |t
                    r|dk     rd}||d<   |pt          j        dz   |d<   |r||d<   |r||d<   |rd|d	<   |                                }|r|	                                d
v r|d|z  z  }t          d|           dS )zSets a cookie.r   i 6eexpiresre   ri   domainsecureThttponly)strictlaxnonez; SameSite=%sz
Set-CookieN)r   r   setr   r   intr   homepathOutputStringr   r   )	namer   r   r   r   r   ri   samesitemorsels	            rP   r   r     s    XXF$--%D
JJtUE%LL)))'3 GaKKF9/S\C/F6N "!x "!x "!z!!E ,HNN$$(AAA8++
<rQ   c                 d   d| v rt                      }	 |                    |            n\# t          $ rO t                      }|                     d          D ](}	 |                    |           # t          $ r Y %w xY wY nw xY wt	          d |                                D                       }n~i }|                     d          D ]f}|                    dd          }t          |          dk    r;|\  }}t          |                                          ||                                <   g|S )a  Parse a HTTP_COOKIE header and return dict of cookie names and decoded values.

    >>> sorted(parse_cookies('').items())
    []
    >>> sorted(parse_cookies('a=1').items())
    [('a', '1')]
    >>> sorted(parse_cookies('a=1%202').items())
    [('a', '1 2')]
    >>> sorted(parse_cookies('a=Z%C3%A9Z').items())
    [('a', 'Z\xc3\xa9Z')]
    >>> sorted(parse_cookies('a=1; b=2; c=3').items())
    [('a', '1'), ('b', '2'), ('c', '3')]

    # TODO: cclauss re-enable this test
    # >>> sorted(parse_cookies('a=1; b=w("x")|y=z; c=3').items())
    # [('a', '1'), ('b', 'w('), ('c', '3')]

    >>> sorted(parse_cookies('a=1; b=w(%22x%22)|y=z; c=3').items())
    [('a', '1'), ('b', 'w("x")|y=z'), ('c', '3')]

    >>> sorted(parse_cookies('keebler=E=mc2').items())
    [('keebler', 'E=mc2')]
    >>> sorted(parse_cookies(r'keebler="E=mc2; L=\"Loves\"; fudge=\012;"').items())
    [('keebler', 'E=mc2; L="Loves"; fudge=\n;')]
    ";c                 @    g | ]\  }}|t          |j                  fS rU   )r   r   )r   rN   rO   s      rP   r   z!parse_cookies.<locals>.<listcomp>;  s+    III$!QGAG,,-IIIrQ   =r      )	r   loadr   r[   r   rI   lenr   strip)http_cookiecookie
attr_valuer   	key_valuekeyr   s          rP   parse_cookiesr    sZ   6 k
	KK$$$$ 	 	 	 "^^F)//44  
KK
++++"   D 		 II&,,..IIIJJ $**3// 	> 	>I!Q//I9~~""&
U'.u{{}}'='=		$Ns3   * /BA0/B0
A=:B<A==BBc                     dt           vr9t           j                            dd          }t          |          t           _        	 t          t           j        g| R i |S # t          $ r t                       t                      w xY w)a(  Returns a `storage` object with all the request cookies in it.

    See `storify` for how `requireds` and `defaults` work.

    This is forgiving on bad HTTP_COOKIE input, it tries to parse at least
    the cookies it can.

    The values are converted to unicode if _unicode=True is passed.
    _parsed_cookiesHTTP_COOKIErE   )	r   r   r   r  r  r
   r   r7   StopIteration)r   r   r  s      rP   r   r   G  s     ##gkk-44+K88s*CYCCC(CCC   oos   A 'Bc                      	 t           j        d         }n#  t          j        }Y nxY w| D ]%}t	          t          j        |          |           &dS )z=
    Prints a prettyprinted version of `args` to stderr.
    wsgi.errors)filerE   )r   r   sysstderrprintpprintpformat)argsr   args      rP   r   r   ]  s]    k-(j - -fnS!!,,,,,2    %c                 ~    	 t           j        d         }n#  t          j        }Y nxY w|                    |            d S )Nr  )r   r   r  r  write)r   r   s     rP   _debugwriter$  j  s<    k-(jIIaLLLLLr!  a%  
A `storage` object containing various information about the request:

`environ` (aka `env`)
   : A dictionary containing the standard WSGI environment variables.

`host`
   : The domain (`Host` header) requested by the user.

`home`
   : The base path for the application.

`ip`
   : The IP address of the requester.

`method`
   : The HTTP method used.

`path`
   : The path request.

`query`
   : If there are no query arguments, the empty string. Otherwise, a `?` followed
     by the query string.

`fullpath`
   : The full path requested, including query arguments (`== path + query`).

### Response Data

`status` (default: "200 OK")
   : The status code to be used in the response.

`headers`
   : A list of 2-tuples to be used in the response.

`output`
   : A string to be used as the response.
__main__)NNNrG   rv   )rE   NFFNN)\rZ   cgir  r  r   ior   urllib.parser   utilsr   r   r   r	   r
   r   r   r   http.cookiesr   r   r   ImportErrorurllibCookie__all__r   rJ   r   ra   r   r   r   r   r    r   r!   r   r"   r'   r#   r(   r$   r)   r%   r*   r&   r+   r,   r7   r-   r8   r.   r9   r   r/   r:   r0   r;   r1   r<   r2   r=   r3   r>   r4   r?   r5   r@   r   r6   rA   r   rB   rC   FieldStorager   r   r   r   r   r   r  r   r   r$  r#  r   contextrR   doctesttestmodrU   rQ   rP   <module>r3     s   
 


  



                    K K K K K K K K K K K K K K K K9++++++++>>>>>>>>>>> 9 9 9%%%%%%%%88888888888898 8 8t 
) ) ) ) )	 ) ) )   " ,xb
)
)
) )R L// /'"l>22 28$%566 6	I6 6 6 6 6y 6 6 6, E E E E EH E E E 	I I I I Ix I I I 5 5 5 5 5) 5 5 5 R R R R R8 R R R K K K K K K K K 
K K K K K9 K K K K K K K K	 K K K 	K K K K K	 K K K    @ @ @ @ @y @ @ @$ K K K K KI K K K K K K K Ky K K K K K K K K9 K K K K K K K K K K K ( K K K K K9 K K K , K K K K K) K K K- - - - 8 K K K K KY K K K        - - - - -c& - - -"% % % %&-U -U -U -U`     	       <2 2 2j  ,
 
 
    g&P zNNNGO s   A AA