cppcms::url_parser
Role
This class is used for spawning requests according given URL by matching them by regular expressions.
It is defined in cppcms/url.h
header.
Constructor
url_parser(); url_parser(worker_thread *w);
Creates new URL parser. First version creates standalone parser that can be used for any input string. Second derives the string from CGI variable PATH_INFO
.
General Recommendations
Always use callbacks to setup your url dispatching functions.
Public Member Functions
Setting Handlers
Binding to callback
void add(char const *expr,callbac_t );
Binds expression to callback. callback_t
is boost::function with 9 string parameters where:
- First 8 parameters are matched expressions --- $1,..,$8 in "perl" semantics.
- Last 9 parameter is entry expression -- $0 in "perl" semantics.
For example:
class app : public application { void page(string id); ... app(...) ... { url.add("^/page/(\\d+)$", boost::bind(&app::page,this,_1)); } };
Would setup callback to page
member function and pass string id with matched number.
In versions prior to CppCMS 0.0.4, you could use macros $0, $1, ... $8 that were defined in url.h
header. They are
depricated and should not be used becuase $
in identifiers
is not supported on some platforms like ARM.
Binding to ID
void add(char const *exp,int id);
Causes url_parser::parse
return id
when match had found.
Binding to other url_parser:
void add(char const *exp,url_parser &url);
Forces url_parser
to pass string to other parser if match was found. It may be used for faster matching urls. For example:
admin_url.add("^/admin/add_user$",...); admin_url.add("^/admin/remove_user$",...); ... app_url.add("^/app/article/(\\d+)$",...); ... url.add("^/admin/.*",admin_url); url.add("^/app/.*",app_url);
Parsing URL
int parse(); int parse(string &s);
Parses URL using s
or PATH_INFO
. It returns
url_parser::ok
if callback was dispatchedurl_parser::not_found
if no match was found.- Other positive number if URL was associated with number and not callback.
If you using url_parser
that returns id, you can find matched parameters using operator []
. For example:
url.add("^/page/(\\d+)$",1); switch(url.parse()) { case 1: id=url[1]; }