Skip to content

Commit

Permalink
fix(http): fix parse path from message (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
hycdong authored Nov 16, 2020
1 parent 81b1005 commit 82e10a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
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) {
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

0 comments on commit 82e10a2

Please sign in to comment.