-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.c
75 lines (58 loc) · 1.66 KB
/
response.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "response.h"
struct lal_response *
lal_create_response(const char *status)
{
struct lal_response *resp = (struct lal_response *)malloc(
sizeof(struct lal_response));
int len = strlen(status);
resp->status = (char *)calloc(sizeof(char), len + 1);
memcpy(resp->status, status, len);
resp->headers = lal_new_entry();
resp->body = lal_new_body_part();
return resp;
}
char *
lal_serialize_response(struct lal_response *resp, long long *len)
{
char *r, *body_str, *header_str, content_length_str[100];
struct lal_entry *entry = resp->headers;
struct lal_body_part *header = lal_create_body_part("HTTP/1.1 ");
lal_append_to_body(header, resp->status);
lal_append_to_body(header, "\r\n");
body_str = lal_join_body(resp->body, NULL);
*len = strlen(body_str);
sprintf(content_length_str, "%lli", *len);
lal_append_to_entries(resp->headers, "Content-Length", content_length_str);
while (entry) {
lal_append_to_body(header, entry->key);
lal_append_to_body(header, ": ");
lal_append_to_body(header, entry->val);
lal_append_to_body(header, "\r\n");
entry = entry->next;
}
lal_append_to_body(header, "\r\n");
header_str = lal_join_body(header, NULL);
lal_destroy_body(header);
*len = strlen(header_str) + *len;
r = (char *)calloc(*len + 1, sizeof(char));
strcpy(r, header_str);
strcat(r, body_str);
free(header_str);
free(body_str);
return r;
}
void
lal_destroy_response(struct lal_response *resp)
{
struct lal_entry *prev, *entry = resp->headers;
while (entry) {
free((void *)entry->val);
free((void *)entry->key);
prev = entry;
entry = entry->next;
free(prev);
}
lal_destroy_body(resp->body);
free(resp->status);
free(resp);
}