Exemplo n.º 1
0
    def prettyprintbody(self, prefix):
        ''' Pretty print body '''

        return  # Slow in some cases; disable

        if self["content-type"] not in ("application/json", "text/xml",
                                        "application/xml"):
            return

        # Grab the whole body
        if not isinstance(self.body, basestring):
            body = self.body.read()
        else:
            body = self.body

        # Decode the body
        if self["content-type"] == "application/json":
            string = compat.json.dumps(compat.json.loads(body),
              indent=4, sort_keys=True)
        elif self["content-type"] in ("text/xml", "application/xml"):
            string = body

        # Prettyprint
        for line in string.split("\n"):
            logging.debug("%s %s", prefix, line.rstrip())

        # Seek to the beginning if needed
        if not isinstance(self.body, basestring):
            utils.safe_seek(self.body, 0)
Exemplo n.º 2
0
def prettyprintbody(m, prefix):
    if m["content-type"] not in ("application/json", "text/xml",
                                 "application/xml"):
        return

    # Grab the whole body
    if not isinstance(m.body, basestring):
        body = m.body.read()
    else:
        body = m.body

    # Decode the body
    if m["content-type"] == "application/json":
        s = compat.json.dumps(compat.json.loads(body),
          indent=4, sort_keys=True)
    elif m["content-type"] in ("text/xml", "application/xml"):
        s = body

    # Prettyprint
    for ln in s.split("\n"):
        LOG.debug("%s %s" % (prefix, ln.rstrip()))

    # Seek to the beginning if needed
    if not isinstance(m.body, basestring):
        utils.safe_seek(m.body, 0)
Exemplo n.º 3
0
    def compose(self, **kwargs):
        ''' Prepare a request on the client side '''
        self.method = kwargs.get("method", "")

        if kwargs.get("uri", ""):
            self.uri = kwargs.get("uri", "")
            (self.scheme, self.address,
             self.port, self.pathquery) = http_misc.urlsplit(self.uri)
            self["host"] = self.address + ":" + self.port
        else:
            self.scheme = kwargs.get("scheme", "")
            self.address = kwargs.get("address", "")
            self.port = kwargs.get("port", "")
            self.pathquery = kwargs.get("pathquery", "")
            if self.method:
                #
                # "A client MUST include a Host header field in all HTTP/1.1
                # request messages.  If the requested URI does not include
                # an Internet host name for the service being requested, then
                # the Host header field MUST be given with an empty value."
                #               -- RFC 2616
                #
                self["host"] = kwargs.get("host", "")
                if not self["host"]:
                    logging.warning("Missing host header")

        self.code = kwargs.get("code", "")
        self.reason = kwargs.get("reason", "")
        self.protocol = kwargs.get("protocol", "HTTP/1.1")

        if kwargs.get("nocache", True):
            if self.method:
                self["pragma"] = "no-cache"
            self["cache-control"] = "no-cache"

        if kwargs.get("date", True):
            self["date"] = email.utils.formatdate(usegmt=True)

        if not kwargs.get("keepalive", True):
            self["connection"] = "close"

        #
        # Curl(1) does not enter into up_to_eof mode unless a
        # content-type is specified, for this reason I've added
        # an OOPS below.
        # TODO Looking again at RFC2616 after adding this bits
        # realized that another patch is due to make the code
        # that deals with body and length better.
        #
        if kwargs.get("up_to_eof", False):
            if not "mimetype" in kwargs:
                logging.warning("up_to_eof without mimetype")
            self["content-type"] = kwargs.get("mimetype",
                                              "text/plain")

        elif kwargs.get("body", None):
            self.body = kwargs.get("body", None)
            if not hasattr(self.body, 'tell'):
                self.length = len(self.body)
            else:
                utils.safe_seek(self.body, 0, os.SEEK_END)
                self.length = self.body.tell()
                utils.safe_seek(self.body, 0, os.SEEK_SET)
            self["content-length"] = str(self.length)
            if kwargs.get("mimetype", ""):
                self["content-type"] = kwargs.get("mimetype", "")

        elif kwargs.get("chunked", None):
            self.body = kwargs.get("chunked", None)
            self.length = -1
            self["transfer-encoding"] = "chunked"
            if kwargs.get("mimetype", ""):
                self["content-type"] = kwargs.get("mimetype", "")

        else:
            self["content-length"] = "0"

        self.family = kwargs.get("family", socket.AF_INET)
Exemplo n.º 4
0
    def compose(self, **kwargs):
        ''' Prepare a request on the client side '''
        self.method = kwargs.get("method", "")

        if kwargs.get("uri", ""):
            self.uri = kwargs.get("uri", "")
            (self.scheme, self.address,
             self.port, self.pathquery) = http_misc.urlsplit(self.uri)
            self["host"] = self.address + ":" + self.port
        else:
            self.scheme = kwargs.get("scheme", "")
            self.address = kwargs.get("address", "")
            self.port = kwargs.get("port", "")
            self.pathquery = kwargs.get("pathquery", "")
            if self.method:
                #
                # "A client MUST include a Host header field in all HTTP/1.1
                # request messages.  If the requested URI does not include
                # an Internet host name for the service being requested, then
                # the Host header field MUST be given with an empty value."
                #               -- RFC 2616
                #
                self["host"] = kwargs.get("host", "")
                if not self["host"]:
                    raise RuntimeError('missing host header')

        self.code = kwargs.get("code", "")
        self.reason = kwargs.get("reason", "")
        self.protocol = kwargs.get("protocol", "HTTP/1.1")

        if kwargs.get("nocache", True):
            if self.method:
                self["pragma"] = "no-cache"
            self["cache-control"] = "no-cache"

        if kwargs.get("date", True):
            self["date"] = email.utils.formatdate(usegmt=True)

        if not kwargs.get("keepalive", True):
            self["connection"] = "close"

        #
        # Curl(1) does not enter into up_to_eof mode unless a
        # content-type is specified, for this reason I've added
        # an OOPS below.
        #
        # TODO Looking again at RFC2616 after adding this bits
        # realized that another patch is due to make the code
        # that deals with body and length better.
        #
        if kwargs.get("up_to_eof", False):
            if not "mimetype" in kwargs:
                raise RuntimeError("up_to_eof without mimetype")
            self["content-type"] = kwargs.get("mimetype",
                                              "text/plain")

        elif kwargs.get("body", None):
            self.body = kwargs.get("body", None)
            if not hasattr(self.body, 'tell'):
                self.length = len(self.body)
            else:
                utils.safe_seek(self.body, 0, os.SEEK_END)
                self.length = self.body.tell()
                utils.safe_seek(self.body, 0, os.SEEK_SET)
            self["content-length"] = str(self.length)
            if kwargs.get("mimetype", ""):
                self["content-type"] = kwargs.get("mimetype", "")

        elif kwargs.get("chunked", None):
            self.body = kwargs.get("chunked", None)
            self.length = -1
            self["transfer-encoding"] = "chunked"
            if kwargs.get("mimetype", ""):
                self["content-type"] = kwargs.get("mimetype", "")

        else:
            self["content-length"] = "0"

        # make sure we catch unicode doofus early...
        if hasattr(self.body, 'mode') and self.body.mode != 'rb':
            raise RuntimeError('body is not opened in binary mode')
        elif PY3 and isinstance(self.body, str):
            raise RuntimeError('body is a string with encoding')

        self.family = kwargs.get("family", socket.AF_INET)