websUrlHandlerDefine | GoAhead WebServer API | GoAhead EMF |
Define a new URL handler
#include "webs.h" int websUrlHandlerDefine(char_t *path, int (*fn)(webs_t wp, char_t *url, char_t *path, char_t *query), int flags);
wp | Web server connection handle |
url | Request URL |
path | Request path portion of the URL |
query | Query string portion of the URL |
flags | Specify a sorting value to modify the calling order of the handler |
The websUrlHandlerDefine procedure is used to add a new URL handler by associating a URL path segment with a processing function. URL handlers are called in sorted order beginning with the handlers with longer path segments. For example: the handler for "/myUrl" would be called before the handler for "/my".
The flags parameter can specify two special sorting values:
If specified, the flags modify the calling order of the handler. Multiple handlers may specify these flags, in which case the order of all the handlers specifiying WEBS_HANDLER_FIRST is undefined. Similarly for multiple handlers using WEBS_HANDLER_LAST.
A URL handler is a C procedure according to the following prototype:
int myHandler(webs_t wp, char_t *url, char_t *path, char_t *query);
The url parameter contains the entire URL. The path parameter holds the URL portion after the hostname and port number. The query parameter holds any optional query.
The URL handler must return 1 if it elects to process the URL. Otherwise it should return 0 to indicate that a later URL handler should process the URL. A handler may modify the URL request using any of the websSetRequest APIs. In this case, if it returns 0, a subsequent URL handler will process the modified request.
Returns 0 if successful. Otherwise returns -1.
int myHandler(webs_t wp, char_t *url, char_t *path, char_t *query) { /* * Processing here */ return 1; } websUrlHandlerDefine("", myHandler, WEBS_HANDLER_FIRST); websUrlHandlerDefine("/mypath", myOtherHandler, 0);