robiwano/siesta

Problem with mapping URI

Opened this issue · 1 comments

Say I have

holder += httpServer->addRoute(HttpMethod::GET, "/a/:b/:c", foo);
holder += httpServer->addRoute(HttpMethod::GET, "/a", bar);

The above code will work with URI like "/a/bb/cc" being directed to the function foo and "/a" to bar.

However if I switch the order around

holder += httpServer->addRoute(HttpMethod::GET, "/a", bar);
holder += httpServer->addRoute(HttpMethod::GET, "/a/:b/:c", foo);

"/a/bb/cc" can no longer be directed to the correct function.

How to reproduce:

#include <siesta/server.h>
using namespace siesta;

#include <chrono>
#include <iostream>
#include <sstream>
#include <thread>

#include "ctrl_c_handler.h"

void foo(const server::rest::Request& req, server::rest::Response& resp)
{
    std::cout << "This is foo\n" << std::endl;
}
void bar(const server::rest::Request& req, server::rest::Response& resp)
{
    std::cout << "This is bar\n" << std::endl;
}

int main(int argc, char** argv)
{
    ctrlc::set_signal_handler();
    try {
        bool rest_shutdown = false;
        std::string addr   = "http://127.0.0.1:8090";
        if (argc > 1) {
            addr = argv[1];
        }
        auto server = server::createServer(addr);
        server->start();
        std::cout << "Server started, listening on port " << server->port()
                  << std::endl;

        server::TokenHolder h;
        
        h += server->addRoute(HttpMethod::GET, "/a", bar);
        h += server->addRoute(HttpMethod::GET, "/a/:b/:c", foo);
        
        while (!ctrlc::signalled() && !rest_shutdown) {
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }

        std::cout << "Server stopped!" << std::endl;
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        return -1;
    }

    return 0;
}

Thanks for reporting. Reproduced. Problem is that the second addRoute replaces the first. Need to think how to represent multiple routes with same "base URI".