yhirose/cpp-httplib

After setting up a routing mapping function for GET requests, it fails to be invoked correctly and directly returns a 404 error.

qin-fire opened this issue · 2 comments

I set up a simple video-on-demand server using httplib, and registered some routing mapping functions before starting it. Strangely, while other methods can be invoked and return results correctly, the GET request handling function registered for the "/video" path is not called and directly returns a 404 error. I'm not sure whether this is related to httplib or a code error, and I don't know how to solve it at present.

bool RunModule()
        {
            // 1.initialize
            tb_video = new TableVideo();
            vod::FileUtil(WWW_ROOT).CreateDirectory();
            std::string video_real_path = WWW_ROOT + std::string(VIDEO_ROOT);
            vod::FileUtil(video_real_path).CreateDirectory();
            std::string image_real_path = WWW_ROOT + std::string(IMAGE_ROOT);
            vod::FileUtil(image_real_path).CreateDirectory();
            // 2.set up server
            _svr.set_mount_point("/", WWW_ROOT);
            _svr.Post("/video", Insert);
            _svr.Delete("/video/(\\d+)", Delete);
            _svr.Put("/video/(\\d+)", Update);
            _svr.Get("/video", SelectAll);
            _svr.Get("/video/(\\d+)", SelectOne);
            // 3.start
            _svr.listen("0.0.0.0", _port);

            return true;
        }
namespace vod
{
#define WWW_ROOT "./www"
#define VIDEO_ROOT "/video/"
#define IMAGE_ROOT "/image/"
    TableVideo *tb_video = nullptr;
    class Server
    {
    private:
        int _port;
        httplib::Server _svr;

    private:
        static void Insert(const httplib::Request &req, httplib::Response &res)
        {
            ......
        }
        static void Update(const httplib::Request &req, httplib::Response &res)
        {
           ......
        }
        static void Delete(const httplib::Request &req, httplib::Response &res)
        {
            ......
        }
        static void SelectOne(const httplib::Request &req, httplib::Response &res)
        {
           ......
        }
        static void SelectAll(const httplib::Request &req, httplib::Response &res)
        {
            //cout<<"Server:SelectAll"<<endl;//debug:no call
            bool select_flag = true; 
            std::string search_key;
            if (req.has_param("search") == true)
            {
                select_flag = false; 
                search_key = req.get_param_value("search");
            }
            Json::Value videos;
            if (select_flag == true)
            {
                if (tb_video->SelectAll(&videos) == false)
                {
                    res.status = 500;
                    res.body = R"({"result":false, "reason":"mysql select all failed!"})";
                    res.set_header("Content-Type", "application/json");
                    return;
                }
            }
            else
            {
                if (tb_video->SelectLike(search_key, &videos) == false)
                {
                    res.status = 500;
                    res.body = R"({"result":false, "reason":"mysql select pattern failed!"})";
                    res.set_header("Content-Type", "application/json");
                    return;
                }
            }
            JsonUtil::Serialize(videos, &res.body);
            res.set_header("Content-Type", "application/json");
            return;
        }

    public:
        Server(int port) : _port(port) {}
        bool RunModule()
        {
            //1.
            tb_video = new TableVideo();
            vod::FileUtil(WWW_ROOT).CreateDirectory();
            std::string video_real_path = WWW_ROOT + std::string(VIDEO_ROOT);
            vod::FileUtil(video_real_path).CreateDirectory();
            std::string image_real_path = WWW_ROOT + std::string(IMAGE_ROOT);
            vod::FileUtil(image_real_path).CreateDirectory();
            // 2.
            _svr.set_mount_point("/", WWW_ROOT);
            _svr.Post("/video", Insert);
            _svr.Delete("/video/(\\d+)", Delete);
            _svr.Put("/video/(\\d+)", Update);
            _svr.Get("/video", SelectAll);
            _svr.Get("/video/(\\d+)", SelectOne);
            // 3.
            _svr.listen("0.0.0.0", _port);

            return true;
        }
    };
}

@qin-fire thank you for the report, but I am not able to give you meaningful information because I don't know your network environment and how a client sends requests...