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
65 MatrixClient * client)
\r
67 MatrixHttpConnection * conn =
\r
68 (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
\r
70 client->httpUserData = conn;
\r
72 mg_mgr_init(&conn->mgr);
\r
74 struct mg_connection * c =
\r
75 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
77 while (! conn->connected)
\r
78 mg_mgr_poll(&conn->mgr, 1000);
\r
80 return conn->connected;
\r
85 MatrixClient * client)
\r
87 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
89 mg_mgr_free(&conn->mgr);
\r
98 MatrixClient * client,
\r
100 char * outResponseBuffer, int outResponseCap,
\r
101 bool authenticated)
\r
103 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
105 conn->dataReceived = false;
\r
107 struct mg_str host = mg_url_host(client->server);
\r
109 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
111 sprintf(authorizationHeader,
\r
112 "Authorization: Bearer %s\r\n", client->accessToken);
\r
113 // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
114 // "Authorization: Bearer %s\r\n", client->accessToken);
\r
116 authorizationHeader[0] = '\0';
\r
118 mg_printf(conn->connection,
\r
119 "GET %s HTTP/1.1\r\n"
\r
124 host.len, host.ptr,
\r
125 authorizationHeader);
\r
127 conn->data = outResponseBuffer;
\r
128 conn->dataCap = outResponseCap;
\r
130 while (! conn->dataReceived)
\r
131 mg_mgr_poll(&conn->mgr, 1000);
\r
133 return conn->dataReceived;
\r
138 MatrixClient * client,
\r
140 const char * requestBuffer,
\r
141 char * outResponseBuffer, int outResponseCap,
\r
142 bool authenticated)
\r
144 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
146 conn->dataReceived = false;
\r
148 struct mg_str host = mg_url_host(client->server);
\r
150 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
152 sprintf(authorizationHeader,
\r
153 "Authorization: Bearer %s\r\n", client->accessToken);
\r
155 authorizationHeader[0] = '\0';
\r
157 mg_printf(conn->connection,
\r
158 "POST %s HTTP/1.0\r\n"
\r
161 "Content-Type: application/json\r\n"
\r
162 "Content-Length: %d\r\n"
\r
167 host.len, host.ptr,
\r
168 authorizationHeader,
\r
169 strlen(requestBuffer),
\r
172 conn->data = outResponseBuffer;
\r
173 conn->dataCap = outResponseCap;
\r
175 while (! conn->dataReceived)
\r
176 mg_mgr_poll(&conn->mgr, 1000);
\r
178 return conn->dataReceived;
\r
183 MatrixClient * client,
\r
185 const char * requestBuffer,
\r
186 char * outResponseBuffer, int outResponseCap,
\r
187 bool authenticated)
\r
189 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
191 conn->dataReceived = false;
\r
193 struct mg_str host = mg_url_host(client->server);
\r
195 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
197 sprintf(authorizationHeader,
\r
198 "Authorization: Bearer %s\r\n", client->accessToken);
\r
200 authorizationHeader[0] = '\0';
\r
202 printf("PUT %s HTTP/1.0\r\n"
\r
205 "Content-Type: application/json\r\n"
\r
206 "Content-Length: %d\r\n"
\r
211 host.len, host.ptr,
\r
212 authorizationHeader,
\r
213 strlen(requestBuffer),
\r
216 mg_printf(conn->connection,
\r
217 "PUT %s HTTP/1.0\r\n"
\r
220 "Content-Type: application/json\r\n"
\r
221 "Content-Length: %d\r\n"
\r
226 host.len, host.ptr,
\r
227 authorizationHeader,
\r
228 strlen(requestBuffer),
\r
231 conn->data = outResponseBuffer;
\r
232 conn->dataCap = outResponseCap;
\r
234 while (! conn->dataReceived)
\r
235 mg_mgr_poll(&conn->mgr, 1000);
\r
237 return conn->dataReceived;
\r