a c{@szddlZddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlmZmZddlmZmZmZddlmZdd lmZdd lmZdd lmZmZdd lm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&Gddde'Z(Gddde Z)ej*e+dddZ,ej-e dddZ.ej-e dddZ/ej-e0dddZ1e0ej2e0efej2e0e0fej3e0e4fdddZ5e6d Z7e0ej3ej8e0ej2e0effd!d"d#Z9Gd$d%d%Z:Gd&d'd'e:Z;Gd(d)d)e:Ze?d.Z@Gd/d0d0ejAe@ZBej-ej*gejCfej-ej*gejAfd1d2d3ZDGd4d5d5ZEGd6d7d7ZFdS)8N)asynccontextmanager)Enum)is_async_callable)run_in_threadpool)CONVERTOR_TYPES Convertor)URLHeadersURLPath) HTTPException) Middleware)Request)PlainTextResponseRedirectResponse)ASGIAppReceiveScopeSend) WebSocketWebSocketClosecs6eZdZdZeejeejfddfdd ZZ S) NoMatchFoundz} Raised by `.url_for(name, **path_params)` and `.url_path_for(name, **path_params)` if no matching route exists. Nname path_paramsreturncs0dt|}td|d|ddS)N, zNo route exists for name "z" and params "z".)joinlistkeyssuper__init__)selfrrparams __class__^/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/starlette/routing.pyr szNoMatchFound.__init__) __name__ __module__ __qualname____doc__strtypingDictAnyr __classcell__r%r%r#r&rsrc@seZdZdZdZdZdS)MatchrN)r'r(r)NONEPARTIALFULLr%r%r%r&r0#sr0)objrcCs*tdtt|tjr |j}q t|S)z~ Correctly determines if an object is a coroutine function, including those wrapped in functools.partial objects. zViscoroutinefunction_or_partial is deprecated, and will be removed in a future release.) warningswarnDeprecationWarning isinstance functoolspartialfuncinspectiscoroutinefunction)r6r%r%r&iscoroutinefunction_or_partial)s r@r=rcs&ttttddfdd }|S)zi Takes a function or coroutine `func(request) -> response`, and returns an ASGI application. NscopereceivesendrcsHt|||d}r"|IdH}nt|IdH}||||IdHdSN)rDrE)r r)rCrDrErequestresponser=Z is_coroutiner%r&app?s zrequest_response..app)rrrrr=rJr%rIr&request_response8srLcstttddfdd }|S)zM Takes a coroutine `func(session)`, and returns an ASGI application. NrBcs t|||d}|IdHdSrF)r)rCrDrEsessionr=r%r&rJPszwebsocket_session..app)rrrrKr%rNr&websocket_sessionJsrO)endpointrcCs"t|st|r|jS|jjSN)r> isroutineisclassr'r$)rPr%r%r&get_nameWsrT)pathparam_convertorsrrcCs^t|D]H\}}d|d|vr ||}||}|d|d|}||q ||fS)N{})ritemsZ to_stringreplacepop)rUrVrkeyvalue convertorr%r%r&replace_params]s  r_z4{([a-zA-Z_][a-zA-Z0-9_]*)(:[a-zA-Z_][a-zA-Z0-9_]*)?})rUrcCs|d }d}d}t}d}i}t|D]}|d\}} | d} | tvs`Jd| dt| } |t||| 7}|d |d | j d 7}|||| 7}|d |7}||vr| || ||<| }q,|r"d t|} t|dkrdnd} td| d| d||rR||ddd} |t| d7}n|t||dd7}|||d7}t|||fS)a* Given a path string, like: "/{username:str}", or a host string, like: "{subdomain}.mydomain.org", return a three-tuple of (regex, format, {param_name:convertor}). regex: "/(?P[^/]+)" format: "/{username}" convertors: {"username": StringConvertor()} /^rr+:zUnknown path convertor ''z(?P<>)z{%s}rr1szDuplicated param name z at path N$) startswithset PARAM_REGEXfinditergroupslstriprreescapestartregexaddendrsortedlen ValueErrorsplitcompile)rUZis_host path_regex path_formatZduplicated_paramsidxrVmatch param_nameZconvertor_typer^namesendinghostnamer%r%r& compile_pathos@      rc@sbeZdZeejeefdddZeej e dddZ ee e ddd d Zee e ddd d ZdS) BaseRouterCrcCs tdSrQNotImplementedError)r!rCr%r%r&matchesszBaseRoute.matchesrcKs tdSrQr)r!rrr%r%r& url_path_forszBaseRoute.url_path_forNrBcs tdSrQrr!rCrDrEr%r%r&handleszBaseRoute.handlecs||\}}|tjkrl|ddkrDtddd}||||IdHn$|ddkrht}||||IdHdS||||||IdHdS)z A route may be used in isolation as a stand-alone ASGI app. This is a somewhat contrived case, as they'll almost always be used within a Router, but could be useful for some tooling and minimal apps. typehttp Not Found status_codeN websocket)rr0r3rrupdater)r!rCrDrEr~ child_scoperHwebsocket_closer%r%r&__call__s     zBaseRoute.__call__)r'r(r)rr,Tupler0rr+r.r rrrrrr%r%r%r&rsrc@seZdZddddeejejejeejeeddddZ e ej e e fddd Z eejed d d Ze eedd ddZejedddZedddZdS)RouteNTmethodsrinclude_in_schemarUrPrrrrcCs|dsJd||_||_|dur.t|n||_||_|}t|tjrR|j }q>t |sft |rt ||_|durdg}n||_|durd|_n&dd|D|_d|jvr|jdt|\|_|_|_dS)Nr` Routed paths must start with '/'GETcSsh|] }|qSr%)upper).0methodr%r%r& z!Route.__init__..HEAD)rjrUrPrTrrr:r;r<r=r> isfunctionismethodrLrJrrtrr{r|rV)r!rUrPrrrendpoint_handlerr%r%r&r s&     zRoute.__init__rcCs|ddkr|j|d}|r|}|D]\}}|j||||<q0t|di}|||j |d}|j r|d|j vrt j |fSt j |fSt jifS)NrrrUrrPrr)r{r~ groupdictrYrVconvertdictgetrrPrr0r4r5r3r!rCr~matched_paramsr\r]rrr%r%r&rs     z Route.matchesrcKs^t|}t|j}||jks,||kr6t||t|j|j|\}}|rRJt|ddS)NrrUprotocolrkrrVrrr_r|r r!rrZ seen_paramsZexpected_paramsrUremaining_paramsr%r%r&rs   zRoute.url_path_forrBcst|jr\|d|jvr\dd|ji}d|vr:td|dntdd|d}||||IdHn||||IdHdS)NrZAllowrrJi)rheaderszMethod Not Allowed)rrr rrJ)r!rCrDrErrHr%r%r&r sz Route.handleotherrcCs.t|to,|j|jko,|j|jko,|j|jkSrQ)r:rrUrPrr!rr%r%r&__eq__s    z Route.__eq__rcCs@|jj}t|jpg}|j|j}}|d|d|d|dS)N(path=, name=z , methods=rf)r$r'rvrrUr)r! class_namerrUrr%r%r&__repr__szRoute.__repr__)r'r(r)r+r,CallableOptionalListboolr rrr0rr.r rrrrrrr%r%r%r&rs  $  rc@seZdZddeejejeddddZeej e efdddZ eej e d d d Zeeedd d dZej edddZedddZdS)WebSocketRouteNrrUrPrrcCs|dsJd||_||_|dur.t|n||_|}t|tjrL|j}q8t |s`t |rlt ||_ n||_ t|\|_|_|_dS)Nr`r)rjrUrPrTrr:r;r<r=r>rrrOrJrr{r|rV)r!rUrPrrr%r%r&r &s  zWebSocketRoute.__init__rcCs|ddkr~|j|d}|r~|}|D]\}}|j||||<q0t|di}|||j |d}t j |fSt j ifS)NrrrUrr) r{r~rrYrVrrrrrPr0r5r3rr%r%r&r:s    zWebSocketRoute.matchesrcKs^t|}t|j}||jks,||kr6t||t|j|j|\}}|rRJt|ddS)Nrrrrr%r%r&rGs   zWebSocketRoute.url_path_forrBcs||||IdHdSrQrJrr%r%r&rTszWebSocketRoute.handlercCs"t|to |j|jko |j|jkSrQ)r:rrUrPrr%r%r&rWs    zWebSocketRoute.__eq__rcCs|jjd|jd|jdS)Nrrrf)r$r'rUrr!r%r%r&r^szWebSocketRoute.__repr__)r'r(r)r+r,rrr rrr0rr.r rrrrrrrr%r%r%r&r%s   rc @seZdZdddeejeejejeejeejeje ddddZ e ej edddZ eejeefd d d Zeejed d dZeeeddddZejedddZedddZdS)MountN) middleware)rUrJroutesrrrcCs|dks|dsJd|dus2|dus2Jd|d|_|durN||_n t|d|_|j|_|durt|D]\}}|fd|ji||_qr||_t|jd\|_ |_ |_ dS)Nrbr`rz0Either 'app=...', or 'routes=' must be specified)rrJz /{path:path}) rjrstriprU _base_appRouterrJreversedrrr{r|rV)r!rUrJrrrclsoptionsr%r%r&r cs&   zMount.__init__rcCst|jdgSNr)getattrrrr%r%r&r~sz Mount.routesrc Cs|ddvr|d}|j|}|r|}|D]\}}|j||||<q4d|d}|dt| }t| di} | || dd} | | d| | |||j d } t j | fSt jifS) NrrrrUr`r root_pathrb app_root_path)rrrrUrP)r{r~rrYrVrr[rwrrrrJr0r5r3) r!rCrUr~rr\r]Zremaining_pathZ matched_pathrrrr%r%r&rs(      z Mount.matchesrc Ks2|jdurR||jkrRd|vrR|dd|d<t|j|j|\}}|sPt|dSn|jdusn||jdr$|jdur~|}n|t|jdd}|d}d|d<t|j|j|\}}|dur||d<|j pgD]N}z4|j |fi|} t| dt | | j dWSty Yq0qt||dS)NrUr`)rUrcr1rbr)rror_r|rVr rjrwrrrrr+rr) r!rrrUrremaining_nameZ path_kwargZ path_prefixrouteurlr%r%r&rs6      zMount.url_path_forrBcs||||IdHdSrQrrr%r%r&rsz Mount.handlercCs"t|to |j|jko |j|jkSrQ)r:rrUrJrr%r%r&rs    z Mount.__eq__cCs2|jj}|jpd}|d|jd|d|jdS)Nrbrr, app=rf)r$r'rrUrJr!rrr%r%r&rs zMount.__repr__)NNN)r'r(r)r+r,rrSequencerr r propertyrrrrr0rr.r rrrrrrrr%r%r%r&rbs( !rc@seZdZdeeejeddddZeej e dddZ e ej ee fdd d Zeejed d d Ze eeddddZejedddZedddZdS)HostNhostrJrrcCs<|drJd||_||_||_t|\|_|_|_dS)Nr`zHost must not start with '/')rjrrJrr host_regex host_formatrV)r!rrJrr%r%r&r s z Host.__init__rcCst|jdgSr)rrJrr%r%r&rsz Host.routesrc Cs|ddvrt|d}|dddd}|j|}|r|}|D]\}}|j||||<qLt |di}| |||j d } t j | fSt jifS) NrrrCrrbrcrr)rrP)r rryrr~rrYrVrrrrJr0r5r3) r!rCrrr~rr\r]rrr%r%r&rs      z Host.matchesrc Ks|jdurL||jkrLd|vrL|d}t|j|j|\}}|st||dSn|jdusf||jdr|jdurv|}n|t|jdd}t|j|j|\}}|jpgD]D}z,|j |fi|}tt ||j |dWSt yYq0qt ||dS)NrU)rUrrcr1)rUrr) rr[r_rrVr rjrwrrr+rr) r!rrrUrrrrrr%r%r&rs*     zHost.url_path_forrBcs||||IdHdSrQrrr%r%r&rsz Host.handlercCs"t|to |j|jko |j|jkSrQ)r:rrrJrr%r%r&rs    z Host.__eq__cCs2|jj}|jpd}|d|jd|d|jdS)Nrbz(host=rrrf)r$r'rrrJrr%r%r&r s z Host.__repr__)N)r'r(r)r+rr,rr rrrrrrr0rr.r rrrrrrrr%r%r%r&rs  r_Tc@sbeZdZejedddZedddZejej e eje eje j eje ddd Zd S) _AsyncLiftContextManager)cmcCs ||_dSrQ)_cm)r!rr%r%r&r sz!_AsyncLiftContextManager.__init__rcs |jSrQ)r __enter__rr%r%r& __aenter__sz#_AsyncLiftContextManager.__aenter__)exc_type exc_value tracebackrcs|j|||SrQ)r__exit__)r!rrrr%r%r& __aexit__sz"_AsyncLiftContextManager.__aexit__N)r'r(r)r,ContextManagerrr rrType BaseExceptiontypes TracebackTyperrr%r%r%r&rs r)lifespan_contextrcs.t|ttjtdfdd }|S)N)rJrcs t|SrQ)rrZcmgrr%r&wrapper*sz+_wrap_gen_lifespan_context..wrapper) contextlibcontextmanagerr;wrapsr,r.r)rrr%rr&_wrap_gen_lifespan_context%s rc@sJeZdZddddZddddZedd d d Zeeed d dZdS)_DefaultLifespanr)routercCs ||_dSrQ)_router)r!rr%r%r&r 2sz_DefaultLifespan.__init__Nrcs|jIdHdSrQ)rstartuprr%r%r&r5sz_DefaultLifespan.__aenter__)exc_inforcs|jIdHdSrQ)rshutdown)r!rr%r%r&r8sz_DefaultLifespan.__aexit__)r!rJrcCs|SrQr%)r!rJr%r%r&r;sz_DefaultLifespan.__call__) r'r(r)r robjectrrrr%r%r%r&r1src @seZdZd0ejejeeejeejejej ejejej ejej ej gej fddddZ e eeddddZeej ed d d Zdd d dZdd ddZe eeddddZe eeddddZej edddZd1eeejeddddZd2eeejeddddZd3eej ejejeejeedddd Zd4eej ejedd!d"d#Zd5eejejeejeeej d$d%d&Zd6eejeej d'd(d)Zeej dd*d+d,Z eej d-d.d/Z!dS)7rNT)rredirect_slashesdefault on_startup on_shutdownlifespanrcCs|dur gnt||_||_|dur*|jn||_|durisasyncgenfunctionr7r8r9risgeneratorfunctionr)r!rrrrrrr%r%r&r @s2   zRouter.__init__rBcs^|ddkr(t}||||IdHdSd|vr>> routes = [Route(path, endpoint=...), ...] >>> app = Starlette(routes=routes) zThe `route` decorator is deprecated, and will be removed in version 1.0.0.Refer to https://www.starlette.io/routing/#http-routing for the recommended approach.rAcsj|d|S)Nr)rrNrrrrUr!r%r& decoratorszRouter.route..decoratorr7r8r9r,r)r!rUrrrrr%rr&rs  z Router.route)rUrrcs,tdttjtjdfdd }|S)a We no longer document this decorator style API, and its usage is discouraged. Instead you should use the following approach: >>> routes = [WebSocketRoute(path, endpoint=...), ...] >>> app = Starlette(routes=routes) zThe `websocket_route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/routing/#websocket-routing for the recommended approach.rAcsj|d|S)Nr)rrNrrUr!r%r&r7sz)Router.websocket_route..decoratorr)r!rUrrr%rr&websocket_route's zRouter.websocket_route) event_typer=rcCs2|dvs J|dkr"|j|n |j|dS)N)rrr)rrr)r!rr=r%r%r&add_event_handler=s zRouter.add_event_handler)rrcs*tdttjtjdfdd }|S)NzThe `on_event` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/events/#registering-events for recommended approach.rAcs||SrQ)rrNrr!r%r&rNs z"Router.on_event..decoratorr)r!rrr%rr&on_eventGs zRouter.on_event)NTNNNN)N)N)NNT)N)NNT)N)"r'r(r)r,rrrrrrr.AsyncContextManagerr rrrrr+r rrrrrrrrrrrrrrrr%r%r%r&r?s +  4       !   r)Grr;r>rprrr,r7renumrZstarlette._utilsrZstarlette.concurrencyrZstarlette.convertorsrrZstarlette.datastructuresrr r Zstarlette.exceptionsr Zstarlette.middlewarer Zstarlette.requestsr Zstarlette.responsesrrZstarlette.typesrrrrZstarlette.websocketsrr Exceptionrr0r.rr@rrLrOr+rTr-rrr_rzrlPatternrrrrrrTypeVarrrr Generatorrrrr%r%r%r&s\              :^=hH