2013-05-01 00:35:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Joris Vink <joris@coders.se>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-01 20:10:45 +02:00
|
|
|
#include <time.h>
|
2013-05-01 12:23:21 +02:00
|
|
|
#include <zlib.h>
|
2013-05-01 00:35:33 +02:00
|
|
|
|
|
|
|
#include "spdy.h"
|
|
|
|
#include "kore.h"
|
|
|
|
#include "http.h"
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
TAILQ_HEAD(, http_request) http_requests;
|
|
|
|
|
2013-05-01 12:23:21 +02:00
|
|
|
static int http_generic_404(struct http_request *);
|
2013-05-01 08:09:04 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
http_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&http_requests);
|
|
|
|
}
|
|
|
|
|
2013-05-01 00:35:33 +02:00
|
|
|
int
|
2013-05-01 21:16:09 +02:00
|
|
|
http_request_new(struct connection *c, struct spdy_stream *s, char *host,
|
2013-05-02 03:51:04 +02:00
|
|
|
char *method, char *path, struct http_request **out)
|
2013-05-01 00:35:33 +02:00
|
|
|
{
|
2013-05-01 08:09:04 +02:00
|
|
|
struct http_request *req;
|
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
kore_log("http_request_new(%p, %p, %s, %s, %s)", c, s,
|
2013-05-01 00:35:33 +02:00
|
|
|
host, method, path);
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
req = (struct http_request *)kore_malloc(sizeof(*req));
|
2013-05-02 14:47:02 +02:00
|
|
|
req->flags = 0;
|
2013-05-01 08:09:04 +02:00
|
|
|
req->owner = c;
|
|
|
|
req->stream = s;
|
2013-05-02 14:47:02 +02:00
|
|
|
req->post_data = NULL;
|
2013-05-01 08:09:04 +02:00
|
|
|
req->host = kore_strdup(host);
|
|
|
|
req->path = kore_strdup(path);
|
2013-05-02 03:51:04 +02:00
|
|
|
TAILQ_INIT(&(req->resp_headers));
|
|
|
|
TAILQ_INIT(&(req->req_headers));
|
2013-05-02 14:47:02 +02:00
|
|
|
|
|
|
|
if (!strcasecmp(method, "get")) {
|
|
|
|
req->method = HTTP_METHOD_GET;
|
|
|
|
req->flags |= HTTP_REQUEST_COMPLETE;
|
|
|
|
} else if (!strcasecmp(method, "post")) {
|
|
|
|
req->method = HTTP_METHOD_POST;
|
|
|
|
} else {
|
|
|
|
kore_log("invalid method specified in request: %s", method);
|
|
|
|
http_request_free(req);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
TAILQ_INSERT_TAIL(&http_requests, req, list);
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
if (out != NULL)
|
|
|
|
*out = req;
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
void
|
|
|
|
http_response_header_add(struct http_request *req, char *header, char *value)
|
|
|
|
{
|
|
|
|
struct http_header *hdr;
|
|
|
|
|
|
|
|
kore_log("http_response_header_add(%p, %s, %s)", req, header, value);
|
|
|
|
|
|
|
|
hdr = (struct http_header *)kore_malloc(sizeof(*hdr));
|
|
|
|
hdr->header = kore_strdup(header);
|
|
|
|
hdr->value = kore_strdup(value);
|
2013-05-02 03:51:04 +02:00
|
|
|
TAILQ_INSERT_TAIL(&(req->resp_headers), hdr, list);
|
2013-05-01 21:16:09 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
void
|
|
|
|
http_request_free(struct http_request *req)
|
|
|
|
{
|
2013-05-01 21:16:09 +02:00
|
|
|
struct http_header *hdr, *next;
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
for (hdr = TAILQ_FIRST(&(req->resp_headers)); hdr != NULL; hdr = next) {
|
2013-05-01 21:16:09 +02:00
|
|
|
next = TAILQ_NEXT(hdr, list);
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
TAILQ_REMOVE(&(req->resp_headers), hdr, list);
|
|
|
|
free(hdr->header);
|
|
|
|
free(hdr->value);
|
|
|
|
free(hdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hdr = TAILQ_FIRST(&(req->req_headers)); hdr != NULL; hdr = next) {
|
|
|
|
next = TAILQ_NEXT(hdr, list);
|
|
|
|
|
|
|
|
TAILQ_REMOVE(&(req->req_headers), hdr, list);
|
2013-05-01 21:16:09 +02:00
|
|
|
free(hdr->header);
|
|
|
|
free(hdr->value);
|
|
|
|
free(hdr);
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
free(req->path);
|
|
|
|
free(req->host);
|
|
|
|
free(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-05-01 21:16:09 +02:00
|
|
|
http_response(struct http_request *req, int status, u_int8_t *d, u_int32_t len)
|
2013-05-01 08:09:04 +02:00
|
|
|
{
|
|
|
|
u_int32_t hlen;
|
2013-05-01 21:16:09 +02:00
|
|
|
struct http_header *hdr;
|
2013-05-02 03:51:04 +02:00
|
|
|
struct kore_buf *buf;
|
2013-05-01 08:09:04 +02:00
|
|
|
u_int8_t *htext;
|
|
|
|
struct spdy_header_block *hblock;
|
2013-05-02 03:51:04 +02:00
|
|
|
char sbuf[64];
|
2013-05-01 08:09:04 +02:00
|
|
|
|
|
|
|
kore_log("http_response(%p, %d, %p, %d)", req, status, d, len);
|
|
|
|
|
2013-05-01 13:43:47 +02:00
|
|
|
if (req->owner->proto == CONN_PROTO_SPDY) {
|
2013-05-01 12:23:21 +02:00
|
|
|
snprintf(sbuf, sizeof(sbuf), "%d", status);
|
2013-05-01 21:16:09 +02:00
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
hblock = spdy_header_block_create(SPDY_HBLOCK_NORMAL);
|
2013-05-01 12:23:21 +02:00
|
|
|
spdy_header_block_add(hblock, ":status", sbuf);
|
2013-05-01 08:09:04 +02:00
|
|
|
spdy_header_block_add(hblock, ":version", "HTTP/1.1");
|
2013-05-02 03:51:04 +02:00
|
|
|
TAILQ_FOREACH(hdr, &(req->resp_headers), list)
|
2013-05-01 21:16:09 +02:00
|
|
|
spdy_header_block_add(hblock, hdr->header, hdr->value);
|
|
|
|
|
2013-05-01 12:23:21 +02:00
|
|
|
htext = spdy_header_block_release(req->owner, hblock, &hlen);
|
|
|
|
if (htext == NULL)
|
2013-05-01 08:09:04 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2013-05-02 14:47:02 +02:00
|
|
|
spdy_frame_send(req->owner, SPDY_CTRL_FRAME_SYN_REPLY,
|
2013-05-02 17:30:06 +02:00
|
|
|
0, hlen, req->stream, 0);
|
2013-05-02 14:47:02 +02:00
|
|
|
net_send_queue(req->owner, htext, hlen, 0, NULL, NULL);
|
2013-05-02 03:51:04 +02:00
|
|
|
free(htext);
|
2013-05-02 14:47:02 +02:00
|
|
|
|
2013-05-01 12:23:21 +02:00
|
|
|
if (len > 0) {
|
2013-05-02 14:47:02 +02:00
|
|
|
spdy_frame_send(req->owner, SPDY_DATA_FRAME,
|
2013-05-02 17:30:06 +02:00
|
|
|
0, len, req->stream, 0);
|
2013-05-02 14:47:02 +02:00
|
|
|
net_send_queue(req->owner, d, len, 0, NULL, NULL);
|
2013-05-01 12:23:21 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:47:02 +02:00
|
|
|
spdy_frame_send(req->owner, SPDY_DATA_FRAME,
|
2013-05-02 17:30:06 +02:00
|
|
|
FLAG_FIN, 0, req->stream, 0);
|
2013-05-01 08:09:04 +02:00
|
|
|
} else {
|
2013-05-02 03:51:04 +02:00
|
|
|
buf = kore_buf_create(KORE_BUF_INITIAL);
|
|
|
|
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "HTTP/1.1 %d\r\n", status);
|
|
|
|
kore_buf_append(buf, (u_int8_t *)sbuf, strlen(sbuf));
|
|
|
|
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "Content-length: %d\r\n", len);
|
|
|
|
kore_buf_append(buf, (u_int8_t *)sbuf, strlen(sbuf));
|
|
|
|
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "Connection: close\r\n");
|
|
|
|
kore_buf_append(buf, (u_int8_t *)sbuf, strlen(sbuf));
|
|
|
|
|
|
|
|
TAILQ_FOREACH(hdr, &(req->resp_headers), list) {
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "%s: %s\r\n",
|
|
|
|
hdr->header, hdr->value);
|
|
|
|
kore_buf_append(buf, (u_int8_t *)sbuf, strlen(sbuf));
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:25:14 +02:00
|
|
|
kore_buf_append(buf, (u_int8_t *)"\r\n", 2);
|
2013-05-02 03:51:04 +02:00
|
|
|
htext = kore_buf_release(buf, &hlen);
|
2013-05-02 14:47:02 +02:00
|
|
|
net_send_queue(req->owner, htext, hlen, 0, NULL, NULL);
|
2013-05-02 03:51:04 +02:00
|
|
|
free(htext);
|
2013-05-02 14:47:02 +02:00
|
|
|
|
|
|
|
net_send_queue(req->owner, d, len, 0, NULL, NULL);
|
2013-05-01 08:09:04 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 00:35:33 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-05-01 08:09:04 +02:00
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
int
|
|
|
|
http_request_header_get(struct http_request *req, char *header, char **out)
|
|
|
|
{
|
2013-05-02 03:51:04 +02:00
|
|
|
int r;
|
|
|
|
struct http_header *hdr;
|
2013-05-01 21:16:09 +02:00
|
|
|
|
|
|
|
if (req->owner->proto == CONN_PROTO_SPDY) {
|
|
|
|
r = spdy_stream_get_header(req->stream->hblock, header, out);
|
|
|
|
} else {
|
2013-05-02 03:51:04 +02:00
|
|
|
TAILQ_FOREACH(hdr, &(req->req_headers), list) {
|
|
|
|
if (!strcasecmp(hdr->header, header)) {
|
|
|
|
r = strlen(hdr->value) + 1;
|
|
|
|
*out = (char *)kore_malloc(r);
|
|
|
|
kore_strlcpy(*out, hdr->value, r);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
r = KORE_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:09:04 +02:00
|
|
|
void
|
|
|
|
http_process(void)
|
|
|
|
{
|
2013-05-01 16:03:48 +02:00
|
|
|
struct http_request *req, *next;
|
2013-05-02 14:47:02 +02:00
|
|
|
int r, (*hdlr)(struct http_request *);
|
2013-05-01 08:09:04 +02:00
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&http_requests))
|
|
|
|
return;
|
|
|
|
|
|
|
|
kore_log("http_process()");
|
|
|
|
for (req = TAILQ_FIRST(&http_requests); req != NULL; req = next) {
|
|
|
|
next = TAILQ_NEXT(req, list);
|
2013-05-02 14:47:02 +02:00
|
|
|
if (!(req->flags & HTTP_REQUEST_COMPLETE))
|
|
|
|
continue;
|
2013-05-01 08:09:04 +02:00
|
|
|
|
2013-05-02 14:47:02 +02:00
|
|
|
hdlr = kore_module_handler_find(req->host, req->path);
|
|
|
|
if (hdlr == NULL)
|
2013-05-02 09:10:35 +02:00
|
|
|
r = http_generic_404(req);
|
|
|
|
else
|
2013-05-02 14:47:02 +02:00
|
|
|
r = hdlr(req);
|
2013-05-02 09:10:35 +02:00
|
|
|
|
|
|
|
if (r != KORE_RESULT_ERROR)
|
|
|
|
net_send_flush(req->owner);
|
|
|
|
else
|
|
|
|
kore_server_disconnect(req->owner);
|
2013-05-01 08:09:04 +02:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&http_requests, req, list);
|
|
|
|
http_request_free(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
int
|
|
|
|
http_header_recv(struct netbuf *nb)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
struct http_header *hdr;
|
|
|
|
struct http_request *req;
|
2013-05-02 22:35:29 +02:00
|
|
|
int h, i, v, skip;
|
2013-05-02 03:51:04 +02:00
|
|
|
char *request[4], *host[3], *hbuf;
|
|
|
|
char *headers[HTTP_REQ_HEADER_MAX];
|
|
|
|
struct connection *c = (struct connection *)nb->owner;
|
|
|
|
|
|
|
|
kore_log("http_header_recv(%p)", nb);
|
|
|
|
|
|
|
|
nb->buf[nb->len] = '\0';
|
|
|
|
if ((p = strrchr((char *)nb->buf, '\r')) == NULL)
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
if (nb->len > 2 && strncmp((p - 2), "\r\n\r\n", 4))
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
|
2013-05-02 09:10:35 +02:00
|
|
|
nb->flags |= NETBUF_FORCE_REMOVE;
|
2013-05-02 03:51:04 +02:00
|
|
|
hbuf = kore_strdup((const char *)nb->buf);
|
|
|
|
|
|
|
|
h = kore_split_string(hbuf, "\r\n", headers, HTTP_REQ_HEADER_MAX);
|
|
|
|
if (h < 2) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(headers[0]) > 3 && strncasecmp(headers[0], "get", 3)) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = kore_split_string(headers[0], " ", request, 4);
|
|
|
|
if (v != 3) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-05-02 22:35:29 +02:00
|
|
|
host[0] = NULL;
|
|
|
|
for (i = 0; i < h; i++) {
|
|
|
|
if (strncasecmp(headers[i], "host",
|
|
|
|
MIN(strlen(headers[i]), strlen("host"))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
v = kore_split_string(headers[i], ":", host, 3);
|
|
|
|
if (v != 2) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(host[0]) != 4 || strncasecmp(host[0], "host", 4) ||
|
|
|
|
strlen(host[1]) < 4) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
host[1]++;
|
|
|
|
skip = i;
|
|
|
|
break;
|
2013-05-02 03:51:04 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 22:35:29 +02:00
|
|
|
if (host[0] == NULL) {
|
2013-05-02 03:51:04 +02:00
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!http_request_new(c, NULL, host[1], request[0], request[1], &req)) {
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-05-02 22:35:29 +02:00
|
|
|
for (i = 1; i < h; i++) {
|
|
|
|
if (i == skip)
|
|
|
|
continue;
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
p = strchr(headers[i], ':');
|
|
|
|
if (p == NULL) {
|
|
|
|
kore_log("malformed header: '%s'", headers[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(p++) = '\0';
|
|
|
|
hdr = (struct http_header *)kore_malloc(sizeof(*hdr));
|
|
|
|
hdr->header = kore_strdup(headers[i]);
|
|
|
|
hdr->value = kore_strdup(p);
|
|
|
|
TAILQ_INSERT_TAIL(&(req->req_headers), hdr, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(hbuf);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-01 12:23:21 +02:00
|
|
|
static int
|
|
|
|
http_generic_404(struct http_request *req)
|
|
|
|
{
|
|
|
|
kore_log("http_generic_404(%s, %s, %s)",
|
|
|
|
req->host, req->method, req->path);
|
|
|
|
|
2013-05-01 21:16:09 +02:00
|
|
|
return (http_response(req, 404, NULL, 0));
|
2013-05-01 12:23:21 +02:00
|
|
|
}
|