89b98a90f076846f72e3bb51a24e62046880cd36
[iotcloud.git] / src / server / iotquery.cpp
1 #include "iotquery.h"
2 #include <string.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <sys/file.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9
10 using namespace std;
11
12 const char * query_str="QUERY_STRING";
13 const char * uri_str="REQUEST_URI";
14 const char * method_str="REQUEST_METHOD";
15 const char * iotcloudroot_str="IOTCLOUD_ROOT";
16 const char * length_str="CONTENT_LENGTH";
17
18 IoTQuery::IoTQuery(FCGX_Request *request) :
19   request(request),
20   data(NULL),
21   directory(NULL),
22   uri(NULL),
23   query(NULL),
24   method(NULL),
25   iotcloudroot(NULL),
26   dir(NULL),
27   length(0),
28   fd(-1)
29 {
30 }
31
32
33 IoTQuery::~IoTQuery() {
34   if (fd >= 0) {
35     close(fd);
36   }
37   if (directory)
38     delete directory;
39   if (data)
40     delete data;
41   if (dir != NULL)
42     closedir(dir);
43 }
44   
45 void IoTQuery::processQuery() {
46   parseQuery();
47   getDirectory();
48   readData();
49
50   if (strncmp(method, "POST", 4) != 0)
51     return;
52   
53   if (directory == NULL ||
54       (dir = opendir(directory)) == NULL)
55     return;
56   
57   if (openMaxFile() < 0)
58     return;
59
60   flock(fd, LOCK_EX);
61       
62   cout << "Content-type: text/html\r\n"
63        << "\r\n"
64        << "<html>\n"
65        << "  <head>\n"
66        << "    <title>Hello, World!</title>\n"
67        << "  </head>\n"
68        << "  <body>\n"
69        << "    <h1>Hello, World!</h1>\n"
70        << "  </body>\n";
71   
72   cout << uri_str << " " << uri << "\n";
73   cout << query_str << " " << query << "\n";
74   cout << method_str << " " << method << "\n";
75   cout << iotcloudroot_str << " " << iotcloudroot << "\n";
76   if (data)
77     cout << "[" << data << "]";      
78   
79   
80   
81   cout << "</html>\n";
82 }
83
84
85 void IoTQuery::readData() {
86   if (length) {
87     data = new char[length+1];
88     memset(data, 0, length+1);
89     cin.read(data, length);
90   }
91   do {
92     char dummy;
93     cin >> dummy;
94   } while (!cin.eof());
95 }
96
97
98 void IoTQuery::parseQuery() {
99   uri = FCGX_GetParam(uri_str, request->envp);
100   query = FCGX_GetParam(query_str, request->envp);
101   method = FCGX_GetParam(method_str, request->envp);
102   iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
103
104   char * reqlength = FCGX_GetParam(length_str, request->envp);
105   if (length) {
106     length=strtol(reqlength, NULL, 10);
107   } else {
108     length=0;
109   }
110 }
111
112 void IoTQuery::getDirectory() {
113   char * split = strchr((char *)uri, '?');
114   if (split == NULL)
115     return;
116   int split_len = (int) (split-uri);
117   int rootdir_len = strlen(iotcloudroot);
118   int directory_len = split_len + rootdir_len + 1;
119   directory = new char[directory_len];
120   memcpy(directory, iotcloudroot, rootdir_len);
121   memcpy(directory + rootdir_len, uri, split_len);
122   directory[directory_len]=0;
123 }
124
125 int IoTQuery::openMaxFile() {
126   char maxfile[]="queuesize";
127   int len=strlen(directory);
128
129   char * filename=new char[len+sizeof(maxfile)+2];
130   memcpy(filename, directory, len);
131   filename[len]='/';
132   memcpy(filename+len+1, maxfile, sizeof(maxfile));
133   filename[len+sizeof(maxfile)+1]=0;
134   fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
135   delete filename;
136   return fd;
137 }