Example #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
Example #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
Example #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
Example #4
0
    def _write_headers(self):
        if not self.writing:
            # Last header modifications

            if self._length is None:
                # If no length specified, it can't be a persistent connection
                self.persistent = False

            if self.protocol == http.Protocols.HTTP11:
                if not self.persistent:
                    self.set_header("connection", "close")
            elif self.protocol == http.Protocols.HTTP10:
                if self.persistent:
                    self.set_header("connection", "Keep-Alive")
            else:
                self.persistent = False

            self.debug('Writing headers on %s', self.log_ident)
            self.writing = True

            lines = []

            http.compose_response(self.status_code, self.protocol,
                                  self.status_message, buffer=lines)
            http.compose_headers(self._headers, buffer=lines)
            http.compose_cookies(self._cookies, buffer=lines)

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

            self._activate_transport()

            self.transport.writeSequence(seq)