about summary refs log tree commit diff
path: root/src/response.c
blob: 96e27cfbd3035af7baf56258b84444af057785d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "libhttpc.h"

#include <stddef.h>
#include <stdio.h>
#include <string.h>


char *LibHTTPC_dumpResponse(struct LibHTTPC_Response *response, char *buf, size_t buf_len)
{
    char status[10];

    if (!response->version)
        response->version = "HTTP/1.1";
    if (!response->status)
        response->status = LibHTTPC_Status_OK;
    if (!response->phrase)
        response->phrase = LibHTTPC_dumpStatus(response->status);

    snprintf(status, sizeof(status), "%d", response->status);

#define append(X) strncat(buf, (X), buf_len - strlen(buf))
    append(response->version);
    append("");
    append(status);
    append(" ");
    append(response->phrase);
    append("\r\n");
    for (size_t i = 0; i < response->header_count; ++i)
    {
        append(response->headers[i].name);
        append(": ");
        append(response->headers[i].value);
        append("\r\n");
    }
    append("\r\n");
    if (response->body)
        append(response->body);
#undef append

    return NULL;
}