HTTP发布请求解析器

时间:2020-05-03 09:49:36

标签: c++ rest http boost boost-asio

我打算在服务器代码中解析从客户端收到的http POST请求。我正在使用Postman应用程序通过POST方法将文件发送到服务器上。我的问题是如何解析服务器端的POST请求。我的服务器代码是C ++,客户端将使用POST请求发送〜80MB文件。 我已经参考了示例代码,但是它们都没有显示如何解析传入文件中的POST请求。

https://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/http/server/

有人可以为此提供帮助的示例代码吗?

致谢

1 个答案:

答案 0 :(得分:1)

让我们开始在端口8081上接受单个连接:

net::io_context io;
tcp::acceptor a(io, {{}, 8081});

tcp::socket s(io);
a.accept(s);

现在,让我们阅读一个HTTP请求:

http::request<http::string_body> req;
net::streambuf buf;
http::read(s, buf, req);

仅此而已。我们可以打印一些详细信息,然后发送回复。假设我们要保存上传的文件:

std::cout << "Writing " << req.body().size() << " bytes to " << fname << "\n";
std::ofstream(fname) << req.body();

我们还将整个有效负载作为响应内容发送回去:

    http::response<http::string_body> response;
    response.reason("File was accepted");
    response.body() = std::move(req.body());
    response.keep_alive(false);
    response.set("XXX-Filename", fname);

    http::write(s, response);

完整演示

Live On Coliru

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/optional/optional_io.hpp>
#include <iostream>
#include <fstream>

namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using net::ip::tcp;
using namespace std::string_literals;

static std::string const fname = "upload.txt";

int main() {
    net::io_context io;
    tcp::acceptor a(io, {{}, 8081});

    tcp::socket s(io);
    a.accept(s);

    std::cout << "Receiving request from " << s.remote_endpoint() << "\n";

    http::request<http::string_body> req;
    net::streambuf buf;
    http::read(s, buf, req);

    std::cout << "Method: " << req.method() << "\n";
    std::cout << "URL: " << req.target() << "\n";
    std::cout << "Content-Length: "
        << (req.has_content_length()? "explicit ":"implicit ")
        << req.payload_size() << "\n";

    std::cout << "Writing " << req.body().size() << " bytes to " << fname << "\n";
    std::ofstream(fname) << req.body();

    {
        http::response<http::string_body> response;
        response.reason("File was accepted");
        response.body() = std::move(req.body());
        response.keep_alive(false);
        response.set("XXX-Filename", fname);

        http::write(s, response);
    }
}

在使用CLI POST实用程序(例如Ubuntu上的apt install libwww-perl)进行测试时:

POST http://localhost:8081/this/url?id=$RANDOM -H 'Host: demo.site' -H 'CustomHeader' -E -C 'user:password' < test.cpp

它将打印如下内容:

POST http://localhost:8081/this/url?id=31912
Host: demo.site
User-Agent: lwp-request/6.31 libwww-perl/6.31
Content-Length: 1300
Content-Type: application/x-www-form-urlencoded
CustomHeader: 

200 File was accepted
Connection: close
Client-Date: Sun, 03 May 2020 20:58:58 GMT
Client-Peer: 127.0.0.1:8081
Client-Response-Num: 1
XXX-Filename: upload.txt

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/optional/optional_io.hpp>
...

接着是test.cpp文件的其余部分

您可以在没有POST的情况下进行类似的请求,例如使用curl

curl http://127.0.0.1:8081/this/url?id=$RANDOM -H 'Host: demo.site' -d @test.cpp