/*
Serval DNA essential server HTTP pages
Copyright (C) 2016 Flinders University
Copyright (C) 2015 Serval Project Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "serval.h"
#include "httpd.h"
#include "conf.h"
#include "overlay_address.h"
#include "overlay_interface.h"
#include "str.h"
#include "os.h"
#include "route_link.h"
DEFINE_FEATURE(http_server);
DECLARE_HANDLER("/", root_page);
DECLARE_HANDLER("/static/", static_page);
DECLARE_HANDLER("/interface/", interface_page);
DECLARE_HANDLER("/neighbour/", neighbour_page);
DECLARE_HANDLER("/favicon.ico", fav_icon_header);
static int root_page(httpd_request *r, const char *remainder)
{
if (*remainder)
return 404;
if (r->http.verb != HTTP_VERB_GET)
return 405;
char temp[8192];
strbuf b = strbuf_local_buf(temp);
strbuf_sprintf(b, "
"
"Hello, I'm %s*
",
alloca_tohex_sid_t_trunc(get_my_subscriber(1)->sid, 16));
if (config.server.motd[0]) {
strbuf_puts(b, "");
strbuf_html_escape(b, config.server.motd, strlen(config.server.motd));
strbuf_puts(b, "
");
}
strbuf_puts(b, "Interfaces;
");
int i;
for (i=0;i%d: %s, TX: %d, RX: %d
",
i, i, overlay_interfaces[i].name, overlay_interfaces[i].tx_count, overlay_interfaces[i].recv_count);
}
strbuf_puts(b, "Neighbours;
");
link_neighbour_short_status_html(b, "/neighbour");
if (is_rhizome_http_enabled()){
strbuf_puts(b, "Rhizome Status
");
}
strbuf_puts(b, "");
if (strbuf_overrun(b)) {
WHY("HTTP Root page buffer overrun");
return 500;
}
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, temp, strbuf_len(b));
return 1;
}
static int fav_icon_header(httpd_request *r, const char *remainder)
{
if (*remainder)
return 404;
http_request_response_static(&r->http, 200, &CONTENT_TYPE_FAVICON, (const char *)favicon_bytes, favicon_len);
return 1;
}
static int neighbour_page(httpd_request *r, const char *remainder)
{
if (r->http.verb != HTTP_VERB_GET)
return 405;
char buf[8*1024];
strbuf b = strbuf_local_buf(buf);
sid_t neighbour_sid;
if (str_to_sid_t(&neighbour_sid, remainder) == -1)
return 404;
struct subscriber *neighbour = find_subscriber(neighbour_sid.binary, sizeof(neighbour_sid.binary), 0);
if (!neighbour)
return 404;
strbuf_puts(b, "");
link_neighbour_status_html(b, neighbour);
strbuf_puts(b, "");
if (strbuf_overrun(b))
return -1;
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, buf, strbuf_len(b));
return 1;
}
static int interface_page(httpd_request *r, const char *remainder)
{
if (r->http.verb != HTTP_VERB_GET)
return 405;
char buf[8*1024];
strbuf b=strbuf_local_buf(buf);
int index=atoi(remainder);
if (index<0 || index>=OVERLAY_MAX_INTERFACES)
return 404;
strbuf_puts(b, "");
interface_state_html(b, &overlay_interfaces[index]);
strbuf_puts(b, "");
if (strbuf_overrun(b))
return -1;
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, buf, strbuf_len(b));
return 1;
}
static int static_file_generator(struct http_request *hr, unsigned char *buf, size_t bufsz, struct http_content_generator_result *result)
{
struct httpd_request *r=(struct httpd_request *)hr;
uint64_t remain = r->http.response.header.content_length + r->http.response.header.content_range_start - r->u.file.offset;
if (bufsz < remain)
remain = bufsz;
ssize_t bytes = read(r->u.file.fd, buf, remain);
if (bytes == -1)
return -1;
r->u.file.offset+=bytes;
result->generated = bytes;
return (r->u.file.offset >= r->http.response.header.content_length + r->http.response.header.content_range_start)?0:1;
}
static void finalise_union_close_file(httpd_request *r)
{
if (r->u.file.fd==-1)
return;
close(r->u.file.fd);
r->u.file.fd=-1;
}
static int static_page(httpd_request *r, const char *remainder)
{
if (r->http.verb != HTTP_VERB_GET)
return 405;
char path[PATH_MAX];
if (!*remainder)
remainder="index.html";
if (FORMF_SERVAL_ETC_PATH(path, "static/%s", remainder)==0)
return 500;
struct stat stat;
if (lstat(path, &stat))
return 404;
r->u.file.fd = open(path, O_RDONLY);
if (r->u.file.fd==-1)
return 404;
r->finalise_union=finalise_union_close_file;
// Set Content-Type header based on the extension of the file name. All text and html is assumed
// to be in UTF-8 encoding.
const struct mime_content_type *content_type = &CONTENT_TYPE_BLOB;
if (strcase_endswith(path, ".txt", NULL)) {
content_type = &CONTENT_TYPE_TEXT;
}
else if (strcase_endswith(path, ".htm", NULL) || strcase_endswith(path, ".html", NULL)) {
content_type = &CONTENT_TYPE_HTML;
}
else if (strcase_endswith(path, ".json", NULL)) {
content_type = &CONTENT_TYPE_JSON;
}
else if (strcase_endswith(path, ".rhm", NULL)) {
content_type = &CONTENT_TYPE_RHIZOME_MANIFEST;
}
http_response_init_content_range(r, stat.st_size);
if (r->http.response.header.content_range_start){
if (lseek64(r->u.file.fd, r->http.response.header.content_range_start, SEEK_SET)){
WARNF_perror("lseek(%s)", path);
return 500;
}
}
r->u.file.offset=r->http.response.header.content_range_start;
http_request_response_generated(&r->http, 200, content_type, static_file_generator);
return 1;
}