matt-42 / lithium

Easy to use C++17 HTTP Server with no compromise on performances. https://matt-42.github.io/lithium
https://matt-42.github.io/lithium
MIT License
1.26k stars 91 forks source link

Adding subapi deletes home page route. #98

Closed CJFEdu closed 2 years ago

CJFEdu commented 2 years ago

I'm looking through the code to try find a work around for this, but if you have any ideas I'd appreciate it.

As far as I can tell, the only way to get a home page to load on just the plain url is to pass an empty string to the get method like this.

li::http_api api;

api.get("") = [&](li::http_request& request, li::http_response& response) {
    response.write("hello world.");
  };

That works just fine so no problem there. However, when you add a subapi this gets removed or overwritten some how and you get the error.

Route / does not exist.

This wouldn't be a huge problem except I'm trying to add a static files directory

api.add_subapi("/root", serve_directory("/path/to/the/directory/to/serve"));

CJFEdu commented 2 years ago

An update on what I've found so far.

If you use api.get("") a home page will open on the default url no problem. However, if you use api.get("/") you get the route does not exist error. Not entirely sure why this is since as far as I can tell the / is removed when adding it to the routing table.

However, when you add a subapi api.get("") stops working. Again, not entirely sure why since the original route seems to still be in the route table. However, it has at least something to do with the search for the route in the call function. The route is supposed to remove the trailing / from route2. However, for some reason route2 is getting a space added to the end of it which is then getting removed instead of the trailing /. I added a simple while loop to remove all the spaces at the end before the code to remove the trailing / and the homepage started working again. Again, I don't know exactly why this fixed the problem, since I'm pretty sure that space is there all the time.

A possible solution would be to change the if statement that removes the trailing / to a while loop that keeps removing the last element from route2 while that element is a space or a trailing / and the size of route2 is > 0. However, since I don't know where half of this behavior is coming from I could be completely wrong on that.

matt-42 commented 2 years ago

Hi @CJFEdu Thanks a lot for the report. I fixed it in d79053f7aef4a3b77f7df8b1b34512ccaf028131