Debian Codegraph @

src/backend/SessionFactory.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 "SessionFactory.hpp"
23 #include <stdlib.h>
24 #include <neon/neonsessionfactory.hpp>
25 #include <curl/CurlSessionFactory.hpp>
26
27 #define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str()
28
29 namespace Davix {
30
31 //------------------------------------------------------------------------------
32 // Constructor
33 //------------------------------------------------------------------------------
34 SessionFactory::SessionFactory() {
35 _neon_factory.reset(new NEONSessionFactory());
36 _curl_factory.reset(new CurlSessionFactory());
37 }
38
39 //------------------------------------------------------------------------------
40 // Destructor
41 //------------------------------------------------------------------------------
42 SessionFactory::~SessionFactory() {}
43
44 //------------------------------------------------------------------------------
45 // Get neon session factory
46 //------------------------------------------------------------------------------
47 NEONSessionFactory& SessionFactory::getNeon() {
48 return *(_neon_factory.get());
49 }
50
51 //------------------------------------------------------------------------------
52 // Get curl session factory
53 //------------------------------------------------------------------------------
54 CurlSessionFactory& SessionFactory::getCurl() {
55 return *(_curl_factory.get());
56 }
57
58 //------------------------------------------------------------------------------
59 // Set caching on or off
60 //------------------------------------------------------------------------------
61 void SessionFactory::setSessionCaching(bool caching) {
62 _neon_factory->setSessionCaching(caching);
63 }
64
65 //------------------------------------------------------------------------------
66 // Get caching status
67 //------------------------------------------------------------------------------
68 bool SessionFactory::getSessionCaching() const {
69 return _neon_factory->getSessionCaching();
70 }
71
72 //------------------------------------------------------------------------------
73 // "httpize" protocol
74 //------------------------------------------------------------------------------
75 std::string SessionFactory::httpizeProtocol(const std::string &protocol) {
76 std::string proto = protocol;
77 if(proto.compare(0,4, "http") == 0 ||
78 proto.compare(0,2, "s3") == 0 ||
79 proto.compare(0,3, "dav") == 0 ||
80 proto.compare(0, 6, "gcloud") == 0 ||
81 proto.compare(0, 5, "swift") == 0 ||
82 proto.compare(0, 3, "cs3") == 0){
83
84 proto.assign("http");
85
86 if(*(protocol.rbegin()) == 's') {
87 proto.append("s");
88 }
89 }
90
91 return proto;
92 }
93
94 //------------------------------------------------------------------------------
95 // Create session key based on Uri
96 //------------------------------------------------------------------------------
97 std::string SessionFactory::makeSessionKey(const Uri &uri) {
98 return SSTR(httpizeProtocol(uri.getProtocol()) << uri.getHost() << ":" << uri.getPort());
99 }
100
101
102 }