-
Notifications
You must be signed in to change notification settings - Fork 0
HTTP module
This module is based on CURL and FCGI++ libraries and provides the following functionalities:
- web server (via FCGI)
- sending external HTTP request (via CURL)
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.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
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)