http.c (darkstat-3.0.719) | : | http.c (darkstat-3.0.721) | ||
---|---|---|---|---|
/* darkstat 3 | /* darkstat 3 | |||
* copyright (c) 2001-2014 Emil Mikulic. | * copyright (c) 2001-2016 Emil Mikulic. | |||
* | * | |||
* http.c: embedded webserver. | * http.c: embedded webserver. | |||
* This borrows a lot of code from darkhttpd. | * This borrows a lot of code from darkhttpd. | |||
* | * | |||
* You may use, modify and redistribute this file under the terms of the | * You may use, modify and redistribute this file under the terms of the | |||
* GNU General Public License version 2. (see COPYING.GPL) | * GNU General Public License version 2. (see COPYING.GPL) | |||
*/ | */ | |||
#include "cdefs.h" | #include "cdefs.h" | |||
#include "config.h" | #include "config.h" | |||
skipping to change at line 45 | skipping to change at line 45 | |||
#include <string.h> | #include <string.h> | |||
#include <time.h> | #include <time.h> | |||
#include <unistd.h> | #include <unistd.h> | |||
#include <zlib.h> | #include <zlib.h> | |||
static char *http_base_url = NULL; | static char *http_base_url = NULL; | |||
static int http_base_len = 0; | static int http_base_len = 0; | |||
static const char mime_type_xml[] = "text/xml"; | static const char mime_type_xml[] = "text/xml"; | |||
static const char mime_type_html[] = "text/html; charset=us-ascii"; | static const char mime_type_html[] = "text/html; charset=us-ascii"; | |||
static const char mime_type_text_prometheus[] = "text/plain; version=0.0.4"; | ||||
static const char mime_type_css[] = "text/css"; | static const char mime_type_css[] = "text/css"; | |||
static const char mime_type_js[] = "text/javascript"; | static const char mime_type_js[] = "text/javascript"; | |||
static const char mime_type_png[] = "image/png"; | ||||
static const char encoding_identity[] = "identity"; | static const char encoding_identity[] = "identity"; | |||
static const char encoding_gzip[] = "gzip"; | static const char encoding_gzip[] = "gzip"; | |||
static const char server[] = PACKAGE_NAME "/" PACKAGE_VERSION; | static const char server[] = PACKAGE_NAME "/" PACKAGE_VERSION; | |||
static int idletime = 60; | static int idletime = 60; | |||
#define MAX_REQUEST_LENGTH 4000 | #define MAX_REQUEST_LENGTH 4000 | |||
static int *insocks = NULL; | static int *insocks = NULL; | |||
static unsigned int insock_num = 0; | static unsigned int insock_num = 0; | |||
skipping to change at line 457 | skipping to change at line 459 | |||
char *pos; | char *pos; | |||
/* find start */ | /* find start */ | |||
pos = strstr(conn->request, field); | pos = strstr(conn->request, field); | |||
if (pos == NULL) | if (pos == NULL) | |||
return (NULL); | return (NULL); | |||
bound1 = pos - conn->request + strlen(field); | bound1 = pos - conn->request + strlen(field); | |||
/* find end */ | /* find end */ | |||
for (bound2 = bound1; | for (bound2 = bound1; | |||
conn->request[bound2] != '\r' && | bound2 < conn->request_length && | |||
bound2 < conn->request_length; bound2++) | conn->request[bound2] != '\r'; bound2++) | |||
; | ; | |||
/* copy to buffer */ | /* copy to buffer */ | |||
return (split_string(conn->request, bound1, bound2)); | return (split_string(conn->request, bound1, bound2)); | |||
} | } | |||
/* --------------------------------------------------------------------------- | /* --------------------------------------------------------------------------- | |||
* Parse an HTTP request like "GET /hosts/?sort=in HTTP/1.1" to get the method | * Parse an HTTP request like "GET /hosts/?sort=in HTTP/1.1" to get the method | |||
* (GET), the uri (/hosts/), the query (sort=in) and whether the UA will | * (GET), the uri (/hosts/), the query (sort=in) and whether the UA will | |||
* accept gzip encoding. Remember to deallocate all these buffers. Query | * accept gzip encoding. Remember to deallocate all these buffers. Query | |||
skipping to change at line 528 | skipping to change at line 530 | |||
/* FIXME: maybe we need a smarter way of doing static pages: */ | /* FIXME: maybe we need a smarter way of doing static pages: */ | |||
/* --------------------------------------------------------------------------- | /* --------------------------------------------------------------------------- | |||
* Web interface: static stylesheet. | * Web interface: static stylesheet. | |||
*/ | */ | |||
static void | static void | |||
static_style_css(struct connection *conn) | static_style_css(struct connection *conn) | |||
{ | { | |||
#include "stylecss.h" | #include "stylecss.h" | |||
conn->reply = style_css; | conn->reply = (char*)style_css; | |||
conn->reply_length = style_css_len; | conn->reply_length = style_css_len; | |||
conn->reply_dont_free = 1; | conn->reply_dont_free = 1; | |||
conn->mime_type = mime_type_css; | conn->mime_type = mime_type_css; | |||
} | } | |||
/* --------------------------------------------------------------------------- | /* --------------------------------------------------------------------------- | |||
* Web interface: static JavaScript. | * Web interface: static JavaScript. | |||
*/ | */ | |||
static void | static void | |||
static_graph_js(struct connection *conn) | static_graph_js(struct connection *conn) | |||
{ | { | |||
#include "graphjs.h" | #include "graphjs.h" | |||
conn->reply = graph_js; | conn->reply = (char*)graph_js; | |||
conn->reply_length = graph_js_len; | conn->reply_length = graph_js_len; | |||
conn->reply_dont_free = 1; | conn->reply_dont_free = 1; | |||
conn->mime_type = mime_type_js; | conn->mime_type = mime_type_js; | |||
} | } | |||
/* --------------------------------------------------------------------------- | /* --------------------------------------------------------------------------- | |||
* Web interface: favicon. | ||||
*/ | ||||
static void | ||||
static_favicon(struct connection *conn) | ||||
{ | ||||
#include "favicon.h" | ||||
conn->reply = (char*)favicon_png; | ||||
conn->reply_length = sizeof(favicon_png); | ||||
conn->reply_dont_free = 1; | ||||
conn->mime_type = mime_type_png; | ||||
} | ||||
/* --------------------------------------------------------------------------- | ||||
* gzip a reply, if requested and possible. Don't bother with a minimum | * gzip a reply, if requested and possible. Don't bother with a minimum | |||
* length requirement, I've never seen a page fail to compress. | * length requirement, I've never seen a page fail to compress. | |||
*/ | */ | |||
static void | static void | |||
process_gzip(struct connection *conn) | process_gzip(struct connection *conn) | |||
{ | { | |||
char *buf; | char *buf; | |||
size_t len; | size_t len; | |||
z_stream zs; | z_stream zs; | |||
skipping to change at line 660 | skipping to change at line 676 | |||
str_extract(buf, &(conn->reply_length), &(conn->reply)); | str_extract(buf, &(conn->reply_length), &(conn->reply)); | |||
conn->mime_type = mime_type_html; | conn->mime_type = mime_type_html; | |||
} | } | |||
else if (str_starts_with(safe_url, "/graphs.xml")) { | else if (str_starts_with(safe_url, "/graphs.xml")) { | |||
struct str *buf = xml_graphs(); | struct str *buf = xml_graphs(); | |||
str_extract(buf, &(conn->reply_length), &(conn->reply)); | str_extract(buf, &(conn->reply_length), &(conn->reply)); | |||
conn->mime_type = mime_type_xml; | conn->mime_type = mime_type_xml; | |||
/* hack around Opera caching the XML */ | /* hack around Opera caching the XML */ | |||
conn->header_extra = "Pragma: no-cache\r\n"; | conn->header_extra = "Pragma: no-cache\r\n"; | |||
} | } | |||
else if (str_starts_with(safe_url, "/metrics")) { | ||||
struct str *buf = text_metrics(); | ||||
str_extract(buf, &(conn->reply_length), &(conn->reply)); | ||||
conn->mime_type = mime_type_text_prometheus; | ||||
} | ||||
else if (strcmp(safe_url, "/style.css") == 0) | else if (strcmp(safe_url, "/style.css") == 0) | |||
static_style_css(conn); | static_style_css(conn); | |||
else if (strcmp(safe_url, "/graph.js") == 0) | else if (strcmp(safe_url, "/graph.js") == 0) | |||
static_graph_js(conn); | static_graph_js(conn); | |||
else { | else if (strcmp(safe_url, "/favicon.ico") == 0) { | |||
/* serves a PNG instead of an ICO, might cause problems for IE6 */ | ||||
static_favicon(conn); | ||||
} else { | ||||
default_reply(conn, 404, "Not Found", | default_reply(conn, 404, "Not Found", | |||
"The page you requested could not be found."); | "The page you requested could not be found."); | |||
free(safe_url); | free(safe_url); | |||
return; | return; | |||
} | } | |||
free(safe_url); | free(safe_url); | |||
process_gzip(conn); | process_gzip(conn); | |||
assert(conn->mime_type != NULL); | assert(conn->mime_type != NULL); | |||
generate_header(conn, 200, "OK"); | generate_header(conn, 200, "OK"); | |||
End of changes. 9 change blocks. | ||||
6 lines changed or deleted | 30 lines changed or added |