httpSetMethodHandler()
The function can be used to redirect an HTTP method to a user-defined CTRL callback function.
Synopsis
int httpSetMethodHandler( string method, string callback[, bool ignoreAuth = false]);
Parameter
| Parameter | Description |
|---|---|
| method | The HTTP method that should be redirected to a callback function. |
| callback | The callback function that is called. |
| ignoreAuth | Optional. If set to true, authentication
and permission checking is bypassed for incoming requests
using this method, and the callback function is always
executed. Default: false. |
Return value
The function returns 0 when it was successfully executed and -1 in case of errors.
Error
See the return values above.
Additionally, a warning is shown if unsupported or non-existent HTTP methods are called. In this case, the HTTP server also returns a warning.
Description
The function can be used to redirect an HTTP method ( e.g.: "PATCH", "PUT", "TRACE", "OPTIONS" or "DELETE") to a user-defined CTRL callback function. In other words, when an HTTP server receives a message that contains one of these methods, the specified CTRL callback function is called. The function must contain the following arguments:
callback(string clientIP, string url, int idx);
If the optional parameter ignoreAuth is set
to true, authentication and permission checking is bypassed
for all incoming requests using this method. The callback function is always
executed, regardless of configured authentication and permission checks. This
can be used, for example, for the HTTP method OPTIONS to answer preflight
requests without requiring authentication information in the request. When
ignoreAuth is set to false (default),
all configured authorization and permission checks are executed as usual, and
the callback function is only called when these checks pass.
| Parameter | Description |
|---|---|
| clientIP | IP of the requesting client. See example below. |
| url | The requested URL. See example below. |
| idx | The client index, meaning an internal client number. See example below. |
Example
The example redirects the HTTP method "DELETE" to the callback function "deleteCB".
#uses "CtrlHTTP"
main()
{
dyn_string names, values;
names = makeDynString("first", "second");
values = makeDynString("first value", "second-value");
DebugN(httpMakeParamString(names, values)); // => "first=first%20value&second=second-value"
//Example for httpSetMethodHandler
httpServer(false, 8080, 0);
httpSetMethodHandler("DELETE", "deleteCB");
/* For this example, the client is the local computer and the
client sends the HTTP DELETE message */
string result;
/* netDelete deletes the resource */
netDelete("http://localhost:8080/path/to/document", result);
if ( result == "done" )
DebugN("resource was deleted");
}
string deleteCB(string clientIP, string url, int idx)
{
DebugN("client " + clientIP + " has requested to delete " +
url);
return "done";
}
Assignment
Availability
CTRL
