o b5@sdZgdZddlZddlmZmZmZddlmZddl m Z ddl m Z ddl mZmZdd lmZGd d d eZd d ZeeGdddZddZGdddeZGdddeZGdddeZGdddeZeeGddde eZdS)z4 Implementation of the lowest-level Resource class. ) IResourcegetChildForRequestResource ErrorPage NoResourceForbiddenResourceEncodingResourceWrapperN) Attribute Interface implementer) nativeString)proxyForInterface)prefixedMethodNames) FORBIDDEN NOT_FOUND)UnsupportedMethodc@s0eZdZdZedZddZddZddZd S) 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. cCdS)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)namerequestrr6/usr/lib/python3/dist-packages/twisted/web/resource.pygetChildWithDefault+zIResource.getChildWithDefaultcCr)a Put a child 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}. @type path: C{bytes} Nr)pathchildrrrputChild>rzIResource.putChildcCr)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. NrrrrrrenderJrzIResource.renderN) __name__ __module__ __qualname____doc__r isLeafrrrrrrrrs rcCs@|jr|js|jd}|j||||}|jr|jr|S)zE Traverse resource tree to find who will handle the request. r)postpathr"popprepathappendr)resourcer pathElementrrrrZs     rc@seZdZdZeZdZddZdZddZ dd Z d d Z d d Z ddZ d&ddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%ZdS)'rz Define a web-accessible resource. This serves 2 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. NcCs i|_dS)z Initialize. Nchildrenselfrrr__init__ss zResource.__init__rcCt|jSN)listr*keysr+rrrlistStaticNames}zResource.listStaticNamescCr.r/)r0r*itemsr+rrrlistStaticEntitiesr3zResource.listStaticEntitiescCt||Sr/)r0r2listDynamicNamesr+rrr listNameszResource.listNamescCr6r/)r0r5listDynamicEntitiesr+rrr listEntitiesr9zResource.listEntitiescCgSr/rr+rrrr7zResource.listDynamicNamescCr<r/rr,rrrrr:r=zResource.listDynamicEntitiescCs |j|Sr/)r*getr,rrrrgetStaticEntity zResource.getStaticEntitycCs||jvr |||SdSr/r*getChild)r,rrrrrgetDynamicEntitys  zResource.getDynamicEntitycCs |j|=dSr/r)r@rrr delEntityrBzResource.delEntitycCs||j|<dSr/r))r,rentityrrrreallyPutEntityr3zResource.reallyPutEntitycCstdS)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}. zNo such child resource.)rr,rrrrrrDszResource.getChildcCs ||jvr |j|S|||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} rCrIrrrrs   zResource.getChildWithDefaultcCstdtdt||S)Nz+Please use module level getChildForRequest.)warningswarnDeprecationWarningrr>rrrrs zResource.getChildForRequestcCs:t|tstjdt|tdd||j|<|j|_dS)a 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. @type path: L{bytes} @param child: The child resource to register. @type child: L{IResource} @see: L{IResource.putChild} zcPath segment must be bytes; passing {} has never worked, and will raise an exception in the future.rJ)category stacklevelN) isinstancebytesrKrLformattyperMr*server)r,rrrrrrs    zResource.putChildcCsTt|dt|jd}|s&z|j}Wt|ty%t|}Yt|w||S)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)getattrr methodallowedMethodsAttributeError_computeAllowedMethodsr)r,rmrXrrrrs zResource.rendercCs ||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_GETr>rrr render_HEADs zResource.render_HEADr/)rrr r!r entityTyperTr-r"r2r5r8r;r7r:rArErFrHrDrrrrr]rrrrres,  rcCs*g}t|jdD] }||dq|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. rUascii)r __class__r&encode)r'rXrrrrrZsrZc@s,eZdZdZdZddZddZddZd S) ra L{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. @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{ErrorPage.__init__}. - C{"brief"}: The brief description passed to L{ErrorPage.__init__}. - C{"detail"}: The detailed description passed to L{ErrorPage.__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

cCs t|||_||_||_dSr/)rr-codebriefdetail)r,statusrcrdrrrr-Es  zErrorPage.__init__cCsJ||j|dd|jt|j|j|jd}t|tr#| dS|S)Ns content-typestext/html; charset=utf-8)rbrcrdzutf-8) setResponseCoderb setHeadertemplatedictrcrdrPstrra)r,r interpolatedrrrrKs     zErrorPage.rendercCs|Sr/r)r,chnamrrrrrDUr=zErrorPage.getChildN)rrr r!rhr-rrDrrrrr!s   rc@eZdZdZdddZdS)rzr L{NoResource} is a specialization of L{ErrorPage} which returns the HTTP response code I{NOT FOUND}. %Sorry. No luck finding that resource.cCt|td|dS)NzNo Such Resource)rr-rr,messagerrrr-_r9zNoResource.__init__N)rnrrr r!r-rrrrrYrc@rm)rzy L{ForbiddenResource} is a specialization of L{ErrorPage} which returns the I{FORBIDDEN} HTTP response code. Sorry, resource is forbidden.cCro)NzForbidden Resource)rr-rrprrrr-ir9zForbiddenResource.__init__N)rtrrrrrrrcrsrc@seZdZdZddZdS)_IEncodingResourcezT A resource which knows about L{_IRequestEncoderFactory}. @since: 12.3 cCr)z Parse the request and return an encoder if applicable, using L{_IRequestEncoderFactory.encoderForRequest}. @return: A L{_IRequestEncoder}, or L{None}. Nrrrrr getEncodertrz_IEncodingResource.getEncoderN)rrr r!rvrrrrrums rucs(eZdZdZfddZddZZS)ra 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 cst|||_dSr/)superr- _encoders)r,originalencodersr`rrr-s  z EncodingResourceWrapper.__init__cCs*|jD]}||}|dur|SqdS)zR Browser the list of encoders looking for one applicable encoder. N)rxencoderForRequest)r,rencoderFactoryencoderrrrrvs  z"EncodingResourceWrapper.getEncoder)rrr r!r-rv __classcell__rrr{rr}s r)r!__all__rKzope.interfacer r r twisted.python.compatr twisted.python.componentsr twisted.python.reflectrtwisted.web._responsesrrtwisted.web.errorrrrrrZrrrrurrrrrs*     ; -8