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

websocket在回复101时Content-Length为0导致连接被关闭 #569

Open
linxiaoyan opened this issue Jun 13, 2024 · 1 comment
Open

websocket在回复101时Content-Length为0导致连接被关闭 #569

linxiaoyan opened this issue Jun 13, 2024 · 1 comment

Comments

@linxiaoyan
Copy link

使用nginx代理websocket时发现hv在回复101时Content-Length为0,导致nginx将连接关闭。
做了两处修改,请 @ithewei 帮忙review下
1、
文件:HttpMessage.h
代码:

size_t ContentLength() {
    if (content_length == 0) {
        FillContentLength();
    }
    return content_length;
}

改为:

size_t ContentLength() {
    if (content_length == 0 && !IsUpgrade()) {
        FillContentLength();
    }
    return content_length;
}

2、
文件:HttpMessage.cpp
代码:

void HttpMessage::FillContentLength() {
    auto iter = headers.find("Content-Length");
    if (iter != headers.end()) {
        content_length = atoll(iter->second.c_str());
    }
    if (content_length == 0) {
        DumpBody();
        content_length = body.size();
    }
    if (iter == headers.end() && !IsChunked() && content_type != TEXT_EVENT_STREAM) {
        if (content_length != 0 || type == HTTP_RESPONSE) {
            headers["Content-Length"] = hv::to_string(content_length);
        }
    }
}

改为:

void HttpMessage::FillContentLength() {
    auto iter = headers.find("Content-Length");
    if (iter != headers.end()) {
        content_length = atoll(iter->second.c_str());
    }
    if (content_length == 0) {
        DumpBody();
        content_length = body.size();
    }
    if (iter == headers.end() && !IsChunked() && content_type != TEXT_EVENT_STREAM) {
        if ((content_length != 0 || type == HTTP_RESPONSE) && !IsUpgrade()) {
            headers["Content-Length"] = hv::to_string(content_length);
        }
    }
}
@ithewei
Copy link
Owner

ithewei commented Jun 13, 2024

只改第二处就可以了,
另外应该是if (content_length != 0 || (type == HTTP_RESPONSE && !IsUpgrade())
content_length不等于0,无论如何都需要设置Content-Length头部

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

No branches or pull requests

2 participants