Some code towards a FastCGI server
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Thu, 21 Jul 2016 21:24:15 +0000 (14:24 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Thu, 21 Jul 2016 21:24:15 +0000 (14:24 -0700)
src/server/Makefile [new file with mode: 0644]
src/server/iotcloud.cpp [new file with mode: 0644]
src/server/iotcloud.fcgi [new file with mode: 0755]

diff --git a/src/server/Makefile b/src/server/Makefile
new file mode 100644 (file)
index 0000000..9b6231b
--- /dev/null
@@ -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 (file)
index 0000000..16bb28e
--- /dev/null
@@ -0,0 +1,110 @@
+#include <iostream>
+#include "fcgio.h"
+#include "fcgi_stdio.h"
+#include <stdlib.h>
+#include <string.h>
+#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"
+          << "<html>\n"
+          << "  <head>\n"
+          << "    <title>Hello, World!</title>\n"
+          << "  </head>\n"
+          << "  <body>\n"
+          << "    <h1>Hello, World!</h1>\n"
+          << "  </body>\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 << "</html>\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 (executable)
index 0000000..f5f828e
Binary files /dev/null and b/src/server/iotcloud.fcgi differ