how to parse http request in c++

后端 未结 4 2019
梦如初夏
梦如初夏 2021-02-09 01:01

I\'m trying to write a small c++ webserver which handles GET, POST, HEAD requests. My problem is I don\'t know how to parse the headers, message body, etc. It\'s listening on th

4条回答
  •  無奈伤痛
    2021-02-09 01:39

    Boost.Asio is a good library but relativel low-level. You’ll really want to use a higher level library. There’s a modern C++ library called node.native which you should check out. A very server can be implemented as follows:

    #include 
    #include 
    using namespace native::http;
    
    int main() {
        http server;
        if(!server.listen("0.0.0.0", 8080, [](request& req, response& res) {
            res.set_status(200);
            res.set_header("Content-Type", "text/plain");
            res.end("C++ FTW\n");
        })) return 1; // Failed to run server.
    
        std::cout << "Server running at http://0.0.0.0:8080/" << std::endl;
        return native::run();
    }
    

    It doesn’t get much simpler than this.

提交回复
热议问题