Skip to content

HTTP module

Francesco Saverio Castellano edited this page Mar 25, 2020 · 72 revisions

This module is based on CURL and FCGI++ libraries and provides the following functionalities:

  • web server (based on FCGI)
    • receiving (Http.accept) and processing of HTTP requests (Http.getParam, Http.getParams, Http.getRequestBody)
    • writing HTTP response (Http.print).
  • sending external HTTP request (via CURL)

To interact with the HTTP layer provided by this module it is advised to use the Web Controller package called TINN Web.
TINN Web wraps and simplfies the APIs of the Http module and provides a simple web controller to process HTTP request/response with ease.

Http.openSocket(string addr)
Creates the listening socket for FCGI connections. Call this function only once in the main thread.

_addr_ is a string that set the host/ip and port to listen to (e.g. '127.0.0.1:8200')  
 
NOTE FOR WINDOWS USERS: The TINN windows package comes with a nginx executable (\nginx directory) which can be used to quickly run TINN based http apps.
Under windows the TINN init script (.init.js) rewraps Http.openSocket and the rewrapped version takes care of automaticallty starting/stopping NGINX. So basically when calling Http.openSocket a NGINX server is automatically started and configured to point to the FCGI port specified in the Http.openSocket call (the automatic generation of the NGINX configuration file is based on a template located in \nginx\nginx.conf.tpl).\ By default NGINX is configured to run on port 80, but this can be changed by specifying a different port number as a second argument to Http.openSocket. With the following code NGINX will use port 8080:

Http.openSocket(':8200', 8080);

Http.init()
Initializes FCGI library to prepare it to receive requests. Call this function only once in every thread.  
 

Http.accept()
This function is blocking and is used to wait for the next request on the current thread. When a request is sent to the thread by the underlying FCGI library the execution continues.  
 

Http.print(string buf)
Writes buf to the response buffer. Use this to write your HTTP response.  
 

Http.finish()
Call this when you have done writing the response. This function closes the response buffer and sends the response.  
 

Http.getParam(string param, [string defaultValue])
Use this function to read request attributes that NGINX (or whatever other HTTP frontend you are using) passes to FCGI.
The second argument defaultValue is optional. When provided getParam returns defaultValue if the requested parameter isn't found.
What parameters are available depends on how the HTTP frontend is configured. A common configuration for NGINX defines the following parameters:

        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;

According to the above configuration you can get the querytring of the request by doing:

var qstr = Http.getParam('QUERY_STRING');

You get the HTTP method of the request by doing:

var method = Http.getParam('REQUEST_METHOD');

You get the full requested URL by doing:

var url = Http.getParam('REQUEST_URI');

You get the full requested URL by doing:

var ip = Http.getParam('REMOTE_ADDR');

 
Http.getParams()
Returns an array with all parameters that are passed through the FCGI socket for the current request.
These parameters include the request headers (prefixed with 'HTTP_') and all the parameters defined by NGINX (or whatever other web server is used) for FCGI .  
 
Http.getRequestBody()
This function returns the request body (as a string) for POST requests.  
 
Http.closeSocket()
Closes the FCGI socket.  
 
Http.request(string url, string method, array headers, string body, [int timeout])
Sends a HTTP request.

url: url of the request
The value for this parameter can be either a string or an array. The array form allows specifying a proxy to be used to send out the request. In this case the array must contain two string arguments: first argument is the URL and second argument is the proxy address (in the ip:port form).

method: HTTP method (POST, GET)

headers: array of strings containing the HTTP request that must be sent with the request

body: request body (only for POST requests)

timeout: connection timeout in seconds (defaults to 60 seconds)
 

The following example shows a HTTP POST request, using a proxy and specifying a 10 seconds timeout:

var res = Http.request(
    ["http://myserver","myproxy:3128"], 
    'POST', 
    ["Content-Type": application/x-www-form-urlencoded"], 
    "key=value", 
    10);

 
 

Clone this wiki locally