-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhandler.c
More file actions
144 lines (120 loc) · 5.05 KB
/
handler.c
File metadata and controls
144 lines (120 loc) · 5.05 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "run.h"
#include <strings.h> /* strcasecmp */
void lw_route(http_method_t method, const char *path, route_handler_t handler) {
if (lw_ctx.route_count >= MAX_ROUTES) {
fprintf(stderr, "[ERR] Maximum number of routes exceeded\n");
return;
}
route_t *route = &lw_ctx.routes[lw_ctx.route_count];
route->method = method;
strncpy(route->path, path, MAX_PATH_LENGTH - 1);
route->path[MAX_PATH_LENGTH - 1] = '\0';
route->handler = handler;
lw_ctx.route_count++;
LW_VERBOSE ? printf("[LW] Route registered: %s %s\n", method_to_string(method), path) : 0;
}
void lw_send_response(http_response_t *response, int client_socket, SSL *client_ssl, const char *accept_encoding) {
(LW_VERBOSE) ? printf("[COMP] LW_COMPRESS=%d Accept-Encoding=%s body=%zu\n",
LW_COMPRESS, accept_encoding ? accept_encoding : "NULL", response->body_length) : 1;
if (LW_COMPRESS &&
response->body &&
response->body_length > 0 &&
accept_encoding &&
strstr(accept_encoding, "zstd")) {
size_t bound = ZSTD_compressBound(response->body_length);
void *zbuf = malloc(bound);
if (!zbuf) goto no_compress;
size_t zlen = ZSTD_compress(zbuf, bound,
response->body,
response->body_length,
3); // level 1-22
if (ZSTD_isError(zlen)) {
free(zbuf);
goto no_compress;
}
char hdr[2048];
int hoff = snprintf(hdr, sizeof(hdr),
"HTTP/1.1 %d %s\r\n"
"Content-Encoding: zstd\r\n"
"Content-Length: %zu\r\n",
response->status_code,
response->status_code == 200 ? "OK" :
response->status_code == 404 ? "Not Found" :
response->status_code == 500 ? "Internal Server Error" :
"Unknown",
zlen);
for (int i = 0; i < response->header_count; ++i)
hoff += snprintf(hdr + hoff, sizeof(hdr) - hoff,
"%s\r\n", response->headers[i]);
hoff += snprintf(hdr + hoff, sizeof(hdr) - hoff, "\r\n");
if (LW_SSL_ENABLED && client_ssl) {
SSL_write(client_ssl, hdr, hoff);
SSL_write(client_ssl, zbuf, zlen);
} else {
write(client_socket, hdr, hoff);
write(client_socket, zbuf, zlen);
}
free(zbuf);
return;
}
no_compress:
char response_buffer[BUFFER_SIZE * 2];
int offset = 0;
offset += snprintf(response_buffer + offset,
sizeof(response_buffer) - offset,
"HTTP/1.1 %d %s\r\n",
response->status_code,
response->status_code == 200 ? "OK" :
response->status_code == 404 ? "Not Found" :
response->status_code == 500 ? "Internal Server Error" :
"Unknown");
// Headers
for (int i = 0; i < response->header_count; ++i)
offset += snprintf(response_buffer + offset,
sizeof(response_buffer) - offset,
"%s\r\n", response->headers[i]);
// Content-Length (uncompressed)
if (response->body && response->body_length > 0)
offset += snprintf(response_buffer + offset,
sizeof(response_buffer) - offset,
"Content-Length: %zu\r\n",
response->body_length);
offset += snprintf(response_buffer + offset,
sizeof(response_buffer) - offset,
"\r\n");
// Body
if (response->body && response->body_length > 0) {
memcpy(response_buffer + offset,
response->body,
response->body_length);
offset += response->body_length;
}
// Send
if (LW_SSL_ENABLED && client_ssl)
SSL_write(client_ssl, response_buffer, offset);
else
write(client_socket, response_buffer, offset);
}
void lw_set_header(http_response_t *response, const char *header) {
if (response->header_count >= MAX_HEADERS) {
fprintf(stderr, "[ERR] Maximum number of headers exceeded\n");
return;
}
response->headers[response->header_count] = malloc(strlen(header) + 1);
strcpy(response->headers[response->header_count], header);
response->header_count++;
}
void lw_set_body(http_response_t *response, const char *body) {
if (response->body)
free(response->body);
response->body_length = strlen(body);
response->body = malloc(response->body_length + 1);
strcpy(response->body, body);
}
void lw_set_body_bin(http_response_t *response, const char *body, size_t length) {
if (response->body)
free(response->body);
response->body_length = length;
response->body = malloc(length);
memcpy(response->body, body, length);
}