diff --git a/libraries/http_server/http_server/content_types.hh b/libraries/http_server/http_server/content_types.hh index ad883d5..d212e8a 100644 --- a/libraries/http_server/http_server/content_types.hh +++ b/libraries/http_server/http_server/content_types.hh @@ -35,7 +35,6 @@ static std::unordered_map 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"}, @@ -871,6 +870,8 @@ static std::unordered_map 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"}, diff --git a/libraries/http_server/http_server/dynamic_routing_table.hh b/libraries/http_server/http_server/dynamic_routing_table.hh index f469871..783c262 100644 --- a/libraries/http_server/http_server/dynamic_routing_table.hh +++ b/libraries/http_server/http_server/dynamic_routing_table.hh @@ -60,6 +60,8 @@ template 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)) @@ -72,16 +74,15 @@ template 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); diff --git a/libraries/http_server/tests/subapi.cc b/libraries/http_server/tests/subapi.cc index 360c8fe..4141e18 100644 --- a/libraries/http_server/tests/subapi.cc +++ b/libraries/http_server/tests/subapi.cc @@ -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."); } diff --git a/libraries/json/tests/number_parser.cc b/libraries/json/tests/number_parser.cc index 8d2e905..255e78d 100644 --- a/libraries/json/tests/number_parser.cc +++ b/libraries/json/tests/number_parser.cc @@ -1,10 +1,12 @@ #include +#include #include #include #include +template int test_integer(const char* str) { - int f; + I f; const char* end; li::internal::parse_int(&f, str, &end); @@ -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(#N) == N); #define TESTF(N) assert(std::abs(test_float(#N) - N) < 1e-5); int main() { + assert(test_integer("18446744073709551615") == 18446744073709551615ULL); TEST(0); TEST(1); TEST(-1); diff --git a/single_headers/lithium.hh b/single_headers/lithium.hh index 4390ef4..9e0139d 100644 --- a/single_headers/lithium.hh +++ b/single_headers/lithium.hh @@ -6560,6 +6560,8 @@ template 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)) @@ -6573,12 +6575,14 @@ template 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); @@ -8050,7 +8054,6 @@ static std::unordered_map 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"}, @@ -8886,6 +8889,8 @@ static std::unordered_map 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"}, diff --git a/single_headers/lithium_http_server.hh b/single_headers/lithium_http_server.hh index 5090608..1266d16 100644 --- a/single_headers/lithium_http_server.hh +++ b/single_headers/lithium_http_server.hh @@ -3380,6 +3380,8 @@ template 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)) @@ -3393,12 +3395,14 @@ template 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); @@ -4870,7 +4874,6 @@ static std::unordered_map 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"}, @@ -5706,6 +5709,8 @@ static std::unordered_map 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"},