paraproxio.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:paraproxio 作者: intagger 项目源码 文件源码
def process_normally(self, message: RawRequestMessage, payload) -> aiohttp.Response:
        """Process request normally."""
        req_data = payload if not isinstance(payload, EmptyStreamReader) else None

        # Request from a host.
        try:
            async with aiohttp.ClientSession(headers=message.headers, loop=self._loop) as session:
                async with session.request(message.method, message.path,
                                           data=req_data,
                                           allow_redirects=False) as host_resp:  # type: aiohttp.ClientResponse
                    client_res = aiohttp.Response(
                        self.writer, host_resp.status, http_version=message.version)

                    # Process host response headers.
                    for name, value in host_resp.headers.items():
                        if name == hdrs.CONTENT_ENCODING:
                            continue
                        if name == hdrs.CONTENT_LENGTH:
                            continue
                        if name == hdrs.TRANSFER_ENCODING:
                            if value.lower() == 'chunked':
                                client_res.enable_chunked_encoding()
                        client_res.add_header(name, value)

                    # Send headers to the client.
                    client_res.send_headers()

                    # Send a payload.
                    while True:
                        chunk = await host_resp.content.read(self._chunk_size)
                        if not chunk:
                            break
                        client_res.write(chunk)

                    if client_res.chunked or client_res.autochunked():
                        await client_res.write_eof()
            return client_res
        except aiohttp.ClientResponseError:
            self.log_debug("CANCELLED {!s} {!r}.".format(message.method, message.path))
            raise
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号