Processing HTTP requests
The 4D web server provides several features to handle HTTP requests:
- the
On Web Connection
database method, a router for your web application, - the
/4DACTION
URL to call server-side code WEB GET VARIABLES
to get values from HTML objects sent to the server- other commands such as
WEB GET HTTP BODY
,WEB GET HTTP HEADER
, orWEB GET BODY PART
allow to customize the request processing, including cookies. - the COMPILER_WEB project method, to declare your variables.
On Web Connection
The On Web Connection
database method can be used as the entry point for the 4D Web server.
Database method calls
The On Web Connection
database method is automatically called when the server receives any URL that is not a path to an existing page on the server. The database method is called with the URL.
For example, the URL "a/b/c" will call the database method, but "a/b/c.html" will not call the database method if the page "c.html" exists in the "a/b" subfolder of the WebFolder.
The request should have previously been accepted by the
On Web Authentication
database method (if it exists) and the web server must be launched.
Syntax
On Web Connection( $1 : Text ; $2 : Text ; $3 : Text ; $4 : Text ; $5 : Text ; $6 : Text )
Parameters | Type | Description | |
---|---|---|---|
$1 | Text | <- | URL |
$2 | Text | <- | HTTP headers + HTTP body (up to 32 kb limit) |
$3 | Text | <- | IP address of the web client (browser) |
$4 | Text | <- | IP address of the server |
$5 | Text | <- | User name |
$6 | Text | <- | Password |
You must declare these parameters as shown below:
//On Web Connection database method
C_TEXT($1;$2;$3;$4;$5;$6)
//Code for the method
Alternatively, you can use the named parameters syntax:
// On Web Connection database method
#DECLARE ($url : Text; $header : Text; \
$BrowserIP : Text; $ServerIP : Text; \
$user : Text; $password : Text)
Calling a 4D command that displays an interface element (
DIALOG
,ALERT
, etc.) is not allowed and ends the method processing.
$1 - URL extra data
The first parameter ($1) is the URL entered by users in the address area of their web browser, without the host address.
Let’s use an intranet connection as an example. Suppose that the IP address of your 4D Web Server machine is 123.4.567.89. The following table shows the values of $1 depending on the URL entered in the web browser:
URL entered in web browser | Value of parameter $1 |
---|---|
123.4.567.89 | / |
http://123.45.67.89 | / |
123.4.567.89/Customers | /Customers |
http://123.45.67.89/Customers/Add | /Customers/Add |
123.4.567.89/Do_This/If_OK/Do_That | /Do_This/If_OK/Do_That |
Note that you are free to use this parameter at your convenience. 4D simply ignores the value passed beyond the host part of the URL. For example, you can establish a convention where the value "/Customers/Add" means “go directly to add a new record in the [Customers]
table.” By supplying the web users with a list of possible values and/or default bookmarks, you can provide shortcuts to different parts of your application. This way, web users can quickly access resources of your website without going through the entire navigation path each time they make a new connection.
$2 - Header and Body of the HTTP request
The second parameter ($2) is the header and the body of the HTTP request sent by the web browser. Note that this information is passed to your On Web Connection
database method "as is". Its contents will vary depending on the nature of the web browser attempting the connection.
If your application uses this information, it is up to you to parse the header and the body. You can use the WEB GET HTTP HEADER
and the WEB GET HTTP BODY
commands.
For performance reasons, the size of data passing through the $2 parameter must not exceed 32 KB. Beyond this size, they are truncated by the 4D HTTP server.
$3 - Web client IP address
The $3 parameter receives the IP address of the browser’s machine. This information can allow you to distinguish between intranet and internet connections.
4D returns IPv4 addresses in a hybrid IPv6/IPv4 format written with a 96-bit prefix, for example ::ffff:192.168.2.34 for the IPv4 address 192.168.2.34. For more information, refer to the IPv6 Support section.
$4 - Server IP address
The $4 parameter receives the IP address requested by the 4D Web Server. 4D allows for multi-homing, which allows you to use machines with more than one IP address. For more information, please refer to the Configuration page.
$5 and $6 - User Name and Password
The $5 and $6 parameters receive the user name and password entered by the user in the standard identification dialog box displayed by the browser, if applicable (see the authentication page).
If the user name sent by the browser exists in 4D, the $6 parameter (the user’s password) is not returned for security reasons.
/4DACTION
**/4DACTION/**MethodName
**/4DACTION/**MethodName/Param
Parameters | Type | Description | |
---|---|---|---|
MethodName | Text | -> | Name of the 4D project method to be executed |
Param | Text | -> | Text parameter to pass to the project method |
Usage: URL or Form action.
This URL allows you to call the MethodName 4D project method with an optional Param text parameter. The method will receive this parameter in $1.
- The 4D project method must have been allowed for web requests: the “Available through 4D tags and URLs (4DACTION...)” attribute value must have been checked in the properties of the method. If the attribute is not checked, the web request is rejected.
- When 4D receives a
/4DACTION/MethodName/Param
request, theOn Web Authentication
database method (if it exists) is called.
4DACTION/
can be associated with a URL in a static Web page:
<A HREF="/4DACTION/MyMethod/hello">Do Something</A>
The MyMethod
project method should generally return a "reply" (sending of an HTML page using WEB SEND FILE
or WEB SEND TEXT
, etc.). Be sure to make the processing as short as possible in order not to block the browser.
A method called by
/4DACTION
must not call interface elements (DIALOG
,ALERT
, etc.).
Example
This example describes the association of the /4DACTION
URL with an HTML picture object in order to dynamically display a picture in the page. You insert the following instructions in a static HTML page:
<IMG SRC="/4DACTION/getPhoto/smith">
The getPhoto
method is as follows:
C_TEXT($1) // This parameter must always be declared
var $path : Text
var $PictVar : Picture
var $BlobVar : Blob
//find the picture in the Images folder within the Resources folder
$path:=Get 4D folder(Current resources folder)+"Images"+Folder separator+$1+".psd"
READ PICTURE FILE($path;$PictVar) //put the picture in the picture variable
PICTURE TO BLOB($PictVar;$BLOB;".png") //convert the picture to ".png" format
WEB SEND BLOB($BLOB;"image/png")
4DACTION to post forms
The 4D Web server also allows you to use “posted” forms, which are static HTML pages that send data to the Web server, and to easily retrieve all the values. The POST type must be associated to them and the form’s action must imperatively start with /4DACTION/MethodName.
A form can be submitted through two methods (both can be used with 4D):
- POST, usually used to send data to the Web server,
- GET, usually used to request data from the Web server.
When the Web server receives a posted form, it calls the
On Web Authentication
database method (if it exists).
In the called method, you must call the WEB GET VARIABLES
command in order to retrieve the names and values of all the fields included in an HTML page submitted to the server.
Example to define the action of a form:
<FORM ACTION="/4DACTION/MethodName" METHOD=POST>