From dc5fd23089d23f18649df40bec82fa01af7782f4 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Wed, 14 Aug 2013 08:51:46 +0800 Subject: [PATCH] Add FastCGI approach --- testing/fastcgi-approach/README.txt | 20 ++++++++++++++++ testing/fastcgi-approach/fastcgi_test.c | 23 +++++++++++++++++++ .../fastcgi-approach/nginx_server_config.txt | 22 ++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 testing/fastcgi-approach/README.txt create mode 100644 testing/fastcgi-approach/fastcgi_test.c create mode 100644 testing/fastcgi-approach/nginx_server_config.txt diff --git a/testing/fastcgi-approach/README.txt b/testing/fastcgi-approach/README.txt new file mode 100644 index 0000000..6d42c25 --- /dev/null +++ b/testing/fastcgi-approach/README.txt @@ -0,0 +1,20 @@ +The application could be quite easily made to use FastCGI. Unlike normal CGI, +with FastCGI (fcgi), the process is executed once, and continues to run. The +process will receive responses from the browser in the response loop. Hence, +sensor data can be read in another thread while the response loop runs. + +Setup: +Compile fastcgi_test.c with: +gcc fastcgi_test.c -lfcgi -o fastcgi_test + +Configure nginx to pass all requests to the address /cgi/ to the application: +Edit /etc/nginx/sites-enabled/default by adding the contents of nginx_server_config.txt + +Restart nginx: +/etc/init.d/nginx restart + +Run the application: +spawn-fcgi -p9005 -n ./fastcgi_test + +You can see the results at: +http://your.domain/cgi \ No newline at end of file diff --git a/testing/fastcgi-approach/fastcgi_test.c b/testing/fastcgi-approach/fastcgi_test.c new file mode 100644 index 0000000..c881a24 --- /dev/null +++ b/testing/fastcgi-approach/fastcgi_test.c @@ -0,0 +1,23 @@ +#include "fcgi_stdio.h" /* fcgi library; put it first*/ +#include + +int main (int argc, char *argv[]) +{ + int count = 0; + + //Spawn thread to get sensor data here? + //Response loop + while (FCGI_Accept() >= 0) { + printf("Content-type: text/html\r\n" + "\r\n" + "FastCGI Hello! (C, fcgi_stdio library)" + "

FastCGI Hello! (C, fcgi_stdio library)

" + "Request number %d running on host %s\n", + count++, getenv("SERVER_HOSTNAME")); + + char *data = getenv("QUERY_STRING"); + if (data) { + printf("
Query string is: '%s'\n", data); + } + } +} diff --git a/testing/fastcgi-approach/nginx_server_config.txt b/testing/fastcgi-approach/nginx_server_config.txt new file mode 100644 index 0000000..6642639 --- /dev/null +++ b/testing/fastcgi-approach/nginx_server_config.txt @@ -0,0 +1,22 @@ + #Custom cgi + location /cgi/ { + fastcgi_pass 127.0.0.1:9005; + fastcgi_param GATEWAY_INTERFACE CGI/1.1; + fastcgi_param SERVER_HOSTNAME mctxsoft; + 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; + } -- 2.20.1