Пример #1
0
 def renderHTTP_exception(self, ctx, f):
     try:
         text, code = humanize_failure(f)
     except:
         log.msg("exception in humanize_failure")
         log.msg("argument was %s" % (f,))
         log.err()
         text, code = str(f), None
     if code is not None:
         return self.simple(ctx, text, code)
     if f.check(server.UnsupportedMethod):
         # twisted.web.server.Request.render() has support for transforming
         # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
         # return code, but nevow does not.
         req = IRequest(ctx)
         method = req.method
         return self.simple(ctx,
                            "I don't know how to treat a %s request." % method,
                            http.NOT_IMPLEMENTED)
     req = IRequest(ctx)
     accept = req.getHeader("accept")
     if not accept:
         accept = "*/*"
     if "*/*" in accept or "text/*" in accept or "text/html" in accept:
         super = appserver.DefaultExceptionHandler
         return super.renderHTTP_exception(self, ctx, f)
     # use plain text
     traceback = f.getTraceback()
     return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
Пример #2
0
 def renderHTTP_exception(self, ctx, f):
     try:
         text, code = humanize_failure(f)
     except:
         log.msg("exception in humanize_failure")
         log.msg("argument was %s" % (f, ))
         log.err()
         text, code = str(f), None
     if code is not None:
         return self.simple(ctx, text, code)
     if f.check(server.UnsupportedMethod):
         # twisted.web.server.Request.render() has support for transforming
         # this into an appropriate 501 NOT_IMPLEMENTED or 405 NOT_ALLOWED
         # return code, but nevow does not.
         req = IRequest(ctx)
         method = req.method
         return self.simple(
             ctx, "I don't know how to treat a %s request." % method,
             http.NOT_IMPLEMENTED)
     req = IRequest(ctx)
     accept = req.getHeader("accept")
     if not accept:
         accept = "*/*"
     if "*/*" in accept or "text/*" in accept or "text/html" in accept:
         super = appserver.DefaultExceptionHandler
         return super.renderHTTP_exception(self, ctx, f)
     # use plain text
     traceback = f.getTraceback()
     return self.simple(ctx, traceback, http.INTERNAL_SERVER_ERROR)
Пример #3
0
    def render_PUT(self, ctx):
        req = IRequest(ctx)
        t = get_arg(req, "t", "").strip()
        replace = parse_replace_arg(get_arg(req, "replace", "true"))

        assert self.parentnode and self.name
        if req.getHeader("content-range"):
            raise WebError("Content-Range in PUT not yet supported", http.NOT_IMPLEMENTED)
        if not t:
            return self.replace_me_with_a_child(req, self.client, replace)
        if t == "uri":
            return self.replace_me_with_a_childcap(req, self.client, replace)

        raise WebError("PUT to a file: bad t=%s" % t)
Пример #4
0
    def child_giveup(self, ctx):
        """Handle reservation cancellation requests."""
        srv = IService(ctx)
        user = must_user(ctx).user

        if ctx.arg('giveup'):
            srv.giveupItem(user, self.item)  # pylint: disable-msg=E1101
            message(ctx, u"Votre modification est enregistrée.")

        request = IRequest(ctx)
        referer = request.getHeader('referer')
        if referer:
            return url.URL.fromString(referer)
        return url.URL.fromContext(ctx).up()
Пример #5
0
    def render_PUT(self, ctx):
        req = IRequest(ctx)
        t = get_arg(req, "t", "").strip()
        replace = parse_replace_arg(get_arg(req, "replace", "true"))

        assert self.parentnode and self.name
        if req.getHeader("content-range"):
            raise WebError("Content-Range in PUT not yet supported",
                           http.NOT_IMPLEMENTED)
        if not t:
            return self.replace_me_with_a_child(req, self.client, replace)
        if t == "uri":
            return self.replace_me_with_a_childcap(req, self.client, replace)

        raise WebError("PUT to a file: bad t=%s" % t)
Пример #6
0
    def locateChild(self, context, segments):
        """
        Delegate dispatch to a sharing resource if the request is for a user
        subdomain, otherwise fall back to the wrapped resource's C{locateChild}
        implementation.
        """
        request = IRequest(context)
        hostname = request.getHeader('host')

        info = self.subdomain(hostname)
        if info is not None:
            username, domain = info
            index = UserIndexPage(IRealm(self.siteStore), self.webViewer)
            resource = index.locateChild(None, [username])[0]
            return resource, segments
        return self.wrapped.locateChild(context, segments)
Пример #7
0
    def locateChild(self, context, segments):
        """
        Delegate dispatch to a sharing resource if the request is for a user
        subdomain, otherwise fall back to the wrapped resource's C{locateChild}
        implementation.
        """
        request = IRequest(context)
        hostname = request.getHeader('host')

        info = self.subdomain(hostname)
        if info is not None:
            username, domain = info
            index = UserIndexPage(IRealm(self.siteStore),
                                  self.webViewer)
            resource = index.locateChild(None, [username])[0]
            return resource, segments
        return self.wrapped.locateChild(context, segments)
Пример #8
0
    def renderHTTP(self, ctx):
        req = IRequest(ctx)
        gte = static.getTypeAndEncoding
        ctype, encoding = gte(self.filename,
                              static.File.contentTypes,
                              static.File.contentEncodings,
                              defaultType="text/plain")
        req.setHeader("content-type", ctype)
        if encoding:
            req.setHeader("content-encoding", encoding)

        if boolean_of_arg(get_arg(req, "save", "False")):
            # tell the browser to save the file rather display it we don't
            # try to encode the filename, instead we echo back the exact same
            # bytes we were given in the URL. See the comment in
            # FileNodeHandler.render_GET for the sad details.
            req.setHeader("content-disposition",
                          'attachment; filename="%s"' % self.filename)

        filesize = self.filenode.get_size()
        assert isinstance(filesize, (int, long)), filesize
        first, size = 0, None
        contentsize = filesize
        req.setHeader("accept-ranges", "bytes")
        if not self.filenode.is_mutable():
            # TODO: look more closely at Request.setETag and how it interacts
            # with a conditional "if-etag-equals" request, I think this may
            # need to occur after the setResponseCode below
            si = self.filenode.get_storage_index()
            if si:
                req.setETag(base32.b2a(si))
        # TODO: for mutable files, use the roothash. For LIT, hash the data.
        # or maybe just use the URI for CHK and LIT.
        rangeheader = req.getHeader('range')
        if rangeheader:
            ranges = self.parse_range_header(rangeheader)

            # ranges = None means the header didn't parse, so ignore
            # the header as if it didn't exist.  If is more than one
            # range, then just return the first for now, until we can
            # generate multipart/byteranges.
            if ranges is not None:
                first, last = ranges[0]

                if first >= filesize:
                    raise WebError('First beyond end of file',
                                   http.REQUESTED_RANGE_NOT_SATISFIABLE)
                else:
                    first = max(0, first)
                    last = min(filesize - 1, last)

                    req.setResponseCode(http.PARTIAL_CONTENT)
                    req.setHeader(
                        'content-range', "bytes %s-%s/%s" %
                        (str(first), str(last), str(filesize)))
                    contentsize = last - first + 1
                    size = contentsize

        req.setHeader("content-length", str(contentsize))
        if req.method == "HEAD":
            return ""

        # Twisted >=9.0 throws an error if we call req.finish() on a closed
        # HTTP connection. It also has req.notifyFinish() to help avoid it.
        finished = []

        def _request_finished(ign):
            finished.append(True)

        if hasattr(req, "notifyFinish"):
            req.notifyFinish().addBoth(_request_finished)

        d = self.filenode.read(req, first, size)

        def _finished(ign):
            if not finished:
                req.finish()

        def _error(f):
            lp = log.msg("error during GET",
                         facility="tahoe.webish",
                         failure=f,
                         level=log.UNUSUAL,
                         umid="xSiF3w")
            if finished:
                log.msg("but it's too late to tell them",
                        parent=lp,
                        level=log.UNUSUAL,
                        umid="j1xIbw")
                return
            req._tahoe_request_had_error = f  # for HTTP-style logging
            if req.startedWriting:
                # The content-type is already set, and the response code has
                # already been sent, so we can't provide a clean error
                # indication. We can emit text (which a browser might
                # interpret as something else), and if we sent a Size header,
                # they might notice that we've truncated the data. Keep the
                # error message small to improve the chances of having our
                # error response be shorter than the intended results.
                #
                # We don't have a lot of options, unfortunately.
                req.write("problem during download\n")
                req.finish()
            else:
                # We haven't written anything yet, so we can provide a
                # sensible error message.
                eh = MyExceptionHandler()
                eh.renderHTTP_exception(ctx, f)

        d.addCallbacks(_finished, _error)
        return req.deferred
Пример #9
0
    def renderHTTP(self, ctx):
        req = IRequest(ctx)
        gte = static.getTypeAndEncoding
        ctype, encoding = gte(self.filename,
                              static.File.contentTypes,
                              static.File.contentEncodings,
                              defaultType="text/plain")
        req.setHeader("content-type", ctype)
        if encoding:
            req.setHeader("content-encoding", encoding)

        if boolean_of_arg(get_arg(req, "save", "False")):
            # tell the browser to save the file rather display it we don't
            # try to encode the filename, instead we echo back the exact same
            # bytes we were given in the URL. See the comment in
            # FileNodeHandler.render_GET for the sad details.
            req.setHeader("content-disposition",
                          'attachment; filename="%s"' % self.filename)

        filesize = self.filenode.get_size()
        assert isinstance(filesize, (int,long)), filesize
        first, size = 0, None
        contentsize = filesize
        req.setHeader("accept-ranges", "bytes")

        # TODO: for mutable files, use the roothash. For LIT, hash the data.
        # or maybe just use the URI for CHK and LIT.
        rangeheader = req.getHeader('range')
        if rangeheader:
            ranges = self.parse_range_header(rangeheader)

            # ranges = None means the header didn't parse, so ignore
            # the header as if it didn't exist.  If is more than one
            # range, then just return the first for now, until we can
            # generate multipart/byteranges.
            if ranges is not None:
                first, last = ranges[0]

                if first >= filesize:
                    raise WebError('First beyond end of file',
                                   http.REQUESTED_RANGE_NOT_SATISFIABLE)
                else:
                    first = max(0, first)
                    last = min(filesize-1, last)

                    req.setResponseCode(http.PARTIAL_CONTENT)
                    req.setHeader('content-range',"bytes %s-%s/%s" %
                                  (str(first), str(last),
                                   str(filesize)))
                    contentsize = last - first + 1
                    size = contentsize

        req.setHeader("content-length", b"%d" % contentsize)
        if req.method == "HEAD":
            return ""

        finished = []
        def _request_finished(ign):
            finished.append(True)
        req.notifyFinish().addBoth(_request_finished)

        d = self.filenode.read(req, first, size)

        def _finished(ign):
            if not finished:
                req.finish()
        def _error(f):
            lp = log.msg("error during GET", facility="tahoe.webish", failure=f,
                         level=log.UNUSUAL, umid="xSiF3w")
            if finished:
                log.msg("but it's too late to tell them", parent=lp,
                        level=log.UNUSUAL, umid="j1xIbw")
                return
            req._tahoe_request_had_error = f # for HTTP-style logging
            if req.startedWriting:
                # The content-type is already set, and the response code has
                # already been sent, so we can't provide a clean error
                # indication. We can emit text (which a browser might
                # interpret as something else), and if we sent a Size header,
                # they might notice that we've truncated the data. Keep the
                # error message small to improve the chances of having our
                # error response be shorter than the intended results.
                #
                # We don't have a lot of options, unfortunately.
                req.write("problem during download\n")
                req.finish()
            else:
                # We haven't written anything yet, so we can provide a
                # sensible error message.
                eh = MyExceptionHandler()
                eh.renderHTTP_exception(ctx, f)
        d.addCallbacks(_finished, _error)
        return req.deferred