From: Brian Demsky Date: Fri, 22 Jul 2016 01:37:23 +0000 (-0700) Subject: restructuring X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=commitdiff_plain;h=18ea5b9b9fd5b062e156245a29ce8157b687045d restructuring --- diff --git a/src/server/Makefile b/src/server/Makefile index 9b6231b..0818b43 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -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 diff --git a/src/server/iotcloud.cpp b/src/server/iotcloud.cpp index 16bb28e..dac7ee8 100644 --- a/src/server/iotcloud.cpp +++ b/src/server/iotcloud.cpp @@ -1,26 +1,8 @@ #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"; +#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" - << "\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); @@ -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; -} diff --git a/src/server/iotcloud.fcgi b/src/server/iotcloud.fcgi index f5f828e..757b3b7 100755 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 index 0000000..89b98a9 --- /dev/null +++ b/src/server/iotquery.cpp @@ -0,0 +1,137 @@ +#include "iotquery.h" +#include +#include +#include +#include +#include +#include +#include + +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" + << "\n" + << " \n" + << " Hello, World!\n" + << " \n" + << " \n" + << "

Hello, World!

\n" + << " \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 << "\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 index 0000000..ccadd39 --- /dev/null +++ b/src/server/iotquery.h @@ -0,0 +1,32 @@ +#ifndef IOTQUERY_H +#define IOTQUERY_H +#include +#include "fcgio.h" +#include "fcgi_stdio.h" +#include + +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