Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with mapping URI #4

Open
Rivers47 opened this issue Oct 13, 2021 · 1 comment
Open

Problem with mapping URI #4

Rivers47 opened this issue Oct 13, 2021 · 1 comment

Comments

@Rivers47
Copy link

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;
}
@robiwano
Copy link
Owner

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants