about summary refs log tree commit diff
path: root/src/request.c
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2024-11-24 15:51:45 +0300
committerNakidai <nakidai@disroot.org>2024-11-24 15:51:45 +0300
commit0cd2e735fd54f96e4765b823cded724c61572031 (patch)
treeb74fd7d78daac4dd6933717a1c74a09bcbef198b /src/request.c
downloadlibhttpc-0cd2e735fd54f96e4765b823cded724c61572031.tar.gz
libhttpc-0cd2e735fd54f96e4765b823cded724c61572031.zip
Add code
Diffstat (limited to 'src/request.c')
-rw-r--r--src/request.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/request.c b/src/request.c
new file mode 100644
index 0000000..b582dcf
--- /dev/null
+++ b/src/request.c
@@ -0,0 +1,88 @@
+#include "libhttpc.h"
+
+#include <stddef.h>
+#include <string.h>
+
+
+struct LibHTTPC_Request *LibHTTPC_loadRequest(struct LibHTTPC_Request *request_buf, char *buf)
+{
+    char *next;
+    int selfmalloc = 0;
+
+    if (!buf)
+        return NULL;
+
+    if (!request_buf)
+    {
+        request_buf = LibHTTPC_malloc(sizeof(struct LibHTTPC_Request));
+        memset(request_buf, '\0', sizeof(struct LibHTTPC_Request));
+        request_buf->selfalloc = 1;
+        request_buf->header_count = -1;
+    }
+
+    if (request_buf->header_count == -1)
+    {
+        if (!LibHTTPC_malloc || !LibHTTPC_realloc)
+            return NULL;
+        selfmalloc = 1;
+        request_buf->header_selfalloc = 1;
+        request_buf->header_count = 0;
+    }
+
+    request_buf->method = request_buf->buf = buf;
+
+    next = strchr(request_buf->method, ' ');
+    *next = '\0';
+    request_buf->uri = next + 1;
+
+    next = strchr(request_buf->uri, ' ');
+    *next = '\0';
+    request_buf->version = next + 1;
+
+    next = strstr(request_buf->version, "\r\n");
+    *next++ = '\0';
+
+    for (size_t i = 0; (next = strstr(next, "\r\n")); ++next, ++i)
+    {
+        next += 2;
+
+        if (strstr(next, "\r\n") == next)
+            break;
+
+        if (selfmalloc)
+        {
+            ++request_buf->header_count;
+
+            if (!request_buf->header_names)
+                request_buf->header_names = LibHTTPC_malloc(sizeof(char *));
+            else
+                request_buf->header_names = LibHTTPC_realloc(request_buf->header_names, sizeof(char *));
+
+            if (!request_buf->header_values)
+                request_buf->header_values = LibHTTPC_malloc(sizeof(char *));
+            else
+                request_buf->header_values = LibHTTPC_realloc(request_buf->header_values, sizeof(char *));
+        }
+        if (i < request_buf->header_count)
+        {
+            request_buf->header_names[i] = next;
+            next = strchr(next, ':');
+            *next++ = '\0';
+            request_buf->header_values[i] = next;
+        }
+    }
+    request_buf->body = next + 2;
+
+    return request_buf;
+}
+
+void LibHTTPC_Request_(struct LibHTTPC_Request *request)
+{
+    if (request->header_selfalloc)
+    {
+        free(request->header_names);
+        free(request->header_values);
+    }
+    if (request->selfalloc)
+        free(request);
+}