2016-09-27 07:26:44 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2015-08-24 03:18:00 +00:00
|
|
|
|
|
|
|
#include "serval.h"
|
|
|
|
#include "httpd.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "overlay_address.h"
|
|
|
|
#include "overlay_interface.h"
|
2017-10-19 23:25:21 +00:00
|
|
|
#include "str.h"
|
2015-08-24 03:18:00 +00:00
|
|
|
#include "os.h"
|
2015-08-31 05:48:08 +00:00
|
|
|
#include "route_link.h"
|
2015-08-24 03:18:00 +00:00
|
|
|
|
Switch to feature-driven linking
This introduces a new way of linking Serval executables and dynamic
libraries from static libraries like libservald.a -- called
"feature-driven" linking.
The Makefile now links servald and serval-tests from libservald.a,
rather than from an explicit list of object (.o) files. Thanks to the
section-based method for registering functions such as HTTP handlers,
CLI commands and MDP handlers, these object files had become
"stand-alone" and hence were no longer included in the link because
there was no unresolved reference that required them to be linked in.
The new "feature.h" provides the DECLARE_FEATURE(name) macro that each
stand-alone source file uses to declare the named feature(s) it
provides. Each executable can call the USE_FEATURE(name) macro in any
of its explicitly-linked source files to cause the corresponding
object(s) to be included in the link, eg, servald_features.c.
The DEFINE_BINDING() macro has been extended so that every individual
MDP binding is given a feature name based on its port number macro, eg,
"mdp_binding_MDP_PORT_ECHO".
Some features have been factored into their own separate source files so
they can be omitted or included in a build independently of each other:
- the MDP bindings for MDP_PORT_DNALOOKUP, MDP_PORT_ECHO,
MDP_PORT_TRACE, MDP_PORT_KEYMAPREQUEST, MDP_PORT_RHIZOME_xxx,
MDP_PORT_PROBE, MDP_PORT_STUN, MDP_PORT_STUNREQ
- the CLI "log" and "echo" commands
- the CLI "rhizome direct" command
The JNI source files are only compiled if the <jni.h> header is present,
otherwise they are omitted from libservald.so.
2016-10-13 02:58:23 +00:00
|
|
|
DEFINE_FEATURE(http_server);
|
|
|
|
|
|
|
|
DECLARE_HANDLER("/", root_page);
|
2015-08-24 03:18:00 +00:00
|
|
|
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];
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf b = strbuf_local_buf(temp);
|
2015-08-24 03:18:00 +00:00
|
|
|
strbuf_sprintf(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>"
|
|
|
|
"<h1>Hello, I'm %s*</h1>",
|
2016-10-19 09:26:11 +00:00
|
|
|
alloca_tohex_sid_t_trunc(get_my_subscriber(1)->sid, 16));
|
2015-08-24 03:18:00 +00:00
|
|
|
if (config.server.motd[0]) {
|
|
|
|
strbuf_puts(b, "<p>");
|
|
|
|
strbuf_html_escape(b, config.server.motd, strlen(config.server.motd));
|
|
|
|
strbuf_puts(b, "</p>");
|
|
|
|
}
|
|
|
|
strbuf_puts(b, "Interfaces;<br />");
|
|
|
|
int i;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP)
|
|
|
|
strbuf_sprintf(b, "<a href=\"/interface/%d\">%d: %s, TX: %d, RX: %d</a><br />",
|
|
|
|
i, i, overlay_interfaces[i].name, overlay_interfaces[i].tx_count, overlay_interfaces[i].recv_count);
|
|
|
|
}
|
|
|
|
strbuf_puts(b, "Neighbours;<br />");
|
|
|
|
link_neighbour_short_status_html(b, "/neighbour");
|
|
|
|
if (is_rhizome_http_enabled()){
|
|
|
|
strbuf_puts(b, "<a href=\"/rhizome/status\">Rhizome Status</a><br />");
|
|
|
|
}
|
|
|
|
strbuf_puts(b, "</body></html>");
|
|
|
|
if (strbuf_overrun(b)) {
|
|
|
|
WHY("HTTP Root page buffer overrun");
|
|
|
|
return 500;
|
|
|
|
}
|
2017-10-19 23:25:21 +00:00
|
|
|
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, temp, strbuf_len(b));
|
2015-08-24 03:18:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fav_icon_header(httpd_request *r, const char *remainder)
|
|
|
|
{
|
|
|
|
if (*remainder)
|
|
|
|
return 404;
|
2017-10-19 23:25:21 +00:00
|
|
|
http_request_response_static(&r->http, 200, &CONTENT_TYPE_FAVICON, (const char *)favicon_bytes, favicon_len);
|
2015-08-24 03:18:00 +00:00
|
|
|
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];
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf b = strbuf_local_buf(buf);
|
2015-08-24 03:18:00 +00:00
|
|
|
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, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>");
|
|
|
|
link_neighbour_status_html(b, neighbour);
|
|
|
|
strbuf_puts(b, "</body></html>");
|
|
|
|
if (strbuf_overrun(b))
|
|
|
|
return -1;
|
2017-10-19 23:25:21 +00:00
|
|
|
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, buf, strbuf_len(b));
|
2015-08-24 03:18:00 +00:00
|
|
|
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];
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf b=strbuf_local_buf(buf);
|
2015-08-24 03:18:00 +00:00
|
|
|
int index=atoi(remainder);
|
|
|
|
if (index<0 || index>=OVERLAY_MAX_INTERFACES)
|
|
|
|
return 404;
|
|
|
|
strbuf_puts(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>");
|
|
|
|
interface_state_html(b, &overlay_interfaces[index]);
|
|
|
|
strbuf_puts(b, "</body></html>");
|
|
|
|
if (strbuf_overrun(b))
|
|
|
|
return -1;
|
2017-10-19 23:25:21 +00:00
|
|
|
http_request_response_static(&r->http, 200, &CONTENT_TYPE_HTML, buf, strbuf_len(b));
|
2015-08-24 03:18:00 +00:00
|
|
|
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;
|
|
|
|
|
2017-10-19 23:31:15 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-08-24 03:18:00 +00:00
|
|
|
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;
|
2017-10-19 23:31:15 +00:00
|
|
|
http_request_response_generated(&r->http, 200, content_type, static_file_generator);
|
2015-08-24 03:18:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|