From 82e10a2ff255e2ac05f8857d4b3b7bca40eb08fb Mon Sep 17 00:00:00 2001 From: HeYuchen <377710264@qq.com> Date: Mon, 16 Nov 2020 11:14:55 +0800 Subject: [PATCH] fix(http): fix parse path from message (#663) --- src/http/http_server.cpp | 21 ++++++++++++++++++++- src/http/test/http_server_test.cpp | 16 ++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/http/http_server.cpp b/src/http/http_server.cpp index 9fad528b14..58027e8a55 100644 --- a/src/http/http_server.cpp +++ b/src/http/http_server.cpp @@ -151,7 +151,26 @@ void http_server::serve(message_ex *msg) if (!unresolved_path.empty() && *unresolved_path.crbegin() == '\0') { unresolved_path.pop_back(); } - ret.path = std::move(unresolved_path); + + // parse path + std::vector args; + boost::split(args, unresolved_path, boost::is_any_of("/")); + std::vector real_args; + for (std::string &arg : args) { + if (!arg.empty()) { + real_args.emplace_back(std::move(arg)); + } + } + if (real_args.size() == 0) { + ret.path = ""; + } else { + std::string path = real_args[0]; + for (int i = 1; i < real_args.size(); i++) { + path += '/'; + path += real_args[i]; + } + ret.path = std::move(path); + } // find if there are method args (://?=&=) if (!unresolved_query.empty()) { diff --git a/src/http/test/http_server_test.cpp b/src/http/test/http_server_test.cpp index 82eb6a21e9..109014b930 100644 --- a/src/http/test/http_server_test.cpp +++ b/src/http/test/http_server_test.cpp @@ -21,14 +21,14 @@ TEST(http_server, parse_url) std::string path; } tests[] = { {"http://127.0.0.1:34601", ERR_OK, ""}, - {"http://127.0.0.1:34601/", ERR_OK, "/"}, - {"http://127.0.0.1:34601///", ERR_OK, "///"}, - {"http://127.0.0.1:34601/threads", ERR_OK, "/threads"}, - {"http://127.0.0.1:34601/threads/?detail", ERR_OK, "/threads/"}, - {"http://127.0.0.1:34601//pprof/heap/", ERR_OK, "//pprof/heap/"}, - {"http://127.0.0.1:34601//pprof///heap?detailed=true", ERR_OK, "//pprof///heap"}, - {"http://127.0.0.1:34601/pprof/heap/arg/", ERR_OK, "/pprof/heap/arg/"}, - {"http://127.0.0.1:34601/pprof///heap///arg/", ERR_OK, "/pprof///heap///arg/"}, + {"http://127.0.0.1:34601/", ERR_OK, ""}, + {"http://127.0.0.1:34601///", ERR_OK, ""}, + {"http://127.0.0.1:34601/threads", ERR_OK, "threads"}, + {"http://127.0.0.1:34601/threads/?detail", ERR_OK, "threads"}, + {"http://127.0.0.1:34601//pprof/heap/", ERR_OK, "pprof/heap"}, + {"http://127.0.0.1:34601//pprof///heap?detailed=true", ERR_OK, "pprof/heap"}, + {"http://127.0.0.1:34601/pprof/heap/arg/", ERR_OK, "pprof/heap/arg"}, + {"http://127.0.0.1:34601/pprof///heap///arg/", ERR_OK, "pprof/heap/arg"}, }; for (auto tt : tests) {