Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

fix(http): fix parse path from message #663

Merged
merged 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/http/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> args;
boost::split(args, unresolved_path, boost::is_any_of("/"));
std::vector<std::string> real_args;
for (std::string &arg : args) {
if (!arg.empty()) {
real_args.emplace_back(std::move(arg));
}
}
if (real_args.size() == 0) {
Copy link
Contributor

@levy5307 levy5307 Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 std::string path;
for (const auto &real_arg: real_rags) {
            path += real_arg;
            path += '/';
}

if (!path.empty()) {
    ret.path = std::move(path.substr(value.size() - 1));
}

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 (<ip>:<port>/<service>/<method>?<arg>=<val>&<arg>=<val>)
if (!unresolved_query.empty()) {
Expand Down
16 changes: 8 additions & 8 deletions src/http/test/http_server_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down