1

Libevent library (https://libevent.org/) provides functionality to create your own HTTP server. As I know, there are two ways to set request handlers (appropriate callbacks and general callback):

    // Set callback for appropriate request
    evhttp_set_cb (server, "/test", handler_1, 0);

    // Set callback for appropriate request
    evhttp_set_cb (server, "/test/about", handler_2, 0);

    // Set general callback for all other requests
    evhttp_set_gencb (server, general_handler, 0);

http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/http_8h.html

I wonder if it is possible somehow to use evhttp_set_cb function to set one callback that will be called on appropriate pattern, like:

    /* 
      Call `handler_1` callback with next requests: 

      /test
      /test?param=value
      /test/id1 
      /test/id2 
      ...
    */
    evhttp_set_cb (server, "/test*", handler_test, 0);

I know that general callback can be used for such purposes (evhttp_set_gencb) but it will handle absolutely all requests (not only that start from /test).

Thanks in advance.

Nick
  • 65
  • 1
  • 5
  • Wouldn't it already choose the handler from most specific to least specific, with `/test/about` being more specific than `/test`? – Paul Ogilvie Jan 04 '20 at 10:00
  • With `/test/about` will be called only `handler_2` and with `/test` - `handler_1`. It's also possible to specify one callback for both `/test/about` and `/test` requests, but in that case, I should specify all endpoints in anvance in **compile time**. – Nick Jan 04 '20 at 12:24
  • 1
    Then use one handler for `/test`, which includes all its sub-paths, and dispatch in your handler for specific cases, such as `/test/about`. So don't have a separate handler for `/test/about`. – Paul Ogilvie Jan 04 '20 at 14:00
  • `/test` will not include all sub-paths. Even if I specify one handler for `/test` and `/test/about`(i.e. `test_handler`), only these two requests will use this handler. If I follow `/test/describe` request, `test_handler` will not be called for it. So, I'm looking for a way how to specify one pattern for a group of appropriate requests(i.e. `/test*`) to use one handler for all similar requests, without specifying them manually (i.e. `/test/new_endpoint`). – Nick Jan 04 '20 at 15:41
  • @Nick Were you able to figure it out? I’m doing something similar right now. – rudy Jun 05 '22 at 19:31

0 Answers0