Ϫf&<dZddlmZgdZddlZddlmZddlmZm Z m Z ddl m Z ddl mZdd lmZdd lmZdd lmZdd lmZmZdd lmZGdde ZdZe eGddZdZGddeZGddeZGddeZ eZ!eZ"e Z#ee ddddde$dee ddddde$d ee ddddd!e$d"Gd#d$e Z%e e%Gd%d&eeZ&y)'zp Implementation of the lowest-level Resource class. See L{twisted.web.pages} for some utility implementations. ) annotations) IResourcegetChildForRequestResource ErrorPage NoResourceForbiddenResourceEncodingResourceWrapperN)Sequence) Attribute Interface implementer)Version) nativeString)proxyForInterface)deprecatedModuleAttribute)prefixedMethodNames) FORBIDDEN NOT_FOUND)UnsupportedMethodc4eZdZdZedZdZddZdZy)rz A web resource. z Signal if this IResource implementor is a "leaf node" or not. If True, getChildWithDefault will not be called on this Resource. cy)a Return a child with the given name for the given request. This is the external interface used by the Resource publishing machinery. If implementing IResource without subclassing Resource, it must be provided. However, if subclassing Resource, getChild overridden instead. @param name: A single path component from a requested URL. For example, a request for I{http://example.com/foo/bar} will result in calls to this method with C{b"foo"} and C{b"bar"} as values for this argument. @type name: C{bytes} @param request: A representation of all of the information about the request that is being made for this child. @type request: L{twisted.web.server.Request} N)namerequests 6/usr/lib/python3/dist-packages/twisted/web/resource.pygetChildWithDefaultzIResource.getChildWithDefault1cy)a0 Put a child L{IResource} implementor at the given path. @param path: A single path component, to be interpreted relative to the path this resource is found at, at which to put the given child. For example, if resource A can be found at I{http://example.com/foo} then a call like C{A.putChild(b"bar", B)} will make resource B available at I{http://example.com/foo/bar}. The path component is I{not} URL-encoded -- pass C{b'foo bar'} rather than C{b'foo%20bar'}. Nr)pathchilds rputChildzIResource.putChildDrrcy)ap Render a request. This is called on the leaf resource for a request. @return: Either C{server.NOT_DONE_YET} to indicate an asynchronous or a C{bytes} instance to write as the response to the request. If C{NOT_DONE_YET} is returned, at some point later (for example, in a Deferred callback) call C{request.write(b"")} to write data to the request, and C{request.finish()} to send the data to the browser. @raise twisted.web.error.UnsupportedMethod: If the HTTP verb requested is not supported by this resource. Nrrs rrenderzIResource.renderRrrN)r!bytesr"z 'IResource'returnNone) __name__ __module__ __qualname____doc__r isLeafrr#r&rrrrr%s' F &    rrc|jrm|jsa|jjd}|jj ||j ||}|jr |jsa|S)zE Traverse resource tree to find who will handle the request. r)postpathr.popprepathappendr)resourcer pathElements rrrbse   8??&&**1- {+// WE   8?? OrceZdZUdZeZded<dZdZdZ dZ dZ d Z d Z d Zdd Zd ZdZdZdZdZdZdZddZdZdZy)ra Define a web-accessible resource. This serves two main purposes: one is to provide a standard representation for what HTTP specification calls an 'entity', and the other is to provide an abstract directory structure for URL retrieval. zSequence[bytes]allowedMethodsNci|_y)z Initialize. Nchildrenselfs r__init__zResource.__init__|s  rrcHt|jjSN)listr:keysr;s rlistStaticNameszResource.listStaticNamessDMM&&())rcHt|jjSr?)r@r:itemsr;s rlistStaticEntitieszResource.listStaticEntitiessDMM'')**rcVt|j|jzSr?)r@rBlistDynamicNamesr;s r listNameszResource.listNamess$D((*+d.C.C.EEErcVt|j|jzSr?)r@rElistDynamicEntitiesr;s r listEntitieszResource.listEntitiess$D++-.1I1I1KKKrcgSr?rr;s rrGzResource.listDynamicNames rcgSr?rr<rs rrJzResource.listDynamicEntitiesrMrc8|jj|Sr?)r:getr<rs rgetStaticEntityzResource.getStaticEntitys}}  &&rcD||jvr|j||Syr?r:getChild)r<rrs rgetDynamicEntityzResource.getDynamicEntitys" t}} $==w/ /rc|j|=yr?r9rRs r delEntityzResource.delEntitys MM$ rc"||j|<yr?r9)r<rentitys rreallyPutEntityzResource.reallyPutEntitys$ drctS)a< Retrieve a 'child' resource from me. Implement this to create dynamic resource generation -- resources which are always available may be registered with self.putChild(). This will not be called if the class-level variable 'isLeaf' is set in your subclass; instead, the 'postpath' attribute of the request will be left as a list of the remaining path elements. For example, the URL /foo/bar/baz will normally be:: | site.resource.getChild('foo').getChild('bar').getChild('baz'). However, if the resource returned by 'bar' has isLeaf set to true, then the getChild call will never be made on it. Parameters and return value have the same meaning and requirements as those defined by L{IResource.getChildWithDefault}. )_UnsafeNoResourcer<r!rs rrVzResource.getChilds *!""rc`||jvr|j|S|j||S)a Retrieve a static or dynamically generated child resource from me. First checks if a resource was added manually by putChild, and then call getChild to check for dynamic resources. Only override if you want to affect behaviour of all child lookups, rather than just dynamic ones. This will check to see if I have a pre-registered child resource of the given name, and call getChild if I do not. @see: L{IResource.getChildWithDefault} rUr_s rrzResource.getChildWithDefaults0 4== ==& &}}T7++rcPtjdtdt||S)zz Deprecated in favor of L{getChildForRequest}. @see: L{twisted.web.resource.getChildForRequest}. z+Please use module level getChildForRequest.)warningswarnDeprecationWarningrrOs rrzResource.getChildForRequests'  9;Mq "$00rct|tstd|dt|||j|<|j |_y)aR Register a static child. You almost certainly don't want '/' in your path. If you intended to have the root of a folder, e.g. /foo/, you want path to be ''. @param path: A single path component. @param child: The child resource to register. @see: L{IResource.putChild} z Path segment must be bytes, but z is N) isinstancer' TypeErrortyper:server)r<r!r"s rr#zResource.putChildsG$&>thd4PT:,WX X# d{{ rct|dt|jzd}|s |j}t |||S#t$rt |}Yt |wxYw)aF Render a given resource. See L{IResource}'s render method. I delegate to methods of self with the form 'render_METHOD' where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one. render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class's responsibility to write the results using C{request.write(data)} and then call C{request.finish()}. Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET. @see: L{IResource.render} render_N)getattrrmethodr7AttributeError_computeAllowedMethodsr)r<rmr7s rr&zResource.rendersp( D)l7>>&BBD I >!%!4!4$N3 3z" >!7!=#N3 3 >s AA'&A'c$|j|S)z Default handling of HEAD method. I just return self.render_GET(request). When method is HEAD, the framework will handle this correctly. ) render_GETrOs r render_HEADzResource.render_HEADsw''rr?)r!r'r"rr(r))r*r+r,r-r entityType__annotations__rjr=r.rBrErHrKrGrJrSrWrYr\rVrrr#r&rtrrrrrmswJ## F F*+FL'  % #.,$ 1#,:(rrcg}t|jdD]"}|j|jd$|S)z Compute the allowed methods on a C{Resource} based on defined render_FOO methods. Used when raising C{UnsupportedMethod} but C{Resource} does not define C{allowedMethods} attribute. rlascii)r __class__r3encode)r4r7rs rrprpsF N#H$6$6 B4 dkk'23 4 rc&eZdZdZdZdZdZdZy)_UnsafeErrorPagea L{_UnsafeErrorPage}, publicly available via the deprecated alias C{ErrorPage}, is a resource which responds with a particular (parameterized) status and a body consisting of HTML containing some descriptive text. This is useful for rendering simple error pages. Deprecated in Twisted 22.10.0 because it permits HTML injection; use L{twisted.web.pages.errorPage} instead. @ivar template: A native string which will have a dictionary interpolated into it to generate the response body. The dictionary has the following keys: - C{"code"}: The status code passed to L{_UnsafeErrorPage.__init__}. - C{"brief"}: The brief description passed to L{_UnsafeErrorPage.__init__}. - C{"detail"}: The detailed description passed to L{_UnsafeErrorPage.__init__}. @ivar code: An integer status code which will be used for the response. @type code: C{int} @ivar brief: A short string which will be included in the response body as the page title. @type brief: C{str} @ivar detail: A longer string which will be included in the response body. @type detail: C{str} z %(code)s - %(brief)s

%(brief)s

%(detail)s

cXtj|||_||_||_yr?)rr=codebriefdetail)r<statusrrs rr=z_UnsafeErrorPage.__init__Rs%$   rc|j|j|jdd|jt |j|j |j z}t|tr|jdS|S)Ns content-typestext/html; charset=utf-8)r~rrzutf-8) setResponseCoder~ setHeadertemplatedictrrrgstrrz)r<r interpolateds rr&z_UnsafeErrorPage.renderXsp */+FG}}t$**T[[(   lC (&&w/ /rc|Sr?r)r<chnamrs rrVz_UnsafeErrorPage.getChildbs rN)r*r+r,r-rr=r&rVrrrr|r|)s<H rr|ceZdZdZddZy)r^a5 L{_UnsafeNoResource}, publicly available via the deprecated alias C{NoResource}, is a specialization of L{_UnsafeErrorPage} which returns the HTTP response code I{NOT FOUND}. Deprecated in Twisted 22.10.0 because it permits HTML injection; use L{twisted.web.pages.notFound} instead. c<tj|td|y)NzNo Such Resource)r|r=rr<messages rr=z_UnsafeNoResource.__init__ps!!$ 3EwOrN)z%Sorry. No luck finding that resource.r*r+r,r-r=rrrr^r^fs Prr^ceZdZdZddZy)_UnsafeForbiddenResourceaC L{_UnsafeForbiddenResource}, publicly available via the deprecated alias C{ForbiddenResource} is a specialization of L{_UnsafeErrorPage} which returns the I{FORBIDDEN} HTTP response code. Deprecated in Twisted 22.10.0 because it permits HTML injection; use L{twisted.web.pages.forbidden} instead. c<tj|td|y)NzForbidden Resource)r|r=rrs rr=z!_UnsafeForbiddenResource.__init__~s!!$ 3GQrN)zSorry, resource is forbidden.rrrrrrts RrrTwisted zEUse twisted.web.pages.errorPage instead, which properly escapes HTML.rzDUse twisted.web.pages.notFound instead, which properly escapes HTML.rzEUse twisted.web.pages.forbidden instead, which properly escapes HTML.r ceZdZdZdZy)_IEncodingResourcezT A resource which knows about L{_IRequestEncoderFactory}. @since: 12.3 cy)z Parse the request and return an encoder if applicable, using L{_IRequestEncoderFactory.encoderForRequest}. @return: A L{_IRequestEncoder}, or L{None}. Nrr%s r getEncoderz_IEncodingResource.getEncoderrrN)r*r+r,r-rrrrrrs   rrc(eZdZdZfdZdZxZS)r a Wrap a L{IResource}, potentially applying an encoding to the response body generated. Note that the returned children resources won't be wrapped, so you have to explicitly wrap them if you want the encoding to be applied. @ivar encoders: A list of L{_IRequestEncoderFactory} returning L{_IRequestEncoder} that may transform the data passed to C{Request.write}. The list must be sorted in order of priority: the first encoder factory handling the request will prevent the others from doing the same. @type encoders: C{list}. @since: 12.3 c2t||||_yr?)superr= _encoders)r<originalencodersrys rr=z EncodingResourceWrapper.__init__s "!rcT|jD]}|j|}||cSy)zR Browser the list of encoders looking for one applicable encoder. N)rencoderForRequest)r<rencoderFactoryencoders rrz"EncodingResourceWrapper.getEncoders2#nn N$66w?G" r)r*r+r,r-r=r __classcell__)rys@rr r s$"rr )'r- __future__r__all__rctypingr zope.interfacer r r incrementalrtwisted.python.compatrtwisted.python.componentsrtwisted.python.deprecatertwisted.python.reflectrtwisted.web._responsesrrtwisted.web.errorrrrrrpr|r^rrrr r*rr rrrrsC  # <<.7>67/: : z Yi(i(i(X :x:z P( P R/ R   , Ir2q!K   Ir2q!J   Ir2q!K        / :!r