PhotonLibOS
PhotonLibOS copied to clipboard
HTTPServerRequest::Body() does not return the body of a request
BeastErrorCode get_req(EaseTCPStream &stream) {
BeastErrorCode ec{};
BeastBuffer buffer;
char buf[4096];
BeastRequestParser rp(req);
rp.get().body().data = buf;
rp.get().body().size = sizeof(buf);
rp.get().body().more = true;
boost::beast::http::read_header(stream, buffer, rp, ec);
while (!rp.is_done()) {
rp.get().body().data = buf;
rp.get().body().size = sizeof(buf);
rp.get().body().more = true;
boost::beast::http::read(stream, buffer, rp, ec);
if (ec == BeastError::end_of_chunk) ec = {};
if (ec) break;
}
if (!ec) {
req = rp.release();
m_origin_host = to_std_sv(req["Host"]);
}
return ec;
}
Obviously Body()
returns a pointer to a stack variable which makes no sense
Why stack variable makes no sense? Could you elaborate?
Why stack variable makes no sense? Could you elaborate?
Stack variables are destroyed once the function exits. The correct behaviour should be to read the following TCP stream bytes according to Content-Length
header, and stores the request body in some buffer that has the same lifetime of the request object.
So I guess your were writing your own HTTP server handler and found that the http request body buffer was corrupted?
Yes, I am handling a POST request and the Body()
returns garbage data
Feel free to fix this if you are convenient. And we will follow up this bug next week as well.
@howardlau1999 Thank you for reporting bugs. We are actually rewriting HTTP server completely, which will be no longer based on boost.beast. I believe this bug will solved by the new one, and I expect it will be released in the next version.
This bug has been fixed by the release of version 0.5 https://github.com/alibaba/PhotonLibOS/releases/tag/v0.5.0