diff options
| author | Nakidai <nakidai@disroot.org> | 2024-12-15 00:29:28 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2024-12-15 00:29:28 +0300 |
| commit | f611496625b0b240a8b849afabcfef9d373fa868 (patch) | |
| tree | 2430ffe07a6368efc840c928d2f9bb02165ca183 | |
| parent | 93c754359d9a52a1e848f1c2ad9603a0c04bf1d7 (diff) | |
| download | libhttpc-f611496625b0b240a8b849afabcfef9d373fa868.tar.gz libhttpc-f611496625b0b240a8b849afabcfef9d373fa868.zip | |
Make LibHTTPC_dumpResponse accept NULL in buf
When LibHTTPC_dumpResponse accepts NULL it will allocate buffer itself using LibHTTPC_malloc
| -rw-r--r-- | TODO.7 | 10 | ||||
| -rw-r--r-- | src/response.c | 30 |
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 |