Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stroika::Frameworks::WebServer::Route Class Reference

#include <Router.h>

Public Member Functions

 Route (const RegularExpression &verbMatch, const RegularExpression &pathMatch, const RequestHandler &handler)
 
nonvirtual bool Matches (const Request &request, Sequence< String > *pathRegExpMatches=nullptr) const
 

Detailed Description

&&& HEAD and OPTIONS can have routes to be overridden. But by default they are handled automatically by the router.

 @todo - we probably want to add ability to generically parse out arguments from url, and include them to handler (as rails does - handy for ID in REST)

 @todo need more generic matching to fit in (maybe optional matcher that takes URL?, or even full Request).

   NOTE - may verb match and path match each OPTIONALS in class and have maybe a LIST of THINGS we know how to match.
        VERB
        RELPATH
        HTTP HEADER (like SOAPACTION)
        RequestObject????
    then we use "Route" to make generic the mapping of a request to a Handler.

  @todo NEED to support NESTED Routes (or aggregated).
          Key is need stuff like 'default error handling' - and just to somehow inherit/copy that.
         (reconsider - maybe easy now that I have 'handled' flag)...
Note
Thread-Safety C++-Standard-Thread-Safety-For-Envelope-Letter-Internally-Synchronized But note that Matches() is a const method, so it can safely be called from any number of threads simultaneously.
Precondition
it is expected aggregated handlers provided MUST be C++-Standard-Thread-Safety-For-Envelope-Letter-Internally-Synchronized as well.

Definition at line 76 of file Router.h.

Constructor & Destructor Documentation

◆ Route()

Stroika::Frameworks::WebServer::Route::Route ( const RegularExpression verbMatch,
const RegularExpression pathMatch,
const RequestHandler handler 
)

Any route to apply the handler, must match ALL argument constraints. If verbMatch is omitted, it it assumed to be IO::Network::HTTP::MethodsRegEx::kGet (NOT RegularExpression::kAny)

Note
that the request handler is called with any String arguments based on the pathMatch regular expression.
that Routes that match on a hostRelativeURI, the hostRelativeURI is first normalized (funny characters translated to unicode, sequences of // removed etc) before matching against the regular expression. This means that it is always a mistake for a route URI match to begin with a slash, as it will never match.
Example Usage (GET with explicit method regexp)
Route{HTTP::MethodsRegEx::kGet, "session(/?)"_RegEx, [this] (Message& m) {
WriteResponse (m.rwResponse (), kSession_, kMapper.FromObject (fWSImpl_->Session_GET ()));
}},
Example Usage (GET with defaulted GET method spec, and no arguments)
Route{"session(/?)"_RegEx, [this] (Message& m) {
WriteResponse (m.rwResponse (), kSession_, kMapper.FromObject (fWSImpl_->Session_GET ()));
}},
Example Usage (GET with arg parsed from URL path)
Route{"resource/(.+)"_RegEx, [this] (Message& m, const String& resID) {
auto r = fWSImpl_->resource_GET (resID);
m.rwResponse().contentType = get<InternetMediaType> (r);
m.rwResponse().write (get<BLOB> (r));
}},
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Example Usage (POST, and grab params from Body)
Route{HTTP::MethodsRegEx::kPost, "HR(/?)"_RegEx, [this] (Message& m) {
if (optional<InternetMediaType> ct = m->request ().contentType()) {
WriteResponse (m.rwResponse (), kHR_, kMapper.FromObject (fWSImpl_->HR_POST (*ct, m->rwRequest().GetBody ())));
}
else {
Execution::Throw (Execution::RuntimeErrorException{"expected Content-Type HTTP Request header"});
}
}},
void Throw(T &&e2Throw)
identical to builtin C++ 'throw' except that it does helpful, type dependent DbgTrace() messages firs...
Definition Throw.inl:43

Definition at line 13 of file Router.inl.

Member Function Documentation

◆ Matches()

bool Route::Matches ( const Request request,
Sequence< String > *  pathRegExpMatches = nullptr 
) const

Check if the given request matches this Route. Overload taking method/hostRelPath can be derived from request, but can be substituted with different values. Overload with optional matches returns variable matches in the regexp for the path

We interpret routes as matching against a relative path from the root

Definition at line 54 of file Router.cpp.


The documentation for this class was generated from the following files: