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

发现代码中因HTTP Header 大小写不统一导致无法正确解析Request Header的BUG #38

Open
LiChen23333 opened this issue Aug 21, 2023 · 1 comment

Comments

@LiChen23333
Copy link

我是在使用了vite 的proxy 进行接口转发时发现的此类问题,应该是vite的代理将我原来的header全部转为小写了,例如将
Content-Length:256368 转为了content-length:256368
这样无法正确读取到body的总长度,因此body长度一旦超过tcp窗口的最大值,则无法正确运行httpreadrequest.cpp 中 HttpReadRequest::readBody的逻辑,导致我无法继续接收下一个tcp包的body数据,所以我的post body被截断了。

修改如下:
在cwf/httpparser.cpp中 56-68行
void HttpParser::doParseHttpHeader(QByteArray &httpMessage)
int size = lines.size();
for(int i = 1, column = 0; i < size; ++i)
{
QByteArray &line = lines[i];
if(line.isEmpty())
continue;
column = line.indexOf(':');
headerField.insert(line.left(column).trimmed(), line.mid(column + 1).trimmed());
}

contentLenght = headerField.value(HTTP::CONTENT_LENGTH).toLongLong();
contentType   = headerField.value(HTTP::CONTENT_TYPE);
multiPart     = contentType.contains(HTTP::MULTIPART);

这里HTTP::CONTENT_LENGTH的定义为
const QByteArray CONTENT_LENGTH = "Content-Length";

我将这里改为了
int size = lines.size();
for(int i = 1, column = 0; i < size; ++i)
{
QByteArray &line = lines[i];
if(line.isEmpty())
continue;
column = line.indexOf(':');

    QByteArray key = line.left(column).trimmed().toLower();
    headerField.insert(key, line.mid(column + 1).trimmed());
}

contentLenght = headerField.value(HTTP::CONTENT_LENGTH.toLower()).toLongLong();
contentType   = headerField.value(HTTP::CONTENT_TYPE.toLower());
multiPart     = contentType.contains(HTTP::MULTIPART.toLower());

使用全小写去匹配,这样就可以正确解析request的header了。

希望对后来人有帮助,如果作者能看到希望可以同步到你仓库的代码中。
感谢作者!

@num8er
Copy link

num8er commented Sep 11, 2023

As we discussed in PR: #39 that client must send proper headers by RFC
https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.5 - Content-Type
https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 - Content-Length

This ISSUE must be closed and opened in clientside library to send correct by protocol standards headers.

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