]> git.vomp.tv Git - jsonserver.git/blob - httpdclient.h
JSON: Switch from StyledWriter to StreamWriterBuilder
[jsonserver.git] / httpdclient.h
1 #ifndef HTTPDCLIENT_H
2 #define HTTPDCLIENT_H
3
4 // Libmicrohttpd: https://www.gnu.org/software/libmicrohttpd/
5 #include <sys/types.h>
6 #include <sys/select.h>
7 #include <sys/socket.h>
8 #include <microhttpd.h>
9
10 #include <string>
11 #include <map>
12
13 // Log docs: https://github.com/gabime/spdlog
14 #include <spdlog/spdlog.h>
15 namespace spd = spdlog;
16
17 #include "vdrclient.h"
18
19 class HTTPDClient
20 {
21   public:
22     static bool StartServer(std::string docRoot, int port, const char* configDir);
23     static void StopServer();
24
25     // static callbacks from libmicrohttpd
26     static MHD_Result uri_key_value(void *cls, enum MHD_ValueKind kind, const char* key, const char* value);
27     static MHD_Result iterate_post(void *clientIdentifier, enum MHD_ValueKind kind, const char *key,
28                                    const char *filename, const char* content_type,
29                                    const char *transfer_encoding, const char *data, uint64_t off, size_t size);
30     static void request_completed(void *cls, struct MHD_Connection *mhd_connection,
31                               void **unused, enum MHD_RequestTerminationCode toe);
32     static void connection_notify(void *cls, struct MHD_Connection* mhd_connection,
33                        void **socket_context, enum MHD_ConnectionNotificationCode toe);
34     static MHD_Result handle_connection(void *cls, struct MHD_Connection *connection,
35                                         const char *url, const char *method,
36                                         const char *version, const char *upload_data,
37                                         long unsigned int* upload_data_size, void **con_cls);
38
39   private:
40     static std::shared_ptr<spd::logger> logger;
41     static struct MHD_Daemon* httpdserver;
42     static std::string docRoot;
43     static std::string configDir;
44
45     HTTPDClient(struct MHD_Connection*, const std::string& configDir);
46     ~HTTPDClient();
47
48     MHD_Result handleConnection(const char *url, const char *method,
49                                 const char *version, const char *upload_data,
50                                 size_t *upload_data_size, void **con_cls);
51     MHD_Result processGET();
52     MHD_Result processPOST();
53
54     std::map<std::string, std::string> getVars;
55     std::map<std::string, std::string> postFields;
56     struct MHD_PostProcessor* postprocessor;
57     char* url;
58     struct MHD_Connection* mhd_connection;
59
60     VDRClient vdrclient;
61
62     void addGetVar(const char* key, const char* value);
63     void addPostField(const char* key, const char* value, int valueLength);
64     void requestComplete();
65     MHD_Result sendStockResponse(int code);
66 };
67
68 #endif