forked from jeremycw/httpserver.h
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrespond.c
More file actions
256 lines (228 loc) · 9.86 KB
/
Copy pathrespond.c
File metadata and controls
256 lines (228 loc) · 9.86 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef HTTPSERVER_IMPL
#include "buffer_util.h"
#include "common.h"
#include "request_util.h"
#include "respond.h"
#endif
char const *hs_status_text[] = {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "",
// 100s
"Continue", "Switching Protocols", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "",
// 200s
"OK", "Created", "Accepted", "Non-Authoritative Information", "No Content",
"Reset Content", "Partial Content", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
// 300s
"Multiple Choices", "Moved Permanently", "Found", "See Other",
"Not Modified", "Use Proxy", "", "Temporary Redirect", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
// 400s
"Bad Request", "Unauthorized", "Payment Required", "Forbidden", "Not Found",
"Method Not Allowed", "Not Acceptable", "Proxy Authentication Required",
"Request Timeout", "Conflict",
"Gone", "Length Required", "", "Payload Too Large", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "",
// 500s
"Internal Server Error", "Not Implemented", "Bad Gateway",
"Service Unavailable", "Gateway Timeout", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", ""};
typedef struct {
char *buf;
int capacity;
int size;
int64_t *memused;
} grwprintf_t;
void _grwprintf_init(grwprintf_t *ctx, int capacity, int64_t *memused) {
ctx->memused = memused;
ctx->size = 0;
ctx->buf = (char *)malloc(capacity);
*ctx->memused += capacity;
assert(ctx->buf != NULL);
ctx->capacity = capacity;
}
void _grwmemcpy(grwprintf_t *ctx, char const *src, int size) {
if (ctx->size + size > ctx->capacity) {
*ctx->memused -= ctx->capacity;
ctx->capacity = ctx->size + size;
*ctx->memused += ctx->capacity;
ctx->buf = (char *)realloc(ctx->buf, ctx->capacity);
assert(ctx->buf != NULL);
}
memcpy(ctx->buf + ctx->size, src, size);
ctx->size += size;
}
void _grwprintf(grwprintf_t *ctx, char const *fmt, ...) {
va_list args;
va_start(args, fmt);
int bytes =
vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args);
if (bytes + ctx->size > ctx->capacity) {
*ctx->memused -= ctx->capacity;
while (bytes + ctx->size > ctx->capacity)
ctx->capacity *= 2;
*ctx->memused += ctx->capacity;
ctx->buf = (char *)realloc(ctx->buf, ctx->capacity);
assert(ctx->buf != NULL);
bytes +=
vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args);
}
ctx->size += bytes;
va_end(args);
}
void _http_serialize_headers_list(http_response_t *response,
grwprintf_t *printctx) {
http_header_t *header = response->headers;
while (header) {
_grwprintf(printctx, "%s: %s\r\n", header->key, header->value);
header = header->next;
}
_grwprintf(printctx, "\r\n");
}
void _http_serialize_headers(http_request_t *request, http_response_t *response,
grwprintf_t *printctx) {
if (HTTP_FLAG_CHECK(request->flags, HTTP_AUTOMATIC)) {
hs_request_detect_keep_alive_flag(request);
}
if (HTTP_FLAG_CHECK(request->flags, HTTP_KEEP_ALIVE)) {
hs_response_set_header(response, "Connection", "keep-alive");
} else {
hs_response_set_header(response, "Connection", "close");
}
_grwprintf(printctx, "HTTP/1.1 %d %s\r\nDate: %s\r\n", response->status,
hs_status_text[response->status], request->server->date);
if (!HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
_grwprintf(printctx, "Content-Length: %d\r\n", response->content_length);
}
_http_serialize_headers_list(response, printctx);
}
void _http_perform_response(http_request_t *request, http_response_t *response,
grwprintf_t *printctx, hs_req_fn_t http_write) {
http_header_t *header = response->headers;
while (header) {
http_header_t *tmp = header;
header = tmp->next;
free(tmp);
}
_hs_buffer_free(&request->buffer, &request->server->memused);
free(response);
request->buffer.buf = printctx->buf;
request->buffer.length = printctx->size;
request->buffer.capacity = printctx->capacity;
request->bytes_written = 0;
request->state = HTTP_SESSION_WRITE;
http_write(request);
}
// See api.h http_response_header
void hs_response_set_header(http_response_t *response, char const *key,
char const *value) {
http_header_t *header = (http_header_t *)malloc(sizeof(http_header_t));
assert(header != NULL);
header->key = key;
header->value = value;
http_header_t *prev = response->headers;
header->next = prev;
response->headers = header;
}
// Serializes the response into the request buffer and calls http_write.
// See api.h http_respond for more details
void hs_request_respond(http_request_t *request, http_response_t *response,
hs_req_fn_t http_write) {
grwprintf_t printctx;
_grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
_http_serialize_headers(request, response, &printctx);
if (response->body) {
_grwmemcpy(&printctx, response->body, response->content_length);
}
_http_perform_response(request, response, &printctx, http_write);
}
// Serializes a chunk into the request buffer and calls http_write.
// See api.h http_respond_chunk for more details.
void hs_request_respond_chunk(http_request_t *request,
http_response_t *response, hs_req_fn_t cb,
hs_req_fn_t http_write) {
grwprintf_t printctx;
_grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
if (!HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
HTTP_FLAG_SET(request->flags, HTTP_CHUNKED_RESPONSE);
hs_response_set_header(response, "Transfer-Encoding", "chunked");
_http_serialize_headers(request, response, &printctx);
}
request->chunk_cb = cb;
_grwprintf(&printctx, "%X\r\n", response->content_length);
_grwmemcpy(&printctx, response->body, response->content_length);
_grwprintf(&printctx, "\r\n");
_http_perform_response(request, response, &printctx, http_write);
}
// Serializes the zero sized final chunk into the request buffer and calls
// http_write. See api.h http_respond_chunk_end for more details.
void hs_request_respond_chunk_end(http_request_t *request,
http_response_t *response,
hs_req_fn_t http_write) {
grwprintf_t printctx;
_grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
_grwprintf(&printctx, "0\r\n");
_http_serialize_headers_list(response, &printctx);
_grwprintf(&printctx, "\r\n");
HTTP_FLAG_CLEAR(request->flags, HTTP_CHUNKED_RESPONSE);
_http_perform_response(request, response, &printctx, http_write);
}
// See api.h http_response_status
void hs_response_set_status(http_response_t *response, int status) {
response->status = status > 599 || status < 100 ? 500 : status;
}
// See api.h http_response_body
void hs_response_set_body(http_response_t *response, char const *body,
int length) {
response->body = body;
response->content_length = length;
}
// See api.h http_response_init
http_response_t *hs_response_init() {
http_response_t *response =
(http_response_t *)calloc(1, sizeof(http_response_t));
assert(response != NULL);
response->status = 200;
return response;
}
// Simple less flexible interface for responses, used for errors.
void hs_request_respond_error(http_request_t *request, int code,
char const *message, hs_req_fn_t http_write) {
struct http_response_s *response = hs_response_init();
hs_response_set_status(response, code);
hs_response_set_header(response, "Content-Type", "text/plain");
hs_response_set_body(response, message, strlen(message));
hs_request_respond(request, response, http_write);
http_write(request);
}