Debian Codegraph @

src/curl/CurlSessionFactory.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 "CurlSessionFactory.hpp"
23 #include "CurlSession.hpp"
24 #include <backend/SessionFactory.hpp>
25 #include <curl/curl.h>
26
27 #define DBG(message) std::cerr << __FILE__ << ":" << __LINE__ << " -- " << #message << " = " << message << std::endl;
28
29 namespace Davix {
30
31 //------------------------------------------------------------------------------
32 // Check if session caching is disabled from environment variables
33 //------------------------------------------------------------------------------
34 static bool isSessionCachingDisabled(){
35 return ( getenv("DAVIX_DISABLE_SESSION_CACHING") != NULL);
36 }
37
38 //------------------------------------------------------------------------------
39 // Constructor
40 //------------------------------------------------------------------------------
41 CurlSessionFactory::CurlSessionFactory() : _session_caching(!isSessionCachingDisabled()) {}
42
43 //------------------------------------------------------------------------------
44 // Destructor
45 //------------------------------------------------------------------------------
46 CurlSessionFactory::~CurlSessionFactory() {}
47
48 //------------------------------------------------------------------------------
49 // Create a CurlSession tied to this class.
50 //------------------------------------------------------------------------------
51 std::unique_ptr<CurlSession> CurlSessionFactory::provideCurlSession(const Uri &uri, const RequestParams &params, Status &st) {
52 CurlHandlePtr handle = getCachedHandle(uri, params);
53
54 if(!handle) {
55 handle = makeNewHandle(uri, params);
56 }
57
58 std::unique_ptr<CurlSession> out(new CurlSession(*this, handle, uri, params, st));
59 if(st.ok()) {
60 return out;
61 }
62
63 out.reset();
64 handle.reset();
65
66 handle = makeNewHandle(uri, params);
67 out.reset(new CurlSession(*this, handle, uri, params, st));
68 return out;
69 }
70
71 //------------------------------------------------------------------------------
72 // Set caching on or off
73 //------------------------------------------------------------------------------
74 void CurlSessionFactory::setSessionCaching(bool caching) {
75 std::lock_guard<std::mutex> lock(_session_caching_mtx);
76 _session_caching = caching && !isSessionCachingDisabled();
77 }
78
79 //------------------------------------------------------------------------------
80 // Get caching status
81 //------------------------------------------------------------------------------
82 bool CurlSessionFactory::getSessionCaching() const {
83 std::lock_guard<std::mutex> lock(_session_caching_mtx);
84 return _session_caching;
85 }
86
87 //------------------------------------------------------------------------------
88 // Retrieve cached handle, if possible
89 //------------------------------------------------------------------------------
90 CurlHandlePtr CurlSessionFactory::getCachedHandle(const Uri &uri, const RequestParams &params) {
91 CurlHandlePtr out;
92 std::string sessionKey = SessionFactory::makeSessionKey(uri);
93
94 if(_session_pool.retrieve(sessionKey, out)) {
95 out->renewHandle();
96 }
97
98 return out;
99 }
100
101 //------------------------------------------------------------------------------
102 // Provide brand-new handle
103 //------------------------------------------------------------------------------
104 CurlHandlePtr CurlSessionFactory::makeNewHandle(const Uri &uri, const RequestParams &params) {
105 std::string sessionKey = SessionFactory::makeSessionKey(uri);
106
107 CURL *handle = curl_easy_init();
108 CURLM *mhandle = curl_multi_init();
109
110 return CurlHandlePtr(new CurlHandle(sessionKey, mhandle, handle));
111 }
112
113 }