Ejemplo n.º 1
0
    def get_dump_file(self, request, arg):
        try:
            request.setHeader('content-type', 'application/x-tar')
            request.setHeader('content-encoding', 'gzip')

            if request.method == 'HEAD':
                return ''

            self.status = '--'

            statinfo = stat(NOX_DUMP)
            size = statinfo[ST_SIZE]
            f = open(NOX_DUMP, 'rb')

            static.FileTransfer(f, size, request)
            return server.NOT_DONE_YET
        except Exception, e:
            return self.err(Failure(), request, "get_dump_file",
                            "Could not retrieve dump file.")
Ejemplo n.º 2
0
class StaticFile(static.File):
    """ taken from twisted.web.static and modified
        accordingly to the patch by John-Mark Gurney
        http://resnet.uoregon.edu/~gurney_j/jmpc/dist/twisted.web.static.patch
    """

    # BEGIN patch for #266
    def __init__(self, path, defaultType="text/html", ignoredExts=(), registry=None, allowExt=0):
        static.File.__init__(self, unquote(path), defaultType=defaultType, ignoredExts=ignoredExts, registry=registry, allowExt=allowExt) # added for #
    # END patch for #266

    def render(self, request):
        #print ""
        #print "StaticFile", request
        #print "StaticFile in", request.received_headers

        """You know what you doing."""
        self.restat()

        if self.type is None:
            self.type, self.encoding = static.getTypeAndEncoding(self.basename(),
                                                          self.contentTypes,
                                                          self.contentEncodings,
                                                          self.defaultType)

        if not self.exists():
            return self.childNotFound.render(request)

        if self.isdir():
            return self.redirect(request)

        #for content-length
        fsize = size = self.getFileSize()

        request.setHeader('accept-ranges','bytes')

        if self.type:
            request.setHeader('content-type', self.type)
        if self.encoding:
            request.setHeader('content-encoding', self.encoding)

        try:
            f = self.openForReading()
        except IOError, e:
            import errno
            if e[0] == errno.EACCES:
                return error.ForbiddenResource().render(request)
            else:
                raise
        if request.setLastModified(self.getmtime()) is http.CACHED:
            return ''
        trans = True

        range = request.getHeader('range')
        #print "StaticFile", range

        tsize = size
        if range is not None:
            # This is a request for partial data...
            bytesrange = range.split('=')
            assert bytesrange[0] == 'bytes',\
                   "Syntactically invalid http range header!"
            start, end = bytesrange[1].split('-', 1)
            if start:
                f.seek(int(start))
                if end:
                    end = int(end)
                else:
                    end = size - 1
            else:
                lastbytes = int(end)
                if size < lastbytes:
                    lastbytes = size
                start = size - lastbytes
                f.seek(start)
                fsize = lastbytes
                end = size - 1
            size = end + 1
            fsize = end - int(start) + 1
            # start is the byte offset to begin, and end is the byte offset
            # to end..  fsize is size to send, tsize is the real size of
            # the file, and size is the byte position to stop sending.

            if fsize <= 0:
                request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
                fsize = tsize
                trans = False
            else:
                request.setResponseCode(http.PARTIAL_CONTENT)
                request.setHeader('content-range',"bytes %s-%s/%s " % (
                    str(start), str(end), str(tsize)))
                #print "StaticFile", start, end, tsize

        request.setHeader('content-length', str(fsize))

        if request.method == 'HEAD' or trans == False:
            # pretend we're a HEAD request, so content-length
            # won't be overwritten.
            #print "HEAD request"
            request.method = 'HEAD'
            return ''

        #print "StaticFile out", request.headers, request.code

        # return data
        # size is the byte position to stop sending, not how many bytes to send
        static.FileTransfer(f, size, request)
        # and make sure the connection doesn't get closed
        return server.NOT_DONE_YET
Ejemplo n.º 3
0
def StaticSendOld(f, flength, request):
    """This is for compatibility with older versions of Twisted.
	"""
    static.FileTransfer(f, flength, request)
Ejemplo n.º 4
0
 def transferFile():
     self._sendResponseHeaders()
     static.FileTransfer(appIterator.f, appIterator.last + 1,
                         self.request)