Esempio n. 1
0
    def request(self, method, location,
                protocol=None, headers=None, body=None):
        headers = dict(headers) if headers is not None else {}
        if body:
            # without typecast to str, in case of unicode input
            # the server just breaks connection with me
            # TODO: think if it cannot be fixed better
            body = body.encode('utf-8')
            headers["content-length"] = len(body)
        lines = []
        http.compose_request(method, location, protocol, buffer=lines)
        http.compose_headers(headers, buffer=lines)

        seq = []
        for line in lines:
            line = line.encode('utf-8')
            self.log("<<< %s", line)
            seq.append(line)
            seq.append("\r\n")
        seq.append("\r\n")

        if body:
            seq.append(body)

        d = defer.Deferred()
        self._requests.append(d)

        self.transport.writeSequence(seq)

        return d
Esempio n. 2
0
    def request(self, method, location,
                protocol=None, headers=None, body=None):
        headers = dict(headers) if headers is not None else {}
        if body:
            headers["content-length"] = len(body)
        lines = []
        http.compose_request(method, location, protocol, buffer=lines)
        http.compose_headers(headers, buffer=lines)

        seq = []
        for line in lines:
            self.log("<<< %s", line)
            seq.append(line)
            seq.append("\r\n")
        seq.append("\r\n")

        if body:
            seq.append(body)

        d = defer.Deferred()
        self._requests.append(d)

        self.transport.writeSequence(seq)

        return d
Esempio n. 3
0
    def request(self, method, location,
                protocol=None, headers=None, body=None, decoder=None):
        self.cancel_timeout("inactivity")
        self.reset_timeout('headers')

        headers = dict(headers) if headers is not None else {}
        if body:
            body = self._encode_body(body)
            headers["content-length"] = len(body)
        lines = []
        http.compose_request(method, location, protocol, buffer=lines)
        http.compose_headers(headers, buffer=lines)

        seq = []
        for line in lines:
            line = line.encode('utf-8')
            self.log("<<< %s", line)
            seq.append(line)
            seq.append("\r\n")
        seq.append("\r\n")

        if body:
            seq.append(body)

        if decoder is None:
            decoder = ResponseDecoder()
        # The parameters below are used to format a nice error message
        # shall this request fail in any way
        scheme, host, port = self._get_target()

        decoder.request_params = {
            'method': method.name,
            'location': location,
            'scheme': scheme,
            'host': host,
            'port': port,
            'started_epoch': time.time()}

        self._requests.append(decoder)

        self.transport.writeSequence(seq)
        finished_decoding = decoder.get_result()

        d = defer.Deferred(canceller=self._cancel_request)
        # Reference to decoder is unsed by canceller
        d.decoder = decoder
        finished_decoding.chainDeferred(d)
        return d