Esempio n. 1
0
 def do_send(self):
     tries = 2
     while True:
         try:
             # get socket
             self._sock = self.make_connection()
             
             # apply on request filters
             for bf in self.request_filters:
                 bf.on_request(self)
             
             # build request headers
             self.req_headers = req_headers = self._req_headers()
             
             # send request
             log.info('Start request: %s %s' % (self.method, self.url))
             log.debug("Request headers: [%s]" % str(req_headers))
             
             self._sock.sendall("".join(req_headers))
             
             if self.body is not None:
                 if hasattr(self.body, 'read'):
                     if hasattr(self.body, 'seek'): self.body.seek(0)
                     sock.sendfile(self._sock, self.body, self.chunked)
                 elif isinstance(self.body, types.StringTypes):
                     sock.send(self._sock, self.body, self.chunked)
                 else:
                     sock.sendlines(self._sock, self.body, self.chunked)
                     
                 if self.chunked: # final chunk
                     sock.send_chunk(self._sock, "")
                     
             return self.start_response()
         except socket.gaierror, e:
             self.clean_connections()
             raise
         except socket.error, e:
             if e[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EPIPE,
                         errno.ECONNREFUSED) or tries <= 0:
                 self.clean_connections()
                 raise
             if e[0] == errno.EPIPE:
                 log.debug("Got EPIPE")
                 self.clean_connections()
Esempio n. 2
0
    def perform(self, request):
        """ perform the request. If an error happen it will first try to
        restart it """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to perform request: %s %s %s" %
                    (request.host, request.method, request.path))

        tries = self.max_tries
        wait = self.wait_tries
        while tries > 0:
            try:
                # get or create a connection to the remote host
                connection = self.get_connection(request)
                sck = connection.socket()

                # send headers
                msg = self.make_headers_string(request,
                        connection.extra_headers)

                # send body
                if request.body is not None:
                    chunked = request.is_chunked()
                    if request.headers.iget('content-length') is None and \
                            not chunked:
                        raise RequestError(
                                "Can't determine content length and " +
                                "Transfer-Encoding header is not chunked")


                    # handle 100-Continue status
                    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
                    hdr_expect = request.headers.iget("expect")
                    if hdr_expect is not None and \
                            hdr_expect.lower() == "100-continue":
                        sck.sendall(msg)
                        msg = None
                        resp = http.Request(http.Unreader(self._sock))
                        if resp.status_int != 100:
                            self.reset_request()
                            if log.isEnabledFor(logging.DEBUG):
                                log.debug("return response class")
                            return self.response_class(connection,
                                    request, resp)

                    chunked = request.is_chunked()
                    if log.isEnabledFor(logging.DEBUG):
                        log.debug("send body (chunked: %s)" % chunked)


                    if isinstance(request.body, types.StringTypes):
                        if msg is not None:
                            send(sck, msg + request.body, chunked)
                        else:
                            send(sck, request.body, chunked)
                    else:
                        if msg is not None:
                            sck.sendall(msg)

                        if hasattr(request.body, 'read'):
                            if hasattr(request.body, 'seek'): request.body.seek(0)
                            sendfile(sck, request.body, chunked)
                        else:
                            sendlines(sck, request.body, chunked)
                    if chunked:
                        send_chunk(sck, "")
                else:
                    sck.sendall(msg)

                return self.get_response(request, connection)
            except socket.gaierror, e:
                try:
                    connection.close()
                except:
                    pass

                raise RequestError(str(e))
            except socket.timeout, e:
                try:
                    connection.close()
                except:
                    pass

                if tries <= 0:
                    raise RequestTimeout(str(e))
Esempio n. 3
0
    def perform(self, request):
        """ perform the request. If an error happen it will first try to
        restart it """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to perform request: %s %s %s" %
                    (request.host, request.method, request.path))

        tries = self.max_tries
        wait = self.wait_tries
        while tries > 0:
            try:
                # get or create a connection to the remote host
                connection = self.get_connection(request)
                sck = connection.socket()

                # send headers
                msg = self.make_headers_string(request,
                        connection.extra_headers)

                # send body
                if request.body is not None:
                    chunked = request.is_chunked()
                    if request.headers.iget('content-length') is None and \
                            not chunked:
                        raise RequestError(
                                "Can't determine content length and " +
                                "Transfer-Encoding header is not chunked")


                    # handle 100-Continue status
                    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
                    hdr_expect = request.headers.iget("expect")
                    if hdr_expect is not None and \
                            hdr_expect.lower() == "100-continue":
                        sck.sendall(msg)
                        msg = None
                        resp = Request(http.Unreader(self._sock))
                        if resp.status_int != 100:
                            self.reset_request()
                            if log.isEnabledFor(logging.DEBUG):
                                log.debug("return response class")
                            return self.response_class(connection,
                                    request, resp)

                    chunked = request.is_chunked()
                    if log.isEnabledFor(logging.DEBUG):
                        log.debug("send body (chunked: %s)" % chunked)


                    if isinstance(request.body, types.StringTypes):
                        if msg is not None:
                            send(sck, msg + request.body, chunked)
                        else:
                            send(sck, request.body, chunked)
                    else:
                        if msg is not None:
                            sck.sendall(msg)

                        if hasattr(request.body, 'read'):
                            if hasattr(request.body, 'seek'): request.body.seek(0)
                            sendfile(sck, request.body, chunked)
                        else:
                            sendlines(sck, request.body, chunked)
                    if chunked:
                        send_chunk(sck, "")
                else:
                    sck.sendall(msg)

                return self.get_response(request, connection)
            except socket.gaierror, e:
                try:
                    connection.close()
                except:
                    pass

                raise RequestError(str(e))
            except socket.timeout, e:
                try:
                    connection.close()
                except:
                    pass

                if tries <= 0:
                    raise RequestTimeout(str(e))