-1

I'd like to use pion 5.0.6 as a small webserver in a VS2017 c++ project. For static routes I can use

add_resource("/my/static/route", <handler>)

I would need dynamic routes as well - like "/data/:id/info How do I do this?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
x y
  • 911
  • 9
  • 27
  • This is *not* a tutorial site. Show your code and ask a *specific* question related to it or this is off-topic. – Jesper Juhl Jun 12 '18 at 20:27
  • 1
    Ähm, sorry if my question sounded like a simple tutorial request - it is not! Pion only allows static routes, and if someone who has already worked with that lib has already faced this problem, I would be glad to get a more constructive answer than Jesper's.. – x y Jun 12 '18 at 21:18
  • @x y - I didn't provide an *answer*. Merely a *comment*. – Jesper Juhl Jun 12 '18 at 21:23
  • .. which is fine, except if the comment is too hastily. As I said before, the "How do I do this?" might be misleading. I am just trying to find a workaround for the fact, that *pion* is *not* pistache - nor ist it *restbed*. But I *have* to use it. So, if you can help me with another software related "comment" I would be glad ;) – x y Jun 12 '18 at 21:32

1 Answers1

0

For those who may need it: I found a solution to add dynamic routing to the pion webserver. It requires smart router code I found at hxoht on github, and works the way that

  • all routes - static and dynamic - are set with httpd->add_resource(<url>, <handler);
  • a 404-handler has to be set with httpd->set_not_found_handler(<handler>); and is responsible for dispatching the dynamic routes to the handlers added above.
  • your webserver class must derive from pion::http::server in order to find the handler by name with httpd->find_request_handler(<url>, <handler>);
  • in your 404-handler, you use the Match::test(<dynamic-route>) method to detect a dynamic route - like in the following code fragment:

    void handle_404(http::request_ptr& req, tcp::connection_ptr& con)
    {
        Route target;
        Match dynamic = target.set(req->get_resource());
        for (auto& route : dynamic_routes) // Our list of dynamic routes
        {
            if (dynamic.test(route)) // Does the url match the dynamic route pattern?
            {
                request_handler_t h;
                if (find_request_handler(route, h))
                {
                    auto name = get_param_name(route); // e.g. /a/:b -> "b"
                    value = dynamic.get(name); // Save value in string or map<name, value>
                    h(req, con); // Call original handler with value set properly
                    return;
                }
            }
        }
        // If no match then return a 404.
        http::response_writer_ptr w(http::response_writer::create(con, *req,
        boost::bind(&tcp::connection::finish, con)));
        http::response& res = w->get_response();
        res.set_status_code(http::types::RESPONSE_CODE_NOT_FOUND);
        res.set_status_message(http::types::RESPONSE_MESSAGE_NOT_FOUND);
        w->send();
    }
    

For using the pion webserver in a multi-threaded way, I would store the parsed value inside the request object, which would be derived from pion::http::request.

This would work for Windows and Linux :)

x y
  • 911
  • 9
  • 27