2011-12-21 09:55:05 +00:00
|
|
|
/*
|
2013-11-12 01:10:47 +00:00
|
|
|
Serval DNA Rhizome HTTP external interface
|
2014-02-03 12:35:15 +00:00
|
|
|
Copyright (C) 2013,2014 Serval Project Inc.
|
2011-12-21 09:55:05 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-12-11 05:29:46 +00:00
|
|
|
#include "conf.h"
|
2014-02-03 12:35:15 +00:00
|
|
|
#include "serval.h"
|
|
|
|
#include "httpd.h"
|
2012-08-06 02:55:45 +00:00
|
|
|
#include "str.h"
|
2013-12-30 07:39:56 +00:00
|
|
|
#include "strbuf.h"
|
2013-09-24 00:20:45 +00:00
|
|
|
|
2014-01-31 07:32:41 +00:00
|
|
|
int rhizome_file_page(httpd_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
/* Stream the specified payload */
|
|
|
|
if (!is_rhizome_http_enabled())
|
2013-12-16 01:51:45 +00:00
|
|
|
return 403;
|
|
|
|
if (r->http.verb != HTTP_VERB_GET)
|
|
|
|
return 405;
|
2013-10-16 07:55:58 +00:00
|
|
|
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");
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2013-10-16 07:55:58 +00:00
|
|
|
}
|
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-12-16 02:02:16 +00:00
|
|
|
int ret = rhizome_response_content_init_filehash(r, &filehash);
|
2013-12-13 06:06:37 +00:00
|
|
|
if (ret)
|
2013-12-16 01:51:45 +00:00
|
|
|
return ret;
|
2014-02-07 05:58:40 +00:00
|
|
|
http_request_response_generated(&r->http, 200, CONTENT_TYPE_BLOB, rhizome_payload_content);
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 07:32:41 +00:00
|
|
|
int manifest_by_prefix_page(httpd_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
|
|
|
if (!is_rhizome_http_enabled())
|
2013-12-16 01:51:45 +00:00
|
|
|
return 403;
|
|
|
|
if (r->http.verb != HTTP_VERB_GET)
|
|
|
|
return 405;
|
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)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404; // not found
|
2013-12-30 07:39:56 +00:00
|
|
|
if ((r->manifest = rhizome_new_manifest()) == NULL)
|
|
|
|
return 500;
|
|
|
|
int ret = rhizome_retrieve_manifest_by_prefix(prefix.binary, prefix_len, r->manifest);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (ret == -1)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 500;
|
|
|
|
if (ret == 0) {
|
2014-02-07 05:58:40 +00:00
|
|
|
http_request_response_static(&r->http, 200, CONTENT_TYPE_BLOB, (const char *)r->manifest->manifestdata, r->manifest->manifest_all_bytes);
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 404;
|
2013-09-24 00:20:45 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 07:32:41 +00:00
|
|
|
int rhizome_status_page(httpd_request *r, const char *remainder)
|
2013-09-24 00:20:45 +00:00
|
|
|
{
|
2014-01-31 07:32:41 +00:00
|
|
|
if (!is_rhizome_http_enabled())
|
|
|
|
return 403;
|
2013-09-24 00:20:45 +00:00
|
|
|
if (*remainder)
|
2013-12-16 01:51:45 +00:00
|
|
|
return 404;
|
|
|
|
if (r->http.verb != HTTP_VERB_GET)
|
|
|
|
return 405;
|
2014-01-31 07:32:41 +00:00
|
|
|
char buf[32*1024];
|
|
|
|
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>", httpd_request_count);
|
|
|
|
strbuf_sprintf(b, "%d Bundles transferring via MDP<br>", rhizome_cache_count());
|
|
|
|
rhizome_fetch_status_html(b);
|
2013-09-24 00:20:45 +00:00
|
|
|
strbuf_puts(b, "</body></html>");
|
2014-01-31 07:32:41 +00:00
|
|
|
if (strbuf_overrun(b))
|
|
|
|
return -1;
|
2014-02-07 05:58:40 +00:00
|
|
|
http_request_response_static(&r->http, 200, CONTENT_TYPE_HTML, buf, strbuf_len(b));
|
2013-12-16 01:51:45 +00:00
|
|
|
return 1;
|
2011-12-22 17:55:18 +00:00
|
|
|
}
|