o
    Zhy                     @   s  U d dl Z d dlZd dlZd dlZ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 d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d dl(m)Z)m*Z+ dd	l,m-Z- dd
l m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZD ddlEmFZF dZGerddlHmIZI eeJeJf ZKneLZKeMeNeOe+Pddd ZQee#eOdf  eRd< eSdZTee eJ  eRd< eSdZUee eJ  eRd< eVdZWeeJ eRd< eeAgeeeD  f ZXe#ed e!eJ f ZYG d d! d!e%d"d#ZZG d$d% d%e"ed& Z[G d'd& d&e j\Z]G d(d deKe.Z^G d)d* d*e^Z_d+eAd,dfd-d.Z`G d/d0 d0e[ZaG d1d2 d2eaZbG d3d4 d4eaZcG d5d6 d6e[ZdG d7d8 d8edZeG d9d: d:edZfG d;d< d<e j\ZgG d=d> d>egZhG d?d@ d@ehZiG dAdB dBefZjG dCdD dDe]ZkG dEdF dFe]ZlG dGdH dHe0ZmG dIdJ dJe"ee[ ee[ ZnG dKdL dLe"ee] ee] ZoG dMdN dNe/eeJe[f ZpdOeJd,eJfdPdQZqdOeJd,eJfdRdSZrdOeJd,eJfdTdUZsdS )V    N)contextmanager)wraps)Path)MappingProxyType)TYPE_CHECKINGAny	AwaitableCallable	ContainerDictFinal	GeneratorIterableIteratorListMappingNoReturnOptionalPatternSetSizedTupleType	TypedDictUnioncast)URL__version__   )hdrs)AbstractMatchInfoAbstractRouterAbstractView)DEBUG)HttpVersion11)HandlerPathLike)HTTPExceptionHTTPExpectationFailedHTTPForbiddenHTTPMethodNotAllowedHTTPNotFound)FileResponse)Request)ResponseStreamResponse)AbstractRouteDef)
UrlDispatcherUrlMappingMatchInfoAbstractResourceResourcePlainResourceDynamicResourceAbstractRouteResourceRouteStaticResourceView)Application.   .YARL_VERSIONz$^[0-9A-Za-z!#\$%&'\*\+\-\.\^_`\|~]+$HTTP_METHOD_REz*(\{[_a-zA-Z][^{}]*(?:\{[^{}]*\}[^{}]*)*\})ROUTE_RE/PATH_SEPr2   c                   @   sj   e Zd ZU eed< eed< ee ed< eed< eed< eedf ed< ded	< eed
< ded< eed< dS )	_InfoDictpath	formatterpattern	directoryprefixr7   routesr;   appdomainAbstractRuleMatchingrulehttp_exceptionN)	__name__
__module____qualname__str__annotations__r   r   r   r'    rT   rT   P/var/www/html/lang_env/lib/python3.10/site-packages/aiohttp/web_urldispatcher.pyrC   ^   s   
 rC   F)totalc                   @   s   e Zd Zdddee ddfddZedee fddZeej	defd	d
Z
ej	dedefddZej	dedefddZej	deddfddZej	defddZdddZej	dedefddZdS )r3   NnamerX   returnc                C   
   || _ d S N_nameselfrX   rT   rT   rU   __init__r      
zAbstractResource.__init__c                 C      | j S r[   r\   r_   rT   rT   rU   rX   u      zAbstractResource.namec                 C      dS )zWExposes the resource's canonical path.

        For example '/foo/bar/{name}'

        NrT   rc   rT   rT   rU   	canonicaly       zAbstractResource.canonicalkwargsc                 K   re   )z2Construct url for resource with additional params.NrT   )r_   rh   rT   rT   rU   url_for   rg   zAbstractResource.url_forrequestc                       dS )zWResolve resource.

        Return (UrlMappingMatchInfo, allowed_methods) pair.
        NrT   r_   rj   rT   rT   rU   resolve       zAbstractResource.resolverH   c                 C   re   )zWAdd a prefix to processed URLs.

        Required for subapplications support.
        NrT   r_   rH   rT   rT   rU   
add_prefix   rg   zAbstractResource.add_prefixc                 C   re   z;Return a dict with additional info useful for introspectionNrT   rc   rT   rT   rU   get_info   rg   zAbstractResource.get_infoc                 C      d S r[   rT   rc   rT   rT   rU   freeze      zAbstractResource.freezerD   c                 C   re   )z Perform a raw match against pathNrT   r_   rD   rT   rT   rU   	raw_match   rg   zAbstractResource.raw_matchrY   N)rO   rP   rQ   r   rR   r`   propertyrX   abcabstractmethodrf   r   ri   r-   _Resolverm   rp   rC   rr   rt   boolrw   rT   rT   rT   rU   r3   q   s$    
r3   r7   c                   @   s   e Zd Zddddedeeee f dee	 dee
 ddf
dd	Zedefd
dZedefddZeejdee fddZedee
 fddZejdefddZejdededefddZdedee fddZdS )r7   Nexpect_handlerresourcemethodhandlerr   r   rY   c                   s   |d u rt }t|sJ d|| }t|s"t| dt|s*J |t|r0n0t	|r<t
dt n$t|trGt|trGnt
dt t|dtdtf fdd}| |}|| _|| _|| _|| _d S )	NzCoroutine is expected, got z is not allowed HTTP methodz6Bare generators are deprecated, use @coroutine wrapperz-Bare functions are deprecated, use async onesrj   rY   c                    s0    | }t |r|I d H }t|tsJ |S r[   )asyncioiscoroutine
isinstancer/   )rj   resultZold_handlerrT   rU   handler_wrapper   s   

z/AbstractRoute.__init__.<locals>.handler_wrapper)_default_expect_handlerr   iscoroutinefunctionupperr?   match
ValueErrorcallableinspectisgeneratorfunctionwarningswarnDeprecationWarningr   type
issubclassr"   r   r-   r/   _method_handler_expect_handler	_resource)r_   r   r   r   r   r   rT   r   rU   r`      s>   	



zAbstractRoute.__init__c                 C   rb   r[   )r   rc   rT   rT   rU   r      rd   zAbstractRoute.methodc                 C   rb   r[   )r   rc   rT   rT   rU   r      rd   zAbstractRoute.handlerc                 C   re   )z8Optional route's name, always equals to resource's name.NrT   rc   rT   rT   rU   rX      rg   zAbstractRoute.namec                 C   rb   r[   )r   rc   rT   rT   rU   r      rd   zAbstractRoute.resourcec                 C   re   rq   rT   rc   rT   rT   rU   rr      rg   zAbstractRoute.get_infoargsrh   c                 O   re   z/Construct url for route with additional params.NrT   r_   r   rh   rT   rT   rU   ri      rg   zAbstractRoute.url_forrj   c                    s   |  |I d H S r[   )r   rl   rT   rT   rU   handle_expect_header      z"AbstractRoute.handle_expect_header)rO   rP   rQ   rR   r   r%   r   r"   r   _ExpectHandlerr3   r`   ry   r   r   rz   r{   rX   r   rC   rr   r   ri   r-   r/   r   rT   rT   rT   rU   r7      s8    
3c                       s   e Zd Zdeeef def fddZedefddZ	edefdd	Z
edefd
dZedee fddZdefddZeded fddZd!ddZed"ddZeddded fddZd#ddZdef fdd Z  ZS )$r2   
match_dictroutec                    s(   t  | || _g | _d | _d| _d S NF)superr`   _route_apps_current_app_frozen)r_   r   r   	__class__rT   rU   r`      s
   
zUrlMappingMatchInfo.__init__rY   c                 C      | j jS r[   )r   r   rc   rT   rT   rU   r         zUrlMappingMatchInfo.handlerc                 C   rb   r[   )r   rc   rT   rT   rU   r      rd   zUrlMappingMatchInfo.routec                 C   r   r[   )r   r   rc   rT   rT   rU   r     r   z"UrlMappingMatchInfo.expect_handlerc                 C   rs   r[   rT   rc   rT   rT   rU   rN        z"UrlMappingMatchInfo.http_exceptionc                 C   s
   | j  S r[   )r   rr   rc   rT   rT   rU   rr   	  ra   zUrlMappingMatchInfo.get_info)r;   .c                 C   
   t | jS r[   )tupler   rc   rT   rT   rU   apps  s   
zUrlMappingMatchInfo.appsrJ   r;   Nc                 C   s0   | j rtd| jd u r|| _| jd| d S )Nz-Cannot change apps stack after .freeze() callr   )r   RuntimeErrorr   r   insertr_   rJ   rT   rT   rU   add_app  s
   
zUrlMappingMatchInfo.add_appc                 C   s   | j }|d us	J |S r[   )r   r   rT   rT   rU   current_app  s   zUrlMappingMatchInfo.current_app)NNNc                 c   sJ    t r|| jvrtd| j|| j}|| _z	d V  W || _d S || _w )Nz1Expected one of the following apps {!r}, got {!r})r#   r   r   formatr   )r_   rJ   prevrT   rT   rU   set_current_app  s   
z#UrlMappingMatchInfo.set_current_appc                 C   s
   d| _ d S )NT)r   rc   rT   rT   rU   rt   -  ra   zUrlMappingMatchInfo.freezec                    s   dt    d| j dS )Nz<MatchInfo z: >)r   __repr__r   rc   r   rT   rU   r   0  s   zUrlMappingMatchInfo.__repr__)rJ   r;   rY   N)rY   r;   rx   )rO   rP   rQ   r   rR   r7   r`   ry   r%   r   r   r   r   r   r'   rN   rC   rr   r   r   r   r   r   r   r   rt   r   __classcell__rT   rT   r   rU   r2      s(    

c                       sF   e Zd Zdeddf fddZedefddZdefdd	Z  Z	S )
MatchInfoErrorrN   rY   Nc                    s   || _ t i t| j  d S r[   )
_exceptionr   r`   SystemRouter_   rN   r   rT   rU   r`   5  s   zMatchInfoError.__init__c                 C   rb   r[   )r   rc   rT   rT   rU   rN   9  rd   zMatchInfoError.http_exceptionc                 C   s   d | jj| jjS )Nz<MatchInfoError {}: {}>)r   r   statusreasonrc   rT   rT   rU   r   =  s   zMatchInfoError.__repr__)
rO   rP   rQ   r'   r`   ry   rN   rR   r   r   rT   rT   r   rU   r   4  s
    r   rj   rY   c                    sP   | j tjd}| jtkr&| dkr| jdI dH  dS t	d| ddS )zDefault handler for Expect header.

    Just send "100 Continue" to client.
    raise HTTPExpectationFailed if value of header is not "100-continue"
     z100-continues   HTTP/1.1 100 Continue

NzUnknown Expect: %s)text)
headersgetr   ZEXPECTversionr$   lowerwriterwriter(   )rj   expectrT   rT   rU   r   C  s   
r   c                	       s   e Zd Zdddee ddf fddZddded	eee e	f d
ee
 ddfddZdddZdedefddZejdedeeeef  fddZdefddZded fddZ  ZS )r4   NrW   rX   rY   c                   s   t  j|d g | _d S )NrW   )r   r`   _routesr^   r   rT   rU   r`   R     
zResource.__init__r   r   r   r   r8   c                C   sP   | j D ]}|j|ks|jtjkrtdj|dqt||| |d}| | |S )NzOAdded route will never be executed, method {route.method} is already registered)r   r   )r   r   r   METH_ANYr   r   r8   register_route)r_   r   r   r   	route_objrT   rT   rU   	add_routeV  s   

zResource.add_router   c                 C   s(   t |tsJ d|| j| d S )Nz)Instance of Route class is required, got )r   r8   r   appendr_   r   rT   rT   rU   r   j  s   zResource.register_routerj   c                    sp   t  }| |jj}|d u rd |fS | jD ]}|j}|| ||jks*|tjkr3t	|||f  S qd |fS r[   )
set_matchrel_urlraw_pathr   r   addr   r   r2   )r_   rj   allowed_methodsr   r   Zroute_methodrT   rT   rU   rm   p  s   

zResource.resolverD   c                 C   rs   r[   rT   rv   rT   rT   rU   r     r   zResource._matchc                 C   r   r[   lenr   rc   rT   rT   rU   __len__  ra   zResource.__len__c                 C   r   r[   )iterr   rc   rT   rT   rU   __iter__  ra   zResource.__iter__)r   r8   rY   N)rO   rP   rQ   r   rR   r`   r   r   r"   r%   r   r   r   r-   r|   rm   rz   r{   r   r   intr   r   r   r   rT   rT   r   rU   r4   Q  s$     	

 r4   c                       s   e Zd Zdddedee ddf fddZedefdd	Zdd
dZdeddfddZ	dedee
eef  fddZdedefddZdefddZdefddZdefddZ  ZS )r5   NrW   rD   rX   rY   c                   s*   t  j|d |r|dsJ || _d S )NrW   rA   )r   r`   
startswith_path)r_   rD   rX   r   rT   rU   r`     s   
zPlainResource.__init__c                 C   rb   r[   r   rc   rT   rT   rU   rf     rd   zPlainResource.canonicalc                 C   s   | j sd| _ d S d S )NrA   r   rc   rT   rT   rU   rt     s   
zPlainResource.freezerH   c                 C   s<   | dsJ |drJ t|dksJ || j | _d S NrA   r   )r   endswithr   r   ro   rT   rT   rU   rp     s   zPlainResource.add_prefixc                 C   s   | j |kri S d S r[   r   rv   rT   rT   rU   r     s   
zPlainResource._matchc                 C   
   | j |kS r[   r   rv   rT   rT   rU   rw     ra   zPlainResource.raw_matchc                 C   
   d| j iS )NrD   r   rc   rT   rT   rU   rr     ra   zPlainResource.get_infoc                 C   s   t j| jddS NTrD   encoded)r   buildr   rc   rT   rT   rU   ri        zPlainResource.url_forc                 C   s0   | j d urd| j  d nd}d| d| j dS )N'' r   z<PlainResource  r   )rX   r   r^   rT   rT   rU   r     s   zPlainResource.__repr__rx   )rO   rP   rQ   rR   r   r`   ry   rf   rt   rp   r   r   r}   rw   rC   rr   r   ri   r   r   rT   rT   r   rU   r5     s    $
r5   c                       s   e Zd ZedZedZdZdddede	e ddf fd	d
Z
edefddZdeddfddZdede	eeef  fddZdedefddZdefddZdedefddZdefddZ  ZS )r6   z#\{(?P<var>[_a-zA-Z][_a-zA-Z0-9]*)\}z.\{(?P<var>[_a-zA-Z][_a-zA-Z0-9]*):(?P<re>.+)\}z[^{}/]+NrW   rD   rX   rY   c          	   
      sX  t  j|d d}d}t|D ]d}| j|}|r2|d|d| j7 }|d|d d 7 }q| j	|}|rR|djdi |
 7 }|d|d d 7 }qd|v sZd|v retd| d	| d
t|}||7 }|t|7 }qzt|}W n tjy } ztd| d| d d }~ww |jtsJ |dsJ || _|| _d S )NrW   r   z
(?P<{}>{})var{}z(?P<{var}>{re})zInvalid path 'z'['z']zBad pattern 'z': rA   rT   )r   r`   r@   splitDYN	fullmatchr   groupGOODDYN_WITH_RE	groupdictr   _requote_pathreescapecompileerrorrF   r   rB   _pattern
_formatter)	r_   rD   rX   rF   rE   partr   compiledexcr   rT   rU   r`     s:   
zDynamicResource.__init__c                 C   rb   r[   r   rc   rT   rT   rU   rf     rd   zDynamicResource.canonicalrH   c                 C   sV   | dsJ |drJ t|dksJ tt|| jj | _|| j | _d S r   )	r   r   r   r   r   r   r   rF   r   ro   rT   rT   rU   rp     s
   zDynamicResource.add_prefixc                 C   s.   | j |}|d u rd S dd |  D S )Nc                 S      i | ]	\}}|t |qS rT   )_unquote_path).0keyvaluerT   rT   rU   
<dictcomp>  s    z*DynamicResource._match.<locals>.<dictcomp>)r   r   r   items)r_   rD   r   rT   rT   rU   r     s   
zDynamicResource._matchc                 C   r   r[   r  rv   rT   rT   rU   rw     ra   zDynamicResource.raw_matchc                 C      | j | jdS )N)rE   rF   )r   r   rc   rT   rT   rU   rr        zDynamicResource.get_infopartsc                 K   s(   | j dd | D }tj|ddS )Nc                 S   r  rT   )_quote_path)r  kvrT   rT   rU   r    s    z+DynamicResource.url_for.<locals>.<dictcomp>Tr   )r   
format_mapr	  r   r   )r_   r  urlrT   rT   rU   ri     s   zDynamicResource.url_forc                 C   s,   | j d urd| j  d nd}dj|| jdS )Nr   r   r   z$<DynamicResource {name} {formatter}>)rX   rE   )rX   r   r   r^   rT   rT   rU   r     s   zDynamicResource.__repr__)rO   rP   rQ   r   r   r   r   r   rR   r   r`   ry   rf   rp   r   r   r}   rw   rC   rr   r   ri   r   r   rT   rT   r   rU   r6     s    

$!	r6   c                       sj   e Zd Zdddedee ddf fddZedefdd	Zdeddfd
dZdede	fddZ
  ZS )PrefixResourceNrW   rH   rX   rY   c                   sX   |r| dsJ ||dv s|drJ |t j|d t|| _| jd | _d S )NrA   )r   rA   rW   )r   r   r   r`   r   _prefix_prefix2)r_   rH   rX   r   rT   rU   r`     s
   
zPrefixResource.__init__c                 C   rb   r[   )r  rc   rT   rT   rU   rf   
  rd   zPrefixResource.canonicalc                 C   sH   | dsJ |drJ t|dksJ || j | _| jd | _d S r   )r   r   r   r  r  ro   rT   rT   rU   rp     s
   zPrefixResource.add_prefixc                 C   re   r   rT   ro   rT   rT   rU   rw     ru   zPrefixResource.raw_match)rO   rP   rQ   rR   r   r`   ry   rf   rp   r}   rw   r   rT   rT   r   rU   r    s    $r  c                       s  e Zd ZdZddddddddededee d	ee d
ede	de	de	ddf fddZ
dddedee	 defddZededefddZdefddZdeddfddZdedefddZdefd d!Zdee fd"d#Zdedefd$d%Zd&edefd'd(Zdefd)d*Z  Z S )+r9   r  N   FrX   r   
chunk_size
show_indexfollow_symlinksappend_versionrH   rG   rX   r   r  r  r  r  rY   c          
   
      s   t  j||d z#t|}t|drttjt|}| }|	 s*t
dW n tt
fyB }	 z	t
d| d|	d }	~	ww || _|| _|| _|| _|| _|| _td| j| |dtd| j| |dd	| _d S )
NrW   ~zNot a directoryzNo directory exists at 'r   GETr   HEAD)r  r  )r   r`   r   rR   r   osrD   
expanduserrm   is_dirr   FileNotFoundError
_directory_show_index_chunk_size_follow_symlinksr   _append_versionr8   _handler   )
r_   rH   rG   rX   r   r  r  r  r  r   r   rT   rU   r`     s4   

zStaticResource.__init__)r  filenamec          
   	   C   s  |d u r| j }t|d}tj| jdd}tdk r#||dd }n|| }|r| j	|}z"| j
rFttj|}|| j | }n
| }|| j W n ttfy^   | Y S w | r|d}| }W d    n1 sww   Y  | |}	|| j|	i}|S |S )NrA   Tr   r      %%25rb)r&  rR   lstripr   r   r  r>   replacer"  joinpathr%  r   r  rD   normpathrelative_torm   r   r!  is_fileopenread_get_file_hashZ
with_queryVERSION_KEY)
r_   r(  r  r  unresolved_pathnormalized_pathfilepathfZ
file_byteshrT   rT   rU   ri   D  s8   


zStaticResource.url_for
byte_arrayc                 C   s*   t  }||  t| }|dS )Nascii)hashlibsha256updatebase64urlsafe_b64encodedigestdecode)r=  mZb64rT   rT   rU   r6  m  s   

zStaticResource._get_file_hashc                 C   s   | j | j| jdS )N)rG   rH   rI   )r"  r  r   rc   rT   rT   rU   rr   t  s   zStaticResource.get_infor   c                 C   s.   d| j v r	tdtd|| | jd| j d< d S )NOPTIONSzOPTIONS route was set alreadyr   )r   r   r8   r   )r_   r   rT   rT   rU   set_options_route{  s
   

z StaticResource.set_options_routerj   c                    s|   |j j}|j}t| j}|| js|| jkrd t fS ||vr%d |fS dt|t	| jd d  i}t
|| j| |fS )Nr(  r   )r   r   r   r   r   r   r  r  r  r   r2   )r_   rj   rD   r   r   r   rT   rT   rU   rm     s   

zStaticResource.resolvec                 C   r   r[   r   rc   rT   rT   rU   r     ra   zStaticResource.__len__c                 C   s   t | j S r[   )r   r   valuesrc   rT   rT   rU   r     r  zStaticResource.__iter__c              
      s(  |j d }z2t|}|jrt | j|}| jr-ttj	|}|
| j | }n
| }|
| j W n/ ttfyJ } zt |d }~w tyQ     tyg } z|jj| t |d }~ww | r| jrz
t| |ddW S  ty   t w t | rt|| jdS t)Nr(  z	text/html)r   content_type)r  )
match_infor   anchorr)   r"  r0  r%  r  rD   r1  r2  rm   r   r!  r+   	ExceptionrJ   logger	exceptionr   r#  r.   _directory_as_htmlPermissionErrorr3  r,   r$  )r_   rj   r   r(  r8  r9  r:  r   rT   rT   rU   r'    sJ   


zStaticResource._handler:  c                 C   s   |  sJ || j }d| }d| d}g }| }t|D ])}|| j }| jd | }	|  r?|j d}
n|j}
|dj	|	|
d q#d	d
|}d	| d| d
}d| d}d| d| d}|S )Nz
Index of /z<h1>z</h1>rA   z#<li><a href="{url}">{name}</a></li>)r  rX   z<ul>
{}
</ul>
z<body>
z
</body>z<head>
<title>z</title>
</head>z<html>
z
</html>)r   r2  r"  as_posixiterdirsortedr  rX   r   r   join)r_   r:  Zrelative_path_to_dirZindex_ofZh1Z
index_listZ	dir_index_filerel_pathfile_url	file_nameZulbodyZhead_strhtmlrT   rT   rU   rP    s,   
z!StaticResource._directory_as_htmlc                 C   s0   | j d urd| j  d nd}dj|| j| jdS )Nr   r   z/<StaticResource {name} {path} -> {directory!r}>)rX   rD   rG   )rX   r   r  r"  r^   rT   rT   rU   r     s   
zStaticResource.__repr__)!rO   rP   rQ   r7  rR   r&   r   r   r   r}   r`   r   ri   staticmethodbytesr6  rC   rr   r%   rH  r-   r|   rm   r   r   r7   r   r/   r'  r   rP  r   r   rT   rT   r   rU   r9     sZ    	
*
)+$r9   c                       s   e Zd Zdeddddf fddZdeddf fdd	Zd
ededefddZdefddZ	de
defddZdefddZdee fddZdefddZ  ZS )PrefixedSubAppResourcerH   rJ   r;   rY   Nc                    s0   t  | || _|j D ]}|| qd S r[   )r   r`   _approuter	resourcesrp   )r_   rH   rJ   r   r   rT   rU   r`     s
   zPrefixedSubAppResource.__init__c                    s,   t  | | jj D ]}|| qd S r[   )r   rp   r`  ra  rb  )r_   rH   r   r   rT   rU   rp     s   z!PrefixedSubAppResource.add_prefixr   rh   c                 O      t d)Nz3.url_for() is not supported by sub-application rootr   r   rT   rT   rU   ri        zPrefixedSubAppResource.url_forc                 C   r
  )N)rJ   rH   )r`  r  rc   rT   rT   rU   rr     r  zPrefixedSubAppResource.get_inforj   c                    st   |j j| js|j j| jkrd t fS | jj|I d H }|	| j t
|jtr3|jj}||fS t }||fS r[   )r  r   r   r  r  r   r`  ra  rm   r   r   rN   r*   r   r_   rj   rK  methodsrT   rT   rU   rm     s   
zPrefixedSubAppResource.resolvec                 C      t | jj S r[   )r   r`  ra  rI   rc   rT   rT   rU   r     r   zPrefixedSubAppResource.__len__c                 C   rh  r[   )r   r`  ra  rI   rc   rT   rT   rU   r     r   zPrefixedSubAppResource.__iter__c                 C   s   dj | j| jdS )Nz,<PrefixedSubAppResource {prefix} -> {app!r}>)rH   rJ   )r   r  r`  rc   rT   rT   rU   r     s   zPrefixedSubAppResource.__repr__)rO   rP   rQ   rR   r`   rp   r   ri   rC   rr   r-   r|   rm   r   r   r   r7   r   r   r   rT   rT   r   rU   r_    s    r_  c                   @   sP   e Zd ZejdedefddZejdefddZ	e
ejdefddZd	S )
rL   rj   rY   c                    rk   )z1Return bool if the request satisfies the criteriaNrT   rl   rT   rT   rU   r     rn   zAbstractRuleMatching.matchc                 C   re   rq   rT   rc   rT   rT   rU   rr     rg   zAbstractRuleMatching.get_infoc                 C   re   )zReturn a strNrT   rc   rT   rT   rU   rf      rg   zAbstractRuleMatching.canonicalN)rO   rP   rQ   rz   r{   r-   r}   r   rC   rr   ry   rR   rf   rT   rT   rT   rU   rL     s    rL   c                       s   e Zd ZedZdeddf fddZedefddZ	dedefd	d
Z
dedefddZdedefddZdefddZ  ZS )Domainz(?!-)[a-z\d-]{1,63}(?<!-)rK   rY   Nc                    s   t    | || _d S r[   )r   r`   
validation_domain)r_   rK   r   rT   rU   r`   )  s   
zDomain.__init__c                 C   rb   r[   rk  rc   rT   rT   rU   rf   -  rd   zDomain.canonicalc                    s   t |ts	td|d }|stdd|v rtdtd| }|jd us+J t fdd|j	dD s>td	|j
d
krF|jS |j d|j
 S )NzDomain must be strr<   zDomain cannot be emptyz://zScheme not supportedzhttp://c                 3   s    | ]	} j |V  qd S r[   )re_partr   )r  xrc   rT   rU   	<genexpr>;  s    z$Domain.validation.<locals>.<genexpr>zDomain not validP   :)r   rR   	TypeErrorrstripr   r   r   Zraw_hostallr   port)r_   rK   r  rT   rc   rU   rj  1  s   

zDomain.validationrj   c                    s"   |j tj}|sdS | |S r   )r   r   r   ZHOSTmatch_domain)r_   rj   hostrT   rT   rU   r   A  s
   
zDomain.matchrw  c                 C   s   |  | jkS r[   )r   rk  r_   rw  rT   rT   rU   rv  G  r  zDomain.match_domainc                 C   r   )NrK   rl  rc   rT   rT   rU   rr   J  ra   zDomain.get_info)rO   rP   rQ   r   r   rm  rR   r`   ry   rf   rj  r-   r}   r   rv  rC   rr   r   rT   rT   r   rU   ri  &  s    
ri  c                       sT   e Zd ZedZdeddf fddZedefddZ	d	ede
fd
dZ  ZS )
MaskDomainz(?!-)[a-z\d\*-]{1,63}(?<!-)rK   rY   Nc                    s2   t  | | jdddd}t|| _d S )Nr<   z\.*z.*)r   r`   rk  r/  r   r   _mask)r_   rK   maskr   rT   rU   r`   Q  s   zMaskDomain.__init__c                 C   r   r[   )r{  rF   rc   rT   rT   rU   rf   V  r   zMaskDomain.canonicalrw  c                 C   s   | j |d uS r[   )r{  r   rx  rT   rT   rU   rv  Z  r   zMaskDomain.match_domain)rO   rP   rQ   r   r   rm  rR   r`   ry   rf   r}   rv  r   rT   rT   r   rU   ry  N  s    
ry  c                   @   sb   e Zd ZdeddddfddZedefdd	Zdefd
dZ	de
defddZdefddZdS )MatchedSubAppResourcerM   rJ   r;   rY   Nc                 C   s    t |  d| _|| _|| _d S )Nr   )r3   r`   r  r`  _rule)r_   rM   rJ   rT   rT   rU   r`   _  s   

zMatchedSubAppResource.__init__c                 C   r   r[   )r~  rf   rc   rT   rT   rU   rf   e  r   zMatchedSubAppResource.canonicalc                 C   r
  )N)rJ   rM   )r`  r~  rc   rT   rT   rU   rr   i  r  zMatchedSubAppResource.get_inforj   c                    sh   | j |I d H sd t fS | jj|I d H }|| j t|jt	r-|jj
}||fS t }||fS r[   )r~  r   r   r`  ra  rm   r   r   rN   r*   r   rf  rT   rT   rU   rm   l  s   
zMatchedSubAppResource.resolvec                 C   s   dj | jdS )Nz"<MatchedSubAppResource -> {app!r}>)rJ   )r   r`  rc   rT   rT   rU   r   w  r  zMatchedSubAppResource.__repr__)rO   rP   rQ   rL   r`   ry   rR   rf   rC   rr   r-   r|   rm   r   rT   rT   rT   rU   r}  ^  s    r}  c                       s   e Zd ZdZdddedeeee f de	de
e ddf
 fd	d
ZdefddZede
e fddZdededefddZdefddZ  ZS )r8   zA route with resourceNr   r   r   r   r   rY   c                   s   t  j||||d d S )Nr~   )r   r`   )r_   r   r   r   r   r   rT   rU   r`   ~  s   
zResourceRoute.__init__c                 C   s   dj | j| j| jdS )Nz3<ResourceRoute [{method}] {resource} -> {handler!r})r   r   r   )r   r   r   r   rc   rT   rT   rU   r     s   zResourceRoute.__repr__c                 C   s   | j d u rd S | j jS r[   )r   rX   rc   rT   rT   rU   rX     s   
zResourceRoute.namer   rh   c                 O   s    | j dusJ | j j|i |S r   )r   ri   r   rT   rT   rU   ri     s   zResourceRoute.url_forc                 C   s   | j d usJ | j  S r[   )r   rr   rc   rT   rT   rU   rr     r   zResourceRoute.get_info)rO   rP   rQ   __doc__rR   r   r%   r   r"   r3   r   r   r`   r   ry   rX   r   ri   rC   rr   r   rT   rT   r   rU   r8   {  s&    r8   c                       s   e Zd Zdeddf fddZdededefdd	Zede	e fd
dZ
defddZdedefddZedefddZedefddZdefddZ  ZS )r   rN   rY   Nc                    s   t  tj| j || _d S r[   )r   r`   r   r   r'  _http_exceptionr   r   rT   rU   r`     s   
zSystemRoute.__init__r   rh   c                 O   rc  )Nz).url_for() is not allowed for SystemRouterd  r   rT   rT   rU   ri     re  zSystemRoute.url_forc                 C   rs   r[   rT   rc   rT   rT   rU   rX     r   zSystemRoute.namec                 C   r   )NrN   r  rc   rT   rT   rU   rr     ra   zSystemRoute.get_inforj   c                    s   | j r[   r  rl   rT   rT   rU   r'    s   zSystemRoute._handlec                 C   r   r[   )r  r   rc   rT   rT   rU   r     r   zSystemRoute.statusc                 C   r   r[   )r  r   rc   rT   rT   rU   r     r   zSystemRoute.reasonc                 C   s   dj | dS )Nz*<SystemRoute {self.status}: {self.reason}>rc   )r   rc   rT   rT   rU   r        zSystemRoute.__repr__)rO   rP   rQ   r'   r`   rR   r   ri   ry   r   rX   rC   rr   r-   r/   r'  r   r   r   r   r   rT   rT   r   rU   r     s    r   c                   @   s@   e Zd ZdefddZdeedef fddZdefddZ	dS )	r:   rY   c                    sZ   | j jtjvr|   t| | j j d }|d u r|   | I d H }t|ts+J |S r[   )	rj   r   r   METH_ALL_raise_allowed_methodsgetattrr   r   r/   )r_   r   retrT   rT   rU   _iter  s   z
View._iterNc                 C   s   |    S r[   )r  	__await__rc   rT   rT   rU   r    r  zView.__await__c                    s"    fddt jD }t jj|)Nc                    s   h | ]}t  | r|qS rT   )hasattrr   )r  rF  rc   rT   rU   	<setcomp>  s    z.View._raise_allowed_methods.<locals>.<setcomp>)r   r  r*   rj   r   )r_   r   rT   rc   rU   r    s   zView._raise_allowed_methods)
rO   rP   rQ   r/   r  r   r   r  r   r  rT   rT   rT   rU   r:     s    r:   c                   @   sT   e Zd Zdee ddfddZdefddZdee fdd	Z	d
e
defddZdS )ResourcesViewrb  rY   Nc                 C   rZ   r[   
_resources)r_   rb  rT   rT   rU   r`     ra   zResourcesView.__init__c                 C   r   r[   )r   r  rc   rT   rT   rU   r     ra   zResourcesView.__len__c                 c       | j E d H  d S r[   r  rc   rT   rT   rU   r     r   zResourcesView.__iter__r   c                 C   
   || j v S r[   r  r_   r   rT   rT   rU   __contains__  ra   zResourcesView.__contains__)rO   rP   rQ   r   r3   r`   r   r   r   r   objectr}   r  rT   rT   rT   rU   r    s
    r  c                   @   sP   e Zd Zdee fddZdefddZdee	 fddZ
d	edefd
dZdS )
RoutesViewrb  c                 C   s*   g | _ |D ]}|D ]}| j | q	qd S r[   )r   r   )r_   rb  r   r   rT   rT   rU   r`     s   zRoutesView.__init__rY   c                 C   r   r[   r   rc   rT   rT   rU   r     ra   zRoutesView.__len__c                 c   r  r[   r   rc   rT   rT   rU   r     r   zRoutesView.__iter__r   c                 C   r  r[   r  r   rT   rT   rU   r    ra   zRoutesView.__contains__N)rO   rP   rQ   r   r3   r`   r   r   r   r7   r   r  r}   r  rT   rT   rT   rU   r    s
    r  c                       sv  e Zd ZedZdH fddZdedefddZ	de
e fd	d
ZdefddZdedefddZdedefddZdefddZdefddZdeeef fddZdeddfddZdddedee defddZdddd eded!eee e! f dee d"ee" de#fd#d$Z$ddd%d&d&d&d'd(ede%dee d"ee" d)ed*ed+ed,edefd-d.Z&ded!ed/e'de#fd0d1Z(ded!ed/e'de#fd2d3Z)dd4d5ded!edee d6ed/e'de#fd7d8Z*ded!ed/e'de#fd9d:Z+ded!ed/e'de#fd;d<Z,ded!ed/e'de#fd=d>Z-ded!ed/e'de#fd?d@Z.ded!e e! d/e'de#fdAdBZ/dH fdCdDZ0dEe1e2 de3e# fdFdGZ4  Z5S )Ir1   z[.:-]rY   Nc                    s   t    g | _i | _d S r[   )r   r`   r  _named_resourcesrc   r   rT   rU   r`     s   

zUrlDispatcher.__init__rj   c                    sb   |j }t }| jD ]}||I d H \}}|d ur|  S ||O }q
|r,tt||S tt S r[   )r   r   r  rm   r   r*   r+   )r_   rj   r   r   r   r   allowedrT   rT   rU   rm     s   


zUrlDispatcher.resolvec                 C   r   r[   )r   r  rc   rT   rT   rU   r   	  ra   zUrlDispatcher.__iter__c                 C   r   r[   )r   r  rc   rT   rT   rU   r     ra   zUrlDispatcher.__len__r   c                 C   r  r[   r  r  rT   rT   rU   r    ra   zUrlDispatcher.__contains__rX   c                 C   s
   | j | S r[   r  r^   rT   rT   rU   __getitem__  ra   zUrlDispatcher.__getitem__c                 C   r   r[   )r  r  rc   rT   rT   rU   rb    ra   zUrlDispatcher.resourcesc                 C   r   r[   )r  r  rc   rT   rT   rU   rI     ra   zUrlDispatcher.routesc                 C   r   r[   )r   r  rc   rT   rT   rU   named_resources  ra   zUrlDispatcher.named_resourcesc                 C   s   t |tsJ d|| jrtd|j}|d urR| j|}|D ]}t|r1t	d|d|
 s<t	d|q"|| jv rMt	d|| j| || j|< | j| d S )Nz4Instance of AbstractResource class is required, got z.Cannot register a resource into frozen router.zIncorrect route name z/, python keywords cannot be used for route namezoIncorrect route name {!r}, the name should be a sequence of python identifiers separated by dash, dot or columnz'Duplicate {!r}, already handled by {!r})r   r3   frozenr   rX   NAME_SPLIT_REr   keyword	iskeywordr   isidentifierr   r  r  r   )r_   r   rX   r  r   rT   rT   rU   register_resource  s8   



zUrlDispatcher.register_resourcerW   rD   c                C   s   |r| dstd| jr"| jd }|j|kr"||r"tt|S d|v s>d|v s>t|s>t	t
||d}| | |S t||d}| | |S )NrA   z)path should be started with / or be emptyr   r   rW   )r   r   r  rX   rw   r   r4   r@   searchr5   r   r  r6   )r_   rD   rX   r   rT   rT   rU   add_resource?  s   



zUrlDispatcher.add_resource)rX   r   r   r   r   c                C   s   | j ||d}|j|||dS )NrW   r   )r  r   )r_   r   rD   r   rX   r   r   rT   rT   rU   r   O  s   	zUrlDispatcher.add_router  Fr  rH   r  r  r  r  c          
   
   C   sJ   | dsJ |dr|dd }t||||||||d}	| |	 |	S )z^Add static files view.

        prefix - url prefix
        path - folder with files

        rA   Nr  r  )r   r   r9   r  )
r_   rH   rD   rX   r   r  r  r  r  r   rT   rT   rU   
add_static[  s   


zUrlDispatcher.add_staticrh   c                 K      | j tj||fi |S )z(Shortcut for add_route with method HEAD.)r   r   	METH_HEADr_   rD   r   rh   rT   rT   rU   add_head}     zUrlDispatcher.add_headc                 K   r  )z+Shortcut for add_route with method OPTIONS.)r   r   ZMETH_OPTIONSr  rT   rT   rU   add_options  r  zUrlDispatcher.add_optionsT)rX   
allow_headr  c                K   s>   | j ||d}|r|jtj|fi | |jtj|fi |S )zShortcut for add_route with method GET.

        If allow_head is true, another
        route is added allowing head requests to the same endpoint.
        rW   )r  r   r   r  ZMETH_GET)r_   rD   r   rX   r  rh   r   rT   rT   rU   add_get  s   zUrlDispatcher.add_getc                 K   r  )z(Shortcut for add_route with method POST.)r   r   Z	METH_POSTr  rT   rT   rU   add_post  r  zUrlDispatcher.add_postc                 K   r  )z'Shortcut for add_route with method PUT.)r   r   ZMETH_PUTr  rT   rT   rU   add_put  r  zUrlDispatcher.add_putc                 K   r  )z)Shortcut for add_route with method PATCH.)r   r   Z
METH_PATCHr  rT   rT   rU   	add_patch  r  zUrlDispatcher.add_patchc                 K   r  )z*Shortcut for add_route with method DELETE.)r   r   ZMETH_DELETEr  rT   rT   rU   
add_delete  r  zUrlDispatcher.add_deletec                 K   r  )z?Shortcut for add_route with ANY methods for a class-based view.)r   r   r   r  rT   rT   rU   add_view  s   zUrlDispatcher.add_viewc                    s"   t    | jD ]}|  qd S r[   )r   rt   r  r  r   rT   rU   rt     s   


zUrlDispatcher.freezerI   c                 C   s"   g }|D ]
}| ||  q|S )zAppend routes to route table.

        Parameter should be a sequence of RouteDef objects.

        Returns a list of registered AbstractRoute instances.
        )extendregister)r_   rI   Zregistered_routesZ	route_defrT   rT   rU   
add_routes  s   zUrlDispatcher.add_routesrx   )6rO   rP   rQ   r   r   r  r`   r-   r2   rm   r   rR   r   r   r   r  r}   r  r3   r  r  rb  r  rI   r   r  r  r   r4   r  r   r%   r   r"   r   r7   r   r&   r  r   r  r  r  r  r  r  r  r  rt   r   r0   r   r  r   rT   rT   r   rU   r1     s    
 !
	

"	

"r1   r  c                 C   s$   t dk r
| dd} tj| ddjS )Nr)  r+  r,  Fr   )r>   r/  r   r   r   r  rT   rT   rU   r    s   r  c                 C   s   t j| ddjS r   )r   r   rD   r  rT   rT   rU   r    r   r  c                 C   s    t | }d| v r|dd}|S )Nr+  r,  )r  r/  )r  r   rT   rT   rU   r     s   r   )trz   r   rB  r?  r   r  r  r   r   
contextlibr   	functoolsr   pathlibr   typesr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zyarlr   r   Zyarl_versionr   r   r    r!   r"   helpersr#   httpr$   Ztypedefsr%   r&   Zweb_exceptionsr'   r(   r)   r*   r+   Zweb_fileresponser,   Zweb_requestr-   Zweb_responser.   r/   Zweb_routedefr0   __all__Zweb_appr;   rR   ZBaseDictdictr   mapr   r   r>   rS   r   r?   r@   r   rB   r   r|   rC   r3   ABCr7   r2   r   r   r4   r5   r6   r  r9   r_  rL   ri  ry  r}  r8   r   r:   r  r  r1   r  r  r   rT   rT   rT   rU   <module>   s   
 `0/QC<)L Q,($ Q