X-Git-Url: https://unix4lyfe.org/gitweb/darkstat/blobdiff_plain/7072208f533486bed0c0491bb7cb65f0c1ae9e44..fcc8b8300126636099f90f76a9b48f148fc1e90a:/http.c diff --git a/http.c b/http.c index 9796552..91db646 100644 --- a/http.c +++ b/http.c @@ -10,11 +10,13 @@ #include "darkstat.h" #include "http.h" +#include "config.h" #include "conv.h" #include "hosts_db.h" #include "graph_db.h" #include "err.h" #include "queue.h" +#include "str.h" #include "now.h" #include @@ -38,15 +40,16 @@ static const char mime_type_xml[] = "text/xml"; static const char mime_type_html[] = "text/html; charset=us-ascii"; static const char mime_type_css[] = "text/css"; static const char mime_type_js[] = "text/javascript"; -static const char encoding_gzip[] = - "Vary: Accept-Encoding\r\n" - "Content-Encoding: gzip\r\n"; +static const char encoding_identity[] = "identity"; +static const char encoding_gzip[] = "gzip"; static const char server[] = PACKAGE_NAME "/" PACKAGE_VERSION; static int idletime = 60; -static int sockin = -1; /* socket to accept connections from */ #define MAX_REQUEST_LENGTH 4000 +static int *insocks = NULL; +static unsigned int insock_num = 0; + #ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif @@ -88,6 +91,13 @@ struct connection { static LIST_HEAD(conn_list_head, connection) connlist = LIST_HEAD_INITIALIZER(conn_list_head); +struct bindaddr_entry { + STAILQ_ENTRY(bindaddr_entry) entries; + const char *s; +}; +static STAILQ_HEAD(bindaddrs_head, bindaddr_entry) bindaddrs = + STAILQ_HEAD_INITIALIZER(bindaddrs); + /* --------------------------------------------------------------------------- * Decode URL by converting %XX (where XX are hexadecimal digits) to the * character it represents. Don't forget to free the return value. @@ -269,7 +279,7 @@ static struct connection *new_connection(void) conn->query = NULL; conn->header = NULL; conn->mime_type = NULL; - conn->encoding = ""; + conn->encoding = NULL; conn->header_extra = ""; conn->header_length = 0; conn->header_sent = 0; @@ -295,7 +305,7 @@ static struct connection *new_connection(void) /* --------------------------------------------------------------------------- * Accept a connection from sockin and add it to the connection queue. */ -static void accept_connection(void) +static void accept_connection(const int sockin) { struct sockaddr_storage addrin; socklen_t sin_size; @@ -304,7 +314,7 @@ static void accept_connection(void) int sock; sin_size = (socklen_t)sizeof(addrin); - sock = accept(sockin, &addrin, &sin_size); + sock = accept(sockin, (struct sockaddr *)&addrin, &sin_size); if (sock == -1) { if (errno == ECONNABORTED || errno == EINTR) @@ -363,6 +373,37 @@ static char *rfc1123_date(char *dest, const time_t when) return (dest); } +static void generate_header(struct connection *conn, + const int code, const char *text) +{ + char date[DATE_LEN]; + + assert(conn->header == NULL); + assert(conn->mime_type != NULL); + if (conn->encoding == NULL) + conn->encoding = encoding_identity; + + verbosef("http: %d %s (%s: %d bytes)", code, text, + conn->encoding, conn->reply_length); + conn->header_length = xasprintf(&(conn->header), + "HTTP/1.1 %d %s\r\n" + "Date: %s\r\n" + "Server: %s\r\n" + "Vary: Accept-Encoding\r\n" + "Content-Type: %s\r\n" + "Content-Length: %d\r\n" + "Content-Encoding: %s\r\n" + "X-Robots-Tag: noindex, noarchive\r\n" + "%s" + "\r\n" + , + code, text, + rfc1123_date(date, now), server, + conn->mime_type, conn->reply_length, conn->encoding, + conn->header_extra); + conn->http_code = code; +} + /* --------------------------------------------------------------------------- @@ -371,36 +412,28 @@ static char *rfc1123_date(char *dest, const time_t when) static void default_reply(struct connection *conn, const int errcode, const char *errname, const char *format, ...) { - char *reason, date[DATE_LEN]; + char *reason; va_list va; va_start(va, format); xvasprintf(&reason, format, va); va_end(va); - /* Only really need to calculate the date once. */ - (void)rfc1123_date(date, now); - conn->reply_length = xasprintf(&(conn->reply), "%d %s\n" "

%s

\n" /* errname */ "%s\n" /* reason */ "
\n" - "Generated by %s on %s\n" + "Generated by %s" "\n", - errcode, errname, errname, reason, server, date); + errcode, errname, errname, reason, server); free(reason); - conn->header_length = xasprintf(&(conn->header), - "HTTP/1.1 %d %s\r\n" - "Date: %s\r\n" - "Server: %s\r\n" - "Content-Length: %d\r\n" - "Content-Type: text/html\r\n" - "\r\n", - errcode, errname, date, server, conn->reply_length); - - conn->http_code = errcode; + /* forget any dangling metadata */ + conn->mime_type = mime_type_html; + conn->encoding = encoding_identity; + + generate_header(conn, errcode, errname); } @@ -576,7 +609,6 @@ process_gzip(struct connection *conn) static void process_get(struct connection *conn) { char *decoded_url, *safe_url; - char date[DATE_LEN]; verbosef("http: %s \"%s\" %s", conn->method, conn->uri, (conn->query == NULL)?"":conn->query); @@ -605,6 +637,7 @@ static void process_get(struct connection *conn) if (buf == NULL) { default_reply(conn, 404, "Not Found", "The page you requested could not be found."); + free(safe_url); return; } str_extract(buf, &(conn->reply_length), &(conn->reply)); @@ -624,26 +657,14 @@ static void process_get(struct connection *conn) else { default_reply(conn, 404, "Not Found", "The page you requested could not be found."); + free(safe_url); return; } free(safe_url); process_gzip(conn); assert(conn->mime_type != NULL); - conn->header_length = xasprintf(&(conn->header), - "HTTP/1.1 200 OK\r\n" - "Date: %s\r\n" - "Server: %s\r\n" - "Content-Length: %d\r\n" - "Content-Type: %s\r\n" - "%s" - "%s" - "\r\n" - , - rfc1123_date(date, now), server, - conn->reply_length, conn->mime_type, conn->encoding, - conn->header_extra); - conn->http_code = 200; + generate_header(conn, 200, "OK"); } @@ -748,13 +769,11 @@ static void poll_send_header_and_reply(struct connection *conn) iov[0].iov_base = conn->header; iov[0].iov_len = conn->header_length; - iov[1].iov_base = conn->reply + conn->reply_sent; - iov[1].iov_len = conn->reply_length - conn->reply_sent; + iov[1].iov_base = conn->reply; + iov[1].iov_len = conn->reply_length; sent = writev(conn->socket, iov, 2); conn->last_active = now; - verbosef("poll_send_header_and_reply(%d) sent %d bytes", - conn->socket, (int)sent); /* handle any errors (-1) or closure (0) in send() */ if (sent < 1) { @@ -776,7 +795,7 @@ static void poll_send_header_and_reply(struct connection *conn) conn->header_sent = conn->header_length; sent -= conn->header_length; - if (conn->reply_sent + sent < conn->reply_length) { + if (sent < (ssize_t)conn->reply_length) { verbosef("partially sent reply"); conn->reply_sent += sent; conn->state = SEND_REPLY; @@ -856,72 +875,109 @@ static void poll_send_reply(struct connection *conn) if (conn->reply_sent == conn->reply_length) conn->state = DONE; } - - -/* --------------------------------------------------------------------------- - * Initialize the sockin global. This is the socket that we accept - * connections from. Pass -1 as max_conn for system limit. +/* Use getaddrinfo to figure out what type of socket to create and + * what to bind it to. "bindaddr" can be NULL. Remember to freeaddrinfo() + * the result. */ -void http_init(const char *bindaddr, const unsigned short bindport, - const int max_conn) +static struct addrinfo *get_bind_addr( + const char *bindaddr, const unsigned short bindport) { - struct sockaddr_storage addrin; - struct addrinfo hints, *ai, *aiptr; - char ipaddr[INET6_ADDRSTRLEN], portstr[12]; - int sockopt, ret; + struct addrinfo hints, *ai; + char portstr[6]; + int ret; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; +#ifdef linux + if (bindaddr == NULL) + hints.ai_family = AF_INET6; /* dual stack socket */ +#endif hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; #ifdef AI_ADDRCONFIG hints.ai_flags |= AI_ADDRCONFIG; #endif snprintf(portstr, sizeof(portstr), "%u", bindport); + if ((ret = getaddrinfo(bindaddr, portstr, &hints, &ai))) + err(1, "getaddrinfo(%s,%s) failed: %s", + bindaddr ? bindaddr : "NULL", portstr, gai_strerror(ret)); + if (ai == NULL) + err(1, "getaddrinfo() returned NULL pointer"); + return ai; +} - if ((ret = getaddrinfo(bindaddr, portstr, &hints, &aiptr))) - err(1, "getaddrinfo(): %s", gai_strerror(ret)); - - for (ai = aiptr; ai; ai = ai->ai_next) { - /* create incoming socket */ - sockin = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (sockin == -1) - continue; - - /* reuse address */ - sockopt = 1; - if (setsockopt(sockin, SOL_SOCKET, SO_REUSEADDR, - &sockopt, sizeof(sockopt)) == -1) { - close(sockin); - continue; - } +void http_add_bindaddr(const char *bindaddr) +{ + struct bindaddr_entry *ent; - /* Recover address and port strings. */ - getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, sizeof(ipaddr), - NULL, 0, NI_NUMERICHOST); + ent = xmalloc(sizeof(*ent)); + ent->s = bindaddr; + STAILQ_INSERT_TAIL(&bindaddrs, ent, entries); +} - /* bind socket */ - memcpy(&addrin, ai->ai_addr, ai->ai_addrlen); - if (bind(sockin, (struct sockaddr *)&addrin, ai->ai_addrlen) == -1) { - close(sockin); - continue; - } +static void http_listen_one(struct addrinfo *ai, + const unsigned short bindport) +{ + char ipaddr[INET6_ADDRSTRLEN]; + int sockin, sockopt, ret; + + /* create incoming socket */ + if ((sockin = socket(ai->ai_family, SOCK_STREAM, 0)) == -1) + err(1, "socket() failed"); + + /* reuse address */ + sockopt = 1; + if (setsockopt(sockin, SOL_SOCKET, SO_REUSEADDR, + &sockopt, sizeof(sockopt)) == -1) + err(1, "can't set SO_REUSEADDR"); + + /* format address into ipaddr string */ + if ((ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, + sizeof(ipaddr), NULL, 0, NI_NUMERICHOST)) != 0) + err(1, "getnameinfo failed: %s", gai_strerror(ret)); + + /* bind socket */ + if (bind(sockin, ai->ai_addr, ai->ai_addrlen) == -1) + err(1, "bind(\"%s\") failed", ipaddr); + + /* listen on socket */ + if (listen(sockin, -1) == -1) + err(1, "listen() failed"); + + verbosef("listening on http://%s%s%s:%u/", + (ai->ai_family == AF_INET6) ? "[" : "", + ipaddr, + (ai->ai_family == AF_INET6) ? "]" : "", + bindport); + + /* add to insocks */ + insocks = xrealloc(insocks, sizeof(*insocks) * (insock_num + 1)); + insocks[insock_num++] = sockin; +} - verbosef("listening on %s:%u", ipaddr, bindport); +/* Initialize the http sockets and listen on them. */ +void http_listen(const unsigned short bindport) +{ + /* If the user didn't specify any bind addresses, add a NULL. + * This will become a wildcard. + */ + if (STAILQ_EMPTY(&bindaddrs)) + http_add_bindaddr(NULL); - /* listen on socket */ - if (listen(sockin, max_conn) >= 0) - /* Successfully bound and now listening. */ - break; + /* Listen on every specified interface. */ + while (!STAILQ_EMPTY(&bindaddrs)) { + struct bindaddr_entry *bindaddr = STAILQ_FIRST(&bindaddrs); + struct addrinfo *ai, *ais = get_bind_addr(bindaddr->s, bindport); - /* Next candidate. */ - continue; - } + /* There could be multiple addresses returned, handle them all. */ + for (ai = ais; ai; ai = ai->ai_next) + http_listen_one(ai, bindport); - freeaddrinfo(aiptr); + freeaddrinfo(ais); - if (ai == NULL) - err(1, "getaddrinfo() unable to locate address"); + STAILQ_REMOVE_HEAD(&bindaddrs, entries); + free(bindaddr); + } /* ignore SIGPIPE */ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) @@ -939,24 +995,31 @@ http_fd_set(fd_set *recv_set, fd_set *send_set, int *max_fd, { struct connection *conn, *next; int minidle = idletime + 1; + unsigned int i; #define MAX_FD_SET(sock, fdset) do { \ FD_SET(sock, fdset); *max_fd = max(*max_fd, sock); } while(0) - MAX_FD_SET(sockin, recv_set); + for (i=0; ilast_active; /* Time out dead connections. */ - if (idlefor >= idletime) - { + if (idlefor >= idletime) { char ipaddr[INET6_ADDRSTRLEN]; - getnameinfo((struct sockaddr *) &conn->client, sizeof(conn->client), - ipaddr, sizeof(ipaddr), NULL, 0, NI_NUMERICHOST); - verbosef("http socket timeout from %s (fd %d)", - ipaddr, conn->socket); + /* FIXME: this is too late on FreeBSD, socket is invalid */ + int ret = getnameinfo((struct sockaddr *)&conn->client, + sizeof(conn->client), ipaddr, sizeof(ipaddr), + NULL, 0, NI_NUMERICHOST); + if (ret == 0) + verbosef("http socket timeout from %s (fd %d)", + ipaddr, conn->socket); + else + warn("http socket timeout: getnameinfo error: %s", + gai_strerror(ret)); conn->state = DONE; } @@ -1004,8 +1067,11 @@ http_fd_set(fd_set *recv_set, fd_set *send_set, int *max_fd, void http_poll(fd_set *recv_set, fd_set *send_set) { struct connection *conn; + unsigned int i; - if (FD_ISSET(sockin, recv_set)) accept_connection(); + for (i=0; istate) @@ -1031,4 +1097,22 @@ void http_poll(fd_set *recv_set, fd_set *send_set) } } +void http_stop(void) { + struct connection *conn; + unsigned int i; + + /* Close listening sockets. */ + for (i=0; i