MagicStack / MagicStack/httptools
[Bug] Request body lost when Upgrade: h2c + Transfer-Encoding: chunked is used
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 1.3k
- 分支
- 107
- PR 合併指標
- 30 天內沒有已合併 PR
描述
Overview
When sending a POST request from a Java RestClient (Spring Boot 3.2+, Java 21) to a FastAPI backend running on Uvicorn + httptools, we encountered a strange issue where the request body was missing.
The request looked like this:
POST /endpoint HTTP/1.1
Host: my-api.com
Upgrade: h2c
Connection: Upgrade, HTTP2-Settings
Transfer-Encoding: chunked
Content-Type: application/json
3\r\nabc\r\n0\r\n\r\n
On the server side, Uvicorn logs showed:
Unsupported upgrade requestNo request bodyInvalid HTTP request received
But when we routed the same request through ngrok or used RestTemplate instead of RestClient, it worked fine.
🔍 Root Cause
After analyzing Uvicorn’s httptools_impl.py and httptools parser behavior, we found this:
Upgrade: h2cis ignored by Uvicorn (as expected).- But internally,
httptoolsstill enters the upgrade state. - Since the upgrade is ignored and the parser is not reset, no body is parsed.
- This violates RFC 7230 §6.7, which allows the server to ignore upgrades and proceed normally.
Proposed Fix
Patch parser.pyx to resume HTTP/1.1 parsing after upgrade is ignored:
cdef int cb_on_headers_complete(cparser.llhttp_t* parser) except -1:
cdef HttpParser pyparser = <HttpParser>parser.data
try:
if parser.upgrade and not pyparser._should_upgrade():
cparser.llhttp_resume_after_upgrade(parser)
pyparser._on_headers_complete()
except BaseException as ex:
pyparser._last_error = ex
return -1
return 0
Also expose this from Python:
def resume_after_upgrade(self):
httptools.llhttp_resume_after_upgrade(self.cparser)
Then frameworks like Uvicorn can call it in:
def on_headers_complete(self):
if self.upgrade and self.upgrade.lower() != b"websocket":
self.parser.resume_after_upgrade()
Reproducible Test
def test_chunked_body_with_ignored_upgrade():
headers = {
"Upgrade": "h2c",
"Connection": "Upgrade",
"Transfer-Encoding": "chunked"
}
body = b"4\r\ntest\r\n0\r\n\r\n"
request = b"POST / HTTP/1.1\r\n" + headers_to_bytes(headers) + b"\r\n" + body
parser = HttpRequestParser(TestProtocol())
parser.feed_data(request)
assert protocol.body == b"test"
Why it matters
This is RFC-compliant behavior that should be supported.
RestClient in Java 21+ sends Upgrade: h2c by default.
Any server not resetting its parser state will lose the body.
This breaks many interop scenarios between Spring Boot and Python ASGI apps.
I'm happy to submit a PR if maintainers are open to it. Thanks for your time and for maintaining this great project!
貢獻指南
這個儲存庫沒有索引到貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
解析器回呼位於 parser.pyx;先追蹤 cb_on_headers_complete 和現有的 upgrade 處理,然後檢查 Python 解析器包裝器和可重現的解析器測試。重現分塊的 h2c 請求,並驗證在忽略 upgrade 後 body 仍獲得保留,同時確保相關解析器測試通過。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend, networking
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100