]> 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 int uri_key_value(void *cls, enum MHD_ValueKind kind, const char* key, const char* value);
27     static int 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 int handle_connection(void *cls, struct MHD_Connection *connection,
35                                  const char *url, const char *method,
36                                  const char *version, const char *upload_data,
37                                  size_t *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     void setUrl(const char* url);
49     int processGET();
50     int processPOST();
51
52     std::map<std::string, std::string> getVars;
53     std::map<std::string, std::string> postFields;
54     struct MHD_PostProcessor* postprocessor;
55     char* url;
56     struct MHD_Connection* mhd_connection;
57
58     VDRClient vdrclient;
59
60     void addGetVar(const char* key, const char* value);
61     void addPostField(const char* key, const char* value, int valueLength);
62     void requestComplete();
63     int sendStockResponse(int code);
64 };
65
66 #endif