Skip to content

Fix connection termination when using HTTP proxies#427

Draft
Claude wants to merge 2 commits intomainfrom
claude/fix-connection-termination-issue
Draft

Fix connection termination when using HTTP proxies#427
Claude wants to merge 2 commits intomainfrom
claude/fix-connection-termination-issue

Conversation

@Claude
Copy link
Copy Markdown
Contributor

@Claude Claude AI commented Mar 23, 2026

When using HTTP proxies (e.g., v2ray), rtp2httpd forwarded upstream response headers without ensuring Connection: close was present, causing proxies to keep connections alive indefinitely even after clients disconnected or switched channels.

Changes

  • Modified HTTP proxy response header handling (src/http_proxy.c:1027-1172)

    • Filter out upstream Connection headers in both redirect and normal response paths
    • Always inject Connection: close header before sending headers to clients
    • Ensures proper connection termination regardless of upstream behavior
  • Added e2e tests (e2e/test_http_proxy.py:500-592)

    • Verify Connection: close presence in normal responses
    • Verify Connection: close presence in redirect responses
    • Verify upstream keep-alive is correctly overridden with close

Technical Details

The fix modifies http_proxy_parse_response_headers() to parse and rebuild response headers, skipping any Connection header from upstream:

/* Filter out Connection header from upstream */
while (orig_line != NULL) {
  if (strncasecmp(orig_line, "Connection:", 11) != 0) {
    /* Copy other headers */
  }
  orig_line = strtok(NULL, "\r\n");
}

/* Always add Connection: close */
connection_queue_output(session->conn, (const uint8_t *)"Connection: close\r\n", 19);

This ensures HTTP proxies correctly terminate connections when clients disconnect, fixing the issue where switching channels or closing browsers left connections hanging.

Original prompt

This section details on the original issue you should resolve

<issue_title>系统代理环境下连接终止异常</issue_title>
<issue_description>在启用系统代理时,在自带网页播放器中换台甚至是关闭浏览器都无法终止连接。经观察未发现异常日志。

测试环境:
系统架构:X86_64
操作系统:Win10 22H2
浏览器:Chrome 146.0.7680.80
代理软件:V2rayN 7.18.0
代理模式:自动配置系统代理
代理路由:黑名单

关注到串流流量是通过直连。清除系统代理后,故障现象消失。

本人能力有限,暂不清楚导致该现象的原因。</issue_description>

<agent_instructions>尝试分析和修复。rtp2httpd 本身不支持 http keep-alive,预期每个客户端连接都是 short-lived,只有当客户端断开连接后才会释放资源(rtp2httpd 会发送 Connection: close)。是否由于用户设置了 http proxy,但是 http proxy 本身没有正确处理 Connection: close?

可以结合 https://github.com/2dust/v2rayn 源码,分析它是否正确支持 Connection: close

如果可以修复,需要补充 e2e 测试来复现本 issue 提出的问题。</agent_instructions>

Comments on the Issue (you are @claude[agent] in this section)

When using an HTTP proxy (like v2ray), rtp2httpd was forwarding upstream
response headers without ensuring Connection: close was present. This
caused HTTP proxies to keep connections alive even when clients
disconnected or switched channels.

Changes:
- Filter out Connection header from upstream responses
- Always inject Connection: close in responses to clients
- Add e2e tests to verify Connection: close header presence

Fixes issue where connections cannot be terminated when switching
channels or closing browser while using system proxy.

Co-authored-by: stackia <5107241+stackia@users.noreply.github.com>
Agent-Logs-Url: https://github.com/stackia/rtp2httpd/sessions/492e1c66-6b45-40a3-9efa-2f9757bb72a4
@Claude Claude AI changed the title [WIP] Fix connection termination issue under system proxy Fix connection termination when using HTTP proxies Mar 23, 2026
@Claude Claude AI requested a review from stackia March 23, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

系统代理环境下连接终止异常

2 participants