Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override HTTP CONNECT #4571

Open
andrey-oparin opened this issue Feb 13, 2020 · 9 comments
Open

How to override HTTP CONNECT #4571

andrey-oparin opened this issue Feb 13, 2020 · 9 comments
Assignees

Comments

@andrey-oparin
Copy link

🐞 Describe the bug
I implement HTTP/HTTPS Proxy based on aiohttp and I need override HTTP connect method in subclass of aiohttp.web.View such as this guide

https://aiohttp.readthedocs.io/en/latest/web_reference.html

Overridable coroutine methods: connect(), delete(), get(), head(), options(), patch(), post(), put(), trace().

In this sample code, HTTP connect handler never called

💡 To Reproduce

import asyncio

from aiohttp import web, hdrs
from aiohttp.log import logging as logger


class HttpProxyView(web.View):
    
    async def get(self):
        logger.debug('Handle {}'.format(self.request.path))
        name = self.request.match_info.get('name', "Anonymous")
        text = "Hello, " + name
        return web.Response(text=text)

    async def connect(self):
        logger.debug('Start CONNECT to %s', self.request.path)


async def run_app(app, host, port):
    runner = web.AppRunner(app)
    await runner.setup()

    site = web.TCPSite(runner, host, port)
    await site.start()

    while True:
        await asyncio.sleep(3600)


if __name__ == '__main__':
    logger.basicConfig(level=logger.DEBUG)

    app = web.Application()
    app.add_routes([
        web.view('/', HttpProxyView),
        web.view('/{name:.*}', HttpProxyView)])

    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(run_app(app, host='0.0.0.0', port=8080))
    except KeyboardInterrupt:
        pass
@andrey-oparin andrey-oparin changed the title How to ovveride HTTP CONNECT How to override HTTP CONNECT Feb 13, 2020
@webknjaz
Copy link
Member

aiohttp is a framework for writing web apps, not HTTP proxies. I don't think we'll ever support that.

@webknjaz webknjaz added invalid This doesn't seem right question StackOverflow and removed bug labels Feb 13, 2020
@RemiZOffAlex
Copy link

aiohttp is a framework f

OK. But the header must be processed correctly. For example, give the code 405 Method Not Allowed

@asvetlov
Copy link
Member

I agree the CONNECT support would be nice to have (while 99% of users don't need it).

Pull Request is welcome!

@asvetlov asvetlov removed invalid This doesn't seem right question StackOverflow labels Oct 25, 2020
@derlih
Copy link
Contributor

derlih commented Dec 19, 2020

I've written the test to reproduce:

async def test_view_connect(aiohttp_client: Any) -> None:
    class MyView(web.View):
        async def connect(self) -> web.StreamResponse:
            return web.Response(text="OK")

    app = web.Application()
    app.router.add_route("CONNECT", "/", MyView)
    client = await aiohttp_client(app)
    resp = await client.request("CONNECT", "/")
    assert await resp.text() == "OK"

@DavidRomanovizc
Copy link
Contributor

Hello, @andrey-oparin, there is no activity here for the last 3 years, let's close this issue for now and reopen if needed.

@Dreamsorcerer
Copy link
Member

Does the test still reproduce?
#4571 (comment)

@DavidRomanovizc
Copy link
Contributor

Does the test still reproduce? #4571 (comment)

No, I didn't manage to do that

@Dreamsorcerer
Copy link
Member

Well, if that test is failing, then it's still an issue. As far as I can see, the documentation says that should work.

@Dreamsorcerer Dreamsorcerer reopened this Apr 30, 2023
@Dreamsorcerer
Copy link
Member

OK, so the URL for CONNECT gets changed to the authority-form (https://datatracker.ietf.org/doc/html/rfc7230#section-5.3.3), which then causes the resolve() algorithm to not match any endpoints.

Given that CONNECT should only be used for proxies and we don't intend to support this, maybe it makes the most sense to just remove the connect() method and update the documentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants