o b@sdZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z ddl mZmZddlmZddlmZddlmZmZdd lmZmZmZdd lmZmZdd lmZdd l m!Z!dd l"m#Z#ddl$m%Z%ddl&m'Z'm(Z(m)Z)ddl*m+Z+e(,dZ-ddZ.Gddde(j/Z0eeddddddZ1ddZ2Gddde(j/Z3Gdddej4Z5dej6fd d!Z7d"d#Z8Gd$d%d%e(j/ej9Z:eej;Gd&d'd'ZGd,d-d-e<Z?Gd.d/d/e(j/Z@d0d1ZAGd2d3d3e(j/ZBdS)4z& Static resources for L{twisted.web}. N)escape)AnyCallableDict)quoteunquote) implementer)Version)abstract interfaces) componentsfilepathlog) nativeString networkString) deprecated) platformType)URL)InsensitiveDict)httpresourceserver) redirectTozInvalid request URL.cCs|dkpd|vpttj|vS)Ns../)rosseppathr4/usr/lib/python3/dist-packages/twisted/web/static.py isDangerous%sr c@s$eZdZdZddZddZeZdS)Dataz/ This is a static, in-memory resource. cCstj|||_||_dS)z @param data: The bytes that make up this data resource. @type data: L{bytes} @param type: A native string giving the Internet media type for this content. @type type: L{str} N)rResource__init__datatype)selfr$r%rrrr#.s  z Data.__init__cCs>|dt|j|ddt|jf|jdkrdS|jS)N content-typecontent-length%dHEAD) setHeaderrr%lenr$methodr&requestrrr render_GET;s  zData.render_GETN)__name__ __module__ __qualname____doc__r#r1 render_HEADrrrrr!)s  r!TwistedcCst|S)zK Add a trailing slash to C{request}'s URI. Deprecated, do not use. ) _addSlash)r0rrraddSlashEsr:cCs8t|jd}|jt|jdgd}|dS)a$ Add a trailing slash to C{request}'s URI. @param request: The incoming request to add the ending slash to. @type request: An object conforming to L{twisted.web.iweb.IRequest} @return: A URI with a trailing slash, with query and fragment preserved. @rtype: L{bytes} asciir) rfromTexturidecodereplacelistrasTextencode)r0urlrrrr9Ms r9c@seZdZddZddZdS)RedirectcCstj|t||_dSN)rr"r#r9rDr/rrrr#^s zRedirect.__init__cCs t|j|SrF)rrDr/rrrrenderb zRedirect.renderN)r2r3r4r#rGrrrrrE]s rEc@(eZdZdZddZddZddZdS) Registryz I am a Componentized object that will be made available to internal Twisted file-based dynamic web content such as .rpy and .epy scripts. cCstj|i|_dSrF)r Componentizedr# _pathCacher&rrrr#ls  zRegistry.__init__cCs||j|<dSrF)rL)r&rrsrcrrr cachePathpzRegistry.cachePathcCs |j|SrF)rLget)r&rrrr getCachedPathsrHzRegistry.getCachedPathN)r2r3r4r5r#rOrRrrrrrJfs  rJc Cs.||tjdddddddddd tjS)aS Produces a mapping of extensions (with leading dot) to MIME types. It does this by calling the C{init} function of the L{mimetypes} module. This will have the side effect of modifying the global MIME types cache in that module. Multiple file locations containing mime-types can be passed as a list. The files will be sourced in that order, overriding mime-types from the files sourced beforehand, but only if a new entry explicitly overrides the current entry. @param mimetype_locations: Optional. List of paths to C{mime.types} style files that should be used. @type mimetype_locations: iterable of paths or L{None} @param init: The init function to call. Defaults to the global C{init} function of the C{mimetypes} module. For internal use (testing) only. @type init: callable z text/plainz audio/x-flacz text/x-ozzapplication/x-shockwave-flashztext/vnd.wap.wmlzapplication/vnd.mozilla.xul+xml) z.confz.diffz.flacz.javaz.ozz.swfz.wmlz.xulz.patch) mimetypes types_mapupdate)mimetype_locationsinitrrr loadMimeTypesws rXcCs`t|\}}td|}||vr$||}tj|d}nd}|||}||fS)Nr<)r FilePathsplitext_coerceToFilesystemEncodinglowerrrrQ)filenametypes encodings defaultTypepextencr%rrrgetTypeAndEncodings rec@seZdZUdZeZdddZiZee e e e ge ffe d<gdZdZ d0d d Zd dZedZeZddZddZddZddZddZddZddZddZd d!Zd1d"d#Z d$d%Z!d&d'Z"e"Z#d(d)Z$d*d+Z%d,d-Z&d.d/Z'dS)2Filea@ File is a resource that represents a plain non-interpreted file (although it can look for an extension like .rpy or .cgi and hand the file to a processor for interpretation if you wish). Its constructor takes a file path. Alternatively, you can give a directory path to the constructor. In this case the resource will represent that directory, and its children will be files underneath that directory. This provides access to an entire filesystem tree with a single Resource. If you map the URL 'http://server/FILE' to a resource created as File('/tmp'), then http://server/FILE/ will return an HTML-formatted listing of the /tmp/ directory, and http://server/FILE/foo/bar.html will return the contents of /tmp/foo/bar.html . @cvar childNotFound: L{Resource} used to render 404 Not Found error pages. @cvar forbidden: L{Resource} used to render 403 Forbidden error pages. @ivar contentTypes: a mapping of extensions to MIME types used to set the default value for the Content-Type header. It is initialized with the values returned by L{loadMimeTypes}. @type contentTypes: C{dict} @ivar contentEncodings: a mapping of extensions to encoding types used to set default value for the Content-Encoding header. @type contentEncodings: C{dict} gzipbzip2)z.gzz.bz2 processors)indexz index.htmlz index.htmz index.rpyN text/htmlrrcCsjtj|tj||||_|dvs|r(td|s|r$dg|_n g|_nt ||_|p1t |_ dS)a Create a file with the given path. @param path: The filename of the file from which this L{File} will serve data. @type path: C{str} @param defaultType: A I{major/minor}-style MIME type specifier indicating the I{Content-Type} with which this L{File}'s data will be served if a MIME type cannot be determined based on C{path}'s extension. @type defaultType: C{str} @param ignoredExts: A sequence giving the extensions of paths in the filesystem which will be ignored for the purposes of child lookup. For example, if C{ignoredExts} is C{(".bar",)} and C{path} is a directory containing a file named C{"foo.bar"}, a request for the C{"foo"} child of this resource will succeed with a L{File} pointing to C{"foo.bar"}. @param registry: The registry object being used to handle this request. If L{None}, one will be created. @type registry: L{Registry} @param allowExt: Ignored parameter, only present for backwards compatibility. Do not pass a value for this parameter. )rrYz0ignoredExts should receive a list, not a boolean*N) rr"r#r rZrawarningswarn ignoredExtsrArJregistry)r&rrarorpallowExtrrrr#s     z File.__init__cCs|j|dS)zQIgnore the given extension. Serve file.ext if file is requested N)roappend)r&rcrrr ignoreExtszFile.ignoreExtzFile not found.cCs$|j}|}t|||j|j|jS)z Return a resource that generates an HTML listing of the directory this path represents. @return: A resource that renders the directory to HTML. @rtype: L{DirectoryLister} )r listNamesDirectoryLister contentTypescontentEncodingsra)r&rnamesrrrdirectoryListings zFile.directoryListingcCs t|tr"z|d}Wnty!tdd||jYSw|jdd|s/|jS|rFz| |}Wnt j yE|jYSw|j |j }|durT|S|se|j|j}|dure|jS|d}tdkrxt|j|}n|j|}|rt||j|jS||jS)az If this L{File}"s path refers to a directory, return a L{File} referring to the file named C{path} in that directory. If C{path} is the empty string, return a L{DirectoryLister} instead. @param path: The current path segment. @type path: L{bytes} @param request: The incoming request. @type request: An that provides L{twisted.web.iweb.IRequest}. @return: A resource representing the requested file or directory, or L{NoResource} if the path cannot be accessed. @rtype: An object that provides L{resource.IResource}. zutf-8Nz(Could not decode path segment as utf-8: F)reraiserYwin32) isinstancebytesr?UnicodeDecodeErrorrerr childNotFoundrestatisdirchildr InsecurePathchildSearchPreauth indexNamesryexistssiblingExtensionSearchror[rrrirQr IResourcerrpcreateSimilarFile)r&rr0fpath extension processorrrrgetChilds<          z File.getChildcC|S)zOpen a file and return it.)openrMrrropenForReadingOzFile.openForReadingcCr)zReturn file size.)getsizerMrrr getFileSizeSrzFile.getFileSizec CsZz |dd\}}Wn tytdw|}|dkr%td|ttdttj|d}g}|D]s}z |dd\}}WntyQtd |w|rizt|}Wntyhtd |wd}|rzt|}Wntytd |wd}|dur|dur||krtd |n |durtd ||||fq7|S) a Parse the value of a Range header into (start, stop) pairs. In a given pair, either of start or stop can be None, signifying that no value was provided, but not both. @return: A list C{[(start, stop)]} of pairs of length at least one. @raise ValueError: if the header is syntactically invalid or if the Bytes-Unit is anything other than "bytes'. =rYzMissing '=' separatorbyteszUnsupported Bytes-Unit: N,-zInvalid Byte-Range: ) split ValueErrorstriprAfiltermapr}intrr) r&rangekindvalueunparsedRanges parsedRanges byteRangestartendrrr_parseRangeHeaderWsL       zFile._parseRangeHeadercCsf|}|dur||}|}n|dur|}n||kr|d7}n||kr%|}||kr-d}}|||fS)a Convert a start and end from a Range header to an offset and size. This method checks that the resulting range overlaps with the resource being served (and so has the value of C{getFileSize()} as an indirect input). Either but not both of start or end can be L{None}: - Omitted start means that the end value is actually a start value relative to the end of the resource. - Omitted end means the end of the resource should be the end of the range. End is interpreted as inclusive, as per RFC 2616. If this range doesn't overlap with any of this resource, C{(0, 0)} is returned, which is not otherwise a value return value. @param start: The start value from the header, or L{None} if one was not present. @param end: The end value from the header, or L{None} if one was not present. @return: C{(offset, size)} where offset is how far into this resource this resource the range begins and size is how long the range is, or C{(0, 0)} if the range does not overlap this resource. NrYr)r)r&rrsizerrr_rangeToOffsetAndSizes  zFile._rangeToOffsetAndSizecCstd|||d|fS)a Return a string suitable for the value of a Content-Range header for a range with the given offset and size. The offset and size are not sanity checked in any way. @param offset: How far into this resource the range begins. @param size: How long the range is. @return: The value as appropriate for the value of a Content-Range header. zbytes %d-%d/%drY)rr)r&offsetrrrr _contentRanges zFile._contentRangecCs|\}}|||\}}||krdkr/nn|tj|dtd|f||fS|tj|d|||||fS)a Set up the response for Range headers that specify a single range. This method checks if the request is satisfiable and sets the response code and Content-Range header appropriately. The return value indicates which part of the resource to return. @param request: The Request object. @param startAndEnd: A 2-tuple of start of the byte range as specified by the header and the end of the byte range as specified by the header. At most one of the start and end may be L{None}. @return: A 2-tuple of the offset and size of the range to return. offset == size == 0 indicates that the request is not satisfiable. r content-range bytes */%d) rsetResponseCoderREQUESTED_RANGE_NOT_SATISFIABLEr,rrPARTIAL_CONTENTr)r&r0 startAndEndrrrrrrr_doSingleRangeRequests  zFile._doSingleRangeRequestcCsld}g}d}tttddtd}|jr|j}nd}|D]B\}} ||| \} } | | kr9dkr           zFile.render_GETcCstt||SrF)rr9r/rrrrrPz File.redirectcCs |sgS|}||SrF)rlistdirsort)r& directoryrrrrts zFile.listNamescCstt|fdd|S)NcSs|tj|j|SrF)rrrjoin)fileNamer&rrrsz#File.listEntities..)rArrtrMrrr listEntitiess  zFile.listEntitiescCs:|||j|j|j}|j|_|jdd|_|j|_|SrF) __class__rarorprirr)r&rfrrrrs zFile.createSimilarFile)rkrNrrF)(r2r3r4r5rXrvrwrirstrrrr!__annotations__rr%r#rsr NoResourcerForbiddenResourcerryrrrrrrrrrrr1r6rrtrrrrrrrfs:    + <3+ M 4 rfc@s8eZdZdZejjZddZddZddZ dd Z d S) StaticProducerz Superclass for classes that implement the business of producing. @ivar request: The L{IRequest} to write the contents of the file to. @ivar fileObject: The file the contents of which to write to the request. cCs||_||_dS)z* Initialize the instance. N)r0 fileObject)r&r0rrrrr#s zStaticProducer.__init__cC t|jrF)NotImplementedErrorrrMrrrr zStaticProducer.startcCrrF)rresumeProducingrMrrrrrzStaticProducer.resumeProducingcCs|jd|_dS)z Stop producing data. L{twisted.internet.interfaces.IProducer.stopProducing} is called when our consumer has died, and subclasses also call this method when they are done producing data. N)rrr0rMrrr stopProducings  zStaticProducer.stopProducingN) r2r3r4r5r FileDescriptor bufferSizer#rrrrrrrrs rc@s eZdZdZddZddZdS)rzI A L{StaticProducer} that writes the entire file to the request. cCs|j|ddS)NF)r0registerProducerrMrrrrszNoRangeStaticProducer.startcCsL|jsdS|j|j}|r|j|dS|j|j|dSrF)r0rreadrwriteunregisterProducerfinishrr&r$rrrrs   z%NoRangeStaticProducer.resumeProducingN)r2r3r4r5rrrrrrrs rc@rI) rzR A L{StaticProducer} that writes a single chunk of a file to the request. cCst|||||_||_dS)a  Initialize the instance. @param request: See L{StaticProducer}. @param fileObject: See L{StaticProducer}. @param offset: The offset into the file of the chunk to be written. @param size: The size of the chunk to write. N)rr#rr)r&r0rrrrrrr#s  z"SingleRangeStaticProducer.__init__cCs&|j|jd|_|j|ddSNr)rseekr bytesWrittenr0rrMrrrrszSingleRangeStaticProducer.startcCs|jsdS|jt|j|j|j}|r$|jt|7_|j||jr=|j|jkr?|j |j | dSdSdSrF) r0rrminrrrr-rrrrrrrrrs    z)SingleRangeStaticProducer.resumeProducingN)r2r3r4r5r#rrrrrrrs  rc@s0eZdZdZddZddZddZdd Zd S) rzR A L{StaticProducer} that writes several chunks of a file to the request. cCst|||||_dS)a Initialize the instance. @param request: See L{StaticProducer}. @param fileObject: See L{StaticProducer}. @param rangeInfo: A list of tuples C{[(boundary, offset, size)]} where: - C{boundary} will be written to the request first. - C{offset} the offset into the file of chunk to write. - C{size} the size of the chunk to write. N)rr#r)r&r0rrrrrr#s z$MultipleRangeStaticProducer.__init__cCs&t|j|_||j|ddSr)iterr rangeIter _nextRanger0rrMrrrr)s z!MultipleRangeStaticProducer.startcCs*t|j\|_}|_d|_|j|dSr)nextr partBoundary _partSize_partBytesWrittenrr)r&rrrrr.sz&MultipleRangeStaticProducer._nextRangecCs|jsdSg}d}d}||jkrg|jr#|t|j7}||jd|_|jt|j||j|j }|j t|7_ |t|7}|||jrb|j |jkrbz| Wn t yad}Ynw||jks|j d ||r|j|j|dSdS)NrFTr+)r0rrr-rrrrrrrr StopIterationrrrrr)r&r$ dataLengthdonerbrrrr3sB           z+MultipleRangeStaticProducer.resumeProducingN)r2r3r4r5r#rrrrrrrrs  rc@s"eZdZdZdddZddZdS) ASISProcessorz Serve files exactly as responses without generating a status-line or any headers. Inspired by Apache's mod_asis. NcCs"tj|||_|p t|_dSrF)rr"r#rrJrp)r&rrprrrr#Zs zASISProcessor.__init__cCs d|_t|j|jd}||S)NrY)rp)startedWritingrfrrprG)r&r0resrrrrG_s zASISProcessor.renderrF)r2r3r4r5r#rGrrrrrTs  rcCsD|dkrd|S|dkrd|dS|dkrd|dSd|dS)zG Format the given file size in bytes to human readable format. iz%iBiz%iKi@z%iMz%iGr)rrrrformatFileSizees   rc@sXeZdZdZdZdZdejejdfddZ dd Z d d Z d d Z de fddZeZdS)rua Print the content of a directory. @ivar template: page template used to render the content of the directory. It must contain the format keys B{header} and B{tableContent}. @type template: C{str} @ivar linePattern: template used to render one line in the listing table. It must contain the format keys B{class}, B{href}, B{text}, B{size}, B{type} and B{encoding}. @type linePattern: C{str} @ivar contentTypes: a mapping of extensions to MIME types used to populate the information of a member of this directory. It is initialized with the value L{File.contentTypes}. @type contentTypes: C{dict} @ivar contentEncodings: a mapping of extensions to encoding types. It is initialized with the value L{File.contentEncodings}. @type contentEncodings: C{dict} @ivar defaultType: default type used when no mimetype is detected. @type defaultType: C{str} @ivar dirs: filtered content of C{path}, if the whole content should not be displayed (default to L{None}, which means the actual content of C{path} is printed). @type dirs: L{None} or C{list} @ivar path: directory which content should be listed. @type path: C{str} a %(header)s

%(header)s

%(tableContent)s
Filename Size Content type Content encoding
z %(text)s %(size)s %(type)s %(encoding)s NrkcCs.tj|||_||_||_||_||_dSrF)rr"r#rvrwradirsr)r&pathnamer rvrwrarrrr#s  zDirectoryLister.__init__c Csg}g}|D]c}t|tr|d}t|d}t|}t|j|}| r8| |d|dddddqt ||j |j |j\}} z|} Wn tySYqw| ||d|| rad| pbdt| dq||fS)a Helper returning files and directories in given directory listing, with attributes to be used to build a table content with C{self.linePattern}. @return: tuple of (directories, files) @rtype: C{tuple} of C{list} utf8/r<z [Directory])texthrefrr%rz[%s])r rr%rr)r|r}r?rrr rZrrrrrrervrwrarrr) r&rfilesr rrD escapedPath childPathmimetyperrrrr_getFilesAndDirectoriessD        z'DirectoryLister._getFilesAndDirectoriescCsBg}tddg}t||D]\}}||d<||j|q|S)zs Build a table content using C{self.linePattern} and giving elements odd and even classes. oddevenclass) itertoolscycleziprr linePattern)r&elements tableContent rowClasseselementrowClassrrr_buildTableContent s z"DirectoryLister._buildTableContentcCs|dd|jdurt|j}|n|j}||\}}d|||}d t t t |j }|j||d}|d}|S)zB Render a listing of the content of C{self.path}. r'stext/html; charset=utf-8Nr<zDirectory listing for {})headerrr )r,r rrrrrrr formatrrrr>templaterC)r&r0rr rrr!rrrrrGs     zDirectoryLister.renderreturncCs d|jS)NzrrMrrr__repr__-rzDirectoryLister.__repr__)r2r3r4r5r#rrfrvrwr#rr rGrr%__str__rrrrruss!+  1 ru)Cr5rrrSrrrmhtmlrtypingrrr urllib.parserrzope.interfacer incrementalr twisted.internetr r twisted.pythonr r rtwisted.python.compatrrtwisted.python.deprecatertwisted.python.runtimertwisted.python.urlrtwisted.python.utilr twisted.webrrrtwisted.web.utilrrdangerousPathErrorr r"r!r:r9rErKrJrWrXrerZrf IPullProducerrrrrrrrurrrrsX           % #&?