--- /dev/null
+#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;
+}