summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--TODO.710
-rw-r--r--src/response.c30
2 files changed, 29 insertions, 11 deletions
diff --git a/TODO.7 b/TODO.7
index 12a0adb..80eb496 100644
--- a/TODO.7
+++ b/TODO.7
@@ -18,16 +18,6 @@ parse requests
 from
 .Xr socket 2
 directly.
-.It
-Make
-.Fn LibHTTPC_dumpResponse
-accept
-.Dv NULL
-in
-.Fa buf .
-In this way
-function should
-allocate buffer itself.
 .El
 .
 .Sh AUTHORS
diff --git a/src/response.c b/src/response.c
index b930fc6..d2442ff 100644
--- a/src/response.c
+++ b/src/response.c
@@ -43,11 +43,39 @@ char *LibHTTPC_dumpResponse(struct LibHTTPC_Response *response, char *buf, size_
 
     snprintf(status, sizeof(status), "%d", response->status);
 
+    if (!buf)
+    {
+        if (!LibHTTPC_malloc)
+            return NULL;
+
+        size_t buf_len = (
+            1
+            + strlen(response->version) + 1
+            + strlen(status) + 1
+            + strlen(response->phrase) + 2
+            + 2
+        );
+        for (size_t i = 0; i < response->header_count; ++i)
+            buf_len += (
+                strlen(response->headers[i].name)
+                + 2
+                + strlen(response->headers[i].value)
+                + 2
+            );
+        if (response->body)
+            buf_len += strlen(response->body);
+        buf = malloc(buf_len);
+        if (!buf)
+            return NULL;
+
+        *buf = '\0';
+    }
+
 #define append(X) strncat(buf, (X), buf_len - strlen(buf))
     WRITE();
 #undef append
 
-    return NULL;
+    return buf;
 }
 
 #ifdef LibHTTPC_SOCK