]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
matrix.c: add masterKey and verified, add HandleSync/HandleEvent
[matrix_esp_thesis] / src / matrix_http_mongoose.c
1 #include "matrix.h"\r
2 \r
3 #include <stdio.h>\r
4 #include <stdlib.h>\r
5 #include <string.h>\r
6 #include <stdbool.h>\r
7 #include <mongoose.h>\r
8 \r
9 //#define HTTP_DATA_SIZE 1024\r
10 #define AUTHORIZATION_HEADER_LEN 64\r
11 \r
12 typedef struct MatrixHttpConnection {\r
13     struct mg_mgr mgr;\r
14     struct mg_connection * connection;\r
15     \r
16     const char * host;\r
17     const char * accessToken;\r
18 \r
19     bool connected;\r
20     char * data;\r
21     int dataCap;\r
22     int dataLen;\r
23     bool dataReceived;\r
24 } MatrixHttpConnection;\r
25 \r
26 static void\r
27 MatrixHttpCallback(\r
28     struct mg_connection *c,\r
29     int ev,\r
30     void *ev_data,\r
31     void *fn_data)\r
32 {\r
33     MatrixHttpConnection * conn = (MatrixHttpConnection *)fn_data;\r
34 \r
35     if (ev == MG_EV_CONNECT)\r
36     {\r
37         struct mg_str host = mg_url_host(conn->host);\r
38 \r
39         // If s_url is https://, tell client connection to use TLS\r
40         if (mg_url_is_ssl(conn->host))\r
41         {\r
42             static struct mg_tls_opts opts;\r
43             opts.srvname = host;\r
44             mg_tls_init(c, &opts);\r
45         }\r
46 \r
47         conn->connection = c;\r
48         conn->connected = true;\r
49     }\r
50     if (ev == MG_EV_HTTP_CHUNK)\r
51     {\r
52         \r
53     }\r
54     if (ev == MG_EV_HTTP_MSG)\r
55     {\r
56         // Response\r
57         struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
58         // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
59         // client->dataLen = hm->message.len;\r
60         memcpy(conn->data, hm->body.ptr, hm->body.len);\r
61         // memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
62         conn->data[hm->body.len] = '\0';\r
63         conn->dataLen = hm->body.len;\r
64         conn->dataReceived = true;\r
65 \r
66         //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
67     }\r
68     if (ev == MG_EV_CLOSE)\r
69     {\r
70         conn->connection = NULL;\r
71         conn->connected = false;\r
72     }\r
73 }\r
74 \r
75 bool\r
76 MatrixHttpConnect(\r
77     MatrixHttpConnection * hc)\r
78 {    \r
79     //struct mg_connection * c =\r
80         mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc);\r
81 \r
82     while (! hc->connected)\r
83         mg_mgr_poll(&hc->mgr, 1000);\r
84 \r
85     return hc->connected;\r
86 }\r
87 \r
88 bool\r
89 MatrixHttpInit(\r
90     MatrixHttpConnection ** hc,\r
91     const char * host)\r
92 {\r
93     *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection));\r
94     \r
95     (*hc)->host = host;\r
96     \r
97     mg_mgr_init(&(*hc)->mgr);\r
98 \r
99     MatrixHttpConnect(*hc);\r
100 \r
101     return true;\r
102 }\r
103 \r
104 bool\r
105 MatrixHttpDeinit(\r
106     MatrixHttpConnection ** hc)\r
107 {\r
108     mg_mgr_free(&(*hc)->mgr);\r
109 \r
110     free(*hc);\r
111     *hc = NULL;\r
112 \r
113     return true;\r
114 }\r
115 \r
116 bool\r
117 MatrixHttpSetAccessToken(\r
118     MatrixHttpConnection * hc,\r
119     const char * accessToken)\r
120 {\r
121     hc->accessToken = accessToken;\r
122 \r
123     return true;\r
124 }\r
125 \r
126 bool\r
127 MatrixHttpGet(\r
128     MatrixHttpConnection * hc,\r
129     const char * url,\r
130     char * outResponseBuffer, int outResponseCap,\r
131     bool authenticated)\r
132 {\r
133     if (! hc->connected)\r
134         MatrixHttpConnect(hc);\r
135 \r
136     hc->dataReceived = false;\r
137 \r
138     struct mg_str host = mg_url_host(hc->host);\r
139 \r
140     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
141     if (authenticated)\r
142         sprintf(authorizationHeader,\r
143             "Authorization: Bearer %s\r\n", hc->accessToken);\r
144         // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
145         //     "Authorization: Bearer %s\r\n", client->accessToken);\r
146     else\r
147         authorizationHeader[0] = '\0';\r
148 \r
149     printf(\r
150         "GET %s HTTP/1.1\r\n"\r
151         "Host: %.*s\r\n"\r
152         "%s"\r
153         "\r\n",\r
154         url,\r
155         host.len, host.ptr,\r
156         authorizationHeader);\r
157 \r
158     mg_printf(hc->connection,\r
159         "GET %s HTTP/1.1\r\n"\r
160         "Host: %.*s\r\n"\r
161         "%s"\r
162         "\r\n",\r
163         url,\r
164         host.len, host.ptr,\r
165         authorizationHeader);\r
166 \r
167     hc->data = outResponseBuffer;\r
168     hc->dataCap = outResponseCap;\r
169     \r
170     while (! hc->dataReceived)\r
171         mg_mgr_poll(&hc->mgr, 1000);\r
172 \r
173     return hc->dataReceived;\r
174 }\r
175 \r
176 bool\r
177 MatrixHttpPost(\r
178     MatrixHttpConnection * hc,\r
179     const char * url,\r
180     const char * requestBuffer,\r
181     char * outResponseBuffer, int outResponseCap,\r
182     bool authenticated)\r
183 {\r
184     if (! hc->connected)\r
185         MatrixHttpConnect(hc);\r
186 \r
187     hc->dataReceived = false;\r
188 \r
189     struct mg_str host = mg_url_host(hc->host);\r
190 \r
191     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
192     if (authenticated)\r
193         sprintf(authorizationHeader,\r
194             "Authorization: Bearer %s\r\n", hc->accessToken);\r
195     else\r
196         authorizationHeader[0] = '\0';\r
197 \r
198     printf(\r
199             "POST %s HTTP/1.0\r\n"\r
200             "Host: %.*s\r\n"\r
201             "%s"\r
202             "Content-Type: application/json\r\n"\r
203             "Content-Length: %d\r\n"\r
204             "\r\n"\r
205             "%s"\r
206             "\r\n",\r
207             url,\r
208             host.len, host.ptr,\r
209             authorizationHeader,\r
210             strlen(requestBuffer),\r
211             requestBuffer);\r
212 \r
213     mg_printf(hc->connection,\r
214             "POST %s HTTP/1.0\r\n"\r
215             "Host: %.*s\r\n"\r
216             "%s"\r
217             "Content-Type: application/json\r\n"\r
218             "Content-Length: %d\r\n"\r
219             "\r\n"\r
220             "%s"\r
221             "\r\n",\r
222             url,\r
223             host.len, host.ptr,\r
224             authorizationHeader,\r
225             strlen(requestBuffer),\r
226             requestBuffer);\r
227 \r
228     hc->data = outResponseBuffer;\r
229     hc->dataCap = outResponseCap;\r
230     \r
231     while (! hc->dataReceived)\r
232         mg_mgr_poll(&hc->mgr, 1000);\r
233 \r
234     return hc->dataReceived;\r
235 }\r
236 \r
237 bool\r
238 MatrixHttpPut(\r
239     MatrixHttpConnection * hc,\r
240     const char * url,\r
241     const char * requestBuffer,\r
242     char * outResponseBuffer, int outResponseCap,\r
243     bool authenticated)\r
244 {\r
245     if (! hc->connected)\r
246         MatrixHttpConnect(hc);\r
247 \r
248     hc->dataReceived = false;\r
249 \r
250     struct mg_str host = mg_url_host(hc->host);\r
251 \r
252     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
253     if (authenticated)\r
254         sprintf(authorizationHeader,\r
255             "Authorization: Bearer %s\r\n", hc->accessToken);\r
256     else\r
257         authorizationHeader[0] = '\0';\r
258 \r
259     \r
260     printf(\r
261             "PUT %s HTTP/1.0\r\n"\r
262             "Host: %.*s\r\n"\r
263             "%s"\r
264             "Content-Type: application/json\r\n"\r
265             "Content-Length: %d\r\n"\r
266             "\r\n"\r
267             "%s"\r
268             "\r\n",\r
269             url,\r
270             host.len, host.ptr,\r
271             authorizationHeader,\r
272             strlen(requestBuffer),\r
273             requestBuffer);\r
274 \r
275     mg_printf(hc->connection,\r
276             "PUT %s HTTP/1.0\r\n"\r
277             "Host: %.*s\r\n"\r
278             "%s"\r
279             "Content-Type: application/json\r\n"\r
280             "Content-Length: %d\r\n"\r
281             "\r\n"\r
282             "%s"\r
283             "\r\n",\r
284             url,\r
285             host.len, host.ptr,\r
286             authorizationHeader,\r
287             strlen(requestBuffer),\r
288             requestBuffer);\r
289 \r
290     hc->data = outResponseBuffer;\r
291     hc->dataCap = outResponseCap;\r
292     \r
293     while (! hc->dataReceived)\r
294         mg_mgr_poll(&hc->mgr, 1000);\r
295 \r
296     return hc->dataReceived;\r
297 }\r