PhotonLibOS icon indicating copy to clipboard operation
PhotonLibOS copied to clipboard

HTTPServerRequest::Body() does not return the body of a request

Open howardlau1999 opened this issue 1 year ago • 6 comments

    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

howardlau1999 avatar Sep 17 '22 08:09 howardlau1999

Why stack variable makes no sense? Could you elaborate?

beef9999 avatar Sep 17 '22 08:09 beef9999

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.

howardlau1999 avatar Sep 17 '22 09:09 howardlau1999

So I guess your were writing your own HTTP server handler and found that the http request body buffer was corrupted?

beef9999 avatar Sep 17 '22 09:09 beef9999

Yes, I am handling a POST request and the Body() returns garbage data

howardlau1999 avatar Sep 17 '22 09:09 howardlau1999

Feel free to fix this if you are convenient. And we will follow up this bug next week as well.

beef9999 avatar Sep 17 '22 09:09 beef9999

@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.

lihuiba avatar Sep 17 '22 09:09 lihuiba

This bug has been fixed by the release of version 0.5 https://github.com/alibaba/PhotonLibOS/releases/tag/v0.5.0

beef9999 avatar Dec 09 '22 08:12 beef9999