]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
work on SendEncrypted
[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     mg_printf(conn->connection,\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     conn->data = outResponseBuffer;\r
145     conn->dataCap = outResponseCap;\r
146     \r
147     while (! conn->dataReceived)\r
148         mg_mgr_poll(&conn->mgr, 1000);\r
149 \r
150     return conn->dataReceived;\r
151 }\r
152 \r
153 bool\r
154 MatrixHttpPost(\r
155     MatrixClient * client,\r
156     const char * url,\r
157     const char * requestBuffer,\r
158     char * outResponseBuffer, int outResponseCap,\r
159     bool authenticated)\r
160 {\r
161     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
162     if (! conn->connected)\r
163         MatrixHttpConnect(client);\r
164 \r
165     conn->dataReceived = false;\r
166 \r
167     struct mg_str host = mg_url_host(client->server);\r
168 \r
169     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
170     if (authenticated)\r
171         sprintf(authorizationHeader,\r
172             "Authorization: Bearer %s\r\n", client->accessToken);\r
173     else\r
174         authorizationHeader[0] = '\0';\r
175 \r
176     mg_printf(conn->connection,\r
177             "POST %s HTTP/1.0\r\n"\r
178             "Host: %.*s\r\n"\r
179             "%s"\r
180             "Content-Type: application/json\r\n"\r
181             "Content-Length: %d\r\n"\r
182             "\r\n"\r
183             "%s"\r
184             "\r\n",\r
185             url,\r
186             host.len, host.ptr,\r
187             authorizationHeader,\r
188             strlen(requestBuffer),\r
189             requestBuffer);\r
190 \r
191     conn->data = outResponseBuffer;\r
192     conn->dataCap = outResponseCap;\r
193     \r
194     while (! conn->dataReceived)\r
195         mg_mgr_poll(&conn->mgr, 1000);\r
196 \r
197     return conn->dataReceived;\r
198 }\r
199 \r
200 bool\r
201 MatrixHttpPut(\r
202     MatrixClient * client,\r
203     const char * url,\r
204     const char * requestBuffer,\r
205     char * outResponseBuffer, int outResponseCap,\r
206     bool authenticated)\r
207 {\r
208     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
209     if (! conn->connected)\r
210         MatrixHttpConnect(client);\r
211 \r
212     conn->dataReceived = false;\r
213 \r
214     struct mg_str host = mg_url_host(client->server);\r
215 \r
216     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
217     if (authenticated)\r
218         sprintf(authorizationHeader,\r
219             "Authorization: Bearer %s\r\n", client->accessToken);\r
220     else\r
221         authorizationHeader[0] = '\0';\r
222 \r
223     printf("PUT %s HTTP/1.0\r\n"\r
224             "Host: %.*s\r\n"\r
225             "%s"\r
226             "Content-Type: application/json\r\n"\r
227             "Content-Length: %d\r\n"\r
228             "\r\n"\r
229             "%s"\r
230             "\r\n",\r
231             url,\r
232             host.len, host.ptr,\r
233             authorizationHeader,\r
234             strlen(requestBuffer),\r
235             requestBuffer);\r
236 \r
237     mg_printf(conn->connection,\r
238             "PUT %s HTTP/1.0\r\n"\r
239             "Host: %.*s\r\n"\r
240             "%s"\r
241             "Content-Type: application/json\r\n"\r
242             "Content-Length: %d\r\n"\r
243             "\r\n"\r
244             "%s"\r
245             "\r\n",\r
246             url,\r
247             host.len, host.ptr,\r
248             authorizationHeader,\r
249             strlen(requestBuffer),\r
250             requestBuffer);\r
251 \r
252     conn->data = outResponseBuffer;\r
253     conn->dataCap = outResponseCap;\r
254     \r
255     while (! conn->dataReceived)\r
256         mg_mgr_poll(&conn->mgr, 1000);\r
257 \r
258     return conn->dataReceived;\r
259 }\r