6 #include <mongoose.h>
\r
8 //#define HTTP_DATA_SIZE 1024
\r
10 typedef struct MatrixHttpConnection {
\r
12 struct mg_connection * connection;
\r
18 } MatrixHttpConnection;
\r
22 struct mg_connection *c,
\r
27 MatrixClient * client = (MatrixClient *)fn_data;
\r
28 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
30 if (ev == MG_EV_CONNECT)
\r
32 struct mg_str host = mg_url_host(client->server);
\r
34 // If s_url is https://, tell client connection to use TLS
\r
35 if (mg_url_is_ssl(client->server))
\r
37 struct mg_tls_opts opts = {.srvname = host};
\r
38 mg_tls_init(c, &opts);
\r
41 conn->connection = c;
\r
42 conn->connected = true;
\r
44 if (ev == MG_EV_HTTP_MSG)
\r
47 struct mg_http_message *hm = (struct mg_http_message *)ev_data;
\r
48 // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);
\r
49 // client->dataLen = hm->message.len;
\r
50 memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);
\r
51 conn->dataLen = hm->body.len;
\r
52 conn->dataReceived = true;
\r
58 MatrixClient * client)
\r
60 MatrixHttpConnection * conn =
\r
61 (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
\r
63 client->httpUserData = conn;
\r
65 mg_mgr_init(&conn->mgr);
\r
67 mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
\r
69 while (! conn->connected)
\r
70 mg_mgr_poll(&conn->mgr, 1000);
\r
72 return conn->connected;
\r
77 MatrixClient * client)
\r
79 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
81 mg_mgr_free(&conn->mgr);
\r
90 MatrixClient * client,
\r
92 char * outResponseBuffer, int outResponseCap)
\r
94 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
96 conn->dataReceived = false;
\r
98 struct mg_str host = mg_url_host(client->server);
\r
100 mg_printf(conn->connection,
\r
101 "GET %s HTTP/1.1\r\n"
\r
105 host.len, host.ptr);
\r
107 conn->data = outResponseBuffer;
\r
108 conn->dataCap = outResponseCap;
\r
110 while (! conn->dataReceived)
\r
111 mg_mgr_poll(&conn->mgr, 1000);
\r
113 return conn->dataReceived;
\r
118 MatrixClient * client,
\r
120 const char * requestBuffer,
\r
121 char * outResponseBuffer, int outResponseCap)
\r
123 MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
\r
125 conn->dataReceived = false;
\r
127 struct mg_str host = mg_url_host(client->server);
\r
129 mg_printf(conn->connection,
\r
130 "POST %s HTTP/1.0\r\n"
\r
132 "Content-Type: application/json\r\n"
\r
133 "Content-Length: %d\r\n"
\r
138 host.len, host.ptr,
\r
139 strlen(requestBuffer),
\r
142 conn->data = outResponseBuffer;
\r
143 conn->dataCap = outResponseCap;
\r
145 while (! conn->dataReceived)
\r
146 mg_mgr_poll(&conn->mgr, 1000);
\r
148 return conn->dataReceived;
\r