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);

Parameter

Parameter Description
method The HTTP method that should be redirected to a callback function.
callback The callback function that is called.

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);
Anmerkung: Verbs that are not already implemented for use must be added manually.
Wichtig: The following verbs are blacklisted and can not be used: "CONNECT", "HEAD", "GET", "POST" and ""(empty).
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

CTRL PlugIn

Availability

CTRL