Project

General

Profile

Scol standard library package  0.3.1
Common and usefull functions for all Scol applications
The HTTP server API based on the legacy Scol API.

Functions

 std_httpsrvClose (o)
 Close a server and its opened conections. All active connections will be closed too.
 std_httpsrvCloseRequest (o, oreq)
 Close an opened connection request.
 std_httpsrvGetActives (o)
 Get the current number of active (opened) connections.
 std_httpsrvGetCallback (o)
 Return the current called function when a request is received.
 std_httpsrvGetCallbackAccept (o)
 Return the current called function when a connection is accepted.
 std_httpsrvGetHeader (o)
 Return the current default header for the answers.
 std_httpsrvGetId (o)
 Get the internal id of a server.
 std_httpsrvGetLogged (o)
 Return if the operations are (or not) currently logged in the application log file (depending on the Scol settings).
 std_httpsrvGetMaxSizePost (o)
 Return the current max size for a POST request.
 std_httpsrvGetPort (o)
 Return the listened port.
 std_httpsrvGetRequest (oreq)
 Get the full received request.
 std_httpsrvGetRequestClose (oreq)
 Return the defined callback when a connection is closed.
 std_httpsrvGetRequestHeader (oreq)
 Get the current defined header for a request object.
 std_httpsrvGetRequestId (oreq)
 Return the id of a connection.
 std_httpsrvGetRequestIP (oreq)
 Return the IP address of the client (or its proxy, ...).
 std_httpsrvGetRequestSize (oreq)
 Return the size of the buffer request.
 std_httpsrvGetStats (o)
 Returns few stats about this server.
 std_httpsrvGetType (o)
 Return if the server is synchronous or asynchronous.
 std_httpsrvKeyValue (str, verb)
 Function to convert a string parameters like a=b&c=d ... to a list key,value.
 std_httpsrvNewAsync (chn, port, cbfun)
 Create a new asynchronous Simple HTTP server.
 std_httpsrvNewSync (chn, port, cbfun)
 Create a new synchronous Simple HTTP server.
 std_httpsrvParseRequest (oreq)
 Parse a received request.
 std_httpsrvRequestBody (oreq)
 Return the body of a received request object.
 std_httpsrvRequestHeader (oreq)
 Return the header of a received request object.
 std_httpsrvSend (o, oreq)
 Send an anwser to a client by an opened connection request. You should set std_httpsrvSetResponseS or std_httpsrvSetResponseP before use this function.
 std_httpsrvSetCallback (o, cbfun)
 Set the reflex function which is called when a request is received. This callback can be changed as you want.
 std_httpsrvSetCallbackAccept (o, cbfun)
 Set the reflex function which is called when a connection is accepted. The request object given by the callback doesn't contain the string request yet. This string will be present in the request object in the received callback only. This callback can be changed as you want.
 std_httpsrvSetHeader (o, szDefaultHeader)
 Set the default header for the answers.
 std_httpsrvSetLogged (o, state)
 Set if the operations are (or not) currently logged in the application log file (depending on the Scol settings).
 std_httpsrvSetMaxSizePost (o, size)
 Set the max size for a POST request.
 std_httpsrvSetRequestClose (oreq, cbfun)
 Set the callback when a connection is closed.
 std_httpsrvSetRequestHeader (oreq, szHeader)
 Set the header answer of a specific received request. By default, it is the default server header.
 std_httpsrvSetResponseP (oreq, pFile)
 Set the file to send to a specific received request. The header will be automatically added when the answser is sent. The content file won't loaded in Scol memory.
 std_httpsrvSetResponseS (oreq, szResponse)
 Set the answer of a specific received request. The header will be automatically added when the answser is sent.

Detailed Description

Package to load : lib/std/httpsrv.pkg

Dependancies :

Function Documentation

std_httpsrvNewSync ( chn  ,
port  ,
cbfun   
)

Create a new synchronous Simple HTTP server.

Prototype : fun [Chn I fun [STD_HTTP_SERVER_REQUEST] S] STD_HTTP_SERVER

Parameters
Chn: the channel where the server will be created. Generally, it is the current channel ( _channel ).
I: the TCP-IP listen port. By default, it is on 80.
fun[STD_HTTP_SERVER_REQUEST] S : each time that a request is received, this function is called. Its argument is a request object. Its API is in this same package. The function must return a string. This string is the response to the client. Don't forget to include a comprehensive header in the answer.
Returns
STD_HTTP_SERVER : a new server object or nil if an error is occured.
std_httpsrvNewAsync ( chn  ,
port  ,
cbfun   
)

Create a new asynchronous Simple HTTP server.

Prototype : fun [Chn I fun [STD_HTTP_SERVER_REQUEST] S] STD_HTTP_SERVER

Parameters
Chn: the channel where the server will be created. Generally, it is the current channel ( _channel ).
I: the TCP-IP listen port. By default, it is on 80.
fun[STD_HTTP_SERVER_REQUEST] S : each time that a request is received, this function is called. Its argument is a request object. Its API is in this same package. The function should return nil. To answser, see in this same API. As the server will be asynchronous, the application send the response and manage the connection manually.
Returns
STD_HTTP_SERVER : a new server object or nil if an error is occured.
std_httpsrvClose ( )

Close a server and its opened conections. All active connections will be closed too.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a server object
Returns
I : always 0
std_httpsrvGetType ( )

Return if the server is synchronous or asynchronous.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : STD_HTTPSRV_SYNC or STD_HTTPSRV_ASYNC (nil if the object is not valid)
std_httpsrvGetPort ( )

Return the listened port.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : the port number (nil if the object is not valid)
std_httpsrvGetLogged ( )

Return if the operations are (or not) currently logged in the application log file (depending on the Scol settings).

Remarks
The std error API ( lib/std/error.pkg ) allows a better way for this.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : yes (1) or no, value by default (0) (nil if the object is not valid)
std_httpsrvGetCallback ( )

Return the current called function when a request is received.

Prototype :fun [STD_HTTP_SERVER] fun [STD_HTTP_SERVER_REQUEST] S

Remarks
Call this function doesn't execute the callback !
Parameters
STD_HTTP_SERVER: a valid server object
Returns
fun [STD_HTTP_SERVER_REQUEST] S : the callback (nil if the object is not valid)
std_httpsrvGetCallbackAccept ( )

Return the current called function when a connection is accepted.

Prototype :fun [STD_HTTP_SERVER] fun [STD_HTTP_SERVER_REQUEST] I

Remarks
Call this function doesn't execute the callback !
This callback is defined to nil by default.
Parameters
STD_HTTP_SERVER: a valid server object
Returns
fun [STD_HTTP_SERVER_REQUEST] I : the callback (nil if the object is not valid)
std_httpsrvGetHeader ( )

Return the current default header for the answers.

Remarks
This header will be automatically set in each new STD_HTTP_SERVER_REQUEST object. If another header is needed, you can change it.
By default this header is "HTTP/1.0 200 OK\13\10Server: SCOL HTTP server\13\10Content-Type: text/html\13\10\13\10"
See Also
std_httpsrvSetRequestHeader

Prototype :fun [STD_HTTP_SERVER] S

Parameters
STD_HTTP_SERVER: a valid server object
Returns
S : the default header (nil if the object is not valid)
std_httpsrvGetMaxSizePost ( )

Return the current max size for a POST request.

Remarks
POST requests are bufferized on server side until the whole request is received. This function defines the maximum size of the buffer. Larger requests will be ignored. By default, this limit is undefined (0).

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : this limit (nil if the object is not valid)
std_httpsrvGetActives ( )

Get the current number of active (opened) connections.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : the number of opened connections
std_httpsrvGetStats ( )

Returns few stats about this server.

Prototype :fun [STD_HTTP_SERVER] [I I I I]

Parameters
STD_HTTP_SERVER: a valid server object
Returns
[I I I I] : a tuple :
  • the total number of received request from the starter (or nil if "infinite " (NaN))
  • the number of the active connections
  • the number of incoming bytes
  • the number og the outgoing bytes
std_httpsrvGetId ( )

Get the internal id of a server.

Prototype :fun [STD_HTTP_SERVER] I

Parameters
STD_HTTP_SERVER: a valid server object
Returns
I : the identifiant
std_httpsrvSetLogged ( ,
state   
)

Set if the operations are (or not) currently logged in the application log file (depending on the Scol settings).

Remarks
The std error API ( lib/std/error.pkg ) allows a better way for this.

Prototype :fun [STD_HTTP_SERVER I] STD_HTTP_SERVER

Parameters
STD_HTTP_SERVER: a valid server object
I: the new state : 0 (default) or 1. Another value is ignored.
Returns
STD_HTTP_SERVER : the same object
std_httpsrvSetCallback ( ,
cbfun   
)

Set the reflex function which is called when a request is received. This callback can be changed as you want.

Remarks
If the server is synchronous, the callback must return a string. it is the answer, immediately sent to the client. If the server is asynchronous, the callback should be return nil and the application send mannuyaly the answer.
If it set to nil, no response will be sent and the server will be "mute".
If your code need more arguments, you should use the mkfunX functions.

Prototype :fun [STD_HTTP_SERVER fun [STD_HTTP_SERVER_REQUEST] S] STD_HTTP_SERVER

Parameters
STD_HTTP_SERVER: a valid server object
fun[STD_HTTP_SERVER_REQUEST] S : the new callback.
Returns
STD_HTTP_SERVER : the same object
std_httpsrvSetCallbackAccept ( ,
cbfun   
)

Set the reflex function which is called when a connection is accepted. The request object given by the callback doesn't contain the string request yet. This string will be present in the request object in the received callback only. This callback can be changed as you want.

Remarks
By default, this set to nil.
If your code need more arguments, you should use the mkfunX functions.

Prototype :fun [STD_HTTP_SERVER fun [STD_HTTP_SERVER_REQUEST] I] STD_HTTP_SERVER

Parameters
STD_HTTP_SERVER: a valid server object
fun[STD_HTTP_SERVER_REQUEST] I : the new callback.
Returns
STD_HTTP_SERVER : the same object
std_httpsrvSetHeader ( ,
szDefaultHeader   
)

Set the default header for the answers.

See Also
std_httpsrvGetHeader

Prototype :fun [STD_HTTP_SERVER S] STD_HTTP_SERVER

Parameters
STD_HTTP_SERVER: a valid server object
S: the new default header.
Returns
STD_HTTP_SERVER : the same object
std_httpsrvSetMaxSizePost ( ,
size   
)

Set the max size for a POST request.

See Also
std_httpsrvGetMaxSizePost

Prototype :fun [STD_HTTP_SERVER I] STD_HTTP_SERVER

Parameters
STD_HTTP_SERVER: a valid server object
I: the new positive size (a negative value will be ignored)
Returns
STD_HTTP_SERVER : the same object
std_httpsrvGetRequest ( oreq  )

Get the full received request.

Prototype :fun [STD_HTTP_SERVER_REQUEST] S

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
S : the received request
std_httpsrvGetRequestHeader ( oreq  )

Get the current defined header for a request object.

See Also
std_httpsrvSetRequestHeader

Prototype :fun [STD_HTTP_SERVER_REQUEST] S

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
S : the header
std_httpsrvGetRequestIP ( oreq  )

Return the IP address of the client (or its proxy, ...).

Prototype :fun [STD_HTTP_SERVER_REQUEST] S

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
S : the IP
std_httpsrvGetRequestSize ( oreq  )

Return the size of the buffer request.

Prototype :fun [STD_HTTP_SERVER_REQUEST] I

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
I : the size
Remarks
This function can crash the VM, so while it is not fixed, it does nothing and return always 0
std_httpsrvGetRequestId ( oreq  )

Return the id of a connection.

Prototype :fun [STD_HTTP_SERVER_REQUEST] I

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
I : the id
std_httpsrvGetRequestClose ( oreq  )

Return the defined callback when a connection is closed.

Prototype :fun [STD_HTTP_SERVER_REQUEST] fun [I] I

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
fun [I] I : the callback
std_httpsrvSetRequestClose ( oreq  ,
cbfun   
)

Set the callback when a connection is closed.

Prototype :fun [STD_HTTP_SERVER_REQUEST fun [I] I] STD_HTTP_SERVER_REQUEST

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
fun[I] I : function to call. Its argument is the id of the connection.
Returns
STD_HTTP_SERVER_REQUEST : the same request object
std_httpsrvSetRequestHeader ( oreq  ,
szHeader   
)

Set the header answer of a specific received request. By default, it is the default server header.

See Also
std_httpsrvGetHeader

Prototype :fun [STD_HTTP_SERVER_REQUEST S] STD_HTTP_SERVER_REQUEST

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
S: an header
Returns
STD_HTTP_SERVER_REQUEST : the same object request
std_httpsrvSetResponseS ( oreq  ,
szResponse   
)

Set the answer of a specific received request. The header will be automatically added when the answser is sent.

See Also
std_httpsrvSetRequestHeader

Prototype :fun [STD_HTTP_SERVER_REQUEST S] STD_HTTP_SERVER_REQUEST

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
S: an answer
Returns
STD_HTTP_SERVER_REQUEST : the same object request
Remarks
to send a file, see std_httpsrvSetResponseP
This function sould be used with a asynchronous server only
std_httpsrvSetResponseP ( oreq  ,
pFile   
)

Set the file to send to a specific received request. The header will be automatically added when the answser is sent. The content file won't loaded in Scol memory.

See Also
std_httpsrvSetRequestHeader

Prototype :fun [STD_HTTP_SERVER_REQUEST P] STD_HTTP_SERVER_REQUEST

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
P: a read reference file
Returns
STD_HTTP_SERVER_REQUEST : the same object request
Remarks
to send a normal string, see std_httpsrvSetResponseS
This function sould be used with a asynchronous server only
std_httpsrvSend ( ,
oreq   
)

Send an anwser to a client by an opened connection request. You should set std_httpsrvSetResponseS or std_httpsrvSetResponseP before use this function.

See Also
std_httpsrvSetResponseS
std_httpsrvSetResponseP

Prototype :fun [STD_HTTP_SERVER STD_HTTP_SERVER_REQUEST] I

Parameters
STD_HTTP_SERVER: a server to send the asynchronous answer
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
I : 0 if success, another value if error
Remarks
This function sould be used with a asynchronous server only
std_httpsrvCloseRequest ( ,
oreq   
)

Close an opened connection request.

Prototype :fun [STD_HTTP_SERVER STD_HTTP_SERVER_REQUEST] I

Parameters
STD_HTTP_SERVER: server
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
I : 0 if success, another value if fail
std_httpsrvParseRequest ( oreq  )

Parse a received request.

Prototype :fun [STD_HTTP_SERVER_REQUEST] [S S S S S S S S S S [[S S] r1]]

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
[S S S S S S S S S S [[S S] r1]] : a tuple :
  • S : the "host",
  • S : the "port" (80 by default),
  • S : the "user agent",
  • S : the used HTTP "protocol" (generally 1.0 or 1.1),
  • S : the "verb" (GET, POST, ...),
  • S : the data type (= the value of "Content-Type" header),
  • S : the language (= the value of "Content-Language" or "Accept-Language" header),
  • S : the size(lenght) of :
    1. the "url", if the "verb" is "GET",
    2. the "body" (exactly the value of the "Content-Lenght" header), if the "verb" is "POST",
  • S : the asked "url" (without the host),
  • S : the "body", the body content, if the "verb" is "POST" (should be nil with "GET"),
  • [[S S] r1] : a list with key, value which have been received in the request. If unable to find keys,values but the string is not empty, the string is returned in the first item of the tuple (in this case, the list has one element (tuple) only).
Remarks
If a value is not found, it will be nil, except for the port.
See Also
std_netParseRequest
std_httpsrvRequestHeader ( oreq  )

Return the header of a received request object.

Prototype :fun [STD_HTTP_SERVER_REQUEST] S

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
S : the header or nil if the request is invalid
See Also
std_netGetHeader
std_httpsrvRequestBody ( oreq  )

Return the body of a received request object.

Prototype :fun [STD_HTTP_SERVER_REQUEST] S

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
Returns
S : the body or nil if no body found (with "GET" by example)
See Also
std_netGetBody
std_httpsrvKeyValue ( str  ,
verb   
)

Function to convert a string parameters like a=b&c=d ... to a list key,value.

Prototype :fun [STD_HTTP_SERVER_REQUEST S] [[S S] r1]

Parameters
STD_HTTP_SERVER_REQUEST: a valid request object
S: the verb ("GET", "POST", ...)
Returns
[[S S] r1] : this list or nil if the request is invalid
Remarks
If this function is unable to find keys,values, it returns a list with one tuple only, the full string is in the first item of the tuple.
See Also
std_netBuildListFromParams