From: Brian Demsky Date: Thu, 21 Jul 2016 21:24:15 +0000 (-0700) Subject: Some code towards a FastCGI server X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ce0233845fe376ada044e0af6723083631e00e51;p=iotcloud.git Some code towards a FastCGI server --- diff --git a/src/server/Makefile b/src/server/Makefile new file mode 100644 index 0000000..9b6231b --- /dev/null +++ b/src/server/Makefile @@ -0,0 +1,6 @@ +CPPFLAGS=-O0 -g + +all: iotcloud.fcgi + +iotcloud.fcgi: iotcloud.cpp + g++ $(CPPFLAGS) -o iotcloud.fcgi iotcloud.cpp -lfcgi -lfcgi++ diff --git a/src/server/iotcloud.cpp b/src/server/iotcloud.cpp new file mode 100644 index 0000000..16bb28e --- /dev/null +++ b/src/server/iotcloud.cpp @@ -0,0 +1,110 @@ +#include +#include "fcgio.h" +#include "fcgi_stdio.h" +#include +#include +#define MAX_VARS 31 + +const char * query_str="QUERY_STRING"; +const char * uri_str="REQUEST_URI"; +const char * method_str="REQUEST_METHOD"; +const char * iotcloudroot_str="IOTCLOUD_ROOT"; + +using namespace std; + +struct iotquery { + const char * uri; + const char * query; + const char * method; + const char * iotcloudroot; +}; + +void parsequery(struct iotquery *, FCGX_Request *); +char * getdirectory(struct iotquery *); + +int main(void) { + // Backup the stdio streambufs + streambuf * cin_streambuf = cin.rdbuf(); + streambuf * cout_streambuf = cout.rdbuf(); + streambuf * cerr_streambuf = cerr.rdbuf(); + + FCGX_Request request; + + FCGX_Init(); + FCGX_InitRequest(&request, 0, 0); + + while (FCGX_Accept_r(&request) == 0) { + fcgi_streambuf cin_fcgi_streambuf(request.in); + fcgi_streambuf cout_fcgi_streambuf(request.out); + fcgi_streambuf cerr_fcgi_streambuf(request.err); + + cin.rdbuf(&cin_fcgi_streambuf); + cout.rdbuf(&cout_fcgi_streambuf); + cerr.rdbuf(&cerr_fcgi_streambuf); + + struct iotquery query; + parsequery(&query, &request); + char * directory = getdirectory(&query); + + cout << "Content-type: text/html\r\n" + << "\r\n" + << "\n" + << " \n" + << " Hello, World!\n" + << " \n" + << " \n" + << "

Hello, World!

\n" + << " \n"; + char c[80]; + + cout << uri_str << " " << query.uri << "\n"; + cout << query_str << " " << query.query << "\n"; + cout << method_str << " " << query.method << "\n"; + cout << iotcloudroot_str << " " << query.iotcloudroot << "\n"; + + + do { + cin.read(c, 80); + c[cin.gcount()]=0; + cout << "[" << c << "]"; + } while(!cin.eof()); + + + + + cout << "\n"; + // Note: the fcgi_streambuf destructor will auto flush + + if (directory != NULL) + free(directory); + } + + // restore stdio streambufs + cin.rdbuf(cin_streambuf); + cout.rdbuf(cout_streambuf); + cerr.rdbuf(cerr_streambuf); + + return 0; +} + + +void parsequery(struct iotquery * query, FCGX_Request * request) { + query->uri = FCGX_GetParam(uri_str, request->envp); + query->query = FCGX_GetParam(query_str, request->envp); + query->method = FCGX_GetParam(method_str, request->envp); + query->iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp); +} + +char * getdirectory(struct iotquery * query) { + char * split = strchr((char *)query->uri, '?'); + if (split == NULL) + return NULL; + int split_len = (int) (split-query->uri); + int rootdir_len = strlen(query->iotcloudroot); + int directory_len = split_len + rootdir_len + 1; + char * directory = (char *) malloc(directory_len); + memcpy(directory, query->iotcloudroot, rootdir_len); + memcpy(directory + rootdir_len, query->uri, split_len); + directory[directory_len]=0; + return directory; +} diff --git a/src/server/iotcloud.fcgi b/src/server/iotcloud.fcgi new file mode 100755 index 0000000..f5f828e Binary files /dev/null and b/src/server/iotcloud.fcgi differ