a c[3@sddlZddlZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z ddl mZddlmZddlZddlmZddlmZdd lmZdd lmZmZdd lmZmZmZej d krdd lm!Z!n dd l"m!Z!dej#j$j%d<d#ej&e'dfe(ej)ej*e'ej*e'fdddZGdddZ+Gddde+Z,Gddde+Z-Gddde+Z.Gddde+Z/ej&e'e0fZ1ej2e1Z3ej4e1Z5ej&e5e3fZ6Gdd d e+Z7Gd!d"d"e+Z8dS)$N)datetime)format_datetime formatdate)partial) guess_type)quote) md5_hexdigest)BackgroundTask)iterate_in_threadpool)URLMutableHeaders)ReceiveScopeSend)LiteralSameSitesamesiteTos.PathLike[str])urlstrictreturncCstjdkrt|}t||S)Nr)sys version_infoosfspathmimetypes_guess_type)rrr`/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/starlette/responses.pyrs  rc @seZdZdZdZdejeejej e e feje eje ddddZ eje ddd Zdejej e e fdd d d Zeed ddZd e e ejeejejee efe eje eeejeddd ddZd!e e eje eeejedddddZeeeddddZdS)"ResponseNutf-8content status_codeheaders media_type backgroundrcCs4||_|dur||_||_|||_||dSN)r&r(r)renderbody init_headersselfr%r&r'r(r)rrr __init__+s  zResponse.__init__r%rcCs&|dur dSt|tr|S||jS)N) isinstancebytesencodecharsetr/r%rrr r+:s  zResponse.render)r'rc Cs|durg}d}d}n0dd|D}dd|D}d|v}d|v}t|dd}|dur|r|jdks|jd vstt|}|d|d f|j}|dur|r|d r|d |j 7}|d|d f||_ dS) NTcSs(g|] \}}|d|dfqS)latin-1)lowerr5).0kvrrr Isz)Response.init_headers..cSsg|] }|dqS)rr)r:hrrr r=Mr2scontent-lengths content-typer,r#)i0r8ztext/z ; charset=) itemsgetattrr&strlenappendr5r( startswithr6 raw_headers) r/r'rFZpopulate_content_lengthZpopulate_content_typekeysr,content_length content_typerrr r-As6    zResponse.init_headers)rcCst|dst|jd|_|jS)N_headers)raw)hasattrr rFrJ)r/rrr r'bs zResponse.headers/Flax)rOrnone) keyvaluemax_ageexpirespathdomainsecurehttponlyrrc Cstj} || |<|dur&|| |d<|durZt|trNt|dd| |d<n || |d<|durn|| |d<|dur|| |d<|rd| |d<|rd| |d<| dur| d vsJd | | |d <| jd d } |j d| dfdS)Nzmax-ageTusegmtrTrUrVrWrX)rrOrPz1samesite must be either 'strict', 'lax' or 'none'rrM)headers set-cookier8) httpcookies SimpleCookier3rrr9outputstriprFrDr5) r/rQrRrSrTrUrVrWrXrcookieZ cookie_valrrr set_cookiehs.         zResponse.set_cookie)rQrUrVrWrXrrc Cs|j|dd|||||ddS)Nr)rSrTrUrVrWrXr)rb)r/rQrUrVrWrXrrrr delete_cookies zResponse.delete_cookiescopereceivesendrcsL|d|j|jdIdH|d|jdIdH|jdurH|IdHdS)Nhttp.response.starttypestatusr'http.response.body)rjr,)r&rFr,r))r/rerfrgrrr __call__s  zResponse.__call__)Nr#NNN)N)rMNNrNNFFrO)rNNFFrO)__name__ __module__ __qualname__r(r6typingAnyintOptionalMappingrBr r0r4r+r-propertyr r'Unionrboolrrbrcrr rrmrrrr r!'st  !  *  r!c@seZdZdZdS) HTMLResponsez text/htmlNrnrorpr(rrrr rysryc@seZdZdZdS)PlainTextResponse text/plainNrzrrrr r{sr{csbeZdZdZd ejeejeje e feje eje ddfdd Z eje ddd Z ZS) JSONResponsezapplication/jsonr#Nr$cst|||||dSr*)superr0r. __class__rr r0szJSONResponse.__init__r1cCstj|ddddddS)NF),:) ensure_ascii allow_nanindent separatorsr")jsondumpsr5r7rrr r+szJSONResponse.render)r#NNN)rnrorpr(rqrrrsrtDictrBr r0r4r+ __classcell__rrrr r}s r}csLeZdZdejeefeejej eefeje ddfdd Z Z S)RedirectResponse3N)rr&r'r)rcs.tjd|||dtt|dd|jd<dS)Nr2)r%r&r'r)z:/%#?=@[]!$&'()*+,;)safelocation)r~r0rrBr')r/rr&r'r)rrr r0szRedirectResponse.__init__)rNN) rnrorprqrwrBr rsrtrur r0rrrrr rs rc@seZdZUeed<deeejej e e feje eje ddddZ e dddd Zedd d d Zee edd ddZdS)StreamingResponse body_iteratorr#Nr$cCsLt|tjr||_n t||_||_|dur2|jn||_||_||dSr*) r3rq AsyncIterablerr r&r(r)r-r.rrr r0s  zStreamingResponse.__init__)rfrcs |IdH}|ddkrqqdS)Nrjzhttp.disconnectr)r/rfmessagerrr listen_for_disconnects  z'StreamingResponse.listen_for_disconnect)rgrcsv|d|j|jdIdH|j2z83dHW}t|tsB||j}|d|ddIdHq 6|ddddIdHdS)NrhrirlTrjr, more_bodyr2F)r&rFrr3r4r5r6)r/rgchunkrrr stream_responses   z!StreamingResponse.stream_responserdc st4IdHTdddfdd }|t|j||t|j|IdHWdIdHqz1IdHsp0Y|jdur|IdHdS)Nz+typing.Callable[[], typing.Awaitable[None]])funcrcs|IdHjdSr*)Z cancel_scopecancel)rZ task_grouprr wraps z(StreamingResponse.__call__..wrap)anyioZcreate_task_groupZ start_soonrrrr))r/rerfrgrrrr rm s @ zStreamingResponse.__call__)r#NNN)rnrorpAsyncContentStream__annotations__ ContentStreamrsrqrtrurBr r0r rrrrrmrrrr rs  rc @seZdZdZdejedfeejej eefejeeje ejeeje j ejeedd ddZ e j dd d d Zeeedd d dZdS) FileResponseir#N attachmentr) rUr&r'r(r)filename stat_resultmethodcontent_disposition_typerc Cs||_||_||_|duo$|dk|_|durDt|p8|dpBd}||_||_|||jdurt |j} | |jkrd | | } nd | |j} |j d| ||_ |dur||dS)NHEADrr|z{}; filename*=utf-8''{}z{}; filename="{}"zcontent-disposition)rUr&ruppersend_header_onlyrr(r)r-rformatr' setdefaultrset_stat_headers) r/rUr&r'r(r)rrrrZcontent_disposition_filenamecontent_dispositionrrr r0s,     zFileResponse.__init__)rrcCsnt|j}t|jdd}t|jdt|j}t|dd}|jd||jd||jd|dS) NTrY-F)usedforsecurityzcontent-lengthz last-modifiedetag)rBst_sizerst_mtimerr5r'r)r/rrHZ last_modifiedZ etag_baserrrr rBs zFileResponse.set_stat_headersrdc s^|jdurxz&tjtj|jIdH}||Wn$tyTt d|jdYn$0|j }t |sxt d|jd|d|j |j dIdH|jr|dddd IdHntj|jd d IdH4IdHZ}d }|r||jIdH}t||jk}|d||d IdHqWdIdHq@1IdHs60Y|jdurZ|IdHdS) Nz File at path z does not exist.z is not a file.rhrirlr2Frrb)modeT)rrZ to_threadZrun_syncrstatrUrFileNotFoundError RuntimeErrorst_modeS_ISREGr&rFr open_fileread chunk_sizerCr)) r/rerfrgrrfilerrrrr rmLs<     : zFileResponse.__call__)r#NNNNNNr)rnrorprrqrwrBrsrtrur rrr0rrr rrmrrrr rs.   $ r)T)9 http.cookiesr\rrrrrqr email.utilsrr functoolsr mimetypesrr urllib.parserrZstarlette._compatrZstarlette.backgroundr Zstarlette.concurrencyr Zstarlette.datastructuresr r Zstarlette.typesr rrrrZtyping_extensionsr]Morsel _reservedrwrBrxTuplertr!ryr{r}rr4ZContentIteratorZSyncContentStreamrrrrrrrrr sJ             7