7 #include <mongoose.h>
\r
9 //#define HTTP_DATA_SIZE 1024
\r
10 #define AUTHORIZATION_HEADER_LEN 64
\r
12 typedef struct MatrixHttpConnection {
\r
14 struct mg_connection * connection;
\r
17 const char * accessToken;
\r
24 } MatrixHttpConnection;
\r
28 struct mg_connection *c,
\r
33 MatrixHttpConnection * conn = (MatrixHttpConnection *)fn_data;
\r
35 if (ev == MG_EV_CONNECT)
\r
37 struct mg_str host = mg_url_host(conn->host);
\r
39 // If s_url is https://, tell client connection to use TLS
\r
40 if (mg_url_is_ssl(conn->host))
\r
42 static struct mg_tls_opts opts;
\r
43 opts.srvname = host;
\r
44 mg_tls_init(c, &opts);
\r
47 conn->connection = c;
\r
48 conn->connected = true;
\r
50 if (ev == MG_EV_HTTP_CHUNK)
\r
54 if (ev == MG_EV_HTTP_MSG)
\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
66 //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);
\r
68 if (ev == MG_EV_CLOSE)
\r
70 conn->connection = NULL;
\r
71 conn->connected = false;
\r
77 MatrixHttpConnection * hc)
\r
79 //struct mg_connection * c =
\r
80 mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc);
\r
82 while (! hc->connected)
\r
83 mg_mgr_poll(&hc->mgr, 1000);
\r
85 return hc->connected;
\r
90 MatrixHttpConnection ** hc,
\r
93 *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection));
\r
97 mg_mgr_init(&(*hc)->mgr);
\r
99 MatrixHttpConnect(*hc);
\r
106 MatrixHttpConnection ** hc)
\r
108 mg_mgr_free(&(*hc)->mgr);
\r
117 MatrixHttpSetAccessToken(
\r
118 MatrixHttpConnection * hc,
\r
119 const char * accessToken)
\r
121 hc->accessToken = accessToken;
\r
128 MatrixHttpConnection * hc,
\r
130 char * outResponseBuffer, int outResponseCap,
\r
131 bool authenticated)
\r
133 if (! hc->connected)
\r
134 MatrixHttpConnect(hc);
\r
136 hc->dataReceived = false;
\r
138 struct mg_str host = mg_url_host(hc->host);
\r
140 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\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
147 authorizationHeader[0] = '\0';
\r
150 "GET %s HTTP/1.1\r\n"
\r
155 host.len, host.ptr,
\r
156 authorizationHeader);
\r
158 mg_printf(hc->connection,
\r
159 "GET %s HTTP/1.1\r\n"
\r
164 host.len, host.ptr,
\r
165 authorizationHeader);
\r
167 hc->data = outResponseBuffer;
\r
168 hc->dataCap = outResponseCap;
\r
170 while (! hc->dataReceived)
\r
171 mg_mgr_poll(&hc->mgr, 1000);
\r
173 return hc->dataReceived;
\r
178 MatrixHttpConnection * hc,
\r
180 const char * requestBuffer,
\r
181 char * outResponseBuffer, int outResponseCap,
\r
182 bool authenticated)
\r
184 if (! hc->connected)
\r
185 MatrixHttpConnect(hc);
\r
187 hc->dataReceived = false;
\r
189 struct mg_str host = mg_url_host(hc->host);
\r
191 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
193 sprintf(authorizationHeader,
\r
194 "Authorization: Bearer %s\r\n", hc->accessToken);
\r
196 authorizationHeader[0] = '\0';
\r
199 "POST %s HTTP/1.0\r\n"
\r
202 "Content-Type: application/json\r\n"
\r
203 "Content-Length: %d\r\n"
\r
208 host.len, host.ptr,
\r
209 authorizationHeader,
\r
210 strlen(requestBuffer),
\r
213 mg_printf(hc->connection,
\r
214 "POST %s HTTP/1.0\r\n"
\r
217 "Content-Type: application/json\r\n"
\r
218 "Content-Length: %d\r\n"
\r
223 host.len, host.ptr,
\r
224 authorizationHeader,
\r
225 strlen(requestBuffer),
\r
228 hc->data = outResponseBuffer;
\r
229 hc->dataCap = outResponseCap;
\r
231 while (! hc->dataReceived)
\r
232 mg_mgr_poll(&hc->mgr, 1000);
\r
234 return hc->dataReceived;
\r
239 MatrixHttpConnection * hc,
\r
241 const char * requestBuffer,
\r
242 char * outResponseBuffer, int outResponseCap,
\r
243 bool authenticated)
\r
245 if (! hc->connected)
\r
246 MatrixHttpConnect(hc);
\r
248 hc->dataReceived = false;
\r
250 struct mg_str host = mg_url_host(hc->host);
\r
252 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
254 sprintf(authorizationHeader,
\r
255 "Authorization: Bearer %s\r\n", hc->accessToken);
\r
257 authorizationHeader[0] = '\0';
\r
261 "PUT %s HTTP/1.0\r\n"
\r
264 "Content-Type: application/json\r\n"
\r
265 "Content-Length: %d\r\n"
\r
270 host.len, host.ptr,
\r
271 authorizationHeader,
\r
272 strlen(requestBuffer),
\r
275 mg_printf(hc->connection,
\r
276 "PUT %s HTTP/1.0\r\n"
\r
279 "Content-Type: application/json\r\n"
\r
280 "Content-Length: %d\r\n"
\r
285 host.len, host.ptr,
\r
286 authorizationHeader,
\r
287 strlen(requestBuffer),
\r
290 hc->data = outResponseBuffer;
\r
291 hc->dataCap = outResponseCap;
\r
293 while (! hc->dataReceived)
\r
294 mg_mgr_poll(&hc->mgr, 1000);
\r
296 return hc->dataReceived;
\r