Ejemplo n.º 1
0
    def do_http(self, req):
        host, port, uri = http.parseurl(req.uri)
        self.iskeepalive(req)

        if self.VERBOSE:
            req.debug()
        reqx = self.clone_msg(req)
        reqx.keepalive = True
        reqx.remote, reqx.uri = (host, port), uri
        reqx["Host"] = host if port == 80 else "%s:%d" % (host, port)
        if self.VERBOSE:
            reqx.debug()

        req.start_time = datetime.now()
        self.in_query.append(req)

        try:
            res, resx = None, http.round_trip(reqx)

            try:
                if self.VERBOSE:
                    resx.debug()
                res = self.clone_msg(resx)
                if hasattr(self, "reqdone"):
                    self.reqdone(req, res)

                hasbody = reqx.method.upper() != "HEAD"
                if not hasbody or res.body is None:
                    m = None
                else:
                    m = Meter(res.body)
                res.body = m

                res.keepalive = req.keepalive
                if all(
                    (resx.get("Transfer-Encoding", "identity") == "identity", "Content-Length" not in resx, hasbody)
                ):
                    res.keepalive = False

                if self.VERBOSE:
                    res.debug()
                res.sendto(req.stream)
                res.length = 0 if m is None else m.counter

            finally:
                resx.close()
        finally:
            self.in_query.remove(req)
        return res