Esempio n. 1
0
 async def _create_connection(self, req, *args, **kwargs):
     # connection timeout
     try:
         with CeilTimeout(self.__wrapped_conn_timeout, loop=self._loop):
             return await super()._create_connection(req, *args, **kwargs)
     except asyncio.TimeoutError as exc:
         raise aiohttp.ServerTimeoutError('Connection timeout '
                                          'to host {0}'.format(
                                              req.url)) from exc
Esempio n. 2
0
    async def read(self):
        with CeilTimeout(self.__wrapped_read_timeout, loop=self._loop):
            resp_msg, stream_reader = await super().read()

            if hasattr(stream_reader, '_wait'):
                stream_reader._wait = wrapt.FunctionWrapper(
                    stream_reader._wait, self._wrapped_wait)

            return resp_msg, stream_reader
Esempio n. 3
0
    async def _wrap_create_connection(self, protocol_factory,
                                      host, port, **kwargs):
        # aiohttp calls this method for each http request
        proxy = create_proxy(
            loop=self._loop,
            proxy_type=self._proxy_type,
            host=self._proxy_host, port=self._proxy_port,
            username=self._proxy_username, password=self._proxy_password,
            rdns=self._rdns, family=self._proxy_family)

        with CeilTimeout(kwargs["timeout"].sock_connect):
            await proxy.connect(host, port)

        return await super()._wrap_create_connection(
            protocol_factory, None, None, sock=proxy.socket, **kwargs)
Esempio n. 4
0
    async def _wrap_create_connection(self, protocol_factory,
                                      host, port, **kwargs):

        proxy = create_proxy(
            loop=self._loop,
            proxy_type=ProxyType(self._socks_ver),
            host=self._socks_host, port=self._socks_port,
            username=self._socks_username, password=self._socks_password,
            rdns=self._rdns, family=self._socks_family)

        with CeilTimeout(kwargs["timeout"].sock_connect):
            await proxy.connect(host, port)

        return await super()._wrap_create_connection(
            protocol_factory, None, None, sock=proxy.socket, **kwargs)
Esempio n. 5
0
    async def _wrap_create_connection(self, protocol_factory,
                                      host, port, **kwargs):
        # aiohttp calls this method for each http request
        proxy = create_proxy(
            loop=self._loop,
            proxy_type=self._proxy_type,
            host=self._proxy_host, port=self._proxy_port,
            username=self._proxy_username, password=self._proxy_password,
            rdns=self._rdns, family=self._proxy_family)

        timeout = kwargs.get('timeout')
        if timeout is not None and hasattr(timeout, 'sock_connect'):
            with CeilTimeout(timeout.sock_connect):
                await proxy.connect(host, port)
        else:
            await proxy.connect(host, port)

        return await super()._wrap_create_connection(
            protocol_factory, None, None, sock=proxy.socket, **kwargs)
Esempio n. 6
0
    async def _wrap_create_connection(self, protocol_factory,
                                      host, port, **kwargs):

        proxy = create_proxy(
            loop=self._loop,
            proxy_type=ProxyType(self._socks_ver),
            host=self._socks_host, port=self._socks_port,
            username=self._socks_username, password=self._socks_password,
            rdns=self._rdns, family=self._socks_family)

        timeout = kwargs.get('timeout')
        if timeout is not None and hasattr(timeout, 'sock_connect'):
            with CeilTimeout(timeout.sock_connect):
                await proxy.connect(host, port)
        else:
            await proxy.connect(host, port)

        return await super()._wrap_create_connection(
            protocol_factory, None, None, sock=proxy.socket, **kwargs)
Esempio n. 7
0
    def shutdown(self, timeout=15.0):
        """Worker process is about to exit, we need cleanup everything and
        stop accepting requests. It is especially important for keep-alive
        connections."""
        self._force_close = True

        if self._keepalive_handle is not None:
            self._keepalive_handle.cancel()

        # cancel waiters
        for waiter in self._waiters:
            if not waiter.done():
                waiter.cancel()

        # wait for handlers
        with suppress(asyncio.CancelledError, asyncio.TimeoutError):
            with CeilTimeout(timeout, loop=self._loop):
                if self._error_handler and not self._error_handler.done():
                    yield from self._error_handler

                while True:
                    h = None
                    for handler in self._request_handlers:
                        if not handler.done():
                            h = handler
                            break
                    if h:
                        yield from h
                    else:
                        break

        # force-close non-idle handlers
        for handler in self._request_handlers:
            if not handler.done():
                handler.cancel()

        if self.transport is not None:
            self.transport.close()
            self.transport = None

        if self._request_handlers:
            self._request_handlers.clear()
Esempio n. 8
0
 async def _wrapped_wait(self, wrapped, instance, args, kwargs):
     with CeilTimeout(self.__wrapped_read_timeout, loop=self._loop):
         result = await wrapped(*args, **kwargs)
         return result
Esempio n. 9
0
    def handle_request(self, request):
        """Start processing of incoming requests.

        It reads request line, request headers and request payload, then
        calls handle_request() method. Subclass has to override
        handle_request(). start() handles various exceptions in request
        or response handling. Connection is being closed always unless
        keep_alive(True) specified.
        """
        loop = self._loop
        payload = request.content
        request.time_service = self._time_service
        try:
            try:
                resp = yield from self._request_handler(request)
            except HTTPException as exc:
                resp = exc
            except asyncio.CancelledError:
                self.log_debug('Ignored premature client disconnection')
                return
            except asyncio.TimeoutError:
                self.log_debug('Request handler timed out.')
                resp = self.handle_error(request, 504)
            except Exception as exc:
                resp = self.handle_error(request, 500, exc)

            yield from resp.prepare(request)
            yield from resp.write_eof()

            # notify server about keep-alive
            self._keepalive = resp.keep_alive

            # Restore default state.
            # Should be no-op if server code didn't touch these attributes.

            # writer.set_tcp_cork(False)
            # writer.set_tcp_nodelay(True)

            # check payload
            if not payload.is_eof():
                # print("is_eof: false")
                lingering_time = self._lingering_time
                if not self._force_close and lingering_time:
                    self.log_debug('Start lingering close timer for %s sec.',
                                   lingering_time)

                    now = loop.time()
                    end_t = now + lingering_time

                    with suppress(asyncio.TimeoutError,
                                  asyncio.CancelledError):
                        while (not payload.is_eof() and now < end_t):
                            timeout = min(end_t - now, lingering_time)
                            with CeilTimeout(timeout, loop=loop):
                                # read and ignore
                                yield from payload.readany()
                                now = loop.time()

                # if payload still uncompleted
                if not payload.is_eof() and not self._force_close:
                    self.log_debug('Uncompleted request.')
                    self.close()

        except RuntimeError as exc:
            if self.debug:
                self.log_exception('Unhandled runtime exception', exc_info=exc)
                self.force_close()
        except Exception as exc:
            self.log_exception('Unhandled exception', exc_info=exc)
            self.force_close()
        finally:
            if self.transport is None:
                self.log_debug('Ignored premature client disconnection.')
Esempio n. 10
0
    def start(self, request, handler):
        """Start processing of incoming requests.

        It reads request line, request headers and request payload, then
        calls handle_request() method. Subclass has to override
        handle_request(). start() handles various exceptions in request
        or response handling. Connection is being closed always unless
        keep_alive(True) specified.
        """
        loop = self._loop
        handler = handler[0]
        manager = self._manager
        keepalive_timeout = self._keepalive_timeout

        while not self._force_close:
            if self.access_log:
                now = loop.time()

            manager.requests_count += 1
            payload = request.content
            request.time_service = self._time_service
            try:
                try:
                    resp = yield from self._request_handler(request)
                except HTTPException as exc:
                    resp = exc
                except asyncio.CancelledError:
                    self.log_debug('Ignored premature client disconnection')
                    break
                except asyncio.TimeoutError:
                    self.log_debug('Request handler timed out.')
                    resp = self.handle_error(request, 504)
                except Exception as exc:
                    resp = self.handle_error(request, 500, exc)

                yield from resp.prepare(request)
                yield from resp.write_eof()

                # notify server about keep-alive
                self._keepalive = resp.keep_alive

                # Restore default state.
                # Should be no-op if server code didn't touch these attributes.
                # writer.set_tcp_cork(False)
                # writer.set_tcp_nodelay(True)

                # log access
                if self.access_log:
                    self.log_access(message, None, resp, loop.time() - now)

                # check payload
                if not payload.is_eof():
                    lingering_time = self._lingering_time
                    if not self._force_close and lingering_time:
                        self.log_debug(
                            'Start lingering close timer for %s sec.',
                            lingering_time)

                        now = loop.time()
                        end_t = now + lingering_time

                        with suppress(asyncio.TimeoutError,
                                      asyncio.CancelledError):
                            while (not payload.is_eof() and now < end_t):
                                timeout = min(end_t - now, lingering_time)
                                with CeilTimeout(timeout, loop=loop):
                                    # read and ignore
                                    yield from payload.readany()
                                now = loop.time()

                    # if payload still uncompleted
                    if not payload.is_eof() and not self._force_close:
                        self.log_debug('Uncompleted request.')
                        self.close()

            except RuntimeError as exc:
                if self.debug:
                    self.log_exception('Unhandled runtime exception',
                                       exc_info=exc)
                self.force_close()
            except Exception as exc:
                self.log_exception('Unhandled exception', exc_info=exc)
                self.force_close()
            finally:
                if self.transport is None:
                    self.log_debug('Ignored premature client disconnection.')
                elif not self._force_close:
                    if self._messages:
                        request = self._messages.popleft()
                    else:
                        if self._keepalive and not self._close:
                            # start keep-alive timer
                            if keepalive_timeout is not None:
                                now = self._time_service.loop_time
                                self._keepalive_time = now
                                if self._keepalive_handle is None:
                                    self._keepalive_handle = loop.call_at(
                                        now + keepalive_timeout,
                                        self._process_keepalive)

                            # wait for next request
                            waiter = create_future(loop)
                            self._waiters.append(waiter)
                            try:
                                request = yield from waiter
                            except asyncio.CancelledError:
                                # shutdown process
                                break
                        else:
                            break

        # remove handler, close transport if no handlers left
        if not self._force_close:
            self._request_handlers.remove(handler)
            if not self._request_handlers:
                if self.transport is not None:
                    self.transport.close()