Registers a CTRL function as a Web resource |
Synopsis
int httpConnect(string work, string resourceName [, string contentType]); |
Parameter
Parameter |
Description |
work |
Work function, which is called when the given web-resource is requested. |
resourceName |
The URL name, which calls the given CTRL function when requested. |
contentType |
optional definition of the type of the result, which is generated by the given CTRL function and sent to the client. Default: text/html |
Return value
httpConnect() returns 0 on success, otherwise -1. |
Errors
Errors can be queried with getLastError(). Errors can be:
|
Description
The function httpConnect() registers a CTRL-function as a callback-function under the given Web-Resource name in the httpServer. The CTRL-function is executed when the resourceName is requested. A given Web-Resource can only call one CTRL-function, that means a second call to httpConnect() with the same Web-Resource name with lead to an error, and -1 is returned. The callback-function must return a string, which will then be sent to the client. The content of this string must be of the type, which was given at the registration with httpConnect(). Default is: text/html.
|
The given callback-function must have the following definition: |
string|dyn_string workCB([dyn_string names [, dyn_string values [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues ]]]]]]) |
The name of the work-function is arbitrary. The arguments names and values contain the list of variable-names and the corresponding values, which are extracted from the query-part of a given URL. The IP address is according to the common DOT notation. The user is the authorized, or "", if no authorization is desired. If a client sends the following request
www.x.y/example?firstVar=value1&secondVar=value2
then the registered CTRL-function will have 2 elements in the dyn_string names argument, whereby the 1. is "firstVar" and the 2. is "secondVar", and 2 elements in the dyn_string values, whereby the 1. is "value1" and the 2. is "value2".
string workCB([string content [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues]]]]]) When using other content
types, the content of the CTRL callback function has to be a blob, according
to the following syntax: string workCB([blob content [, string user [, string ip [, dyn_string headerNames [, dyn_string headerValues]]]]]) The POST method limits the content length to 100 KByte and can be increased with the function httpSetMaxContentLength(). The content types, which are recognized by the server by their extension, can be extended configurable. (The existing types remain as hard coded defaults). The HTTP server loads all files with the name "contentTypes.txt" in the config directory in the project hierarchy. This file contains - separated by white spaces (tabs, blanks) - two entries per line: contentType FileExtension, for example, text/richtext rtf. In this case the server would send a file with the extension ".rtf" with the contentType "text/richtext". The check of the extension is case insensitive. Lines starting with "#" are ignored (comments). Note The communication between a server and a client can block
the system. Make sure that the HTTP server is started in an own CTRL manager
to guarantee the correct functionality of all functions and of the |
Example |
The CTRL function example() is called if a client requests the resource "/firstExample". Enter http://localhost/firstExample to your browser and the resource firstExample "This is my first HTML example" is shown.
CAUTION Note that if you call the functions via the UI, you have to include the "CtrlHTTP" dll to your code: #uses "CtrlHTTP".
|
main() { httpServer(); // installs the HTTP server httpConnect("example", "/firstExample"); } string example() { return "<html><head><title>First Example</title></head>" "<body>This is my first HTML example</body></html>"; } |
Example
Enter http://localhost/t1 to your browser. In this example the return value is a dyn_string. The text: "This is a Test. X-User_defined_header: this is a test header" is shown.
|
Example
Enter http://localhost/Example
to your browser. In this example the return value
is a dyn_mixed. The text: "This is a Test. Status: 404 Not found is shown.
|
Enter http://localhost/Example to your browser. In this example the return value is a string (content of a file). The file C:/TEXT.TXT has to exist.
|
Member of
CTRL PlugIn |
Availability
CTRL |
See also
HTTP Server, Control Basics, httpServer(), httpDisconnect(), httpSetMaxAge() |
V 3.11 SP1
Copyright