6 #include <mongoose.h>
\r
8 //#define HTTP_DATA_SIZE 1024
\r
9 #define AUTHORIZATION_HEADER_LEN 64
\r
11 typedef struct MatrixHttpConnection {
\r
13 struct mg_connection * connection;
\r
19 } MatrixHttpConnection;
\r
23 struct mg_connection *c,
\r
28 MatrixClient * client = (MatrixClient *)fn_data;
\r
29 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
31 if (ev == MG_EV_CONNECT)
\r
33 struct mg_str host = mg_url_host(client->server);
\r
35 // If s_url is https://, tell client connection to use TLS
\r
36 if (mg_url_is_ssl(client->server))
\r
38 struct mg_tls_opts opts = {.srvname = host};
\r
39 mg_tls_init(c, &opts);
\r
42 conn->connection = c;
\r
43 conn->connected = true;
\r
45 if (ev == MG_EV_HTTP_MSG)
\r
48 struct mg_http_message *hm = (struct mg_http_message *)ev_data;
\r
49 // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);
\r
50 // client->dataLen = hm->message.len;
\r
51 memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);
\r
52 conn->data[hm->body.len] = '\0';
\r
53 conn->dataLen = hm->body.len;
\r
54 conn->dataReceived = true;
\r
60 MatrixClient * client)
\r
62 MatrixHttpConnection * conn =
\r
63 (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
\r
65 client->httpUserData = conn;
\r
67 mg_mgr_init(&conn->mgr);
\r
69 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
71 while (! conn->connected)
\r
72 mg_mgr_poll(&conn->mgr, 1000);
\r
74 return conn->connected;
\r
79 MatrixClient * client)
\r
81 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
83 mg_mgr_free(&conn->mgr);
\r
92 MatrixClient * client,
\r
94 char * outResponseBuffer, int outResponseCap,
\r
97 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
99 conn->dataReceived = false;
\r
101 struct mg_str host = mg_url_host(client->server);
\r
103 static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";
\r
105 sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
106 "Authorization: Bearer %s\r\n", client->accessTokenBuffer);
\r
108 mg_printf(conn->connection,
\r
109 "GET %s HTTP/1.1\r\n"
\r
114 host.len, host.ptr,
\r
115 authorizationHeader);
\r
117 conn->data = outResponseBuffer;
\r
118 conn->dataCap = outResponseCap;
\r
120 while (! conn->dataReceived)
\r
121 mg_mgr_poll(&conn->mgr, 1000);
\r
123 return conn->dataReceived;
\r
128 MatrixClient * client,
\r
130 const char * requestBuffer,
\r
131 char * outResponseBuffer, int outResponseCap,
\r
132 bool authenticated)
\r
134 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
136 conn->dataReceived = false;
\r
138 struct mg_str host = mg_url_host(client->server);
\r
140 static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";
\r
142 sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
143 "Authorization: Bearer %s\r\n", client->accessTokenBuffer);
\r
145 mg_printf(conn->connection,
\r
146 "POST %s HTTP/1.0\r\n"
\r
149 "Content-Type: application/json\r\n"
\r
150 "Content-Length: %d\r\n"
\r
155 host.len, host.ptr,
\r
156 authorizationHeader,
\r
157 strlen(requestBuffer),
\r
160 conn->data = outResponseBuffer;
\r
161 conn->dataCap = outResponseCap;
\r
163 while (! conn->dataReceived)
\r
164 mg_mgr_poll(&conn->mgr, 1000);
\r
166 return conn->dataReceived;
\r
171 MatrixClient * client,
\r
173 const char * requestBuffer,
\r
174 char * outResponseBuffer, int outResponseCap,
\r
175 bool authenticated)
\r
177 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
179 conn->dataReceived = false;
\r
181 struct mg_str host = mg_url_host(client->server);
\r
183 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
185 sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
186 "Authorization: Bearer %s\r\n", client->accessTokenBuffer);
\r
188 authorizationHeader[0] = '\0';
\r
190 mg_printf(conn->connection,
\r
191 "PUT %s HTTP/1.0\r\n"
\r
194 "Content-Type: application/json\r\n"
\r
195 "Content-Length: %d\r\n"
\r
200 host.len, host.ptr,
\r
201 authorizationHeader,
\r
202 strlen(requestBuffer),
\r
205 conn->data = outResponseBuffer;
\r
206 conn->dataCap = outResponseCap;
\r
208 while (! conn->dataReceived)
\r
209 mg_mgr_poll(&conn->mgr, 1000);
\r
211 return conn->dataReceived;
\r