diff --git a/src/injector.cpp b/src/injector.cpp index 21adf340..ebea5907 100644 --- a/src/injector.cpp +++ b/src/injector.cpp @@ -89,6 +89,28 @@ static bool ok_to_cache(const http::response_header& hdr) }); } +//------------------------------------------------------------------------------ +template +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 con @@ -112,17 +134,14 @@ void serve( shared_ptr 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