restructuring
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 01:37:23 +0000 (18:37 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 01:37:23 +0000 (18:37 -0700)
src/server/Makefile
src/server/iotcloud.cpp
src/server/iotcloud.fcgi
src/server/iotquery.cpp [new file with mode: 0644]
src/server/iotquery.h [new file with mode: 0644]

index 9b6231bc67dc490b67c1b3a161839f7325fb7d8d..0818b4363622e8b8b69a3ba80f9dbba29ff8ab48 100644 (file)
@@ -1,6 +1,12 @@
-CPPFLAGS=-O0 -g
+CPPFLAGS=-O0 -g -Wall
 
 all: iotcloud.fcgi
 
-iotcloud.fcgi: iotcloud.cpp
-       g++ $(CPPFLAGS) -o iotcloud.fcgi iotcloud.cpp -lfcgi -lfcgi++
+iotcloud.fcgi: iotcloud.o iotquery.o
+       g++ $(CPPFLAGS) -o iotcloud.fcgi iotcloud.o iotquery.o -lfcgi -lfcgi++
+
+iotcloud.o: iotcloud.cpp iotquery.h
+       g++ $(CPPFLAGS) -c -o iotcloud.o iotcloud.cpp
+
+iotquery.o: iotquery.cpp iotquery.h
+       g++ $(CPPFLAGS) -c -o iotquery.o iotquery.cpp
index 16bb28e4fe357a59e3ae3f1cc368c77c6a65554c..dac7ee8d5aeb064021a7b1717d90d45739fa0c18 100644 (file)
@@ -1,26 +1,8 @@
 #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";
+#include "iotquery.h"
 
 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
@@ -42,42 +24,7 @@ int main(void) {
       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);
@@ -87,24 +34,3 @@ int main(void) {
     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;  
-}
index f5f828e7dd38a84274d9684bdf6a338f2046d04e..757b3b7068990a78a64c4f31665d45d60cec7b04 100755 (executable)
Binary files a/src/server/iotcloud.fcgi and b/src/server/iotcloud.fcgi differ
diff --git a/src/server/iotquery.cpp b/src/server/iotquery.cpp
new file mode 100644 (file)
index 0000000..89b98a9
--- /dev/null
@@ -0,0 +1,137 @@
+#include "iotquery.h"
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+using namespace std;
+
+const char * query_str="QUERY_STRING";
+const char * uri_str="REQUEST_URI";
+const char * method_str="REQUEST_METHOD";
+const char * iotcloudroot_str="IOTCLOUD_ROOT";
+const char * length_str="CONTENT_LENGTH";
+
+IoTQuery::IoTQuery(FCGX_Request *request) :
+  request(request),
+  data(NULL),
+  directory(NULL),
+  uri(NULL),
+  query(NULL),
+  method(NULL),
+  iotcloudroot(NULL),
+  dir(NULL),
+  length(0),
+  fd(-1)
+{
+}
+
+
+IoTQuery::~IoTQuery() {
+  if (fd >= 0) {
+    close(fd);
+  }
+  if (directory)
+    delete directory;
+  if (data)
+    delete data;
+  if (dir != NULL)
+    closedir(dir);
+}
+  
+void IoTQuery::processQuery() {
+  parseQuery();
+  getDirectory();
+  readData();
+
+  if (strncmp(method, "POST", 4) != 0)
+    return;
+  
+  if (directory == NULL ||
+      (dir = opendir(directory)) == NULL)
+    return;
+  
+  if (openMaxFile() < 0)
+    return;
+
+  flock(fd, LOCK_EX);
+      
+  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";
+  
+  cout << uri_str << " " << uri << "\n";
+  cout << query_str << " " << query << "\n";
+  cout << method_str << " " << method << "\n";
+  cout << iotcloudroot_str << " " << iotcloudroot << "\n";
+  if (data)
+    cout << "[" << data << "]";      
+  
+  
+  
+  cout << "</html>\n";
+}
+
+
+void IoTQuery::readData() {
+  if (length) {
+    data = new char[length+1];
+    memset(data, 0, length+1);
+    cin.read(data, length);
+  }
+  do {
+    char dummy;
+    cin >> dummy;
+  } while (!cin.eof());
+}
+
+
+void IoTQuery::parseQuery() {
+  uri = FCGX_GetParam(uri_str, request->envp);
+  query = FCGX_GetParam(query_str, request->envp);
+  method = FCGX_GetParam(method_str, request->envp);
+  iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
+
+  char * reqlength = FCGX_GetParam(length_str, request->envp);
+  if (length) {
+    length=strtol(reqlength, NULL, 10);
+  } else {
+    length=0;
+  }
+}
+
+void IoTQuery::getDirectory() {
+  char * split = strchr((char *)uri, '?');
+  if (split == NULL)
+    return;
+  int split_len = (int) (split-uri);
+  int rootdir_len = strlen(iotcloudroot);
+  int directory_len = split_len + rootdir_len + 1;
+  directory = new char[directory_len];
+  memcpy(directory, iotcloudroot, rootdir_len);
+  memcpy(directory + rootdir_len, uri, split_len);
+  directory[directory_len]=0;
+}
+
+int IoTQuery::openMaxFile() {
+  char maxfile[]="queuesize";
+  int len=strlen(directory);
+
+  char * filename=new char[len+sizeof(maxfile)+2];
+  memcpy(filename, directory, len);
+  filename[len]='/';
+  memcpy(filename+len+1, maxfile, sizeof(maxfile));
+  filename[len+sizeof(maxfile)+1]=0;
+  fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
+  delete filename;
+  return fd;
+}
diff --git a/src/server/iotquery.h b/src/server/iotquery.h
new file mode 100644 (file)
index 0000000..ccadd39
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef IOTQUERY_H
+#define IOTQUERY_H
+#include <iostream>
+#include "fcgio.h"
+#include "fcgi_stdio.h"
+#include <dirent.h>
+
+class IoTQuery {
+ public:
+  IoTQuery(FCGX_Request * request);
+  ~IoTQuery();
+  void processQuery();
+  
+ private:
+  void parseQuery();
+  void getDirectory();
+  void readData();
+  int checkDirectory();
+  int openMaxFile();
+
+  FCGX_Request * request;
+  char *data;
+  char *directory;
+  const char * uri;
+  const char * query;
+  const char * method;
+  const char * iotcloudroot;
+  DIR *dir;
+  long length;
+  int fd;
+};
+#endif