Commit 9df22ffc authored by Peter Jankuliak's avatar Peter Jankuliak
Browse files

Client now starts seeding content when it receives it from the injector

Until now, the client only started seeding a content when
it obtained it from the cache.
parent 502f878c
......@@ -429,9 +429,9 @@ static bool contains_private_data(const http::request_header<>& request)
// https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching
//
// TODO: This function is incomplete.
static bool ok_to_cache( const http::request_header<>& request
, const http::response_header<>& response
, const char** reason = nullptr)
bool CacheControl::ok_to_cache( const http::request_header<>& request
, const http::response_header<>& response
, const char** reason)
{
using boost::iequals;
......@@ -524,28 +524,12 @@ static bool ok_to_cache( const http::request_header<>& request
}
//------------------------------------------------------------------------------
void
CacheControl::try_to_cache( const Request& request
, const Response& response) const
Response CacheControl::filter_before_store(Response response)
{
if (!store) return;
const char* reason = "";
if (!ok_to_cache(request, response, &reason)) {
#ifndef NDEBUG
cerr << "::::: CacheControl: NOT CACHING :::::" << endl;
cerr << ":: " << reason << endl;
cerr << request.base() << response.base() << endl;
cerr << ":::::::::::::::::::::::::::::::::::::" << endl;
#endif
return;
}
// TODO: This list was created by going through some 100 responses from
// bbc.com. Careful selection from all possible (standard) fields is
// needed.
auto filtered_response =
return
util::filter_fields( response
, http::field::server
, http::field::retry_after
......@@ -585,7 +569,28 @@ CacheControl::try_to_cache( const Request& request
, "Access-Control-Expose-Headers" // headers of response to be exposed
);
}
//------------------------------------------------------------------------------
void
CacheControl::try_to_cache( const Request& request
, const Response& response) const
{
if (!store) return;
const char* reason = "";
if (!ok_to_cache(request, response, &reason)) {
#ifndef NDEBUG
cerr << "::::: CacheControl: NOT CACHING :::::" << endl;
cerr << ":: " << reason << endl;
cerr << request.base() << response.base() << endl;
cerr << ":::::::::::::::::::::::::::::::::::::" << endl;
#endif
return;
}
// TODO: Apply similar filter to the request.
store(request, filtered_response);
store(request, filter_before_store(response));
}
......@@ -37,6 +37,12 @@ public:
// Returns ptime() if parsing fails.
static boost::posix_time::ptime parse_date(beast::string_view);
static bool ok_to_cache( const http::request_header<>& request
, const http::response_header<>& response
, const char** reason = nullptr);
static Response filter_before_store(Response);
private:
// TODO: Add cancellation support
Response do_fetch(const Request&, asio::yield_context);
......
......@@ -110,6 +110,9 @@ private:
return _config.repo_root()/OUINET_PID_FILE;
}
string maybe_start_seeding( const Request&
, const Response&
, asio::yield_context);
private:
asio::io_service& _ios;
......@@ -130,6 +133,29 @@ private:
unique_ptr<util::PidFile> _pid_file;
};
//------------------------------------------------------------------------------
string Client::State::maybe_start_seeding( const Request& req
, const Response& res
, asio::yield_context yield)
{
if (!_ipfs_cache)
return or_throw<string>(yield, asio::error::operation_not_supported);
const char* reason = "";
if (!CacheControl::ok_to_cache(req, res, &reason)) {
cerr << "---------------------------------------" << endl;
cerr << "Not caching " << req.target() << endl;
cerr << "Because: \"" << reason << "\"" << endl;
cerr << req.base() << res.base();
cerr << "---------------------------------------" << endl;
return {};
}
return _ipfs_cache->ipfs_add
( util::str(CacheControl::filter_before_store(res))
, yield);
}
//------------------------------------------------------------------------------
static
void handle_bad_request( GenericConnection& con
......@@ -319,9 +345,12 @@ Response Client::State::fetch_fresh( const Request& request
, yield[ec]);
}
if (!ec) return res;
last_error = ec;
continue;
if (ec) { last_error = ec; continue; }
sys::error_code ec_;
string ipfs = maybe_start_seeding(request, res, yield[ec_]);
return res;
}
case responder::_front_end: {
return _front_end.serve( _config.injector_endpoint()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment