Skip to content

Commit

Permalink
Fix dynamic routing table.
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-42 committed May 31, 2022
1 parent 61a97eb commit 9ce90b1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion libraries/http_server/http_server/content_types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"jar", "application/java-archive"},
{"ser", "application/java-serialized-object"},
{"class", "application/java-vm"},
{"js", "application/javascript"},
{"json", "application/json"},
{"jsonml", "application/jsonml+json"},
{"lostxml", "application/lost+xml"},
Expand Down Expand Up @@ -871,6 +870,8 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"csv", "text/csv"},
{"html", "text/html"},
{"htm", "text/html"},
{"js", "text/javascript"},
{"mjs", "text/javascript"},
{"n3", "text/n3"},
{"txt", "text/plain"},
{"text", "text/plain"},
Expand Down
11 changes: 6 additions & 5 deletions libraries/http_server/http_server/dynamic_routing_table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ template <typename V> struct drt_node {
pair.second->for_all_routes(f, prefix + std::string(pair.first));
}
}

// Find a route.
iterator find(const std::string_view& r, unsigned int c) const {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
Expand All @@ -72,16 +74,15 @@ template <typename V> struct drt_node {
if (r[c] == '/')
c++; // skip the first /

if (c >= r.size())
return end();

// Find the next /.
int s = c;
int url_part_start = c;
while (c < r.size() and r[c] != '/')
c++;

// k is the string between the 2 /.
std::string_view k(&r[s], c - s);
std::string_view k;
if (url_part_start < r.size() && url_part_start != c)
k = std::string_view(&r[url_part_start], c - url_part_start);

// look for k in the children.
auto it = children_.find(k);
Expand Down
1 change: 1 addition & 0 deletions libraries/http_server/tests/subapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int main() {
my_api.add_subapi("/sub", my_api2);

http_serve(my_api, 12334, s::non_blocking);
assert(http_get("http://localhost:12334").body == "hello world.");
assert(http_get("http://localhost:12334/").body == "hello world.");
assert(http_get("http://localhost:12334/sub/hello_world").body == "hello world2.");
}
7 changes: 5 additions & 2 deletions libraries/json/tests/number_parser.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <cassert>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <lithium_json.hh>

template <typename I>
int test_integer(const char* str) {
int f;
I f;
const char* end;
li::internal::parse_int(&f, str, &end);

Expand All @@ -23,10 +25,11 @@ double test_float(const char* str) {
return f;
}

#define TEST(N) assert(test_integer(#N) == N);
#define TEST(N) assert(test_integer<int>(#N) == N);
#define TESTF(N) assert(std::abs(test_float(#N) - N) < 1e-5);

int main() {
assert(test_integer<uint64_t>("18446744073709551615") == 18446744073709551615ULL);
TEST(0);
TEST(1);
TEST(-1);
Expand Down
11 changes: 8 additions & 3 deletions single_headers/lithium.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6560,6 +6560,8 @@ template <typename V> struct drt_node {
pair.second->for_all_routes(f, prefix + std::string(pair.first));
}
}

// Find a route.
iterator find(const std::string_view& r, unsigned int c) const {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
Expand All @@ -6573,12 +6575,14 @@ template <typename V> struct drt_node {
c++; // skip the first /

// Find the next /.
int s = c;
int url_part_start = c;
while (c < r.size() and r[c] != '/')
c++;

// k is the string between the 2 /.
std::string_view k(&r[s], c - s);
std::string_view k;
if (url_part_start < r.size() && url_part_start != c)
k = std::string_view(&r[url_part_start], c - url_part_start);

// look for k in the children.
auto it = children_.find(k);
Expand Down Expand Up @@ -8050,7 +8054,6 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"jar", "application/java-archive"},
{"ser", "application/java-serialized-object"},
{"class", "application/java-vm"},
{"js", "application/javascript"},
{"json", "application/json"},
{"jsonml", "application/jsonml+json"},
{"lostxml", "application/lost+xml"},
Expand Down Expand Up @@ -8886,6 +8889,8 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"csv", "text/csv"},
{"html", "text/html"},
{"htm", "text/html"},
{"js", "text/javascript"},
{"mjs", "text/javascript"},
{"n3", "text/n3"},
{"txt", "text/plain"},
{"text", "text/plain"},
Expand Down
11 changes: 8 additions & 3 deletions single_headers/lithium_http_server.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3380,6 +3380,8 @@ template <typename V> struct drt_node {
pair.second->for_all_routes(f, prefix + std::string(pair.first));
}
}

// Find a route.
iterator find(const std::string_view& r, unsigned int c) const {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
Expand All @@ -3393,12 +3395,14 @@ template <typename V> struct drt_node {
c++; // skip the first /

// Find the next /.
int s = c;
int url_part_start = c;
while (c < r.size() and r[c] != '/')
c++;

// k is the string between the 2 /.
std::string_view k(&r[s], c - s);
std::string_view k;
if (url_part_start < r.size() && url_part_start != c)
k = std::string_view(&r[url_part_start], c - url_part_start);

// look for k in the children.
auto it = children_.find(k);
Expand Down Expand Up @@ -4870,7 +4874,6 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"jar", "application/java-archive"},
{"ser", "application/java-serialized-object"},
{"class", "application/java-vm"},
{"js", "application/javascript"},
{"json", "application/json"},
{"jsonml", "application/jsonml+json"},
{"lostxml", "application/lost+xml"},
Expand Down Expand Up @@ -5706,6 +5709,8 @@ static std::unordered_map<std::string_view, std::string_view> content_types = {
{"csv", "text/csv"},
{"html", "text/html"},
{"htm", "text/html"},
{"js", "text/javascript"},
{"mjs", "text/javascript"},
{"n3", "text/n3"},
{"txt", "text/plain"},
{"text", "text/plain"},
Expand Down

0 comments on commit 9ce90b1

Please sign in to comment.