2 * This is free and unencumbered software released into the public domain.
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
46 #define nitems(x) (sizeof(x)/sizeof(x[0]))
49 #define HTTP_MAX_UPLOAD 4096
52 http_init(struct client *clt, int fd)
54 memset(clt, 0, sizeof(*clt));
55 if (bufio_init(&clt->bio) == -1)
57 bufio_set_fd(&clt->bio, fd);
62 http_parse(struct client *clt)
64 struct buffer *rbuf = &clt->bio.rbuf;
65 struct request *req = &clt->req;
68 char *frag, *query, *http, *line;
69 const char *errstr, *m;
71 while (!clt->reqdone) {
72 endln = memmem(rbuf->buf, rbuf->len, "\r\n", 2);
79 if (endln == rbuf->buf)
82 len = endln - rbuf->buf + 2;
83 while (len > 0 && (line[len - 1] == '\r' ||
84 line[len - 1] == '\n' || line[len - 1] == ' ' ||
85 line[len - 1] == '\t'))
89 if (clt->req.method == METHOD_UNKNOWN) {
90 if (!strncmp("GET ", line, 4)) {
91 req->method = METHOD_GET;
93 } else if (!strncmp("POST ", line, 5)) {
94 req->method = METHOD_POST;
101 if ((http = strchr(line, ' ')) == NULL)
106 if ((query = strchr(line, '?')))
108 if ((frag = strchr(line, '#')))
111 clt->req.path = xstrdup(line);
113 if (!strcmp(http, "HTTP/1.0"))
114 clt->req.version = HTTP_1_0;
115 else if (!strcmp(http, "HTTP/1.1")) {
116 clt->req.version = HTTP_1_1;
119 log_warnx("unknown http version %s", http);
124 line = http; /* so that no header below matches */
127 if (!strncasecmp(line, "Content-Length:", 15)) {
129 line += strspn(line, " \t");
130 clt->req.clen = strtonum(line, 0, HTTP_MAX_UPLOAD,
133 log_warnx("content-length is %s: %s",
140 if (!strncasecmp(line, "Connection:", 11)) {
142 line += strspn(line, " \t");
143 if (!strcasecmp(line, "upgrade"))
144 req->flags |= R_CONNUPGR;
147 if (!strncasecmp(line, "Upgrade:", 8)) {
149 line += strspn(line, " \t");
150 if (!strcasecmp(line, "websocket"))
151 req->flags |= R_UPGRADEWS;
154 if (!strncasecmp(line, "Sec-WebSocket-Version:", 22)) {
156 line += strspn(line, " \t");
157 if (strcmp(line, "13") != 0) {
158 log_warnx("unsupported websocket version %s",
163 req->flags |= R_WSVERSION;
166 if (!strncasecmp(line, "Sec-WebSocket-Key:", 18)) {
168 line += strspn(line, " \t");
169 req->secret = xstrdup(line);
172 buf_drain(rbuf, endln - rbuf->buf + 2);
175 if (req->method == METHOD_GET)
177 else if (req->method == METHOD_POST)
181 log_debug("< %s %s HTTP/%s", m, req->path,
182 req->version == HTTP_1_1 ? "1.1" : "1.0");
188 http_read(struct client *clt)
190 struct request *req = &clt->req;
191 struct buffer *rbuf = &clt->bio.rbuf;
194 /* clients may have sent more data than advertised */
195 if (req->clen < rbuf->len)
198 left = req->clen - rbuf->len;
205 buf_write(rbuf, "", 1); /* append a NUL byte */
206 while (rbuf->len > 0 && (rbuf->buf[rbuf->len - 1] == '\r' ||
207 (rbuf->buf[rbuf->len - 1] == '\n')))
208 rbuf->buf[--rbuf->len] = '\0';
214 http_postdata(struct client *clt, char **data, size_t *len)
217 *data = clt->bio.rbuf.buf;
219 *len = clt->bio.rbuf.len;
223 http_reply(struct client *clt, int code, const char *reason,
226 const char *version, *location = NULL;
229 log_debug("> %d %s", code, reason);
232 if (ws_accept_hdr(clt->req.secret, b32, sizeof(b32)) == -1) {
236 free(clt->req.secret);
237 clt->req.secret = NULL;
242 if (code >= 300 && code < 400) {
244 ctype = "text/html;charset=UTF-8";
247 version = "HTTP/1.1";
248 if (clt->req.version == HTTP_1_0)
249 version = "HTTP/1.0";
251 if (http_fmt(clt, "%s %d %s\r\n"
252 "Connection: close\r\n"
253 "Cache-Control: no-store\r\n",
254 version, code, reason) == -1)
257 http_fmt(clt, "Content-Type: %s\r\n", ctype) == -1)
259 if (location != NULL &&
260 http_fmt(clt, "Location: %s\r\n", location) == -1)
263 http_writes(clt, "Transfer-Encoding: chunked\r\n") == -1)
266 if (http_fmt(clt, "Upgrade: websocket\r\n"
267 "Connection: Upgrade\r\n"
268 "Sec-WebSocket-Accept: %s\r\n", b32) == -1)
271 if (http_write(clt, "\r\n", 2) == -1)
274 bufio_set_chunked(&clt->bio, clt->chunked);
277 if (http_writes(clt, "<a href='") == -1 ||
278 http_htmlescape(clt, location) == -1 ||
279 http_writes(clt, "'>") == -1 ||
280 http_htmlescape(clt, reason) == -1 ||
281 http_writes(clt, "</a>") == -1)
293 http_flush(struct client *clt)
301 if (bufio_compose(&clt->bio, clt->buf, clt->len) == -1) {
312 http_write(struct client *clt, const char *d, size_t len)
319 if (!clt->bio.chunked) {
320 if (bufio_compose(&clt->bio, d, len) == -1) {
327 if (clt->buf == NULL) {
329 if ((clt->buf = malloc(clt->cap)) == NULL) {
336 avail = clt->cap - clt->len;
340 memcpy(clt->buf + clt->len, d, avail);
344 if (clt->len == clt->cap) {
345 if (http_flush(clt) == -1)
354 http_writes(struct client *clt, const char *str)
356 return http_write(clt, str, strlen(str));
360 http_fmt(struct client *clt, const char *fmt, ...)
367 r = vasprintf(&str, fmt, ap);
371 log_warn("vasprintf");
376 r = http_write(clt, str, r);
382 http_urlescape(struct client *clt, const char *str)
387 for (; *str; ++str) {
388 if (iscntrl((unsigned char)*str) ||
389 isspace((unsigned char)*str) ||
390 *str == '\'' || *str == '"' || *str == '\\') {
391 r = snprintf(tmp, sizeof(tmp), "%%%2X",
392 (unsigned char)*str);
393 if (r < 0 || (size_t)r >= sizeof(tmp)) {
394 log_warn("snprintf failed");
398 if (http_write(clt, tmp, r) == -1)
400 } else if (http_write(clt, str, 1) == -1)
408 http_htmlescape(struct client *clt, const char *str)
412 for (; *str; ++str) {
415 r = http_writes(clt, "<");
418 r = http_writes(clt, ">");
421 r = http_writes(clt, ">");
424 r = http_writes(clt, """);
427 r = http_writes(clt, "'");
430 r = http_write(clt, str, 1);
442 http_close(struct client *clt)
446 if (clt->len != 0 && http_flush(clt) == -1)
448 if (bufio_compose(&clt->bio, NULL, 0) == -1)
450 return (clt->err ? -1 : 0);
454 http_free(struct client *clt)
458 free(clt->req.secret);
459 free(clt->req.ctype);
461 bufio_free(&clt->bio);