-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.c
More file actions
100 lines (76 loc) · 2.77 KB
/
Copy pathresponse.c
File metadata and controls
100 lines (76 loc) · 2.77 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <sys/time.h>
#include "response.h"
#include "router.h"
#include "status_codes.h"
int init_http_response(struct http_response_t *response, struct http_request_t *request)
{
int err = 0;
memset(response, 0, sizeof(struct http_response_t));
response->protocol = HTTP_PROTOCOL_HTTP_1;
response->status_code = 200;
struct timeval tv;
gettimeofday(&tv, NULL);
response->timestamp = tv.tv_usec;
return err;
}
int add_http_response_header(struct http_response_t *response, char *key, char *value)
{
struct http_header_t *h = &response->headers[response->header_count];
memcpy(h->key, key, strlen(key));
memcpy(h->value, value, strlen(value));
response->header_count++;
return 0;
}
int set_http_response_body(struct http_response_t *response, char *body, long body_length)
{
response->body_length = body_length;
response->body = malloc(sizeof(char) * response->body_length);
memset(response->body, 0, sizeof(char) * response->body_length);
char body_length_str[32];
memset(body_length_str, 0, 32);
int_to_string(response->body_length, body_length_str, 32);
add_http_response_header(response, "Content-Length", body_length_str);
memcpy(response->body, body, response->body_length);
return 0;
}
int render_http_response(struct http_response_t *response, char **result, int *result_length)
{
int err = 0;
char *rendered = malloc(sizeof(char) * (MAX_HTTP_HEADER_COUNT * MAX_HTTP_HEADER_LENGTH + response->body_length));
*result_length = 0;
// Protocol
strcat(rendered, "HTTP/1.1 ");
// Status code
char status_code_str[3];
memset(status_code_str, 0, 3);
int_to_string(response->status_code, status_code_str, 3);
strcat(rendered, status_code_str);
strcat(rendered, " ");
// Description
strcat(rendered, get_status_code_description(response->status_code));
strcat(rendered, "\n");
printf("< %s %s %s\n", "HTTP/1.1", status_code_str, get_status_code_description(response->status_code));
for (int i = 0; i < response->header_count; ++i)
{
struct http_header_t *h = &response->headers[i];
strncat(rendered, h->key, strlen(h->key));
strncat(rendered, ": ", 2);
strncat(rendered, h->value, strlen(h->value));
strncat(rendered, "\r\n", 2);
printf("< %s=%s\n", h->key, h->value);
}
strncat(rendered, "\r\n", sizeof("\r\n"));
*result_length = strlen(rendered) + response->body_length;
memcpy(&rendered[strlen(rendered)], response->body, response->body_length);
if (response->body_length > 0)
{
printf("< Body:\n");
printf("%s\n", response->body);
printf("< BODY END\n");
}
*result = rendered;
return err;
}