o xg;S@sddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZmZddlmZddlmZmZddlmZdd lmZdd lmZdd lmZdd lmZmZdd l m!Z!ddl"m#Z#ddl$m%Z%e%dej&Z'GdddeZ(Gddde)Z*GdddZ+Gddde+Z,Gddde+Z-Gddde-Z.Gddde,Z/Gdd d e/Z0Gd!d"d"e/Z1Gd#d$d$e,Z2Gd%d&d&e,Z3Gd'd(d(e,Z4Gd)d*d*e,Z5Gd+d,d,e,Z6Gd-d.d.e,Z7Gd/d0d0e,Z8Gd1d2d2e9Z:Gd3d4d4e,Z;dS)5N)Mapping)Header) responses)quoteurlparse)settings)signalssigning)DisallowedRedirect)DjangoJSONEncoder) SimpleCookie)timezone)CaseInsensitiveMapping!_destruct_iterable_mapping_values) iri_to_uri) http_date)_lazy_re_compilez ;\s*charset=(?P[^\s;]+)c@s@eZdZddZdddZddZdd Zdd d Zd dZd S)ResponseHeaderscCs@t|tsddt|D}i|_|D]\}}|||<qdS)zm Populate the initial data using __setitem__ to ensure values are correctly encoded. cSsi|]\}}||qSr).0kvrr6/usr/lib/python3/dist-packages/django/http/response.py $sz,ResponseHeaders.__init__..N) isinstancerr_storeitems)selfdataheadervaluerrr__init__s  zResponseHeaders.__init__Fc Cst|ttfs t|}t|trd|vs%d|vs%t|tr+d|vs%d|vr+td|zt|tr9||W|S||}W|Styj}z|rUt|dtj d}n |j d|7_ WYd }~|Sd }~ww) z Convert headers key/value to ascii/latin-1 native strings. `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and `value` can't be represented in the given charset, apply MIME-encoding.     z-Header values can't contain newlines (got %r)zutf-8) maxlinelenz,, HTTP response headers must be in %s formatN) rbytesstrBadHeaderErrorencodedecode UnicodeErrorrsysmaxsizereason)rr charset mime_encodeerrr_convert_to_charset)s0     z#ResponseHeaders._convert_to_charsetcC||dSN)pop)rkeyrrr __delitem__EzResponseHeaders.__delitem__cCs2||d}|j|ddd}||f|j|<dS)Nasciilatin-1T)r1)r3rlowerrr7r rrr __setitem__Hs zResponseHeaders.__setitem__NcCs|j||Sr5)rr6r<)rr7defaultrrrr6MszResponseHeaders.popcCs||vr |||<dSdSr5rr=rrr setdefaultP zResponseHeaders.setdefault)Fr5) __name__ __module__ __qualname__r!r3r8r>r6r@rrrrrs   rc@ eZdZdS)r)NrBrCrDrrrrr)Ur)c@seZdZdZdZd9ddZeddZejddZed d Z e jd d Z d d Z e Z eddZ ddZ ddZddZddZeZddZd:ddZ  d;dd Zd!d"ZdHttpResponseBasez An HTTP response base class with dictionary-accessed headers. This class doesn't handle content. It should not be used directly. Use the HttpResponse and StreamingHttpResponse subclasses instead. Nc Cst|pi|_||_|rd|jvrtdd|jvr(|dur#d|j}||jd<g|_d|_t|_d|_ |durazt ||_ Wn tt fyMt dwd|j kr\dksatdtd||_ dS) N Content-TypezX'headers' must not contain 'Content-Type' when the 'content_type' parameter is provided.ztext/html; charset=%sFz$HTTP status code must be an integer.diWz4HTTP status code must be an integer from 100 to 599.)rheaders_charset ValueErrorr0_resource_closers_handler_classr cookiesclosedint status_code TypeError_reason_phrase)r content_typestatusr/r0rLrrrr!cs2    zHttpResponseBase.__init__cCs|jdur|jSt|jdS)NzUnknown Status Code)rVrgetrTrrrr reason_phrases zHttpResponseBase.reason_phrasecC ||_dSr5)rVrr rrrr[ cCs@|jdur|jS|dd}t|}|r|dddStjS)NrJr0")rMrY_charset_from_content_type_researchreplacerDEFAULT_CHARSET)rrWmatchedrrrr0s   zHttpResponseBase.charsetcCr\r5)rMr]rrrr0r^cs*ddfdd|jD}d|S)zHTTP headers as a bytestring.cSst|tr|S||Sr5)rr'r*)valencodingrrrto_bytessz4HttpResponseBase.serialize_headers..to_bytescs(g|]\}}|dd|dqS)r:s: r;r)rr7r rhrr sz6HttpResponseBase.serialize_headers..s )rLrjoin)rrLrrirserialize_headerss   z"HttpResponseBase.serialize_headerscCsd|jvr d|jdSdS)NrJz, "%s"r_rLrZrrr_content_type_for_reprsz'HttpResponseBase._content_type_for_reprcCs||j|<dSr5rm)rrr rrrr>r9zHttpResponseBase.__setitem__cCs |j|=dSr5rmrrrrrr8 zHttpResponseBase.__delitem__cCs |j|Sr5rmrorrr __getitem__ zHttpResponseBase.__getitem__cCs ||jvS)z$Case-insensitive check for a header.rmrorrr has_headerr^zHttpResponseBase.has_headercCs |jSr5)rLrrZrrrrrrzHttpResponseBase.itemscCs|j||Sr5)rLrY)rr alternaterrrrYr9zHttpResponseBase.getr_/Fc CsB||j|<|dur?t|tjr7t|rt|tj}||} | tjdd} d}t d| j d| j }n||j|d<nd|j|d<|durbt ||j|d<|sbt t||j|d<|durm||j|d <|durx||j|d <|rd |j|d <|rd |j|d <| r| dvrtd| |j|d<dSdS)a+ Set a cookie. ``expires`` can be: - a string in the correct format, - a naive ``datetime.datetime`` object in UTC, - an aware ``datetime.datetime`` object in any time zone. If it is a ``datetime.datetime`` object then calculate ``max_age``. N)secondsriQexpiresr_zmax-agepathdomainTsecurehttponly)laxnonestrictz,samesite must be "lax", "none", or "strict".samesite)rQrdatetimer is_aware make_naiveutcutcnow timedeltamaxdaysrwrSrtimer<rN) rr7r max_agerxryrzr{r|rdeltarrr set_cookies8     zHttpResponseBase.set_cookiecCs|j||dS)z,Set a header unless it has already been set.N)rLr@r=rrrr@zHttpResponseBase.setdefaultcKs*tj||d|}|j||fi|S)N)salt)r get_cookie_signersignr)rr7r rkwargsrrrset_signed_cookiesz"HttpResponseBase.set_signed_cookiec Cs6|dp |o |dk}|j|d|||d|ddS)N)z __Secure-z__Host-r~rzThu, 01 Jan 1970 00:00:00 GMT)rryrzr{rxr) startswithr<r)rr7ryrzrr{rrr delete_cookies   zHttpResponseBase.delete_cookiecCs@t|ttfr t|St|trt||jSt||jS)z=Turn a value into a bytestring encoded in the output charset.)rr' memoryviewr(r*r0r]rrr make_bytess   zHttpResponseBase.make_bytesc CsL|jD]}z|WqtyYqw|jd|_tjj|jddS)NT)sender)rO ExceptionclearrRrrequest_finishedsendrP)rcloserrrrcloses    zHttpResponseBase.closecCtd|jjNz This %s instance is not writableOSError __class__rBrcontentrrrwrite&zHttpResponseBase.writecCsdSr5rrZrrrflush)zHttpResponseBase.flushcCr)Nz)This %s instance cannot tell its positionrrZrrrtell,rzHttpResponseBase.tellcCdSNFrrZrrrreadable2rzHttpResponseBase.readablecCrrrrZrrrseekable5rzHttpResponseBase.seekablecCrrrrZrrrwritable8rzHttpResponseBase.writablecCrrr)rlinesrrr writelines;rzHttpResponseBase.writelines)NNNNNr5)r_NNruNFFN)r_)ruNN)!rBrCrD__doc__rTr!propertyr[setterr0rl __bytes__rnr>r8rqrs __contains__rrYrr@rrrrrrrrrrrrrrrrHYsL         .    rHcseZdZdZdZdfdd ZddZdd ZeZe d d Z e j d d Z d dZ ddZ ddZddZddZddZZS) HttpResponsezs An HTTP response class with a string as content. This content can be read, appended to, or replaced. Fctj|i|||_dSr5)superr!r)rrargsrrrrr!Hs zHttpResponse.__init__cCsd|jj|j|jdS)Nz5<%(cls)s status_code=%(status_code)d%(content_type)s>)clsrTrWrrBrTrnrZrrr__repr__Ms zHttpResponse.__repr__cCs|d|jS)z6Full HTTP message, including headers, as a bytestring.s )rlrrZrrr serializeTrzHttpResponse.serializecC d|jSNr)rk _containerrZrrrrZs zHttpResponse.contentcstt|dr/t|tttfs/dfdd|D}t|dr.z|Wnty-Ynwn|}|g_ dS)N__iter__rc3s|]}|VqdSr5)r)rchunkrZrr esz'HttpResponse.content..r) hasattrrr'rr(rkrrrr)rr rrrZrr^s     cC t|jSr5)iterrrZrrrrprrzHttpResponse.__iter__cCs|j||dSr5)rappendrrrrrrsszHttpResponse.writecCrr5)lenrrZrrrrvrrzHttpResponse.tellcC|jSr5)rrZrrrgetvalueyzHttpResponse.getvaluecCr)NTrrZrrrr|rzHttpResponse.writablecCs|D]}||qdSr5)r)rrlinerrrrrAzHttpResponse.writelines)r)rBrCrDr streamingr!rrrrrrrrrrrr __classcell__rrrrr?s"  rcsdeZdZdZdZdfdd ZeddZedd Zej d d Zd d Z d dZ ddZ Z S)StreamingHttpResponsea$ A streaming HTTP response class with an iterator as content. This should only be iterated once, when the response is streamed to the client. However, it can be appended to or replaced with a new iterator that wraps the original content (or yields entirely new content). Trcrr5)rr!streaming_content)rrrrrrrr!s zStreamingHttpResponse.__init__cCr)NzMThis %s instance has no `content` attribute. Use `streaming_content` instead.)AttributeErrorrrBrZrrrrs zStreamingHttpResponse.contentcCst|j|jSr5)mapr _iteratorrZrrrrz'StreamingHttpResponse.streaming_contentcCr4r5)_set_streaming_contentr]rrrrrcCs*t||_t|dr|j|jdSdS)Nr)rrrrOrrr]rrrrs  z,StreamingHttpResponse._set_streaming_contentcCrr5)rrZrrrrrzStreamingHttpResponse.__iter__cCrr)rkrrZrrrrrpzStreamingHttpResponse.getvalue)r)rBrCrDrrr!rrrrrrrrrrrrrs   rcs@eZdZdZdZdddfdd Zfdd Zd d ZZS) FileResponsez> A streaming HTTP response class optimized for files. iFr_) as_attachmentfilenamecs"||_||_tj|i|dSr5)rrrr!)rrrrrrrrr!szFileResponse.__init__cslt|dsd_t|S|_tdrjjtfddd}t|dS)Nreadrcs jSr5)r block_sizerfilelikerrrs z5FileResponse._set_streaming_content..r) rfile_to_streamrrrOrrr set_headersr]rrrrs     z#FileResponse._set_streaming_contentcCsTdddd}t|dd}t|tr|r|n|j}tj|r)tj|j|j d<n t |dr6| j |j d<|j d d d r\|rWt|\}}| ||}|pRd |j d <nd |j d <|jpdtj|}|r|jrld nd}z|dd|dddd}Wntydt|}Ynwd|||j d<dS|jrd |j d<dSdS)z Set some common response headers (Content-Length, Content-Type, and Content-Disposition) based on the `filelike` response content. zapplication/x-bzipzapplication/gzipzapplication/x-xz)bzip2gzipxznameNzContent-Length getbufferrJr_z text/htmlzapplication/octet-stream attachmentinliner:z filename="{}"\z\\r`z\"zfilename*=utf-8''{}z{}; {}zContent-Disposition)getattrrr(rosryisabsgetsizerrLrrnbytesrYr mimetypes guess_typebasenamerr*formatrcUnicodeEncodeErrorr)rr encoding_maprrWrg disposition file_exprrrrrs>       zFileResponse.set_headers) rBrCrDrrr!rrrrrrrrs   rcs8eZdZgdZfddZeddZddZZS)HttpResponseRedirectBase)httphttpsftpcsRtj|i|t||d<tt|}|jr%|j|jvr'td|jdSdS)NLocationz)Unsafe redirect to URL with protocol '%s')rr!rrr(schemeallowed_schemesr )r redirect_torrparsedrrrr!s   z!HttpResponseRedirectBase.__init__cCs|dS)NrrrZrrrrsz!HttpResponseRedirectBase.cCsd|jj|j|j|jdS)NzD<%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s">)rrTrWurl)rrBrTrnrrZrrrrs z!HttpResponseRedirectBase.__repr__) rBrCrDrr!rrrrrrrrrs   rc@eZdZdZdS)HttpResponseRedirecti.NrBrCrDrTrrrrrrc@r)HttpResponsePermanentRedirecti-Nrrrrrr rrcs0eZdZdZfddZejjddZZS)HttpResponseNotModifiedi0cstj|i||d=dS)Nz content-type)rr!)rrrrrrr!s z HttpResponseNotModified.__init__cCs|rtdg|_dS)Nz7You cannot set content to a 304 (Not Modified) response)rrr]rrrrs zHttpResponseNotModified.content) rBrCrDrTr!rrrrrrrrrs  rc@r)HttpResponseBadRequestiNrrrrrrrrc@r)HttpResponseNotFoundiNrrrrrr!rrc@r)HttpResponseForbiddeniNrrrrrr %rr cs(eZdZdZfddZddZZS)HttpResponseNotAllowedics$tj|i|d||d<dS)Nz, Allow)rr!rk)rpermitted_methodsrrrrrr!,szHttpResponseNotAllowed.__init__cCsd|jj|j|j|ddS)NzC<%(cls)s [%(methods)s] status_code=%(status_code)d%(content_type)s>r )rrTrWmethodsrrZrrrr0s zHttpResponseNotAllowed.__repr__)rBrCrDrTr!rrrrrrr )s r c@r)HttpResponseGoneiNrrrrrr9rrc@r)HttpResponseServerErroriNrrrrrr=rrc@rE)Http404NrFrrrrrArGrcs(eZdZdZeddffdd ZZS) JsonResponseaH An HTTP response class that consumes data to be serialized to JSON. :param data: Data to be dumped into json. By default only ``dict`` objects are allowed to be passed due to a security flaw before EcmaScript 5. See the ``safe`` parameter for more information. :param encoder: Should be a json encoder class. Defaults to ``django.core.serializers.json.DjangoJSONEncoder``. :param safe: Controls if only ``dict`` objects may be serialized. Defaults to ``True``. :param json_dumps_params: A dictionary of kwargs passed to json.dumps(). TNc s^|r t|ts td|duri}|ddtj|fd|i|}tjdd|i|dS)NzTIn order to allow non-dict objects to be serialized set the safe parameter to False.rWzapplication/jsonrrr)rdictrUr@jsondumpsrr!)rrencodersafejson_dumps_paramsrrrrr!Ss zJsonResponse.__init__)rBrCrDrr r!rrrrrrEs  r)sR            8gE-A