Example #1
0
 def req_start(self, method, uri, req_hdrs, req_body_pause):
     """
     Start a request to uri using method, where 
     req_hdrs is a list of (field_name, field_value) for
     the request headers.
     
     Returns a (req_body, req_done) tuple.
     """
     self._req_body_pause_cb = req_body_pause
     req_hdrs = [i for i in req_hdrs \
         if not i[0].lower() in req_remove_hdrs]
     (scheme, authority, path, query, fragment) = urlsplit(uri)
     if scheme.lower() != 'http':
         self._handle_error(ERR_URL, "Only HTTP URLs are supported")
         return dummy, dummy
     if "@" in authority:
         userinfo, authority = authority.split("@", 1)
     if ":" in authority:
         host, port = authority.rsplit(":", 1)
         try:
             port = int(port)
         except ValueError:
             self._handle_error(ERR_URL, "Non-integer port in URL")
             return dummy, dummy
     else:
         host, port = authority, 80
     if path == "":
         path = "/"
     uri = urlunsplit(('', '', path, query, ''))
     self.method, self.uri, self.req_hdrs = method, uri, req_hdrs
     self.req_hdrs.append(("Host", host)) # FIXME: port
     self.req_hdrs.append(("Connection", "keep-alive"))
     try:
         body_len = int(get_hdr(req_hdrs, "content-length").pop(0))
         delimit = COUNTED
     except (IndexError, ValueError):
         body_len = None
         delimit = NOBODY
     self._output_start("%s %s HTTP/1.1" % (self.method, self.uri), self.req_hdrs, delimit)
     _idle_pool.attach(host, port, self._handle_connect, self._handle_connect_error, self.connect_timeout)
     return self.req_body, self.req_done
Example #2
0
    def res_start(self, status_code, status_phrase, res_hdrs, res_body_pause):
        "Start a response. Must only be called once per response."
        self._res_body_pause_cb = res_body_pause
        res_hdrs = [i for i in res_hdrs \
                    if not i[0].lower() in hop_by_hop_hdrs ]

        try:
            body_len = int(get_hdr(res_hdrs, "content-length").pop(0))
        except (IndexError, ValueError):
            body_len = None
        if body_len is not None:
            delimit = COUNTED
            res_hdrs.append(("Connection", "keep-alive"))
        elif 2.0 > self.req_version >= 1.1:
            delimit = CHUNKED
            res_hdrs.append(("Transfer-Encoding", "chunked"))
        else:
            delimit = CLOSE
            res_hdrs.append(("Connection", "close"))

        self._output_start("HTTP/1.1 %s %s" % (status_code, status_phrase), res_hdrs, delimit)
        return self.res_body, self.res_done