10 #include "httpdclient.h"
12 #include "vdrclient.h"
14 #define POSTBUFFERSIZE 512
16 std::shared_ptr<spd::logger> HTTPDClient::logger;
17 struct MHD_Daemon* HTTPDClient::httpdserver = NULL;
18 std::string HTTPDClient::docRoot;
19 std::string HTTPDClient::configDir;
21 bool HTTPDClient::StartServer(std::string _docRoot, int port, const char* _configDir)
24 configDir = std::string(_configDir);
26 logger = spd::get("jsonserver_spdlog");
28 httpdserver = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv6, port, NULL, NULL,
29 &HTTPDClient::handle_connection, NULL,
30 MHD_OPTION_NOTIFY_CONNECTION, &HTTPDClient::connection_notify, NULL,
31 MHD_OPTION_NOTIFY_COMPLETED, &HTTPDClient::request_completed, NULL,
32 MHD_OPTION_CONNECTION_TIMEOUT, 70,
34 if (httpdserver == NULL) { logger.reset(); return false; }
35 logger->info("HTTPDClient: Started");
39 void HTTPDClient::StopServer()
41 if (httpdserver) MHD_stop_daemon(httpdserver);
46 // Now for the libmicrohttpd callbacks
48 int HTTPDClient::uri_key_value(void *cls, enum MHD_ValueKind kind, const char* key, const char* value)
50 if (kind != MHD_GET_ARGUMENT_KIND) return MHD_NO;
51 HTTPDClient* httpdclient = (HTTPDClient*)cls;
52 httpdclient->addGetVar(key, value);
56 int HTTPDClient::iterate_post(void *clientIdentifier, enum MHD_ValueKind kind, const char *key,
57 const char *filename, const char* content_type,
58 const char *transfer_encoding, const char *data, uint64_t off, size_t size)
60 // logger->info("HTTPDClient: add post field: {} {} {}", key, data, size);
61 HTTPDClient* httpdclient = (HTTPDClient*)clientIdentifier;
62 httpdclient->addPostField(key, data, size);
63 return MHD_YES; //Return MHD_YES to continue iterating, MHD_NO to abort the iteration.
66 void HTTPDClient::request_completed(void *cls, struct MHD_Connection *mhd_connection,
67 void **unused, enum MHD_RequestTerminationCode toe)
69 const MHD_ConnectionInfo* mhdc = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_SOCKET_CONTEXT);
70 HTTPDClient* httpdclient = (HTTPDClient*)mhdc->socket_context;
71 if (!httpdclient) return;
72 httpdclient->requestComplete();
75 void HTTPDClient::connection_notify(void *cls, struct MHD_Connection* mhd_connection,
76 void **socket_context, enum MHD_ConnectionNotificationCode toe)
78 if (toe == MHD_CONNECTION_NOTIFY_STARTED)
80 *socket_context = (void*)new HTTPDClient(mhd_connection, configDir);
82 else if (toe == MHD_CONNECTION_NOTIFY_CLOSED)
84 HTTPDClient* httpdclient = (HTTPDClient*)*socket_context;
85 if (!httpdclient) return;
87 *socket_context = NULL;
91 int HTTPDClient::handle_connection(void* cls, struct MHD_Connection* mhd_connection,
92 const char* url, const char* method,
93 const char* version, const char* upload_data,
94 size_t* upload_data_size, void** userData)
96 const MHD_ConnectionInfo* mhdc = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_SOCKET_CONTEXT);
97 HTTPDClient* httpdclient = (HTTPDClient*)mhdc->socket_context;
98 return httpdclient->handleConnection(url, method, version, upload_data, upload_data_size, userData);
101 // End of static callbacks, now for the client object itself
103 HTTPDClient::HTTPDClient(struct MHD_Connection* _mhd_connection, const std::string& _configDir)
104 : postprocessor(NULL), url(NULL), mhd_connection(_mhd_connection), vdrclient(_configDir)
106 // printf("HTTPDClient created %p\n", this);
109 HTTPDClient::~HTTPDClient()
111 // printf("%p HTTPDClient destructor\n", this);
115 int HTTPDClient::handleConnection(const char* turl, const char* method,
116 const char* version, const char* upload_data,
117 size_t* upload_data_size, void** userData)
120 logger->debug("handle_connection called");
121 logger->debug("hc: cls {}", (void*)cls);
122 logger->debug("hc: mhd_connection {}", (void*)mhd_connection);
123 logger->debug("hc: url {}", (void*)url);
124 if (url) logger->debug("hc: url: {}", url);
125 logger->debug("hc: method {}", (void*)method);
126 if (url) logger->debug("hc: method: {}", method);
127 logger->debug("hc: version {}", (void*)version);
128 if (url) logger->debug("hc: version: {}", version);
129 logger->debug("hc: upload_data {}", (void*)upload_data);
130 logger->debug("hc: upload_data_size {}", *upload_data_size);
131 logger->debug("hc: userData {}", (void*)*userData);
134 // Now we are going to use userData as a flag to say first run or not
136 if (!*userData) // new request
138 *userData = (void*)1;
139 int size __attribute__((unused)) = asprintf(&url, "%s", turl);
141 if (!strcmp(method, "POST"))
143 MHD_get_connection_values(mhd_connection, MHD_GET_ARGUMENT_KIND, HTTPDClient::uri_key_value, (void*)this);
145 // The following returns NULL if there are no POST fields to come
146 postprocessor = MHD_create_post_processor(mhd_connection, POSTBUFFERSIZE,
147 HTTPDClient::iterate_post, (void*)this);
149 else if (!strcmp(method, "GET"))
151 // OK, nothing else to do here
161 // Not first go at this request
163 if (!strcmp(method, "GET"))
167 else if (!strcmp(method, "POST"))
169 // HC will be called at least three times. Once above to create the HTTPDClient object (above).
170 // Here the middle calls will be called with upload_data_size > 0
171 // The last call is with upload_data_size == 0 and signals the end, a response must be queued
173 if (*upload_data_size != 0) // There is more to process, and signal run again
175 MHD_post_process(postprocessor, upload_data, *upload_data_size);
176 *upload_data_size = 0;
181 // printf("hc: zero post provided, end of upload\n");
183 MHD_destroy_post_processor(postprocessor);
184 postprocessor = NULL;
186 return processPOST();
191 return sendStockResponse(405);
195 void HTTPDClient::requestComplete()
197 // printf("%p HTTPDClient request complete\n", this);
201 MHD_destroy_post_processor(postprocessor);
202 postprocessor = NULL;
206 void HTTPDClient::addGetVar(const char* key, const char* value)
208 if (strlen(key) > 50) return;
209 if (value && (strlen(value) > 1000)) return;
211 getVars[std::string(key)] = std::string(value);
213 for(auto gv : getVars)
215 printf("%s %s\n", gv.first.c_str(), gv.second.c_str());
220 void HTTPDClient::addPostField(const char* key, const char* value, int valueLength)
222 if (strlen(key) > 50) return;
223 if (strlen(value) > 1000) return;
225 postFields[std::string(key)] = std::string(value, valueLength);
228 for(auto pf : postFields)
230 printf("%s %s\n", pf.first.c_str(), pf.second.c_str());
235 int HTTPDClient::processGET()
237 const char* defaultfilename = "index.html";
239 if (url == NULL) return MHD_NO;
240 if (strstr(url, "..")) return MHD_NO; // MHD deals with these already and limits it. If it occurs here, error.
242 int size __attribute__((unused));
244 int slen = strlen(url);
246 if (url[slen-1] == '/')
247 size = asprintf(&fullpath, "%s%s%s", docRoot.c_str(), url, defaultfilename);
249 size = asprintf(&fullpath, "%s%s", docRoot.c_str(), url);
251 //printf("FILENAME: '%s'\n", fullpath);
254 if ( (stat(fullpath, &sbuf) == -1) // failed to stat
255 || ((sbuf.st_mode & S_IFMT) != S_IFREG) ) // must be regular file
258 return sendStockResponse(404);
261 int fd = open(fullpath, O_RDONLY);
264 if (fd == -1) return MHD_NO;
266 struct MHD_Response* response = MHD_create_response_from_fd(sbuf.st_size, fd);
267 int ret = MHD_queue_response(mhd_connection, MHD_HTTP_OK, response);
271 MHD_destroy_response(response);
276 int HTTPDClient::processPOST()
278 // printf("Process POST:\n");
279 //printf("REQ: %s\n", getVars["req"].c_str());
281 if (strcmp(url, "/jsonserver")) return sendStockResponse(404);
282 if (getVars["req"].empty()) return sendStockResponse(400);
284 std::string returnData;
286 bool success = vdrclient.process(getVars["req"], postFields, returnData);
287 if (!success) return sendStockResponse(500);
289 struct MHD_Response* response = MHD_create_response_from_buffer(strlen(returnData.c_str()), (void *)returnData.c_str(), MHD_RESPMEM_MUST_COPY);
290 MHD_add_response_header(response, "Content-Type", "application/json");
291 int ret = MHD_queue_response(mhd_connection, MHD_HTTP_OK, response);
295 MHD_destroy_response(response);
299 int HTTPDClient::sendStockResponse(int code)
301 const char *page400 = "<html><body>Bad request</body></html>\n";
302 const char *page404 = "<html><body>File not found</body></html>\n";
303 const char *page405 = "<html><body>Method not allowed</body></html>\n";
304 const char *page500 = "<html><body>Internal server error</body></html>\n";
306 if (code == 400) page = page400;
307 else if (code == 404) page = page404;
308 else if (code == 405) page = page405;
309 else if (code == 500) page = page500;
312 struct MHD_Response* response = MHD_create_response_from_buffer(strlen(page), (void *)page, MHD_RESPMEM_PERSISTENT);
313 int ret = MHD_queue_response(mhd_connection, code, response);
317 MHD_destroy_response(response);