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_MSG)
\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
59 printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);
\r
61 if (ev == MG_EV_CLOSE)
\r
63 conn->connection = NULL;
\r
64 conn->connected = false;
\r
70 MatrixClient * client)
\r
72 MatrixHttpConnection * conn =
\r
73 (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
\r
75 client->httpUserData = conn;
\r
77 mg_mgr_init(&conn->mgr);
\r
79 return MatrixHttpConnect(client);
\r
84 MatrixClient * client)
\r
86 MatrixHttpConnection * conn =
\r
87 (MatrixHttpConnection *)client->httpUserData;
\r
89 struct mg_connection * c =
\r
90 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
92 while (! conn->connected)
\r
93 mg_mgr_poll(&conn->mgr, 1000);
\r
95 return conn->connected;
\r
100 MatrixClient * client)
\r
102 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
104 mg_mgr_free(&conn->mgr);
\r
113 MatrixClient * client,
\r
115 char * outResponseBuffer, int outResponseCap,
\r
116 bool authenticated)
\r
118 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
119 if (! conn->connected)
\r
120 MatrixHttpConnect(client);
\r
122 conn->dataReceived = false;
\r
124 struct mg_str host = mg_url_host(client->server);
\r
126 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\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
133 authorizationHeader[0] = '\0';
\r
135 mg_printf(conn->connection,
\r
136 "GET %s HTTP/1.1\r\n"
\r
141 host.len, host.ptr,
\r
142 authorizationHeader);
\r
144 conn->data = outResponseBuffer;
\r
145 conn->dataCap = outResponseCap;
\r
147 while (! conn->dataReceived)
\r
148 mg_mgr_poll(&conn->mgr, 1000);
\r
150 return conn->dataReceived;
\r
155 MatrixClient * client,
\r
157 const char * requestBuffer,
\r
158 char * outResponseBuffer, int outResponseCap,
\r
159 bool authenticated)
\r
161 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
162 if (! conn->connected)
\r
163 MatrixHttpConnect(client);
\r
165 conn->dataReceived = false;
\r
167 struct mg_str host = mg_url_host(client->server);
\r
169 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
171 sprintf(authorizationHeader,
\r
172 "Authorization: Bearer %s\r\n", client->accessToken);
\r
174 authorizationHeader[0] = '\0';
\r
176 mg_printf(conn->connection,
\r
177 "POST %s HTTP/1.0\r\n"
\r
180 "Content-Type: application/json\r\n"
\r
181 "Content-Length: %d\r\n"
\r
186 host.len, host.ptr,
\r
187 authorizationHeader,
\r
188 strlen(requestBuffer),
\r
191 conn->data = outResponseBuffer;
\r
192 conn->dataCap = outResponseCap;
\r
194 while (! conn->dataReceived)
\r
195 mg_mgr_poll(&conn->mgr, 1000);
\r
197 return conn->dataReceived;
\r
202 MatrixClient * client,
\r
204 const char * requestBuffer,
\r
205 char * outResponseBuffer, int outResponseCap,
\r
206 bool authenticated)
\r
208 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
209 if (! conn->connected)
\r
210 MatrixHttpConnect(client);
\r
212 conn->dataReceived = false;
\r
214 struct mg_str host = mg_url_host(client->server);
\r
216 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
218 sprintf(authorizationHeader,
\r
219 "Authorization: Bearer %s\r\n", client->accessToken);
\r
221 authorizationHeader[0] = '\0';
\r
223 mg_printf(conn->connection,
\r
224 "PUT %s HTTP/1.0\r\n"
\r
227 "Content-Type: application/json\r\n"
\r
228 "Content-Length: %d\r\n"
\r
233 host.len, host.ptr,
\r
234 authorizationHeader,
\r
235 strlen(requestBuffer),
\r
238 conn->data = outResponseBuffer;
\r
239 conn->dataCap = outResponseCap;
\r
241 while (! conn->dataReceived)
\r
242 mg_mgr_poll(&conn->mgr, 1000);
\r
244 return conn->dataReceived;
\r