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
20 } MatrixHttpConnection;
\r
24 struct mg_connection *c,
\r
29 MatrixClient * client = (MatrixClient *)fn_data;
\r
30 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
32 if (ev == MG_EV_CONNECT)
\r
34 struct mg_str host = mg_url_host(client->server);
\r
36 // If s_url is https://, tell client connection to use TLS
\r
37 if (mg_url_is_ssl(client->server))
\r
39 static struct mg_tls_opts opts;
\r
40 opts.srvname = host;
\r
41 mg_tls_init(c, &opts);
\r
44 conn->connection = c;
\r
45 conn->connected = true;
\r
47 if (ev == MG_EV_HTTP_CHUNK)
\r
51 if (ev == MG_EV_HTTP_MSG)
\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
63 //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);
\r
65 if (ev == MG_EV_CLOSE)
\r
67 conn->connection = NULL;
\r
68 conn->connected = false;
\r
74 MatrixClient * client)
\r
76 MatrixHttpConnection * conn =
\r
77 (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
\r
79 client->httpUserData = conn;
\r
81 mg_mgr_init(&conn->mgr);
\r
83 return MatrixHttpConnect(client);
\r
88 MatrixClient * client)
\r
90 MatrixHttpConnection * conn =
\r
91 (MatrixHttpConnection *)client->httpUserData;
\r
93 //struct mg_connection * c =
\r
94 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
96 while (! conn->connected)
\r
97 mg_mgr_poll(&conn->mgr, 1000);
\r
99 return conn->connected;
\r
104 MatrixClient * client)
\r
106 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
108 mg_mgr_free(&conn->mgr);
\r
117 MatrixClient * client,
\r
119 char * outResponseBuffer, int outResponseCap,
\r
120 bool authenticated)
\r
122 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
123 if (! conn->connected)
\r
124 MatrixHttpConnect(client);
\r
126 conn->dataReceived = false;
\r
128 struct mg_str host = mg_url_host(client->server);
\r
130 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\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
137 authorizationHeader[0] = '\0';
\r
140 "GET %s HTTP/1.1\r\n"
\r
145 host.len, host.ptr,
\r
146 authorizationHeader);
\r
148 mg_printf(conn->connection,
\r
149 "GET %s HTTP/1.1\r\n"
\r
154 host.len, host.ptr,
\r
155 authorizationHeader);
\r
157 conn->data = outResponseBuffer;
\r
158 conn->dataCap = outResponseCap;
\r
160 while (! conn->dataReceived)
\r
161 mg_mgr_poll(&conn->mgr, 1000);
\r
163 return conn->dataReceived;
\r
168 MatrixClient * client,
\r
170 const char * requestBuffer,
\r
171 char * outResponseBuffer, int outResponseCap,
\r
172 bool authenticated)
\r
174 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
175 if (! conn->connected)
\r
176 MatrixHttpConnect(client);
\r
178 conn->dataReceived = false;
\r
180 struct mg_str host = mg_url_host(client->server);
\r
182 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
184 sprintf(authorizationHeader,
\r
185 "Authorization: Bearer %s\r\n", client->accessToken);
\r
187 authorizationHeader[0] = '\0';
\r
190 "POST %s HTTP/1.0\r\n"
\r
193 "Content-Type: application/json\r\n"
\r
194 "Content-Length: %d\r\n"
\r
199 host.len, host.ptr,
\r
200 authorizationHeader,
\r
201 strlen(requestBuffer),
\r
204 mg_printf(conn->connection,
\r
205 "POST %s HTTP/1.0\r\n"
\r
208 "Content-Type: application/json\r\n"
\r
209 "Content-Length: %d\r\n"
\r
214 host.len, host.ptr,
\r
215 authorizationHeader,
\r
216 strlen(requestBuffer),
\r
219 conn->data = outResponseBuffer;
\r
220 conn->dataCap = outResponseCap;
\r
222 while (! conn->dataReceived)
\r
223 mg_mgr_poll(&conn->mgr, 1000);
\r
225 return conn->dataReceived;
\r
230 MatrixClient * client,
\r
232 const char * requestBuffer,
\r
233 char * outResponseBuffer, int outResponseCap,
\r
234 bool authenticated)
\r
236 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
237 if (! conn->connected)
\r
238 MatrixHttpConnect(client);
\r
240 conn->dataReceived = false;
\r
242 struct mg_str host = mg_url_host(client->server);
\r
244 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
246 sprintf(authorizationHeader,
\r
247 "Authorization: Bearer %s\r\n", client->accessToken);
\r
249 authorizationHeader[0] = '\0';
\r
253 "PUT %s HTTP/1.0\r\n"
\r
256 "Content-Type: application/json\r\n"
\r
257 "Content-Length: %d\r\n"
\r
262 host.len, host.ptr,
\r
263 authorizationHeader,
\r
264 strlen(requestBuffer),
\r
267 mg_printf(conn->connection,
\r
268 "PUT %s HTTP/1.0\r\n"
\r
271 "Content-Type: application/json\r\n"
\r
272 "Content-Length: %d\r\n"
\r
277 host.len, host.ptr,
\r
278 authorizationHeader,
\r
279 strlen(requestBuffer),
\r
282 conn->data = outResponseBuffer;
\r
283 conn->dataCap = outResponseCap;
\r
285 while (! conn->dataReceived)
\r
286 mg_mgr_poll(&conn->mgr, 1000);
\r
288 return conn->dataReceived;
\r