Debian Codegraph @

src/core/RedirectionResolver.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2019
4 * Author: Georgios Bitzes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "RedirectionResolver.hpp"
23 #include <utils/davix_logger_internal.hpp>
24
25
26 using namespace Davix;
27
28 static const std::pair<std::string, std::string> makeKey(const std::string & method, const Uri & origin){
29 std::string mymethod = method;
30 // cache HEAD and GET on same key
31 if(mymethod == "HEAD")
32 mymethod = "GET";
33
34 return {origin.getString(), mymethod};
35 }
36
37 RedirectionResolver::RedirectionResolver(bool act) : active(act), redirCache(256) {
38 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Redirection Session caching {}", (active?"ENABLED":"DISABLED"));
39 }
40
41 // add cached redirection
42 void RedirectionResolver::addRedirection(const std::string & method, const Uri & origin, std::shared_ptr<Uri> dest) {
43 if(!active) {
44 return;
45 }
46
47 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Add cached redirection <{} {} {}>", method.c_str(), origin.getString().c_str(), dest->getString().c_str());
48 redirCache.insert(makeKey(method, origin), dest);
49 }
50
51 // try to find cached redirection, resolve a full chain
52 std::shared_ptr<Uri> RedirectionResolver::redirectionResolve(const std::string & method, const Uri & origin) {
53 std::set<redirectionKey> visited;
54 return redirectionResolve(method, origin, visited);
55 }
56
57 // private function of redirectionResolve (contains set of visited URIs to avoid redirection loop)
58 std::shared_ptr<Uri> RedirectionResolver::redirectionResolve(const std::string& method, const Uri& origin, std::set<redirectionKey>& visited) {
59 auto res = resolveSingle(method, origin);
60
61 if (res != nullptr) {
62 // Identified a loop, stop at the element before the resolved redirection
63 if (visited.find(makeKey(method, *res)) != visited.end()) {
64 return std::make_shared<Uri>(origin);
65 } else {
66 // We haven't seen this resolved redirection before
67 visited.insert(makeKey(method, origin));
68 auto res_rec = redirectionResolve(method, *res, visited);
69
70 if (res_rec != nullptr) {
71 return res_rec;
72 }
73 }
74 }
75
76 return res;
77 }
78
79 // resolve a single redirection chunk
80 std::shared_ptr<Uri> RedirectionResolver::resolveSingle(const std::string & method, const Uri & origin) {
81 std::shared_ptr<Uri> res = redirCache.find(makeKey(method, origin));
82
83 if(res.get() != NULL){
84 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Found redirection <{} {} {}>", method.c_str(), origin.getString().c_str(), res->getString().c_str());
85 }
86 return res;
87 }
88
89 // check if redirections are active
90 bool RedirectionResolver::isActive() const {
91 return active;
92 }
93
94 void RedirectionResolver::redirectionClean(const std::string & method, const Uri & origin) {
95 std::shared_ptr<Uri> res = redirCache.find(makeKey(method, origin));
96 if(res.get() != NULL){
97 DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_HTTP, "Delete Cached redirection for <{} {} {}>", method.c_str(), origin.getString().c_str(), res->getString().c_str());
98 redirCache.erase(makeKey(method, origin));
99 redirectionClean(method, *res);
100 }
101 }
102
103 void RedirectionResolver::redirectionClean(const Uri & origin) {
104 std::pair<std::string, std::string> query = std::make_pair(origin.getString(), "");
105 while(true) {
106 const std::pair<std::string, std::string> nextkey = redirCache.upper_bound(query);
107 if(nextkey.first != origin.getString()) {
108 break;
109 }
110
111 redirectionClean(nextkey.second, nextkey.first);
112 }
113 }