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 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 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
76 while (! conn->connected)
\r
77 mg_mgr_poll(&conn->mgr, 1000);
\r
79 return conn->connected;
\r
84 MatrixClient * client)
\r
86 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
88 mg_mgr_free(&conn->mgr);
\r
97 MatrixClient * client,
\r
99 char * outResponseBuffer, int outResponseCap,
\r
100 bool authenticated)
\r
102 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
104 conn->dataReceived = false;
\r
106 struct mg_str host = mg_url_host(client->server);
\r
108 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
110 sprintf(authorizationHeader,
\r
111 "Authorization: Bearer %s\r\n", client->accessToken);
\r
112 // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,
\r
113 // "Authorization: Bearer %s\r\n", client->accessToken);
\r
115 authorizationHeader[0] = '\0';
\r
117 mg_printf(conn->connection,
\r
118 "GET %s HTTP/1.1\r\n"
\r
123 host.len, host.ptr,
\r
124 authorizationHeader);
\r
126 conn->data = outResponseBuffer;
\r
127 conn->dataCap = outResponseCap;
\r
129 while (! conn->dataReceived)
\r
130 mg_mgr_poll(&conn->mgr, 1000);
\r
132 return conn->dataReceived;
\r
137 MatrixClient * client,
\r
139 const char * requestBuffer,
\r
140 char * outResponseBuffer, int outResponseCap,
\r
141 bool authenticated)
\r
143 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
145 conn->dataReceived = false;
\r
147 struct mg_str host = mg_url_host(client->server);
\r
149 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
151 sprintf(authorizationHeader,
\r
152 "Authorization: Bearer %s\r\n", client->accessToken);
\r
154 authorizationHeader[0] = '\0';
\r
156 mg_printf(conn->connection,
\r
157 "POST %s HTTP/1.0\r\n"
\r
160 "Content-Type: application/json\r\n"
\r
161 "Content-Length: %d\r\n"
\r
166 host.len, host.ptr,
\r
167 authorizationHeader,
\r
168 strlen(requestBuffer),
\r
171 conn->data = outResponseBuffer;
\r
172 conn->dataCap = outResponseCap;
\r
174 while (! conn->dataReceived)
\r
175 mg_mgr_poll(&conn->mgr, 1000);
\r
177 return conn->dataReceived;
\r
182 MatrixClient * client,
\r
184 const char * requestBuffer,
\r
185 char * outResponseBuffer, int outResponseCap,
\r
186 bool authenticated)
\r
188 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
190 conn->dataReceived = false;
\r
192 struct mg_str host = mg_url_host(client->server);
\r
194 static char authorizationHeader[AUTHORIZATION_HEADER_LEN];
\r
196 sprintf(authorizationHeader,
\r
197 "Authorization: Bearer %s\r\n", client->accessToken);
\r
199 authorizationHeader[0] = '\0';
\r
201 mg_printf(conn->connection,
\r
202 "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 conn->data = outResponseBuffer;
\r
217 conn->dataCap = outResponseCap;
\r
219 while (! conn->dataReceived)
\r
220 mg_mgr_poll(&conn->mgr, 1000);
\r
222 return conn->dataReceived;
\r