aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.13k stars 2.02k forks source link

Server; POST-request; BadStatusLine: 400; an endless stream of exception messages that have no impact on operation. #8442

Closed PieceOfGood closed 3 weeks ago

PieceOfGood commented 5 months ago

Describe the bug

I get the following output when receiving requests from Telegram through the installed Webhook:

Error handling request
Traceback (most recent call last):
  File "G:\pyProj\lena-site\venv\Lib\site-packages\aiohttp\web_protocol.py", line 350, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "aiohttp\\_http_parser.pyx", line 557, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: 400, message:
  Invalid method encountered:

    b'":27,"length":5,"type":"bold"}]},"text":"\\u041f\\u044f\\u0442\\u044c."}}POST /telegram/hook HTTP/1.1'
      ^
Error handling request
Traceback (most recent call last):
  File "G:\pyProj\lena-site\venv\Lib\site-packages\aiohttp\web_protocol.py", line 350, in data_received
    messages, upgraded, tail = self._request_parser.feed_data(data)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "aiohttp\\_http_parser.pyx", line 557, in aiohttp._http_parser.HttpParser.feed_data
aiohttp.http_exceptions.BadStatusLine: 400, message:
  Invalid method encountered:

    b't":"CHAT_MESSAGE:\\n\\u041f\\u043e\\u043b\\u044c\\u0437\\u043e\\u0432\\u0430\\u0442\\u0435\\u043b\\u044c Dobry \\u043d\\u0430\\u043f\\u0438\\u0441\\u0430\\u043b \\u0432 \\u0447\\u0430\\u0442:\\n\\n\\u0414\\u0430 \\u0431\\u0443\\u0434\\u0435\\u0442 \\u043f\\u044f\\u0442\\u044c.","entities":[{"offset":0,"length":13,"type":"underline"},{"offset":27,"length":5,"type":"bold"}]},"text":"\\u041f\\u044f\\u0442\\u044c."}}POST /telegram/hook HTTP/1.1'
      ^
...

Tests show that each such output contains a reaction to part of the line from the previous message (that is, received by the handler in the previous request), which, in principle, does not affect the operation of the script in any way. At the same time, sending the same text does not guarantee receiving a message about this error, which is why it is not possible to identify the pattern that influences the occurrence of this situation.

The method(read() | text() | json()) by which the contents of the request are received has no effect on this situation.

To Reproduce

An ordinary POST request handler that reads the content:

@routes.post("/telegram/hook")
@aiohttp_csrf.csrf_exempt
async def telegram_hook(request: Request) -> Response:
    """ Receives data from Telegram, processes it
    and sends a response.
    """

    secret_header = request.headers.get("X-Telegram-Bot-Api-Secret-Token", None)
    if secret_header != TG_SECRET_TOKEN:
        log.debug(
            "An incorrect secret was received in a message from Telegram:"
            f"\n{secret_header!r}"
        )
        return Response(status=404)

    body: bytes = await request.read()
    ...

Expected behavior

Since this exception does not affect the operation of the program in any way, I would like not to see messages about it. I haven't found a way to disable output for this particular situation.

Logs/tracebacks

The contents of the log are described above.

Python Version

$ python --version
Python 3.12.3

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.9.5
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Location: G:\pyProj\lena-site\venv\Lib\site-packages
Requires: aiosignal, attrs, frozenlist, multidict, yarl
Required-by: aiohttp-csrf, aiohttp-jinja2, aiohttp-security, aiohttp-session

multidict Version

$ python -m pip show multidict
Name: multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: G:\pyProj\lena-site\venv\Lib\site-packages
Requires: 
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.9.4
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: G:\pyProj\lena-site\venv\Lib\site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

Windows 10 x64

Related component

Server

Additional context

No response

Code of Conduct

Dreamsorcerer commented 2 months ago

Is this still occurring in 3.10.5? We fixed several parser issues that might have been the cause of these errors.