7 #include <mongoose.h>
\r
9 #define AUTHORIZATION_HEADER_LEN 64
\r
11 typedef struct MatrixHttpConnection {
\r
13 struct mg_connection * connection;
\r
16 const char * accessToken;
\r
23 } MatrixHttpConnection;
\r
27 struct mg_connection *c,
\r
32 MatrixHttpConnection * conn = (MatrixHttpConnection *)fn_data;
\r
34 if (ev == MG_EV_CONNECT)
\r
36 struct mg_str host = mg_url_host(conn->host);
\r
38 // If s_url is https://, tell client connection to use TLS
\r
39 if (mg_url_is_ssl(conn->host))
\r
41 static struct mg_tls_opts opts;
\r
42 opts.srvname = host;
\r
43 mg_tls_init(c, &opts);
\r
46 conn->connection = c;
\r
47 conn->connected = true;
\r
49 if (ev == MG_EV_HTTP_CHUNK)
\r
53 if (ev == MG_EV_HTTP_MSG)
\r
56 struct mg_http_message *hm = (struct mg_http_message *)ev_data;
\r
58 memcpy(conn->data, hm->body.ptr, hm->body.len);
\r
60 conn->data[hm->body.len] = '\0';
\r
61 conn->dataLen = hm->body.len;
\r
62 conn->dataReceived = true;
\r
64 if (ev == MG_EV_CLOSE)
\r
66 conn->connection = NULL;
\r
67 conn->connected = false;
\r
73 MatrixHttpConnection * hc)
\r
75 //struct mg_connection * c =
\r
76 mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc);
\r
78 while (! hc->connected)
\r
79 mg_mgr_poll(&hc->mgr, 1000);
\r
81 return hc->connected;
\r
86 MatrixHttpConnection ** hc,
\r
89 *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection));
\r
93 mg_mgr_init(&(*hc)->mgr);
\r
95 MatrixHttpConnect(*hc);
\r
102 MatrixHttpConnection ** hc)
\r
104 mg_mgr_free(&(*hc)->mgr);
\r
113 MatrixHttpSetAccessToken(
\r
114 MatrixHttpConnection * hc,
\r
115 const char * accessToken)
\r
117 hc->accessToken = accessToken;
\r
124 MatrixHttpConnection * hc,
\r
126 char * outResponseBuffer, int outResponseCap,
\r
127 bool authenticated)
\r
129 if (! hc->connected)
\r
130 MatrixHttpConnect(hc);
\r
132 hc->dataReceived = false;
\r
134 struct mg_str host = mg_url_host(hc->host);
\r
136 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
138 snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
139 "Authorization: Bearer %s\r\n", hc->accessToken);
\r
141 authorizationHeader[0] = '\0';
\r
143 mg_printf(hc->connection,
\r
144 "GET %s HTTP/1.1\r\n"
\r
149 host.len, host.ptr,
\r
150 authorizationHeader);
\r
152 hc->data = outResponseBuffer;
\r
153 hc->dataCap = outResponseCap;
\r
155 while (! hc->dataReceived)
\r
156 mg_mgr_poll(&hc->mgr, 1000);
\r
158 return hc->dataReceived;
\r
163 MatrixHttpConnection * hc,
\r
165 const char * requestBuffer,
\r
166 char * outResponseBuffer, int outResponseCap,
\r
167 bool authenticated)
\r
169 if (! hc->connected)
\r
170 MatrixHttpConnect(hc);
\r
172 hc->dataReceived = false;
\r
174 struct mg_str host = mg_url_host(hc->host);
\r
176 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
178 snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
179 "Authorization: Bearer %s\r\n", hc->accessToken);
\r
181 authorizationHeader[0] = '\0';
\r
183 mg_printf(hc->connection,
\r
184 "POST %s HTTP/1.0\r\n"
\r
187 "Content-Type: application/json\r\n"
\r
188 "Content-Length: %d\r\n"
\r
193 (int)host.len, host.ptr,
\r
194 authorizationHeader,
\r
195 (int)strlen(requestBuffer),
\r
198 hc->data = outResponseBuffer;
\r
199 hc->dataCap = outResponseCap;
\r
201 while (! hc->dataReceived)
\r
202 mg_mgr_poll(&hc->mgr, 1000);
\r
204 return hc->dataReceived;
\r
209 MatrixHttpConnection * hc,
\r
211 const char * requestBuffer,
\r
212 char * outResponseBuffer, int outResponseCap,
\r
213 bool authenticated)
\r
215 if (! hc->connected)
\r
216 MatrixHttpConnect(hc);
\r
218 hc->dataReceived = false;
\r
220 struct mg_str host = mg_url_host(hc->host);
\r
222 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
224 snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
225 "Authorization: Bearer %s\r\n", hc->accessToken);
\r
227 authorizationHeader[0] = '\0';
\r
229 mg_printf(hc->connection,
\r
230 "PUT %s HTTP/1.0\r\n"
\r
233 "Content-Type: application/json\r\n"
\r
234 "Content-Length: %d\r\n"
\r
239 (int)host.len, host.ptr,
\r
240 authorizationHeader,
\r
241 (int)strlen(requestBuffer),
\r
244 hc->data = outResponseBuffer;
\r
245 hc->dataCap = outResponseCap;
\r
247 while (! hc->dataReceived)
\r
248 mg_mgr_poll(&hc->mgr, 1000);
\r
250 return hc->dataReceived;
\r