2011-12-21 09:55:05 +00:00
|
|
|
/*
|
|
|
|
Serval Distributed Numbering Architecture (DNA)
|
|
|
|
Copyright (C) 2010 Paul Gardner-Stephen
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-12-21 05:58:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2011-12-22 11:28:18 +00:00
|
|
|
#include <signal.h>
|
2012-09-05 09:23:22 +00:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2013-10-16 07:55:58 +00:00
|
|
|
#include <assert.h>
|
2011-12-21 05:58:08 +00:00
|
|
|
|
2012-02-23 02:15:42 +00:00
|
|
|
#include "serval.h"
|
2013-02-15 05:02:13 +00:00
|
|
|
#include "overlay_address.h"
|
2012-12-11 05:29:46 +00:00
|
|
|
#include "conf.h"
|
2012-08-06 02:55:45 +00:00
|
|
|
#include "str.h"
|
2011-12-21 05:58:08 +00:00
|
|
|
#include "rhizome.h"
|
2013-10-16 07:55:58 +00:00
|
|
|
#include "http_server.h"
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
#define RHIZOME_SERVER_MAX_LIVE_REQUESTS 32
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_handler{
|
|
|
|
const char *path;
|
|
|
|
int (*parser)(rhizome_http_request *r, const char *remainder);
|
|
|
|
};
|
2012-07-02 05:50:30 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int rhizome_status_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int rhizome_file_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int manifest_by_prefix_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int interface_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int neighbour_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int fav_icon_header(rhizome_http_request *r, const char *remainder);
|
|
|
|
static int root_page(rhizome_http_request *r, const char *remainder);
|
|
|
|
|
|
|
|
extern int rhizome_direct_import(rhizome_http_request *r, const char *remainder);
|
|
|
|
extern int rhizome_direct_enquiry(rhizome_http_request *r, const char *remainder);
|
|
|
|
extern int rhizome_direct_dispatch(rhizome_http_request *r, const char *remainder);
|
|
|
|
|
|
|
|
struct http_handler paths[]={
|
|
|
|
{"/rhizome/status", rhizome_status_page},
|
|
|
|
{"/rhizome/file/", rhizome_file_page},
|
|
|
|
{"/rhizome/import", rhizome_direct_import},
|
|
|
|
{"/rhizome/enquiry", rhizome_direct_enquiry},
|
|
|
|
{"/rhizome/manifestbyprefix/", manifest_by_prefix_page},
|
|
|
|
{"/rhizome/", rhizome_direct_dispatch},
|
|
|
|
{"/interface/", interface_page},
|
|
|
|
{"/neighbour/", neighbour_page},
|
|
|
|
{"/favicon.ico", fav_icon_header},
|
|
|
|
{"/", root_page},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rhizome_dispatch(struct http_request *hr)
|
|
|
|
{
|
|
|
|
rhizome_http_request *r = (rhizome_http_request *) hr;
|
|
|
|
INFOF("RHIZOME HTTP SERVER, %s %s", r->http.verb, r->http.path);
|
|
|
|
r->http.response.content_generator = NULL;
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < NELS(paths); ++i) {
|
|
|
|
const char *remainder;
|
|
|
|
if (str_startswith(r->http.path, paths[i].path, &remainder)){
|
|
|
|
int ret = paths[i].parser(r, remainder);
|
|
|
|
if (ret < 0) {
|
|
|
|
http_request_simple_response(&r->http, 500, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http_request_simple_response(&r->http, 404, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sched_ent server_alarm;
|
|
|
|
struct profile_total server_stats = {
|
|
|
|
.name = "rhizome_server_poll",
|
|
|
|
};
|
2012-07-02 03:49:54 +00:00
|
|
|
|
2011-12-21 05:58:08 +00:00
|
|
|
/*
|
2012-08-28 04:26:47 +00:00
|
|
|
HTTP server and client code for rhizome transfers and rhizome direct.
|
|
|
|
Selection of either use is made when starting the HTTP server and
|
|
|
|
specifying the call-back function to use on client connections.
|
2011-12-21 05:58:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-07 13:07:37 +00:00
|
|
|
uint16_t rhizome_http_server_port = 0;
|
2012-06-26 01:37:01 +00:00
|
|
|
static int rhizome_server_socket = -1;
|
2013-09-24 04:53:40 +00:00
|
|
|
static int request_count=0;
|
2012-08-09 02:44:32 +00:00
|
|
|
static time_ms_t rhizome_server_last_start_attempt = -1;
|
2011-12-21 05:58:08 +00:00
|
|
|
|
2012-01-03 04:15:50 +00:00
|
|
|
// Format icon data using:
|
|
|
|
// od -vt u1 ~/Downloads/favicon.ico | cut -c9- | sed 's/ */,/g'
|
|
|
|
unsigned char favicon_bytes[]={
|
|
|
|
0,0,1,0,1,0,16,16,16,0,0,0,0,0,40,1
|
|
|
|
,0,0,22,0,0,0,40,0,0,0,16,0,0,0,32,0
|
|
|
|
,0,0,1,0,4,0,0,0,0,0,128,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,16,0,0,0,0,0,0,0,104,158
|
|
|
|
,168,0,163,233,247,0,104,161,118,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,17
|
|
|
|
,17,17,17,18,34,17,17,18,34,17,17,18,34,17,17,2
|
|
|
|
,34,17,17,18,34,17,16,18,34,1,17,17,1,17,1,17
|
|
|
|
,1,16,1,16,17,17,17,17,1,17,16,16,17,17,17,17
|
|
|
|
,1,17,18,34,17,17,17,16,17,17,2,34,17,17,17,16
|
|
|
|
,17,16,18,34,17,17,17,16,17,1,17,1,17,17,17,18
|
|
|
|
,34,17,17,16,17,17,17,18,34,17,17,18,34,17,17,18
|
|
|
|
,34,17,17,18,34,17,17,16,17,17,17,18,34,17,17,16
|
|
|
|
,17,17,17,17,17,0,17,1,17,17,17,17,17,17,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
|
|
|
int favicon_len=318;
|
|
|
|
|
2012-11-30 04:17:27 +00:00
|
|
|
int is_rhizome_http_server_running()
|
2012-07-12 02:40:59 +00:00
|
|
|
{
|
|
|
|
return rhizome_server_socket != -1;
|
|
|
|
}
|
|
|
|
|
2012-06-26 01:37:01 +00:00
|
|
|
/* Start the Rhizome HTTP server by creating a socket, binding it to an available port, and
|
|
|
|
marking it as passive. If called repeatedly and frequently, this function will only try to start
|
|
|
|
the server after a certain time has elapsed since the last attempt.
|
|
|
|
Return -1 if an error occurs (message logged).
|
|
|
|
Return 0 if the server was started.
|
|
|
|
Return 1 if the server is already started successfully.
|
|
|
|
Return 2 if the server was not started because it is too soon since last failed attempt.
|
|
|
|
*/
|
2013-10-16 07:55:58 +00:00
|
|
|
int rhizome_http_server_start(uint16_t port_low, uint16_t port_high)
|
2011-12-21 05:58:08 +00:00
|
|
|
{
|
2012-06-26 01:37:01 +00:00
|
|
|
if (rhizome_server_socket != -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Only try to start http server every five seconds. */
|
2012-08-09 02:44:32 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
if (now < rhizome_server_last_start_attempt + 5000)
|
2012-06-26 01:37:01 +00:00
|
|
|
return 2;
|
2012-08-09 02:44:32 +00:00
|
|
|
rhizome_server_last_start_attempt = now;
|
2013-10-16 00:53:45 +00:00
|
|
|
if (config.debug.rhizome_httpd)
|
2012-06-26 01:37:01 +00:00
|
|
|
DEBUGF("Starting rhizome HTTP server");
|
|
|
|
|
2013-10-07 13:07:37 +00:00
|
|
|
uint16_t port;
|
2012-08-28 04:26:47 +00:00
|
|
|
for (port = port_low; port <= port_high; ++port) {
|
2012-07-12 02:40:59 +00:00
|
|
|
/* Create a new socket, reusable and non-blocking. */
|
|
|
|
if (rhizome_server_socket == -1) {
|
|
|
|
rhizome_server_socket = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if (rhizome_server_socket == -1) {
|
|
|
|
WHY_perror("socket");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
int on=1;
|
|
|
|
if (setsockopt(rhizome_server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) == -1) {
|
|
|
|
WHY_perror("setsockopt(REUSEADDR)");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (ioctl(rhizome_server_socket, FIONBIO, (char *)&on) == -1) {
|
|
|
|
WHY_perror("ioctl(FIONBIO)");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Bind it to the next port we want to try. */
|
|
|
|
struct sockaddr_in address;
|
|
|
|
bzero((char *) &address, sizeof(address));
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
2012-07-02 06:54:07 +00:00
|
|
|
address.sin_port = htons(port);
|
2012-07-12 02:40:59 +00:00
|
|
|
if (bind(rhizome_server_socket, (struct sockaddr *) &address, sizeof(address)) == -1) {
|
|
|
|
if (errno != EADDRINUSE) {
|
|
|
|
WHY_perror("bind");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We bound to a port. The battle is half won. Now we have to successfully listen on that
|
|
|
|
port, which could also fail with EADDRINUSE, in which case we have to scrap the socket and
|
|
|
|
create a new one, because once bound, a socket stays bound.
|
|
|
|
*/
|
|
|
|
if (listen(rhizome_server_socket, 20) != -1)
|
|
|
|
goto success;
|
|
|
|
if (errno != EADDRINUSE) {
|
|
|
|
WHY_perror("listen");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
close(rhizome_server_socket);
|
|
|
|
rhizome_server_socket = -1;
|
|
|
|
}
|
2012-06-26 01:37:01 +00:00
|
|
|
}
|
2012-07-12 02:40:59 +00:00
|
|
|
WHYF("No ports available in range %u to %u", RHIZOME_HTTP_PORT, RHIZOME_HTTP_PORT_MAX);
|
|
|
|
error:
|
|
|
|
if (rhizome_server_socket != -1) {
|
2012-06-26 01:37:01 +00:00
|
|
|
close(rhizome_server_socket);
|
|
|
|
rhizome_server_socket = -1;
|
|
|
|
}
|
2012-07-12 02:40:59 +00:00
|
|
|
return WHY("Failed to start rhizome HTTP server");
|
2011-12-22 11:28:18 +00:00
|
|
|
|
2012-07-12 02:40:59 +00:00
|
|
|
success:
|
2013-02-16 22:29:56 +00:00
|
|
|
if (config.rhizome.http.enable)
|
2013-10-07 13:07:37 +00:00
|
|
|
INFOF("RHIZOME HTTP SERVER, START port=%"PRIu16" fd=%d", port, rhizome_server_socket);
|
2013-02-16 22:29:56 +00:00
|
|
|
else
|
2013-10-07 13:07:37 +00:00
|
|
|
INFOF("HTTP SERVER (LIMITED SERVICE), START port=%"PRIu16" fd=%d", port, rhizome_server_socket);
|
2012-08-28 04:26:47 +00:00
|
|
|
|
2012-07-12 02:40:59 +00:00
|
|
|
rhizome_http_server_port = port;
|
2012-06-22 03:55:41 +00:00
|
|
|
/* Add Rhizome HTTPd server to list of file descriptors to watch */
|
2012-07-02 03:49:54 +00:00
|
|
|
server_alarm.function = rhizome_server_poll;
|
2013-10-16 07:55:58 +00:00
|
|
|
server_alarm.stats = &server_stats;
|
2012-07-02 03:49:54 +00:00
|
|
|
server_alarm.poll.fd = rhizome_server_socket;
|
|
|
|
server_alarm.poll.events = POLLIN;
|
|
|
|
watch(&server_alarm);
|
2011-12-21 05:58:08 +00:00
|
|
|
return 0;
|
2012-07-12 02:40:59 +00:00
|
|
|
|
2011-12-21 05:58:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static void rhizome_server_finalise_http_request(struct http_request *_r)
|
2011-12-21 05:58:08 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
rhizome_http_request *r = (rhizome_http_request *) _r;
|
|
|
|
rhizome_read_close(&r->read_state);
|
|
|
|
request_count--;
|
2012-06-22 03:55:41 +00:00
|
|
|
}
|
2011-12-22 11:28:18 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int rhizome_dispatch(struct http_request *);
|
|
|
|
|
|
|
|
static unsigned int rhizome_http_request_uuid_counter = 0;
|
2012-10-02 09:02:48 +00:00
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void rhizome_server_poll(struct sched_ent *alarm)
|
2012-06-22 03:55:41 +00:00
|
|
|
{
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & (POLLIN | POLLOUT)) {
|
|
|
|
struct sockaddr addr;
|
|
|
|
unsigned int addr_len = sizeof addr;
|
|
|
|
int sock;
|
2013-10-16 07:55:58 +00:00
|
|
|
if ((sock = accept(rhizome_server_socket, &addr, &addr_len)) == -1) {
|
|
|
|
if (errno && errno != EAGAIN)
|
|
|
|
WARN_perror("accept");
|
|
|
|
} else {
|
2012-10-05 08:15:30 +00:00
|
|
|
struct sockaddr_in *peerip=NULL;
|
2012-08-22 05:20:14 +00:00
|
|
|
if (addr.sa_family == AF_INET) {
|
2012-10-05 08:15:30 +00:00
|
|
|
peerip = (struct sockaddr_in *)&addr;
|
2012-08-22 05:20:14 +00:00
|
|
|
INFOF("RHIZOME HTTP SERVER, ACCEPT addrlen=%u family=%u port=%u addr=%u.%u.%u.%u",
|
|
|
|
addr_len, peerip->sin_family, peerip->sin_port,
|
|
|
|
((unsigned char*)&peerip->sin_addr.s_addr)[0],
|
|
|
|
((unsigned char*)&peerip->sin_addr.s_addr)[1],
|
|
|
|
((unsigned char*)&peerip->sin_addr.s_addr)[2],
|
|
|
|
((unsigned char*)&peerip->sin_addr.s_addr)[3]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
INFOF("RHIZOME HTTP SERVER, ACCEPT addrlen=%u family=%u data=%s",
|
2012-10-05 08:15:30 +00:00
|
|
|
addr_len, addr.sa_family, alloca_tohex((unsigned char *)addr.sa_data, sizeof addr.sa_data)
|
|
|
|
);
|
2012-08-22 05:20:14 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
rhizome_http_request *request = emalloc_zero(sizeof(rhizome_http_request));
|
2012-08-22 05:20:14 +00:00
|
|
|
if (request == NULL) {
|
2013-10-16 07:55:58 +00:00
|
|
|
WHY("Cannot respond to HTTP request, out of memory");
|
2013-09-24 04:53:40 +00:00
|
|
|
close(sock);
|
2012-08-22 05:20:14 +00:00
|
|
|
} else {
|
2013-09-24 04:53:40 +00:00
|
|
|
request_count++;
|
2013-10-16 07:55:58 +00:00
|
|
|
request->uuid = rhizome_http_request_uuid_counter++;
|
|
|
|
request->data_file_name[0] = '\0';
|
|
|
|
request->read_state.blob_fd = -1;
|
|
|
|
request->read_state.blob_rowid = -1;
|
|
|
|
if (peerip)
|
|
|
|
request->http.client_in_addr = *peerip;
|
|
|
|
request->http.handle_headers = rhizome_dispatch;
|
|
|
|
request->http.debug_flag = &config.debug.rhizome_httpd;
|
2013-10-25 07:08:51 +00:00
|
|
|
request->http.disable_tx_flag = &config.debug.rhizome_nohttptx;
|
2013-10-16 07:55:58 +00:00
|
|
|
request->http.finalise = rhizome_server_finalise_http_request;
|
|
|
|
request->http.free = free;
|
|
|
|
request->http.idle_timeout = RHIZOME_IDLE_TIMEOUT;
|
|
|
|
http_request_init(&request->http, sock);
|
2012-08-22 05:20:14 +00:00
|
|
|
}
|
2011-12-21 05:58:08 +00:00
|
|
|
}
|
2012-07-10 10:29:46 +00:00
|
|
|
}
|
2012-08-22 05:20:14 +00:00
|
|
|
if (alarm->poll.revents & (POLLHUP | POLLERR)) {
|
|
|
|
INFO("Error on tcp listen socket");
|
2012-07-10 10:29:46 +00:00
|
|
|
}
|
2011-12-22 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 00:52:02 +00:00
|
|
|
int is_http_header_complete(const char *buf, size_t len, size_t read_since_last_call)
|
2012-07-13 01:36:10 +00:00
|
|
|
{
|
2013-02-15 07:39:39 +00:00
|
|
|
IN();
|
2012-07-13 08:36:55 +00:00
|
|
|
const char *bufend = buf + len;
|
2013-08-15 06:17:39 +00:00
|
|
|
const char *p = buf;
|
2012-10-04 08:08:33 +00:00
|
|
|
size_t tail = read_since_last_call + 4;
|
2012-07-13 08:36:55 +00:00
|
|
|
if (tail < len)
|
2013-08-15 06:17:39 +00:00
|
|
|
p = bufend - tail;
|
2012-07-13 08:36:55 +00:00
|
|
|
int count = 0;
|
2013-08-15 06:17:39 +00:00
|
|
|
for (; p != bufend; ++p) {
|
|
|
|
switch (*p) {
|
|
|
|
case '\n':
|
|
|
|
if (++count==2)
|
|
|
|
RETURN(p - buf);
|
|
|
|
case '\r': // ignore CR
|
|
|
|
case '\0': // ignore NUL (telnet inserts them)
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
count = 0;
|
|
|
|
break;
|
2012-07-13 08:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 06:17:39 +00:00
|
|
|
RETURN(0);
|
2013-02-16 17:47:24 +00:00
|
|
|
OUT();
|
2012-07-13 08:36:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int neighbour_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
char buf[8*1024];
|
2013-10-16 07:55:58 +00:00
|
|
|
strbuf b = strbuf_local(buf, sizeof buf);
|
2013-09-24 00:20:45 +00:00
|
|
|
sid_t neighbour_sid;
|
|
|
|
if (str_to_sid_t(&neighbour_sid, remainder) == -1)
|
2013-10-16 07:55:58 +00:00
|
|
|
return 1;
|
2013-09-24 00:20:45 +00:00
|
|
|
struct subscriber *neighbour = find_subscriber(neighbour_sid.binary, sizeof(neighbour_sid.binary), 0);
|
|
|
|
if (!neighbour)
|
|
|
|
return 1;
|
|
|
|
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;
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "text/html", buf, strbuf_len(b));
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int interface_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
char buf[8*1024];
|
|
|
|
strbuf b=strbuf_local(buf, sizeof buf);
|
|
|
|
int index=atoi(remainder);
|
|
|
|
if (index<0 || index>=OVERLAY_MAX_INTERFACES)
|
|
|
|
return 1;
|
|
|
|
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;
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "text/html", buf, strbuf_len(b));
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int rhizome_status_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
char buf[32*1024];
|
2013-10-25 07:08:51 +00:00
|
|
|
strbuf b = strbuf_local(buf, sizeof buf);
|
|
|
|
strbuf_puts(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>");
|
|
|
|
strbuf_sprintf(b, "%d HTTP requests<br>", request_count);
|
|
|
|
strbuf_sprintf(b, "%d Bundles transferring via MDP<br>", rhizome_cache_count());
|
|
|
|
rhizome_fetch_status_html(b);
|
|
|
|
strbuf_puts(b, "</body></html>");
|
|
|
|
if (strbuf_overrun(b))
|
2013-09-24 00:20:45 +00:00
|
|
|
return -1;
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "text/html", buf, strbuf_len(b));
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int rhizome_file_content(struct http_request *hr)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
rhizome_http_request *r = (rhizome_http_request *) hr;
|
2013-10-26 09:58:47 +00:00
|
|
|
assert(r->http.response_buffer_sent == 0);
|
|
|
|
assert(r->http.response_buffer_length == 0);
|
|
|
|
assert(r->read_state.offset < r->read_state.length);
|
2013-10-16 07:55:58 +00:00
|
|
|
uint64_t readlen = r->read_state.length - r->read_state.offset;
|
|
|
|
size_t suggested_size = 64 * 1024;
|
|
|
|
if (suggested_size > readlen)
|
|
|
|
suggested_size = readlen;
|
|
|
|
if (r->http.response_buffer_size < suggested_size)
|
|
|
|
http_request_set_response_bufsize(&r->http, suggested_size);
|
|
|
|
if (r->http.response_buffer == NULL)
|
|
|
|
http_request_set_response_bufsize(&r->http, 1);
|
|
|
|
if (r->http.response_buffer == NULL)
|
|
|
|
return -1;
|
2013-10-26 09:58:47 +00:00
|
|
|
ssize_t len = rhizome_read(&r->read_state,
|
|
|
|
(unsigned char *)r->http.response_buffer,
|
|
|
|
r->http.response_buffer_size);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (len == -1)
|
2013-09-24 00:20:45 +00:00
|
|
|
return -1;
|
2013-10-26 09:58:47 +00:00
|
|
|
assert((size_t) len <= r->http.response_buffer_size);
|
|
|
|
r->http.response_buffer_length += (size_t) len;
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int rhizome_file_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
/* Stream the specified payload */
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (r->http.request_header.content_range_count > 1) {
|
|
|
|
// To support byte range sets, eg, Range: bytes=0-100,200-300,400- we would have
|
|
|
|
// to reply with a multipart/byteranges MIME content.
|
|
|
|
http_request_simple_response(&r->http, 501, "Not Implemented: Byte range sets");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t filehash;
|
|
|
|
if (str_to_rhizome_filehash_t(&filehash, remainder) == -1)
|
2013-09-24 00:20:45 +00:00
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
bzero(&r->read_state, sizeof r->read_state);
|
|
|
|
int n = rhizome_open_read(&r->read_state, &filehash);
|
|
|
|
if (n == -1) {
|
|
|
|
http_request_simple_response(&r->http, 500, NULL);
|
|
|
|
return 0;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
if (n != 0)
|
|
|
|
return 1;
|
|
|
|
if (r->read_state.length == -1 && rhizome_read(&r->read_state, NULL, 0)) {
|
|
|
|
rhizome_read_close(&r->read_state);
|
|
|
|
return 1;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
2013-10-16 07:55:58 +00:00
|
|
|
assert(r->read_state.length != -1);
|
2013-10-26 09:58:47 +00:00
|
|
|
r->http.response.header.resource_length = r->read_state.length;
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.request_header.content_range_count > 0) {
|
2013-10-26 09:58:47 +00:00
|
|
|
assert(r->http.request_header.content_range_count == 1);
|
|
|
|
struct http_range closed;
|
|
|
|
unsigned n = http_range_close(&closed, r->http.request_header.content_ranges, 1, r->read_state.length);
|
|
|
|
if (n == 0 || http_range_bytes(&closed, 1) == 0) {
|
2013-10-16 07:55:58 +00:00
|
|
|
http_request_simple_response(&r->http, 416, NULL); // Request Range Not Satisfiable
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-26 09:58:47 +00:00
|
|
|
r->http.response.header.content_range_start = closed.first;
|
|
|
|
r->http.response.header.content_length = closed.last - closed.first + 1;
|
|
|
|
r->read_state.offset = closed.first;
|
|
|
|
} else {
|
|
|
|
r->http.response.header.content_range_start = 0;
|
|
|
|
r->http.response.header.content_length = r->http.response.header.resource_length;
|
|
|
|
r->read_state.offset = 0;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
2013-10-26 09:58:47 +00:00
|
|
|
http_request_response_generated(&r->http, 200, "application/binary", rhizome_file_content);
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int manifest_by_prefix_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t prefix;
|
|
|
|
const char *endp = NULL;
|
|
|
|
unsigned prefix_len = strn_fromhex(prefix.binary, sizeof prefix.binary, remainder, &endp);
|
|
|
|
if (endp == NULL || *endp != '\0' || prefix_len < 1)
|
|
|
|
return 1; // not found
|
2013-09-24 00:20:45 +00:00
|
|
|
rhizome_manifest *m = rhizome_new_manifest();
|
2013-10-03 13:46:45 +00:00
|
|
|
int ret = rhizome_retrieve_manifest_by_prefix(prefix.binary, prefix_len, m);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (ret == -1)
|
|
|
|
http_request_simple_response(&r->http, 500, NULL);
|
|
|
|
else if (ret == 0)
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "application/binary", (const char *)m->manifestdata, m->manifest_all_bytes);
|
2013-09-24 00:20:45 +00:00
|
|
|
rhizome_manifest_free(m);
|
2013-10-16 07:55:58 +00:00
|
|
|
return ret <= 0 ? 0 : 1;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int fav_icon_header(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "image/vnd.microsoft.icon", (const char *)favicon_bytes, favicon_len);
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static int root_page(rhizome_http_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
if (r->http.verb != HTTP_VERB_GET) {
|
|
|
|
http_request_simple_response(&r->http, 405, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
char temp[8192];
|
2013-10-16 07:55:58 +00:00
|
|
|
strbuf b = strbuf_local(temp, sizeof temp);
|
2013-09-24 00:20:45 +00:00
|
|
|
strbuf_sprintf(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>"
|
|
|
|
"<h1>Hello, I'm %s*</h1><br>"
|
|
|
|
"Interfaces;<br>",
|
2013-10-09 08:24:21 +00:00
|
|
|
alloca_tohex_sid_t_trunc(my_subscriber->sid, 16));
|
2013-09-24 00:20:45 +00:00
|
|
|
int i;
|
|
|
|
for (i=0;i<OVERLAY_MAX_INTERFACES;i++){
|
|
|
|
if (overlay_interfaces[i].state==INTERFACE_STATE_UP)
|
2013-10-16 07:55:58 +00:00
|
|
|
strbuf_sprintf(b, "<a href=\"/interface/%d\">%d: %s, TX: %d, RX: %d</a><br>",
|
2013-09-24 00:20:45 +00:00
|
|
|
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>");
|
2013-10-16 07:55:58 +00:00
|
|
|
if (strbuf_overrun(b)) {
|
|
|
|
WHY("HTTP Root page buffer overrun");
|
|
|
|
http_request_simple_response(&r->http, 500, NULL);
|
|
|
|
} else
|
2013-10-25 13:27:23 +00:00
|
|
|
http_request_response_static(&r->http, 200, "text/html", temp, strbuf_len(b));
|
2011-12-22 17:55:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|