]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix_http_curl.c
get Login example working with CURL
[matrix_esp_thesis] / src / matrix_http_curl.c
diff --git a/src/matrix_http_curl.c b/src/matrix_http_curl.c
new file mode 100644 (file)
index 0000000..577c5ec
--- /dev/null
@@ -0,0 +1,113 @@
+#include "matrix.h"\r
+\r
+#include <curl/curl.h>\r
+\r
+\r
+\r
+typedef struct {\r
+    char * ptr;\r
+    int cap;\r
+    int len;\r
+} WriteStr;\r
+\r
+// typedef struct {\r
+//     Str str;\r
+//     size_t pos;\r
+// } ReadStr;\r
+\r
+size_t curlWriteString(char *ptr, size_t size, size_t nmemb, void *userdata) {\r
+    WriteStr *writeStr = (WriteStr *)userdata;\r
+\r
+    int toWrite = (int)size*nmemb;\r
+\r
+    int writable = writeStr->cap - writeStr->len;\r
+    int gonnaWrite = writable < (toWrite) ? writable : (toWrite);\r
+\r
+    for (int i = 0; i < gonnaWrite; i++)\r
+    {\r
+        int offset = writeStr->len;\r
+        writeStr->ptr[i+offset] = ptr[i];\r
+    }\r
+\r
+    writeStr->len += gonnaWrite;\r
+\r
+    return gonnaWrite;\r
+}\r
+// size_t curlReadString(char *dst, size_t size, size_t nmemb, void *userdata) {\r
+//     ReadStr *readStr = (ReadStr *)userdata;\r
+\r
+//     size_t copyAmount = size*nmemb;\r
+//     if (copyAmount > (readStr->str.len - readStr->pos)) {\r
+//         copyAmount = (readStr->str.len - readStr->pos);\r
+//     }\r
+\r
+//     memcpy(dst, readStr->str.str + readStr->pos, copyAmount);\r
+//     readStr->pos += copyAmount;\r
+//     return copyAmount;\r
+// }\r
+\r
+CURLcode\r
+curlPerform(CURL *curl) {\r
+    // struct curl_slist *list = NULL;\r
+    // list = curl_slist_append(list, uTokenHeaderStr);\r
+    \r
+    // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);\r
+    \r
+    CURLcode result = curl_easy_perform(curl);\r
+\r
+    // curl_slist_free_all(list);\r
+\r
+    return result;\r
+}\r
+\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
+    CURL *curl = (CURL *)client->httpUserData;\r
+\r
+    CURLcode res;\r
+    \r
+    if(curl) {\r
+        int urlLen = strlen(url);\r
+\r
+        char fullUrl[MAX_URL_LEN];\r
+        for (int i = 0; i < client->serverLen; i++)\r
+            fullUrl[i] = client->server[i];\r
+        for (int i = 0; i < urlLen; i++)\r
+            fullUrl[client->serverLen+i] = url[i];\r
+        fullUrl[client->serverLen+urlLen] = '\0';\r
+        curl_easy_setopt(curl, CURLOPT_URL, fullUrl);\r
+        \r
+        curl_easy_setopt(curl, CURLOPT_POST, 1L);\r
+        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBuffer);\r
+        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestLen);\r
+    \r
+        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);\r
+        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);\r
+\r
+        WriteStr writeStr = {\r
+            outResponseBuffer,\r
+            outResponseCap,\r
+            0\r
+        };\r
+        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteString);\r
+        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeStr);\r
+\r
+        res = curlPerform(curl);\r
+\r
+        *outResponseLen = writeStr.len;\r
+\r
+        if(res != CURLE_OK)\r
+            fprintf(stderr, "curl_easy_perform() failed: %s\n",\r
+                curl_easy_strerror(res));\r
+    }\r
+    \r
+\r
+    return res == CURLE_OK;\r
+}
\ No newline at end of file