+#include "matrix.h"\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <stdbool.h>\r
+#include <mongoose.h>\r
+\r
+//#define HTTP_DATA_SIZE 1024\r
+\r
+typedef struct MatrixHttpConnection {\r
+ struct mg_mgr mgr;\r
+ struct mg_connection * connection;\r
+ bool connected;\r
+ char * data;\r
+ int dataCap;\r
+ int dataLen;\r
+ bool dataReceived;\r
+} MatrixHttpConnection;\r
+\r
+static void\r
+MatrixHttpCallback(\r
+ struct mg_connection *c,\r
+ int ev,\r
+ void *ev_data,\r
+ void *fn_data)\r
+{\r
+ MatrixClient * client = (MatrixClient *)fn_data;\r
+ MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+ if (ev == MG_EV_CONNECT)\r
+ {\r
+ struct mg_str host = mg_url_host(client->server);\r
+\r
+ // If s_url is https://, tell client connection to use TLS\r
+ if (mg_url_is_ssl(client->server))\r
+ {\r
+ struct mg_tls_opts opts = {.srvname = host};\r
+ mg_tls_init(c, &opts);\r
+ }\r
+\r
+ conn->connection = c;\r
+ conn->connected = true;\r
+ }\r
+ if (ev == MG_EV_HTTP_MSG)\r
+ {\r
+ // Response\r
+ struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
+ // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
+ // client->dataLen = hm->message.len;\r
+ memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
+ conn->dataLen = hm->body.len;\r
+ conn->dataReceived = true;\r
+ }\r
+}\r
+\r
+bool\r
+MatrixHttpInit(\r
+ MatrixClient * client)\r
+{\r
+ MatrixHttpConnection * conn =\r
+ (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
+\r
+ client->httpUserData = conn;\r
+ \r
+ mg_mgr_init(&conn->mgr);\r
+\r
+ mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
+\r
+ while (! conn->connected)\r
+ mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+ return conn->connected;\r
+}\r
+\r
+bool\r
+MatrixHttpDeinit(\r
+ MatrixClient * client)\r
+{\r
+ MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+ \r
+ mg_mgr_free(&conn->mgr);\r
+\r
+ free(conn);\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixHttpGet(\r
+ MatrixClient * client,\r
+ const char * url,\r
+ char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
+{\r
+ MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+ conn->dataReceived = false;\r
+\r
+ struct mg_str host = mg_url_host(client->server);\r
+\r
+ mg_printf(conn->connection,\r
+ "GET %s HTTP/1.1\r\n"\r
+ "Host: %.*s\r\n"\r
+ "\r\n",\r
+ url,\r
+ host.len, host.ptr);\r
+\r
+ conn->data = outResponseBuffer;\r
+ conn->dataCap = outResponseCap;\r
+ \r
+ while (! conn->dataReceived)\r
+ mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+ *outResponseLen = conn->dataLen;\r
+\r
+ return conn->dataReceived;\r
+}\r
+\r
+bool\r
+MatrixHttpPost(\r
+ MatrixClient * client,\r
+ const char * url,\r
+ char * requestBuffer, int requestLen,\r
+ char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
+{\r
+ MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+ conn->dataReceived = false;\r
+\r
+ struct mg_str host = mg_url_host(client->server);\r
+\r
+ mg_printf(conn->connection,\r
+ "POST %s HTTP/1.0\r\n"\r
+ "Host: %.*s\r\n"\r
+ "Content-Type: application/json\r\n"\r
+ "Content-Length: %d\r\n"\r
+ "\r\n"\r
+ "%s"\r
+ "\r\n",\r
+ url,\r
+ host.len, host.ptr,\r
+ requestLen,\r
+ requestBuffer);\r
+\r
+ conn->data = outResponseBuffer;\r
+ conn->dataCap = outResponseCap;\r
+ \r
+ while (! conn->dataReceived)\r
+ mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+ *outResponseLen = conn->dataLen;\r
+\r
+ return conn->dataReceived;\r
+}
\ No newline at end of file