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