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";
18 IoTQuery::IoTQuery(FCGX_Request *request) :
30 numqueueentries(DEFAULT_SIZE),
35 IoTQuery::~IoTQuery() {
46 int IoTQuery::checkDirectory() {
48 int err=stat(directory, &s);
51 return S_ISDIR(s.st_mode);
54 void IoTQuery::processQuery() {
59 if (strncmp(method, "POST", 4) != 0)
62 if (directory == NULL ||
66 if (openStatusFile() < 0)
71 cout << "Content-type: text/html\r\n"
75 << " <title>Hello, World!</title>\n"
78 << " <h1>Hello, World!</h1>\n"
81 cout << uri_str << " " << uri << "\n";
82 cout << query_str << " " << query << "\n";
83 cout << method_str << " " << method << "\n";
84 cout << iotcloudroot_str << " " << iotcloudroot << "\n";
86 cout << "[" << data << "]";
94 void IoTQuery::readData() {
96 data = new char[length+1];
97 memset(data, 0, length+1);
98 cin.read(data, length);
103 } while (!cin.eof());
106 void IoTQuery::parseQuery() {
107 uri = FCGX_GetParam(uri_str, request->envp);
108 query = FCGX_GetParam(query_str, request->envp);
109 method = FCGX_GetParam(method_str, request->envp);
110 iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
112 char * reqlength = FCGX_GetParam(length_str, request->envp);
114 length=strtol(reqlength, NULL, 10);
120 void IoTQuery::getDirectory() {
121 char * split = strchr((char *)uri, '?');
124 int split_len = (int) (split-uri);
125 int rootdir_len = strlen(iotcloudroot);
126 int directory_len = split_len + rootdir_len + 1;
127 directory = new char[directory_len];
128 memcpy(directory, iotcloudroot, rootdir_len);
129 memcpy(directory + rootdir_len, uri, split_len);
130 directory[directory_len]=0;
133 int doread(int fd, void *ptr, size_t count, off_t offset) {
135 size_t bytesread=pread(fd, ptr, count, offset);
136 if (bytesread==count) {
138 } else if (bytesread==0) {
144 void IoTQuery::updateStatusFile() {
145 pwrite(fd, &numqueueentries, sizeof(numqueueentries), OFFSET_MAX);
146 pwrite(fd, &firstentry, sizeof(firstentry), OFFSET_FIRST);
147 pwrite(fd, &lastentry, sizeof(lastentry), OFFSET_LAST);
150 int IoTQuery::openStatusFile() {
151 char statusfile[]="queuestatus";
152 int len=strlen(directory);
154 char * filename=new char[len+sizeof(statusfile)+2];
155 memcpy(filename, directory, len);
157 memcpy(filename+len+1, statusfile, sizeof(statusfile));
158 filename[len+sizeof(statusfile)+1]=0;
159 fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
167 if (doread(fd, &size, sizeof(size), OFFSET_MAX))
168 numqueueentries=size;
173 if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
178 if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))