o ]Lb Y@sddlmZddlmZddlmZmZmZmZddl m Z Gddde Z ej dd Gd d d e Zdd dZGddde ZGddde ZddZd S))absolute_import)attr)encodingerrorpycompatutil)urlutilc@sjeZdZdZddZddZddZdd Zd d Zd d Z dddZ ddZ ddZ ddZ ddZdS) multidictzA dict like object that can store multiple values for a key. Used to store parsed request parameters. This is inspired by WebOb's class of the same name. cCs i|_dSN_itemsselfr9/usr/lib/python3/dist-packages/mercurial/hgweb/request.py__init__! zmultidict.__init__cCs|j|dS)z%Returns the last set value for a key.r rkeyrrr __getitem__$zmultidict.__getitem__cCs|g|j|<dS)z,Replace a values for a key with a new value.Nr rrvaluerrr __setitem__(szmultidict.__setitem__cCs |j|=dS)zDelete all values for a key.Nr rrrr __delitem__,s zmultidict.__delitem__cCs ||jvSr r rrrr __contains__0rzmultidict.__contains__cCs t|jSr )lenr rrrr__len__3rzmultidict.__len__NcCs$z||WSty|YSwr )rKeyError)rrdefaultrrrget6s   z multidict.getcCs|j|g|dS)zQsz+multidict.asdictoflists..)r iteritemsr rrrr asdictoflistsPszmultidict.asdictoflistsr )__name__ __module__ __qualname____doc__rrrrrrr"r%r&r)r1rrrrr s  r T)frozenc@seZdZdZeZeZeZeZ eZ eZ eZ eZ eZeZeZeZeZeZeZeZeZdS) parsedrequestztRepresents a parsed WSGI request. Contains both parsed parameters as well as a handle on the input stream. N)r2r3r4r5ribmethodurlbaseurl advertisedurladvertisedbaseurl urlscheme remoteuser remotehostapppath dispatchparts dispatchpathreponame querystringqsparamsheadersbodyfhrawenvrrrrr7Ts& r7Ncstjrddfddt|D}|s|d}|r!t|}|dd}|r2|jr2|jd}n|}dd }|d rD||d 7}n ||d 7}|||d }|r|jr||j7}|jrb|j}n|jd krm|jsmd}n|jdkrx|jsxd}n|d }|||}n ||d 7}|||d }|}|} |t j |dd7}|t j |dd7}|r|j pd} | r| dsd| } |t j | 7}n |t j |dd7}|t j |dd7}|dr|d|d7}|d|d7}|r |j pd} | r| dsd| } n|dd} |rjd|d} |ds$td|d | s7td|d|f|dt| d} | rU| dsUtd|d|f| d| } | dd}d|} n&d|vr|ddr|ddd}d|} n g}d} ng}d} |dd}t}t j j|ddD] \}}|||qg}t|D]\}}| dr||tdddd|fqd d!lm}||}d"|vrd#|vr|d"|d$<d%|vrd&|vr|d%|d'<|dur|d(}d$|vrt |t|d$pd)}td>id*|d+d,|d-|d.|d/| d0|dd1|d2d3|d4d5| d6|d7| d8|d9|d:|d;|d<|d=|S)?aParse URL components from environment variables. WSGI defines request attributes via environment variables. This function parses the environment variables into a data structure. If ``reponame`` is defined, the leading path components matching that string are effectively shifted from ``PATH_INFO`` to ``SCRIPT_NAME``. This simulates the world view of a WSGI application that processes requests from the base URL of a repo. If ``altbaseurl`` (typically comes from ``web.baseurl`` config option) is defined, it is used - instead of the WSGI environment variables - for constructing URL components up to and including the WSGI application path. For example, if the current WSGI application is at ``/repo`` and a request is made to ``/rev/@`` with this argument set to ``http://myserver:9000/prefix``, the URL and path components will resolve as if the request were to ``http://myserver:9000/prefix/rev/@``. In other words, ``wsgi.url_scheme``, ``SERVER_NAME``, ``SERVER_PORT``, and ``SCRIPT_NAME`` are all effectively replaced by components from this URL. ``bodyfh`` can be used to specify a file object to read the request body from. If not defined, ``wsgi.input`` from the environment dict is used. cSs(t|ts|Stjrt|St|Sr ) isinstancestrr iswindowsr strtolocalfsencode)srrrtobytess   z$parserequestfromenv..tobytescsi|] \}}||qSrrr+rPrrr/sz'parserequestfromenv..s REPO_NAMEswsgi.url_schemes://cSs:|dr|dkr|d|7}|S|dkr|d|7}|S)Nshttps://443:80) startswith)rOportrrraddports   z$parserequestfromenv..addports HTTP_HOSTs SERVER_NAMEs SERVER_PORTshttprTshttpsrRs SCRIPT_NAMEs PATH_INFO/s QUERY_STRING?sreponame requires PATH_INFOs0PATH_INFO does not begin with repo name: %s (%s)NsDreponame prefix of PATH_INFO does not end at path delimiter: %s (%s)T)keep_blank_valuessHTTP__-r' wsgiheaderssCONTENT_LENGTHsHTTP_CONTENT_LENGTHContent-Lengths CONTENT_TYPEsHTTP_CONTENT_TYPEs Content-Types wsgi.input0r9sREQUEST_METHODr:r;r<r=r>r?s REMOTE_USERr@s REMOTE_HOSTrArBrCrDrErFrGrHrIr) rispy3r0r"r r:schemehostrVrurlreqquotepathrUstriprProgrammingErrorrrstripsplitjoinr parseqslr%r$replacer_Headers cappedreaderintr7)envrD altbaseurlrHfullurladvertisedfullurlrWrVr;r=rgrA repoprefixrCrBrErFr-r.rGr_rrQrparserequestfromenvs#                         "             rxc@s0eZdZdZddZddZddZdd Zd S) offsettrackingwriteraA file object like object that is append only and tracks write count. Instances are bound to a callable. This callable is called with data whenever a ``write()`` is attempted. Instances track the amount of written data so they can answer ``tell()`` requests. The intent of this class is to wrap the ``write()`` function returned by a WSGI ``start_response()`` function. Since ``write()`` is a callable and not a file object, it doesn't implement other file object methods. cCs||_d|_dS)Nr)_write_offset)rwritefnrrrrws zoffsettrackingwriter.__init__cCs:||}|dur|jt|7_dS|j|7_dSr )rzr{r)rrOresrrrwrite{s zoffsettrackingwriter.writecCsdSr rrrrrflushszoffsettrackingwriter.flushcCs|jSr )r{rrrrtellszoffsettrackingwriter.tellN)r2r3r4r5rr~rrrrrrryis   ryc@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS) wsgiresponseaPRepresents a response to a WSGI request. A response consists of a status line, headers, and a body. Consumers must populate the ``status`` and ``headers`` fields and make a call to a ``setbody*()`` method before the response can be issued. When it is time to start sending the response over the wire, ``sendresponse()`` is called. It handles emitting the header portion of the response message. It then yields chunks of body data to be written to the peer. Typically, the WSGI application itself calls and returns the value from ``sendresponse()``. cCsL||_||_d|_ddlm}|g|_d|_d|_d|_ d|_ d|_ dS)zCreate an empty response tied to a specific request. ``req`` is a ``parsedrequest``. ``startresponse`` is the ``start_response`` function passed to the WSGI application. Nr'r^F) _req_startresponsestatusror_rprG _bodybytes_bodygen_bodywillwrite_started _bodywritefn)rreq startresponser_rrrrs   zwsgiresponse.__init__cCs(|jdus |jdus |jrtddS)Ns!cannot define body multiple times)rrrrrirrrr _verifybodys   zwsgiresponse._verifybodycCs$|||_dt||jd<dS)ztDefine the response body as static bytes. The empty string signals that there is no response body. s%dr`N)rrrrG)rbrrr setbodybytesszwsgiresponse.setbodybytescCs|||_dS)z1Define the response body as a generator of bytes.N)rr)rgenrrr setbodygens zwsgiresponse.setbodygencCs|d|_dS)aSignal an intent to use write() to emit the response body. **This is the least preferred way to send a body.** It is preferred for WSGI applications to emit a generator of chunks constituting the response body. However, some consumers can't emit data this way. So, WSGI provides a way to obtain a ``write(data)`` function that can be used to synchronously perform an unbuffered write. Calling this function signals an intent to produce the body in this manner. TN)rrrrrrsetbodywillwrites zwsgiresponse.setbodywillwriteccs|jr tdd|_|jstd|jdur&|jdur&|js&td|jdr]|j ddkr8|jd=d d |j D}|rPtd d t ||jdusX|jr]td d}d}|j j dddkrnn|j jdvrun t|j jtjsd}nd}|rd|jd<|rt|j jtjsJ |j jd}|snqdd|jD}|t|j|}|jr|jVdS|jr|jD]}t|trt|}|VqdS|jr||_dStddS)zSend the generated response to the client. Before this is called, ``status`` must be set and one of ``setbodybytes()`` or ``setbodygen()`` must be called. Calling this method multiple times is not allowed. s$sendresponse() called multiple timesTsstatus line not definedNsresponse body not defineds304 r`racSsh|] }|dvr|qS))sdatesetagsexpiress cache-controlscontent-locationscontent-security-policysvary)lower)r,r-rrr sz,wsgiresponse.sendresponse..s"illegal header on 304 response: %ss, s,must use setbodybytes('') with 304 responsesFsExpectrXs 100-continue)sPOSTsPUTsCloses ConnectionicSs$g|]\}}t|t|fqSr)rstrurlr+rrr Esz-wsgiresponse.sendresponse..sdo not know how to send body)rrrirrrrrUrGr"keysrlsortedrrr9rJrHrrqreaditemsrrsysstr bytearraybytesr)r badheadersdrainclosechunk strheadersr~rrr sendresponses|             zwsgiresponse.sendresponsecCs4|jstd|jstd|jsJt|jS)aObtain a file object like object representing the response body. For this to work, you must call ``setbodywillwrite()`` and then ``sendresponse()`` first. ``sendresponse()`` is a generator and the function won't run to completion unless the generator is advanced. The generator yields not items. The easiest way to consume it is with ``list(res.sendresponse())``, which should resolve to an empty list - ``[]``. s"must call setbodywillwrite() firstsWmust call sendresponse() first; did you remember to consume it since it is a generator?)rrrirrryrrrr getbodyfile[s   zwsgiresponse.getbodyfileN) r2r3r4r5rrrrrrrrrrrrs  rcs|fdd}|S)z{For compatibility with old CGI scripts. A plain hgweb() or hgwebdir() can and should now be used as a WSGI application.cs ||Sr r)rsrespond applicationrrrun_wsgixrz!wsgiapplication..run_wsgir) app_makerrrrrwsgiapplicationss r)NNN) __future__r thirdpartyrrorrrrutilsr objectr rOr7rxryrrrrrrs    ; 0e! j