Skip to content

Commit

Permalink
Only store in cache when OK response is received
Browse files Browse the repository at this point in the history
This is a temporary workaround for
#3
  • Loading branch information
inetic committed Dec 14, 2017
1 parent 18bad04 commit cb31fe4
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ static bool ok_to_cache(const http::response_header<Fields>& hdr)
});
}

//------------------------------------------------------------------------------
template<class Req, class Res>
static void try_to_cache( ipfs_cache::Injector& injector
, const Req& req
, const Res& res)
{
if (!ok_to_cache(res)) {
return;
}

stringstream ss;
ss << res;
auto key = req.target().to_string();

injector.insert_content(key, ss.str(),
[key] (sys::error_code ec, auto) {
if (ec) {
cout << "!Insert failed: " << key << " " << ec.message() << endl;
}
});
}

//------------------------------------------------------------------------------
static
void serve( shared_ptr<GenericConnection> con
Expand All @@ -112,17 +134,14 @@ void serve( shared_ptr<GenericConnection> con
if (ec == http::error::end_of_stream) break;
if (ec) return fail(ec, "fetch_http_page");

if (ok_to_cache(res)) {
stringstream ss;
ss << res;
auto key = req.target().to_string();

injector.insert_content(key, ss.str(),
[key] (sys::error_code ec, auto) {
if (ec) {
cout << "!Insert failed: " << key << " " << ec.message() << endl;
}
});
switch (res.result()) {
case http::status::ok:
try_to_cache(injector, req, res);
break;
// TODO: Other response codes
default:
cerr << "Warning: not handling: " << res.result()
<< " HTTP response in terms of cache" << endl;
}

// Forward back the response
Expand Down

0 comments on commit cb31fe4

Please sign in to comment.