cpp-httplib搭建静态文件服务器

静态文件服务器在日常生活中很常用!
本文尝试使用cpp-httplib来搭建一个静态文件服务器

官方文档中关于静态服务器的说明:

// Mount / to ./www directory
auto ret = svr.set_mount_point("/", "./www");
if (!ret) {
  // The specified base directory doesn't exist...
}
// Mount /public to ./www directory
ret = svr.set_mount_point("/public", "./www");
// Mount /public to ./www1 and ./www2 directories
ret = svr.set_mount_point("/public", "./www1"); // 1st order to search
ret = svr.set_mount_point("/public", "./www2"); // 2nd order to search
// Remove mount /
ret = svr.remove_mount_point("/");
// Remove mount /public
ret = svr.remove_mount_point("/public");

官方已经内置的MIME类型:

自定义MIME类型:

// User defined file extension and MIME type mappings
svr.set_file_extension_and_mimetype_mapping("cc", "text/x-c");
svr.set_file_extension_and_mimetype_mapping("cpp", "text/x-c");
svr.set_file_extension_and_mimetype_mapping("hh", "text/x-h");

为了方便省事,直接在上篇文章中的Qt程序中进行更改。
代码如下:

#include <httplib.h>
#include <iostream>

using namespace httplib;

void wuhan(const Request &req, Response &res)
{
    printf("httplib server recv a req: %s
 ", req.path.c_str() );
    res.set_content("<html>  
                    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 
            <h1> 武汉, 加油!</h1></html>",
                                    "text/html");
    res.status = 200;
}

int main(void)
{
    Server svr;
    svr.set_base_dir("./");

    /// Static file server
    // Mount / to ./www directory
    svr.set_file_extension_and_mimetype_mapping("cc", "text/x-c");
    svr.set_file_extension_and_mimetype_mapping("cpp", "text/x-c");
    svr.set_file_extension_and_mimetype_mapping("hh", "text/x-h");
    svr.set_file_extension_and_mimetype_mapping("h", "text/x-h");
    svr.set_file_extension_and_mimetype_mapping("mp3", "audio/mpeg");
    svr.set_file_extension_and_mimetype_mapping("mp4", "video/mpeg");
    svr.set_file_extension_and_mimetype_mapping("avi", "video/x-msvideo");
    auto ret = svr.set_mount_point("/", "./www");
    if (!ret) {
        // The specified base directory doesn't exist...
        std::cout << "The specified base directory doesn't exist..." << std::endl;
    }

    /// Get
    svr.Get("/wuhan", wuhan);

    svr.Get("/hi", [](const Request& req, Response& res) {
        res.set_content("Hello World!", "text/plain");
    });

    svr.Get(R"(/numbers/(d+))", [&](const Request& req, Response& res) {
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
    });

    svr.Get("/body-header-param", [](const Request& req, Response& res) {
        if (req.has_header("Content-Length")) {
            auto val = req.get_header_value("Content-Length");
        }
        if (req.has_param("key")) {
            auto val = req.get_param_value("key");
        }
        res.set_content(req.body, "text/plain");
    });

    svr.Get("/stop", [&](const Request& req, Response& res) {
        svr.stop();
    });

    /// listen
    svr.listen("localhost", 1234);
}

构建运行,前提需要在生成目录下放置好静态资源,我的目录如下:

原文地址:https://www.cnblogs.com/MakeView660/p/12778413.html