From cb31fe4cc4d8cf4cabe18afdba00c5512fd23eb3 Mon Sep 17 00:00:00 2001
From: Peter Jankuliak
Date: Thu, 14 Dec 2017 14:01:30 +0100
Subject: [PATCH] Only store in cache when OK response is received
This is a temporary workaround for
https://github.com/equalitie/ouinet/issues/3
---
src/injector.cpp | 41 ++++++++++++++++++++++++++++++-----------
1 file changed, 30 insertions(+), 11 deletions(-)
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