MagicStack / httptools

Fast HTTP parser
MIT License
1.21k stars 83 forks source link

Uvicorn cannot handle http2 requests after installed httptools, is it a bug for httptools? #102

Closed yluuu16 closed 1 month ago

yluuu16 commented 2 months ago

env:

> conda create -n test -y python=3.10
#   _libgcc_mutex      anaconda/cloud/conda-forge/linux-64::_libgcc_mutex-0.1-conda_forge 
#   _openmp_mutex      anaconda/cloud/conda-forge/linux-64::_openmp_mutex-4.5-2_gnu 
#   bzip2              anaconda/cloud/conda-forge/linux-64::bzip2-1.0.8-h4bc722e_7 
#   ca-certificates    anaconda/cloud/conda-forge/linux-64::ca-certificates-2024.7.4-hbcca054_0 
#   ld_impl_linux-64   anaconda/cloud/conda-forge/linux-64::ld_impl_linux-64-2.40-hf3520f5_7 
#   libffi             anaconda/cloud/conda-forge/linux-64::libffi-3.4.2-h7f98852_5 
#   libgcc-ng          anaconda/cloud/conda-forge/linux-64::libgcc-ng-14.1.0-h77fa898_0 
#   libgomp            anaconda/cloud/conda-forge/linux-64::libgomp-14.1.0-h77fa898_0 
#   libnsl             anaconda/cloud/conda-forge/linux-64::libnsl-2.0.1-hd590300_0 
#   libsqlite          anaconda/cloud/conda-forge/linux-64::libsqlite-3.46.0-hde9e2c9_0 
#   libuuid            anaconda/cloud/conda-forge/linux-64::libuuid-2.38.1-h0b41bf4_0 
#   libxcrypt          anaconda/cloud/conda-forge/linux-64::libxcrypt-4.4.36-hd590300_1 
#   libzlib            anaconda/cloud/conda-forge/linux-64::libzlib-1.3.1-h4ab18f5_1 
#   ncurses            anaconda/cloud/conda-forge/linux-64::ncurses-6.5-h59595ed_0 
#   openssl            anaconda/cloud/conda-forge/linux-64::openssl-3.3.1-hb9d3cd8_3 
#   pip                anaconda/cloud/conda-forge/noarch::pip-24.2-pyhd8ed1ab_0 
#   python             anaconda/cloud/conda-forge/linux-64::python-3.10.14-hd12c33a_0_cpython 
#   readline           anaconda/cloud/conda-forge/linux-64::readline-8.2-h8228510_1 
#   setuptools         anaconda/cloud/conda-forge/noarch::setuptools-72.2.0-pyhd8ed1ab_0 
#   tk                 anaconda/cloud/conda-forge/linux-64::tk-8.6.13-noxft_h4845f30_101 
#   tzdata             anaconda/cloud/conda-forge/noarch::tzdata-2024a-h0c530f3_0 
#   wheel              anaconda/cloud/conda-forge/noarch::wheel-0.44.0-pyhd8ed1ab_0 
#   xz                 anaconda/cloud/conda-forge/linux-64::xz-5.2.6-h166bdaf_0 

> conda activate test

> pip install fastapi
# Successfully installed annotated-types-0.7.0 anyio-4.4.0 exceptiongroup-1.2.2 fastapi-0.112.2 idna-3.8 pydantic-2.8.2 pydantic-core-2.20.1 sniffio-1.3.1 starlette-0.38.2 typing-extensions-4.12.2

> pip install 'uvicorn[standard]'
# Successfully installed click-8.1.7 h11-0.14.0 httptools-0.6.1 python-dotenv-1.0.1 pyyaml-6.0.2 uvicorn-0.30.6 uvloop-0.20.0 watchfiles-0.23.0 websockets-13.0

the python code:

from fastapi import FastAPI, Request

app = FastAPI()

@app.middleware("http")
async def show_request_data(request: Request, call_next):
    print("headers: ", request.headers)
    body = await request.body()
    print(body)
    response = await call_next(request)
    return response

@app.post("/item")
def read_item(item: dict):
    return {"status": "ok", "item": item}

when i make a request with java.net.http.HttpClient, the output of the print is:

headers:  Headers({'connection': 'Upgrade, HTTP2-Settings', 'content-length': '23', 'host': 'localhost:8000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA', 'upgrade': 'h2c', 'user-agent': 'Java-http-client/17.0.9', 'content-type': 'application/json; charset=utf-8'})
b''    # output of 'print(body)' (with httptools)

I uninstall httptools [pip uninstall -y httptools] and make the same request, the output is:

headers: Headers({'connection': 'Upgrade, HTTP2-Settings', 'content-length': '23', 'host': 'localhost:8000', 'http2-settings': 'AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA', 'upgrade': 'h2c', 'user-agent': 'Java-http-client/17.0.9', 'content-type': 'application/json; charset=utf-8'})
b'{"id":1,"name":"test1"}'    # output of 'print(body)' (after httptools uninstalled)
Kludex commented 1 month ago

No... This should be on Uvicorn.

1st1 commented 1 month ago

is it a bug for httptools

Extremely unlikely. Please file an issue with uvicorn.