3 #include "fcgi_stdio.h"
8 const char * query_str="QUERY_STRING";
9 const char * uri_str="REQUEST_URI";
10 const char * method_str="REQUEST_METHOD";
11 const char * iotcloudroot_str="IOTCLOUD_ROOT";
19 const char * iotcloudroot;
22 void parsequery(struct iotquery *, FCGX_Request *);
23 char * getdirectory(struct iotquery *);
26 // Backup the stdio streambufs
27 streambuf * cin_streambuf = cin.rdbuf();
28 streambuf * cout_streambuf = cout.rdbuf();
29 streambuf * cerr_streambuf = cerr.rdbuf();
34 FCGX_InitRequest(&request, 0, 0);
36 while (FCGX_Accept_r(&request) == 0) {
37 fcgi_streambuf cin_fcgi_streambuf(request.in);
38 fcgi_streambuf cout_fcgi_streambuf(request.out);
39 fcgi_streambuf cerr_fcgi_streambuf(request.err);
41 cin.rdbuf(&cin_fcgi_streambuf);
42 cout.rdbuf(&cout_fcgi_streambuf);
43 cerr.rdbuf(&cerr_fcgi_streambuf);
45 struct iotquery query;
46 parsequery(&query, &request);
47 char * directory = getdirectory(&query);
49 cout << "Content-type: text/html\r\n"
53 << " <title>Hello, World!</title>\n"
56 << " <h1>Hello, World!</h1>\n"
60 cout << uri_str << " " << query.uri << "\n";
61 cout << query_str << " " << query.query << "\n";
62 cout << method_str << " " << query.method << "\n";
63 cout << iotcloudroot_str << " " << query.iotcloudroot << "\n";
69 cout << "[" << c << "]";
76 // Note: the fcgi_streambuf destructor will auto flush
78 if (directory != NULL)
82 // restore stdio streambufs
83 cin.rdbuf(cin_streambuf);
84 cout.rdbuf(cout_streambuf);
85 cerr.rdbuf(cerr_streambuf);
91 void parsequery(struct iotquery * query, FCGX_Request * request) {
92 query->uri = FCGX_GetParam(uri_str, request->envp);
93 query->query = FCGX_GetParam(query_str, request->envp);
94 query->method = FCGX_GetParam(method_str, request->envp);
95 query->iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
98 char * getdirectory(struct iotquery * query) {
99 char * split = strchr((char *)query->uri, '?');
102 int split_len = (int) (split-query->uri);
103 int rootdir_len = strlen(query->iotcloudroot);
104 int directory_len = split_len + rootdir_len + 1;
105 char * directory = (char *) malloc(directory_len);
106 memcpy(directory, query->iotcloudroot, rootdir_len);
107 memcpy(directory + rootdir_len, query->uri, split_len);
108 directory[directory_len]=0;