X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/d382d193cb2d550cc769afa76e55823865a39023..ef693c75de0b5719d47ab404616bf97b9f901524:/src/matrix_http_mongoose.c diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c index 8d575e5..3b6970c 100644 --- a/src/matrix_http_mongoose.c +++ b/src/matrix_http_mongoose.c @@ -1,4 +1,5 @@ #include "matrix.h" + #include #include #include @@ -11,6 +12,10 @@ typedef struct MatrixHttpConnection { struct mg_mgr mgr; struct mg_connection * connection; + + const char * host; + const char * accessToken; + bool connected; char * data; int dataCap; @@ -25,22 +30,26 @@ MatrixHttpCallback( void *ev_data, void *fn_data) { - MatrixClient * client = (MatrixClient *)fn_data; - MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + MatrixHttpConnection * conn = (MatrixHttpConnection *)fn_data; if (ev == MG_EV_CONNECT) { - struct mg_str host = mg_url_host(client->server); + struct mg_str host = mg_url_host(conn->host); // If s_url is https://, tell client connection to use TLS - if (mg_url_is_ssl(client->server)) + if (mg_url_is_ssl(conn->host)) { - struct mg_tls_opts opts = {.srvname = host}; + static struct mg_tls_opts opts; + opts.srvname = host; mg_tls_init(c, &opts); } conn->connection = c; conn->connected = true; + } + if (ev == MG_EV_HTTP_CHUNK) + { + } if (ev == MG_EV_HTTP_MSG) { @@ -48,64 +57,96 @@ MatrixHttpCallback( struct mg_http_message *hm = (struct mg_http_message *)ev_data; // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len); // client->dataLen = hm->message.len; - memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len); + memcpy(conn->data, hm->body.ptr, hm->body.len); + // memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len); conn->data[hm->body.len] = '\0'; conn->dataLen = hm->body.len; conn->dataReceived = true; + + //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data); + } + if (ev == MG_EV_CLOSE) + { + conn->connection = NULL; + conn->connected = false; } } +bool +MatrixHttpConnect( + MatrixHttpConnection * hc) +{ + //struct mg_connection * c = + mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc); + + while (! hc->connected) + mg_mgr_poll(&hc->mgr, 1000); + + return hc->connected; +} + bool MatrixHttpInit( - MatrixClient * client) + MatrixHttpConnection ** hc, + const char * host) { - MatrixHttpConnection * conn = - (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection)); - - client->httpUserData = conn; + *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection)); - mg_mgr_init(&conn->mgr); - - mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client); + (*hc)->host = host; + + mg_mgr_init(&(*hc)->mgr); - while (! conn->connected) - mg_mgr_poll(&conn->mgr, 1000); + MatrixHttpConnect(*hc); - return conn->connected; + return true; } bool MatrixHttpDeinit( - MatrixClient * client) + MatrixHttpConnection ** hc) { - MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; - - mg_mgr_free(&conn->mgr); + mg_mgr_free(&(*hc)->mgr); - free(conn); + free(*hc); + *hc = NULL; + + return true; +} + +bool +MatrixHttpSetAccessToken( + MatrixHttpConnection * hc, + const char * accessToken) +{ + hc->accessToken = accessToken; return true; } bool MatrixHttpGet( - MatrixClient * client, + MatrixHttpConnection * hc, const char * url, char * outResponseBuffer, int outResponseCap, bool authenticated) { - MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! hc->connected) + MatrixHttpConnect(hc); - conn->dataReceived = false; + hc->dataReceived = false; - struct mg_str host = mg_url_host(client->server); + struct mg_str host = mg_url_host(hc->host); - static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0"; + static char authorizationHeader[AUTHORIZATION_HEADER_LEN]; if (authenticated) - sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, - "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + sprintf(authorizationHeader, + "Authorization: Bearer %s\r\n", hc->accessToken); + // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, + // "Authorization: Bearer %s\r\n", client->accessToken); + else + authorizationHeader[0] = '\0'; - mg_printf(conn->connection, + printf( "GET %s HTTP/1.1\r\n" "Host: %.*s\r\n" "%s" @@ -114,35 +155,47 @@ MatrixHttpGet( host.len, host.ptr, authorizationHeader); - conn->data = outResponseBuffer; - conn->dataCap = outResponseCap; + mg_printf(hc->connection, + "GET %s HTTP/1.1\r\n" + "Host: %.*s\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader); + + hc->data = outResponseBuffer; + hc->dataCap = outResponseCap; - while (! conn->dataReceived) - mg_mgr_poll(&conn->mgr, 1000); + while (! hc->dataReceived) + mg_mgr_poll(&hc->mgr, 1000); - return conn->dataReceived; + return hc->dataReceived; } bool MatrixHttpPost( - MatrixClient * client, + MatrixHttpConnection * hc, const char * url, const char * requestBuffer, char * outResponseBuffer, int outResponseCap, bool authenticated) { - MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! hc->connected) + MatrixHttpConnect(hc); - conn->dataReceived = false; + hc->dataReceived = false; - struct mg_str host = mg_url_host(client->server); + struct mg_str host = mg_url_host(hc->host); - static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0"; + static char authorizationHeader[AUTHORIZATION_HEADER_LEN]; if (authenticated) - sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, - "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + sprintf(authorizationHeader, + "Authorization: Bearer %s\r\n", hc->accessToken); + else + authorizationHeader[0] = '\0'; - mg_printf(conn->connection, + printf( "POST %s HTTP/1.0\r\n" "Host: %.*s\r\n" "%s" @@ -157,37 +210,69 @@ MatrixHttpPost( strlen(requestBuffer), requestBuffer); - conn->data = outResponseBuffer; - conn->dataCap = outResponseCap; + mg_printf(hc->connection, + "POST %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "%s" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader, + strlen(requestBuffer), + requestBuffer); + + hc->data = outResponseBuffer; + hc->dataCap = outResponseCap; - while (! conn->dataReceived) - mg_mgr_poll(&conn->mgr, 1000); + while (! hc->dataReceived) + mg_mgr_poll(&hc->mgr, 1000); - return conn->dataReceived; + return hc->dataReceived; } bool MatrixHttpPut( - MatrixClient * client, + MatrixHttpConnection * hc, const char * url, const char * requestBuffer, char * outResponseBuffer, int outResponseCap, bool authenticated) { - MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! hc->connected) + MatrixHttpConnect(hc); - conn->dataReceived = false; + hc->dataReceived = false; - struct mg_str host = mg_url_host(client->server); + struct mg_str host = mg_url_host(hc->host); static char authorizationHeader[AUTHORIZATION_HEADER_LEN]; if (authenticated) - sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, - "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + sprintf(authorizationHeader, + "Authorization: Bearer %s\r\n", hc->accessToken); else authorizationHeader[0] = '\0'; - mg_printf(conn->connection, + + printf( + "PUT %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "%s" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader, + strlen(requestBuffer), + requestBuffer); + + mg_printf(hc->connection, "PUT %s HTTP/1.0\r\n" "Host: %.*s\r\n" "%s" @@ -202,11 +287,11 @@ MatrixHttpPut( strlen(requestBuffer), requestBuffer); - conn->data = outResponseBuffer; - conn->dataCap = outResponseCap; + hc->data = outResponseBuffer; + hc->dataCap = outResponseCap; - while (! conn->dataReceived) - mg_mgr_poll(&conn->mgr, 1000); + while (! hc->dataReceived) + mg_mgr_poll(&hc->mgr, 1000); - return conn->dataReceived; + return hc->dataReceived; }