libonion
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Functions
URL handlers

Data Structures

struct  onion_url_t
 Url regexp pack. This is also a handler, and can be converted with onion_url_to_handle. More...

Functions

int onion_url_handler (onion_url_data **dd, onion_request *request, onion_response *response)
 Performs the real request: checks if its for me, and then calls the inside level.
onion_urlonion_url_new ()
 Creates the URL handler to map regex urls to handlersThe onion_url object can be used to add urls as needed using onion_url_add_*.
void onion_url_free (onion_url *url)
 Frees the url.
int onion_url_add_handler (onion_url *url, const char *regexp, onion_handler *next)
 Adds a new handler with the given regexp.Adds the given handler.
int onion_url_add (onion_url *url, const char *regexp, void *handler)
 Helper to simple add basic handlers.
int onion_url_add_with_data (onion_url *url, const char *regexp, void *handler, void *data, void *f)
 Helper to simple add a basic handler with data.
int onion_url_add_url (onion_url *url, const char *regexp, onion_url *handler)
 Adds a regex url, with another url as handler.
int onion_url_add_static (onion_url *url, const char *regexp, const char *text, int http_code)
 Adds a simple handler, it has static data and a default return code.
onion_handleronion_url_to_handler (onion_url *url)
 Returns the related handler for this url object.

Detailed Description

Creates an handler that checks that crrent path matches the current regexp, and passes to next.

Function Documentation

int onion_url_add ( onion_url url,
const char *  regexp,
void *  handler 
)

Helper to simple add basic handlers.

Adds a simple handler, with no custom data.

Returns
0 if everything ok. Else there is a regexp error.

References onion_handler_t::onion_handler_new(), and onion_url_add_handler().

int onion_url_add_handler ( onion_url url,
const char *  regexp,
onion_handler next 
)

Adds a new handler with the given regexp.Adds the given handler.

Adds a handler, using handler methods.

Returns
0 if everything ok. Else there is a regexp error.

References ONION_ERROR, onion_handler_t::onion_handler_get_private_data(), onion_low_free(), onion_low_malloc(), onion_low_strdup(), OUD_REGEXP, and OUD_STRCMP.

Referenced by onion_url_add(), onion_url_add_url(), and onion_url_add_with_data().

int onion_url_add_static ( onion_url url,
const char *  regexp,
const char *  text,
int  http_code 
)

Adds a simple handler, it has static data and a default return code.

References onion_low_malloc(), onion_low_strdup(), and onion_url_add_with_data().

int onion_url_add_url ( onion_url url,
const char *  regexp,
onion_url handler 
)

Adds a regex url, with another url as handler.

Addsa another url on this regexp.

Returns
0 if everything ok. Else there is a regexp error.

References onion_url_add_handler().

int onion_url_add_with_data ( onion_url url,
const char *  regexp,
void *  handler,
void *  data,
void *  f 
)

Helper to simple add a basic handler with data.

Adds a handler, with custom data.

Returns
0 if everything ok. Else there is a regexp error.

References onion_handler_t::onion_handler_new(), and onion_url_add_handler().

Referenced by onion_url_add_static().

void onion_url_free ( onion_url url)

Frees the url.

Frees the url data.

References onion_handler_t::onion_handler_free().

int onion_url_handler ( onion_url_data **  dd,
onion_request request,
onion_response response 
)
onion_url * onion_url_new ( )

Creates the URL handler to map regex urls to handlersThe onion_url object can be used to add urls as needed using onion_url_add_*.

The URLs can be regular expressions or simple strings. The way to discriminate them is just to check the first character; if its ^ its a regular expression.

If a string is passed then the full path must match. If its a regexp, just the begining is matched, unless $ is set at the end. When matched, this is removed from the path.

It is important to note that when the user pretends to match the initial path elements, to later pass to another handler that will do path transversal (another url object, for example), the path must be written with a regular expression, for example: "^login/". If the user just writes the string it will match only for that specific URL, and subpaths are not in the definition.

When looking for a match, they are looked in order.

Examples::

onion_url_add(url, "index.html", index); // Matches the exact filename. Not compiled.
onion_url_add(url, "^static/", onion_handler_export_local_new(".") ); // Export current directory at static
onion_url_add(url, "^icons/(.*)", directory); // Compiles the regexp, and uses the .* as first argument.
onion_url_add(url, "", redirect_to_index); // Matches an empty path. Not compiled.

Regexp can have groups, and they will be added as request query parameters, with just the number of the group as key. The groups start at 1, as 0 should be the full match, but its not added for performance reasons; its a very strange situation that user will need it, and always can access full path with onion_request_get_fullpath. Also all expression can be a group, and passed as nr 1.:

onion_url_add(url, "^index(.html)", index);
...
onion_request_get_query(req, "1") == ".html"

Be careful as . means every character, and dots in URLs must be with a backslash \ (double because of C escaping), if using regexps.

Regular expressions are used by the regexec(3) standard C library. Check that documentation to check how to create proper regular expressions. They are compiled as REG_EXTENDED.

References onion_handler_t::onion_handler_new(), onion_low_calloc(), onion_url_free_data(), and onion_url_handler().

Referenced by onion_root_url().

onion_handler * onion_url_to_handler ( onion_url url)

Returns the related handler for this url object.

Returns the related handler for this url.