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
|
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"
|
2012-07-02 03:49:54 +00:00
|
|
|
#define RHIZOME_SERVER_MAX_LIVE_REQUESTS 32
|
|
|
|
|
2012-07-02 05:50:30 +00:00
|
|
|
struct sched_ent server_alarm;
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total server_stats;
|
2012-07-02 05:50:30 +00:00
|
|
|
|
2012-07-02 06:34:00 +00:00
|
|
|
struct profile_total connection_stats;
|
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
|
|
|
*/
|
|
|
|
|
2012-07-12 02:40:59 +00:00
|
|
|
unsigned short 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-08-28 05:02:12 +00:00
|
|
|
int (*rhizome_http_parse_func)(rhizome_http_request *)=NULL;
|
|
|
|
const char *rhizome_http_parse_func_description="(null)";
|
2012-08-28 04:26:47 +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.
|
|
|
|
*/
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_http_server_start(int (*parse_func)(rhizome_http_request *),
|
|
|
|
const char *parse_func_desc,
|
2012-08-28 04:26:47 +00:00
|
|
|
int port_low,int 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;
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2012-06-26 01:37:01 +00:00
|
|
|
DEBUGF("Starting rhizome HTTP server");
|
|
|
|
|
2012-07-12 02:40:59 +00:00
|
|
|
unsigned short 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)
|
|
|
|
INFOF("RHIZOME HTTP SERVER, START port=%d fd=%d", port, rhizome_server_socket);
|
|
|
|
else
|
|
|
|
INFOF("HTTP SERVER (LIMITED SERVICE), START port=%d fd=%d", port, rhizome_server_socket);
|
2012-08-28 04:26:47 +00:00
|
|
|
|
|
|
|
/* Remember which function to call when handling client connections */
|
2012-08-28 05:02:12 +00:00
|
|
|
rhizome_http_parse_func=parse_func;
|
|
|
|
rhizome_http_parse_func_description=parse_func_desc;
|
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;
|
2012-07-02 05:50:30 +00:00
|
|
|
server_stats.name="rhizome_server_poll";
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-07-02 03:49:54 +00:00
|
|
|
void rhizome_client_poll(struct sched_ent *alarm)
|
2011-12-21 05:58:08 +00:00
|
|
|
{
|
2012-07-13 01:36:10 +00:00
|
|
|
rhizome_http_request *r = (rhizome_http_request *)alarm;
|
2013-06-18 02:43:14 +00:00
|
|
|
if (alarm->poll.revents == 0 || alarm->poll.revents & (POLLHUP | POLLERR)){
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2013-06-18 02:43:14 +00:00
|
|
|
DEBUGF("Closing connection due to timeout or error %d", alarm->poll.revents);
|
2012-07-02 03:49:54 +00:00
|
|
|
rhizome_server_free_http_request(r);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-24 05:45:32 +00:00
|
|
|
|
|
|
|
if (alarm->poll.revents & POLLIN){
|
|
|
|
switch(r->request_type)
|
2012-09-05 06:23:16 +00:00
|
|
|
{
|
2013-09-24 05:45:32 +00:00
|
|
|
case RHIZOME_HTTP_REQUEST_RECEIVING_MULTIPART:
|
|
|
|
{
|
|
|
|
/* Reading multi-part form data. Read some bytes and proces them. */
|
|
|
|
char buffer[16384];
|
|
|
|
sigPipeFlag=0;
|
|
|
|
int bytes = read_nonblock(r->alarm.poll.fd, buffer, 16384);
|
|
|
|
/* If we got some data, see if we have found the end of the HTTP request */
|
|
|
|
if (bytes > 0) {
|
|
|
|
// reset inactivity timer
|
|
|
|
r->alarm.alarm = gettime_ms() + RHIZOME_IDLE_TIMEOUT;
|
|
|
|
r->alarm.deadline = r->alarm.alarm + RHIZOME_IDLE_TIMEOUT;
|
|
|
|
unschedule(&r->alarm);
|
|
|
|
schedule(&r->alarm);
|
|
|
|
rhizome_direct_process_post_multipart_bytes(r,buffer,bytes);
|
|
|
|
}
|
|
|
|
/* We don't drop the connection on an empty read, because that results
|
|
|
|
in connections dropping when they shouldn't, including during testing.
|
|
|
|
The idle timeout should drop the connections instead.
|
|
|
|
*/
|
|
|
|
if (sigPipeFlag) {
|
|
|
|
if (config.debug.rhizome_tx)
|
|
|
|
DEBUG("Received SIGPIPE, closing connection");
|
|
|
|
rhizome_server_free_http_request(r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RHIZOME_HTTP_REQUEST_RECEIVING:
|
|
|
|
/* Keep reading until we have two CR/LFs in a row */
|
|
|
|
r->request[r->request_length] = '\0';
|
2012-09-05 06:23:16 +00:00
|
|
|
sigPipeFlag=0;
|
2013-09-24 05:45:32 +00:00
|
|
|
int bytes = read_nonblock(r->alarm.poll.fd, &r->request[r->request_length], sizeof r->request - r->request_length);
|
2012-09-05 06:23:16 +00:00
|
|
|
/* If we got some data, see if we have found the end of the HTTP request */
|
|
|
|
if (bytes > 0) {
|
|
|
|
// reset inactivity timer
|
|
|
|
r->alarm.alarm = gettime_ms() + RHIZOME_IDLE_TIMEOUT;
|
|
|
|
r->alarm.deadline = r->alarm.alarm + RHIZOME_IDLE_TIMEOUT;
|
|
|
|
unschedule(&r->alarm);
|
|
|
|
schedule(&r->alarm);
|
2013-09-24 05:45:32 +00:00
|
|
|
r->request_length += bytes;
|
|
|
|
r->header_length = http_header_complete(r->request, r->request_length, bytes);
|
|
|
|
if (r->header_length){
|
|
|
|
/* We have the request. Now parse it to see if we can respond to it */
|
|
|
|
if (rhizome_http_parse_func!=NULL)
|
|
|
|
rhizome_http_parse_func(r);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (config.debug.rhizome_tx)
|
|
|
|
DEBUG("Empty read, closing connection");
|
|
|
|
rhizome_server_free_http_request(r);
|
|
|
|
return;
|
2012-09-05 06:23:16 +00:00
|
|
|
}
|
|
|
|
if (sigPipeFlag) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2012-09-05 06:23:16 +00:00
|
|
|
DEBUG("Received SIGPIPE, closing connection");
|
|
|
|
rhizome_server_free_http_request(r);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-24 05:45:32 +00:00
|
|
|
break;
|
2012-09-05 06:23:16 +00:00
|
|
|
}
|
2013-09-24 05:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alarm->poll.revents & POLLOUT){
|
|
|
|
/* Socket already has request -- so just try to send some data. */
|
|
|
|
rhizome_server_http_send_bytes(r);
|
2012-07-02 03:49:54 +00:00
|
|
|
}
|
2012-06-22 03:55:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-22 11:28:18 +00:00
|
|
|
|
2012-10-02 09:02:48 +00:00
|
|
|
static unsigned int rhizome_http_request_uuid_counter=0;
|
|
|
|
|
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-06-18 06:58:26 +00:00
|
|
|
if ((sock = accept(rhizome_server_socket, &addr, &addr_len)) != -1) {
|
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
|
|
|
}
|
|
|
|
rhizome_http_request *request = calloc(sizeof(rhizome_http_request), 1);
|
|
|
|
if (request == NULL) {
|
2013-07-15 00:29:24 +00:00
|
|
|
WHYF_perror("calloc(%u, 1)", (int)sizeof(rhizome_http_request));
|
2012-08-22 05:20:14 +00:00
|
|
|
WHY("Cannot respond to 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++;
|
2012-10-05 08:15:30 +00:00
|
|
|
request->uuid=rhizome_http_request_uuid_counter++;
|
|
|
|
if (peerip) request->requestor=*peerip;
|
|
|
|
else bzero(&request->requestor,sizeof(request->requestor));
|
|
|
|
request->data_file_name[0]=0;
|
2012-08-22 05:20:14 +00:00
|
|
|
/* We are now trying to read the HTTP request */
|
|
|
|
request->request_type=RHIZOME_HTTP_REQUEST_RECEIVING;
|
|
|
|
request->alarm.function = rhizome_client_poll;
|
2013-06-18 06:58:26 +00:00
|
|
|
request->read_state.blob_fd=-1;
|
|
|
|
request->read_state.blob_rowid=-1;
|
2012-08-22 05:20:14 +00:00
|
|
|
connection_stats.name="rhizome_client_poll";
|
|
|
|
request->alarm.stats=&connection_stats;
|
|
|
|
request->alarm.poll.fd=sock;
|
|
|
|
request->alarm.poll.events=POLLIN;
|
|
|
|
request->alarm.alarm = gettime_ms()+RHIZOME_IDLE_TIMEOUT;
|
|
|
|
request->alarm.deadline = request->alarm.alarm+RHIZOME_IDLE_TIMEOUT;
|
|
|
|
// watch for the incoming http request
|
|
|
|
watch(&request->alarm);
|
|
|
|
// set an inactivity timeout to close the connection
|
|
|
|
schedule(&request->alarm);
|
|
|
|
}
|
2011-12-21 05:58:08 +00:00
|
|
|
}
|
2013-09-24 03:43:58 +00:00
|
|
|
if (errno && errno != EAGAIN)
|
2012-08-22 05:20:14 +00:00
|
|
|
WARN_perror("accept");
|
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
|
|
|
}
|
|
|
|
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_server_free_http_request(rhizome_http_request *r)
|
2011-12-22 11:28:18 +00:00
|
|
|
{
|
2012-07-02 03:49:54 +00:00
|
|
|
unwatch(&r->alarm);
|
|
|
|
unschedule(&r->alarm);
|
|
|
|
close(r->alarm.poll.fd);
|
2012-07-13 03:01:10 +00:00
|
|
|
if (r->buffer)
|
|
|
|
free(r->buffer);
|
2013-02-20 03:59:08 +00:00
|
|
|
rhizome_read_close(&r->read_state);
|
2011-12-22 11:28:18 +00:00
|
|
|
free(r);
|
2013-09-24 04:53:40 +00:00
|
|
|
request_count--;
|
2011-12-21 05:58:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-22 11:28:18 +00:00
|
|
|
|
2012-10-04 08:08:33 +00:00
|
|
|
int 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-09-24 00:20:45 +00:00
|
|
|
static int neighbour_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
char buf[8*1024];
|
|
|
|
strbuf b=strbuf_local(buf, sizeof buf);
|
|
|
|
|
|
|
|
sid_t neighbour_sid;
|
|
|
|
if (str_to_sid_t(&neighbour_sid, remainder) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
rhizome_server_simple_http_response(r, 200, buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int interface_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
rhizome_server_simple_http_response(r, 200, buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rhizome_status_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char buf[32*1024];
|
|
|
|
struct strbuf b;
|
|
|
|
strbuf_init(&b, buf, sizeof buf);
|
|
|
|
strbuf_puts(&b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>");
|
2013-09-24 04:53:40 +00:00
|
|
|
strbuf_sprintf(&b, "%d HTTP requests<br>", request_count);
|
|
|
|
strbuf_sprintf(&b, "%d Bundles transferring via MDP<br>", rhizome_cache_count());
|
2013-09-24 00:20:45 +00:00
|
|
|
rhizome_fetch_status_html(&b);
|
|
|
|
strbuf_puts(&b, "</body></html>");
|
|
|
|
if (strbuf_overrun(&b))
|
|
|
|
return -1;
|
|
|
|
rhizome_server_simple_http_response(r, 200, buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rhizome_file_content(rhizome_http_request *r)
|
|
|
|
{
|
|
|
|
int suggested_size=65536;
|
|
|
|
if (suggested_size > r->read_state.length - r->read_state.offset)
|
|
|
|
suggested_size = r->read_state.length - r->read_state.offset;
|
|
|
|
if (suggested_size<=0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (r->buffer_size < suggested_size){
|
|
|
|
r->buffer_size = suggested_size;
|
|
|
|
if (r->buffer)
|
|
|
|
free(r->buffer);
|
|
|
|
r->buffer = malloc(r->buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r->buffer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
r->buffer_length = rhizome_read(&r->read_state, r->buffer, r->buffer_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rhizome_file_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
/* Stream the specified payload */
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!rhizome_str_is_file_hash(remainder))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bzero(&r->read_state, sizeof(r->read_state));
|
|
|
|
|
|
|
|
/* Refuse to honour HTTP request if required (used for debugging and
|
|
|
|
testing transition from HTTP to MDP) */
|
|
|
|
if (rhizome_open_read(&r->read_state, remainder))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (r->read_state.length==-1){
|
|
|
|
if (rhizome_read(&r->read_state, NULL, 0)){
|
|
|
|
rhizome_read_close(&r->read_state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *range=str_str((char*)headers,"Range: bytes=",-1);
|
|
|
|
r->read_state.offset = r->source_index = 0;
|
|
|
|
|
|
|
|
if (range){
|
|
|
|
sscanf(range, "Range: bytes=%"PRId64"-", &r->read_state.offset);
|
|
|
|
if (0)
|
|
|
|
DEBUGF("Found range header %"PRId64,r->read_state.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->read_state.length - r->read_state.offset<=0){
|
|
|
|
rhizome_server_simple_http_response(r, 200, "");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct http_response hr;
|
|
|
|
bzero(&hr, sizeof hr);
|
|
|
|
hr.result_code = 200;
|
|
|
|
hr.content_type = "application/binary";
|
|
|
|
hr.content_start = r->read_state.offset;
|
|
|
|
hr.content_end = r->read_state.length;
|
|
|
|
hr.content_length = r->read_state.length;
|
|
|
|
hr.body = NULL;
|
|
|
|
r->generator = rhizome_file_content;
|
|
|
|
rhizome_server_set_response(r, &hr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int manifest_by_prefix_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 1;
|
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-09-24 00:20:45 +00:00
|
|
|
if (ret==0)
|
|
|
|
rhizome_server_http_response(r, 200, "application/binary", (const char *)m->manifestdata, m->manifest_all_bytes);
|
|
|
|
rhizome_manifest_free(m);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fav_icon_header(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
|
|
|
rhizome_server_http_response(r, 200, "image/vnd.microsoft.icon", (const char *)favicon_bytes, favicon_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int root_page(rhizome_http_request *r, const char *remainder, const char *headers)
|
|
|
|
{
|
|
|
|
if (*remainder)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char temp[8192];
|
|
|
|
strbuf b=strbuf_local(temp, sizeof(temp));
|
|
|
|
strbuf_sprintf(b, "<html><head><meta http-equiv=\"refresh\" content=\"5\" ></head><body>"
|
|
|
|
"<h1>Hello, I'm %s*</h1><br>"
|
|
|
|
"Interfaces;<br>",
|
|
|
|
alloca_tohex(my_subscriber->sid, 8));
|
|
|
|
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))
|
|
|
|
return -1;
|
|
|
|
rhizome_server_simple_http_response(r, 200, temp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct http_handler{
|
|
|
|
const char *path;
|
|
|
|
int (*parser)(rhizome_http_request *r, const char *remainder, const char *headers);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct http_handler paths[]={
|
|
|
|
{"/rhizome/status", rhizome_status_page},
|
|
|
|
{"/rhizome/file/", rhizome_file_page},
|
|
|
|
{"/rhizome/manifestbyprefix/", manifest_by_prefix_page},
|
|
|
|
{"/interface/", interface_page},
|
|
|
|
{"/neighbour/", neighbour_page},
|
|
|
|
{"/favicon.ico", fav_icon_header},
|
|
|
|
{"/", root_page},
|
|
|
|
};
|
|
|
|
|
2012-09-06 03:51:13 +00:00
|
|
|
int rhizome_direct_parse_http_request(rhizome_http_request *r);
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_server_parse_http_request(rhizome_http_request *r)
|
2011-12-22 17:55:18 +00:00
|
|
|
{
|
2012-07-13 08:36:55 +00:00
|
|
|
// Start building up a response.
|
|
|
|
// Parse the HTTP "GET" line.
|
|
|
|
char *path = NULL;
|
2013-09-24 00:20:45 +00:00
|
|
|
char *headers = NULL;
|
2012-11-20 07:37:04 +00:00
|
|
|
if (str_startswith(r->request, "POST ", (const char **)&path)) {
|
2012-09-06 03:51:13 +00:00
|
|
|
return rhizome_direct_parse_http_request(r);
|
2012-11-20 07:37:04 +00:00
|
|
|
} else if (str_startswith(r->request, "GET ", (const char **)&path)) {
|
|
|
|
const char *p;
|
2013-09-24 00:20:45 +00:00
|
|
|
size_t header_length = 0;
|
|
|
|
size_t pathlen = 0;
|
2012-07-13 08:36:55 +00:00
|
|
|
// This loop is guaranteed to terminate before the end of the buffer, because we know that the
|
|
|
|
// buffer contains at least "\n\n" and maybe "\r\n\r\n" at the end of the header block.
|
|
|
|
for (p = path; !isspace(*p); ++p)
|
|
|
|
;
|
|
|
|
pathlen = p - path;
|
|
|
|
if ( str_startswith(p, " HTTP/1.", &p)
|
|
|
|
&& (str_startswith(p, "0", &p) || str_startswith(p, "1", &p))
|
2013-09-24 00:20:45 +00:00
|
|
|
&& (str_startswith(p, "\r\n", (const char **)&headers) || str_startswith(p, "\n", (const char **)&headers))
|
2013-08-15 06:17:39 +00:00
|
|
|
){
|
2012-07-13 08:36:55 +00:00
|
|
|
path[pathlen] = '\0';
|
2013-08-15 06:17:39 +00:00
|
|
|
header_length = r->header_length - (headers - r->request);
|
2013-09-24 00:20:45 +00:00
|
|
|
headers[header_length] = '\0';
|
2013-08-15 06:17:39 +00:00
|
|
|
}else
|
2012-07-13 08:36:55 +00:00
|
|
|
path = NULL;
|
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
|
|
|
|
if (!path) {
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2012-07-20 08:47:43 +00:00
|
|
|
DEBUGF("Received malformed HTTP request: %s", alloca_toprint(120, (const char *)r->request, r->request_length));
|
2012-07-13 08:36:55 +00:00
|
|
|
rhizome_server_simple_http_response(r, 400, "<html><h1>Malformed request</h1></html>\r\n");
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
2011-12-28 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
char *id = NULL;
|
|
|
|
INFOF("RHIZOME HTTP SERVER, GET %s", path);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
r->generator=NULL;
|
|
|
|
|
|
|
|
for (i=0;i<sizeof(paths)/sizeof(struct http_handler);i++){
|
|
|
|
if (str_startswith(path, paths[i].path, (const char **)&id)){
|
|
|
|
int ret=paths[i].parser(r, id, headers);
|
|
|
|
if (ret<0)
|
|
|
|
rhizome_server_simple_http_response(r, 500, "<html><h1>Internal Error</h1></html>\r\n");
|
|
|
|
if (ret>0)
|
|
|
|
rhizome_server_simple_http_response(r, 404, "<html><h1>Not Found</h1></html>\r\n");
|
|
|
|
|
|
|
|
/* Try sending data immediately. */
|
|
|
|
rhizome_server_http_send_bytes(r);
|
2011-12-22 17:55:18 +00:00
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rhizome_server_simple_http_response(r, 404, "<html><h1>Not Found</h1></html>\r\n");
|
2011-12-28 23:41:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-02 22:27:52 +00:00
|
|
|
|
|
|
|
/* Return appropriate message for HTTP response codes, both known and unknown. */
|
2012-07-13 03:01:10 +00:00
|
|
|
static const char *httpResultString(int response_code) {
|
|
|
|
switch (response_code) {
|
|
|
|
case 200: return "OK";
|
2012-10-04 08:08:33 +00:00
|
|
|
case 201: return "Created";
|
2012-07-13 03:01:10 +00:00
|
|
|
case 206: return "Partial Content";
|
|
|
|
case 404: return "Not found";
|
|
|
|
case 500: return "Internal server error";
|
2012-09-05 07:05:08 +00:00
|
|
|
default:
|
|
|
|
if (response_code<=4)
|
|
|
|
return "Unknown status code";
|
|
|
|
else
|
|
|
|
return "A suffusion of yellow";
|
2012-01-02 22:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 03:01:10 +00:00
|
|
|
static strbuf strbuf_build_http_response(strbuf sb, const struct http_response *h)
|
|
|
|
{
|
|
|
|
strbuf_sprintf(sb, "HTTP/1.0 %03u %s\r\n", h->result_code, httpResultString(h->result_code));
|
|
|
|
strbuf_sprintf(sb, "Content-type: %s\r\n", h->content_type);
|
2013-08-15 07:30:43 +00:00
|
|
|
if (h->content_end && h->content_length && (h->content_start!=0 || h->content_end!=h->content_length))
|
2013-08-15 06:17:39 +00:00
|
|
|
strbuf_sprintf(sb,
|
2013-08-15 07:30:43 +00:00
|
|
|
"Content-range: bytes %"PRIu64"-%"PRIu64"/%"PRIu64"\r\n"
|
2013-08-15 06:17:39 +00:00
|
|
|
"Content-length: %"PRIu64"\r\n",
|
|
|
|
h->content_start, h->content_end, h->content_length, h->content_end - h->content_start);
|
|
|
|
else if (h->content_length)
|
|
|
|
strbuf_sprintf(sb, "Content-length: %"PRIu64"\r\n", h->content_length);
|
2012-07-13 03:01:10 +00:00
|
|
|
strbuf_puts(sb, "\r\n");
|
|
|
|
return sb;
|
|
|
|
}
|
2011-12-22 17:55:18 +00:00
|
|
|
|
2012-09-11 03:39:40 +00:00
|
|
|
int rhizome_server_set_response(rhizome_http_request *r, const struct http_response *h)
|
2012-07-13 03:01:10 +00:00
|
|
|
{
|
2013-09-24 00:20:45 +00:00
|
|
|
r->request_type=0;
|
|
|
|
|
2013-09-24 05:45:32 +00:00
|
|
|
if (config.debug.rhizome_nohttptx)
|
|
|
|
unwatch(&r->alarm);
|
|
|
|
else{
|
|
|
|
/* Switching to writing, so update the call-back */
|
|
|
|
r->alarm.poll.events=POLLOUT;
|
|
|
|
watch(&r->alarm);
|
|
|
|
}
|
|
|
|
|
2012-07-13 03:01:10 +00:00
|
|
|
strbuf b = strbuf_local((char *) r->buffer, r->buffer_size);
|
|
|
|
strbuf_build_http_response(b, h);
|
2013-09-24 00:20:45 +00:00
|
|
|
if (r->buffer == NULL || strbuf_overrun(b) || (h->body && strbuf_remaining(b) < h->content_length)) {
|
2012-07-13 03:01:10 +00:00
|
|
|
// Need a bigger buffer
|
|
|
|
if (r->buffer)
|
|
|
|
free(r->buffer);
|
|
|
|
r->buffer_size = strbuf_count(b) + 1;
|
2013-09-24 00:20:45 +00:00
|
|
|
if (h->body)
|
|
|
|
r->buffer_size += h->content_length;
|
2012-07-13 03:01:10 +00:00
|
|
|
r->buffer = malloc(r->buffer_size);
|
|
|
|
if (r->buffer == NULL) {
|
|
|
|
WHYF_perror("malloc(%u)", r->buffer_size);
|
|
|
|
r->buffer_size = 0;
|
|
|
|
return WHY("Cannot send response, out of memory");
|
|
|
|
}
|
|
|
|
strbuf_init(b, (char *) r->buffer, r->buffer_size);
|
|
|
|
strbuf_build_http_response(b, h);
|
2013-09-24 00:20:45 +00:00
|
|
|
if (strbuf_overrun(b) || (h->body && strbuf_remaining(b) < h->content_length))
|
2012-07-13 03:01:10 +00:00
|
|
|
return WHYF("Bug! Cannot send response, buffer not big enough");
|
|
|
|
}
|
|
|
|
r->buffer_length = strbuf_len(b);
|
2013-09-24 00:20:45 +00:00
|
|
|
if (h->body){
|
|
|
|
bcopy(h->body, strbuf_end(b), h->content_length);
|
|
|
|
r->buffer_length+=h->content_length;
|
|
|
|
}
|
2012-07-13 03:01:10 +00:00
|
|
|
r->buffer_offset = 0;
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2012-09-28 08:23:20 +00:00
|
|
|
DEBUGF("Sending HTTP response: %s", alloca_toprint(160, (const char *)r->buffer, r->buffer_length));
|
2011-12-22 17:55:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_server_simple_http_response(rhizome_http_request *r, int result, const char *response)
|
2012-07-13 03:01:10 +00:00
|
|
|
{
|
|
|
|
struct http_response hr;
|
2013-07-11 03:02:06 +00:00
|
|
|
bzero(&hr, sizeof hr);
|
2012-07-13 03:01:10 +00:00
|
|
|
hr.result_code = result;
|
|
|
|
hr.content_type = "text/html";
|
|
|
|
hr.content_length = strlen(response);
|
|
|
|
hr.body = response;
|
2012-09-05 07:05:08 +00:00
|
|
|
if (result==400) {
|
|
|
|
DEBUGF("Rejecting http request as malformed due to: %s",
|
|
|
|
response);
|
|
|
|
}
|
2012-07-13 03:01:10 +00:00
|
|
|
return rhizome_server_set_response(r, &hr);
|
|
|
|
}
|
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
int rhizome_server_http_response(rhizome_http_request *r, int result,
|
|
|
|
const char *mime_type, const char *body, uint64_t bytes)
|
2012-07-13 03:01:10 +00:00
|
|
|
{
|
|
|
|
struct http_response hr;
|
2013-07-11 03:02:06 +00:00
|
|
|
bzero(&hr, sizeof hr);
|
2012-07-13 03:01:10 +00:00
|
|
|
hr.result_code = result;
|
|
|
|
hr.content_type = mime_type;
|
|
|
|
hr.content_length = bytes;
|
2013-09-24 00:20:45 +00:00
|
|
|
hr.body = body;
|
2012-07-13 03:01:10 +00:00
|
|
|
return rhizome_server_set_response(r, &hr);
|
|
|
|
}
|
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
int rhizome_server_http_response_header(rhizome_http_request *r, int result, const char *mime_type, uint64_t bytes)
|
|
|
|
{
|
|
|
|
return rhizome_server_http_response(r, result, mime_type, NULL, bytes);
|
|
|
|
}
|
|
|
|
|
2012-01-02 22:27:52 +00:00
|
|
|
/*
|
|
|
|
return codes:
|
|
|
|
1: connection still open.
|
|
|
|
0: connection finished.
|
|
|
|
<0: an error occurred.
|
|
|
|
*/
|
2012-08-28 05:02:12 +00:00
|
|
|
int rhizome_server_http_send_bytes(rhizome_http_request *r)
|
2011-12-22 17:55:18 +00:00
|
|
|
{
|
2013-08-25 07:23:26 +00:00
|
|
|
// Don't send anything if disabled for testing HTTP->MDP Rhizome failover
|
2013-09-24 05:45:32 +00:00
|
|
|
if (config.debug.rhizome_nohttptx)
|
2013-08-25 07:23:26 +00:00
|
|
|
return 1;
|
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
// write one block of buffered data
|
|
|
|
if(r->buffer_offset < r->buffer_length){
|
|
|
|
int bytes=r->buffer_length - r->buffer_offset;
|
|
|
|
bytes=write(r->alarm.poll.fd,&r->buffer[r->buffer_offset],bytes);
|
|
|
|
if (bytes<0){
|
|
|
|
// stop writing when the tcp buffer is full
|
|
|
|
// TODO errors?
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r->buffer_offset+=bytes;
|
2012-06-27 07:24:42 +00:00
|
|
|
|
2013-09-24 00:20:45 +00:00
|
|
|
// reset inactivity timer
|
|
|
|
r->alarm.alarm = gettime_ms()+RHIZOME_IDLE_TIMEOUT;
|
|
|
|
r->alarm.deadline = r->alarm.alarm+RHIZOME_IDLE_TIMEOUT;
|
|
|
|
unschedule(&r->alarm);
|
|
|
|
schedule(&r->alarm);
|
|
|
|
|
|
|
|
// allow other alarms to fire and wait for the next POLLOUT
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->buffer_offset=r->buffer_length=0;
|
|
|
|
|
|
|
|
if (r->generator){
|
|
|
|
r->generator(r);
|
2012-06-27 07:24:42 +00:00
|
|
|
}
|
2013-09-24 00:20:45 +00:00
|
|
|
|
|
|
|
// once we've written the whole buffer, and nothing new has been generated, close the connection
|
|
|
|
if (!r->buffer_length){
|
2012-12-11 05:29:46 +00:00
|
|
|
if (config.debug.rhizome_tx)
|
2012-10-08 06:23:52 +00:00
|
|
|
DEBUG("Closing connection, done");
|
|
|
|
return rhizome_server_free_http_request(r);
|
|
|
|
}
|
2012-01-02 22:27:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|