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