annotate Resources/Orthanc/Core/HttpClient.cpp @ 30:1099a148f7e5

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 14 Dec 2016 17:05:41 +0100
parents ff1e935768e7
children 7207a407bcd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PrecompiledHeaders.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "HttpClient.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "Toolbox.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "OrthancException.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "Logging.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include "ChunkedBuffer.h"
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
40 #include "SystemToolbox.h"
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include <string.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include <curl/curl.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include <boost/algorithm/string/predicate.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 #include <boost/thread/mutex.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
48 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 // For OpenSSL initialization and finalization
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 # include <openssl/conf.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 # include <openssl/engine.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 # include <openssl/err.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 # include <openssl/evp.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 # include <openssl/ssl.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
58 #if ORTHANC_ENABLE_PKCS11 == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 # include "Pkcs11.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 extern "C"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 static CURLcode GetHttpStatus(CURLcode code, CURL* curl, long* status)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if (code == CURLE_OK)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, status);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 return code;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 *status = 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 return code;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 // This is a dummy wrapper function to suppress any OpenSSL-related
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 // problem in valgrind. Inlining is prevented.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 #if defined(__GNUC__) || defined(__clang__)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 __attribute__((noinline))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 return GetHttpStatus(curl_easy_perform(curl), curl, status);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 namespace Orthanc
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 class HttpClient::GlobalParameters
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 private:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 boost::mutex mutex_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 bool httpsVerifyPeers_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 std::string httpsCACertificates_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 std::string proxy_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 long timeout_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 GlobalParameters() :
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 httpsVerifyPeers_(true),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 timeout_(0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 public:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 // Singleton pattern
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 static GlobalParameters& GetInstance()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 static GlobalParameters parameters;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 return parameters;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 void ConfigureSsl(bool httpsVerifyPeers,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 const std::string& httpsCACertificates)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 httpsVerifyPeers_ = httpsVerifyPeers;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 httpsCACertificates_ = httpsCACertificates;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 void GetSslConfiguration(bool& httpsVerifyPeers,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 std::string& httpsCACertificates)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 httpsVerifyPeers = httpsVerifyPeers_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 httpsCACertificates = httpsCACertificates_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 void SetDefaultProxy(const std::string& proxy)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 LOG(INFO) << "Setting the default proxy for HTTP client connections: " << proxy;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 proxy_ = proxy;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 void GetDefaultProxy(std::string& target)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 target = proxy_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 void SetDefaultTimeout(long seconds)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 LOG(INFO) << "Setting the default timeout for HTTP client connections: " << seconds << " seconds";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 timeout_ = seconds;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 long GetDefaultTimeout()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 return timeout_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
165 #if ORTHANC_ENABLE_PKCS11 == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 bool IsPkcs11Initialized()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 return Pkcs11::IsInitialized();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 void InitializePkcs11(const std::string& module,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 const std::string& pin,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 bool verbose)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 boost::mutex::scoped_lock lock(mutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 Pkcs11::Initialize(module, pin, verbose);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 };
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 struct HttpClient::PImpl
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 CURL* curl_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 struct curl_slist *defaultPostHeaders_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 struct curl_slist *userHeaders_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 };
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 static void ThrowException(HttpStatus status)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 switch (status)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 case HttpStatus_400_BadRequest:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 throw OrthancException(ErrorCode_BadRequest);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 case HttpStatus_401_Unauthorized:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 case HttpStatus_403_Forbidden:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 throw OrthancException(ErrorCode_Unauthorized);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 case HttpStatus_404_NotFound:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 throw OrthancException(ErrorCode_UnknownResource);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 throw OrthancException(ErrorCode_NetworkProtocol);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 static CURLcode CheckCode(CURLcode code)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 if (code == CURLE_NOT_BUILT_IN)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 LOG(ERROR) << "Your libcurl does not contain a required feature, "
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 << "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 throw OrthancException(ErrorCode_InternalError);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 if (code != CURLE_OK)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 LOG(ERROR) << "libCURL error: " + std::string(curl_easy_strerror(code));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 throw OrthancException(ErrorCode_NetworkProtocol);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 return code;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 static size_t CurlBodyCallback(void *buffer, size_t size, size_t nmemb, void *payload)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 ChunkedBuffer& target = *(static_cast<ChunkedBuffer*>(payload));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 size_t length = size * nmemb;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 if (length == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 return 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 target.AddChunk(buffer, length);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 return length;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 struct CurlHeaderParameters
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 bool lowerCase_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 HttpClient::HttpHeaders* headers_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 };
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 static size_t CurlHeaderCallback(void *buffer, size_t size, size_t nmemb, void *payload)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 CurlHeaderParameters& parameters = *(static_cast<CurlHeaderParameters*>(payload));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 assert(parameters.headers_ != NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 size_t length = size * nmemb;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 if (length == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 return 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 std::string s(reinterpret_cast<const char*>(buffer), length);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 std::size_t colon = s.find(':');
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 std::size_t eol = s.find("\r\n");
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 if (colon != std::string::npos &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 eol != std::string::npos)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 std::string tmp(s.substr(0, colon));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 if (parameters.lowerCase_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 Toolbox::ToLowerCase(tmp);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 std::string key = Toolbox::StripSpaces(tmp);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 if (!key.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 std::string value = Toolbox::StripSpaces(s.substr(colon + 1, eol));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 (*parameters.headers_) [key] = value;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 return length;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 void HttpClient::Setup()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 pimpl_->userHeaders_ = NULL;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 pimpl_->defaultPostHeaders_ = NULL;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 if ((pimpl_->defaultPostHeaders_ = curl_slist_append(pimpl_->defaultPostHeaders_, "Expect:")) == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 throw OrthancException(ErrorCode_NotEnoughMemory);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 pimpl_->curl_ = curl_easy_init();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 if (!pimpl_->curl_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 curl_slist_free_all(pimpl_->defaultPostHeaders_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 throw OrthancException(ErrorCode_NotEnoughMemory);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlBodyCallback));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 // This fixes the "longjmp causes uninitialized stack frame" crash
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 // that happens on modern Linux versions.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 url_ = "";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 method_ = HttpMethod_Get;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 lastStatus_ = HttpStatus_200_Ok;
30
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
321 SetVerbose(false);
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 timeout_ = GlobalParameters::GetInstance().GetDefaultTimeout();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 GlobalParameters::GetInstance().GetDefaultProxy(proxy_);
30
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
324 GlobalParameters::GetInstance().GetSslConfiguration(verifyPeers_, caCertificates_);
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 HttpClient::HttpClient() :
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 pimpl_(new PImpl),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 verifyPeers_(true),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 pkcs11Enabled_(false),
11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
332 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
333 redirectionFollowed_(true)
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 Setup();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 HttpClient::HttpClient(const WebServiceParameters& service,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 const std::string& uri) :
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 pimpl_(new PImpl),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 verifyPeers_(true),
11
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
343 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
344 redirectionFollowed_(true)
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 Setup();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 if (service.GetUsername().size() != 0 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 service.GetPassword().size() != 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 SetCredentials(service.GetUsername().c_str(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 service.GetPassword().c_str());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 if (!service.GetCertificateFile().empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 SetClientCertificate(service.GetCertificateFile(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 service.GetCertificateKeyFile(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 service.GetCertificateKeyPassword());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 SetPkcs11Enabled(service.IsPkcs11Enabled());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 SetUrl(service.GetUrl() + uri);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 HttpClient::~HttpClient()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 curl_easy_cleanup(pimpl_->curl_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 curl_slist_free_all(pimpl_->defaultPostHeaders_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 ClearHeaders();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 void HttpClient::SetVerbose(bool isVerbose)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 isVerbose_ = isVerbose;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 if (isVerbose_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 void HttpClient::AddHeader(const std::string& key,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 const std::string& value)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 if (key.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 throw OrthancException(ErrorCode_ParameterOutOfRange);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 std::string s = key + ": " + value;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 if ((pimpl_->userHeaders_ = curl_slist_append(pimpl_->userHeaders_, s.c_str())) == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 throw OrthancException(ErrorCode_NotEnoughMemory);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 void HttpClient::ClearHeaders()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 if (pimpl_->userHeaders_ != NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 curl_slist_free_all(pimpl_->userHeaders_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 pimpl_->userHeaders_ = NULL;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 bool HttpClient::ApplyInternal(std::string& answerBody,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 HttpHeaders* answerHeaders)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 answerBody.clear();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 CurlHeaderParameters headerParameters;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 if (answerHeaders == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERFUNCTION, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERDATA, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 headerParameters.lowerCase_ = headersToLowerCase_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 headerParameters.headers_ = answerHeaders;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERFUNCTION, &CurlHeaderCallback));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERDATA, &headerParameters));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
439 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 // Setup HTTPS-related options
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 if (verifyPeers_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CAINFO, caCertificates_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYHOST, 2)); // libcurl default is strict verifyhost
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 1));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYHOST, 0));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 // Setup the HTTPS client certificate
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 if (!clientCertificateFile_.empty() &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 pkcs11Enabled_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 LOG(ERROR) << "Cannot enable both client certificates and PKCS#11 authentication";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 throw OrthancException(ErrorCode_ParameterOutOfRange);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 if (pkcs11Enabled_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
465 #if ORTHANC_ENABLE_PKCS11 == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 if (GlobalParameters::GetInstance().IsPkcs11Initialized())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLENGINE, Pkcs11::GetEngineIdentifier()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "ENG"));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERTTYPE, "ENG"));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 LOG(ERROR) << "Cannot use PKCS#11 for a HTTPS request, because it has not been initialized";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 LOG(ERROR) << "This version of Orthanc is compiled without support for PKCS#11";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 throw OrthancException(ErrorCode_InternalError);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 else if (!clientCertificateFile_.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
484 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERTTYPE, "PEM"));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERT, clientCertificateFile_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 if (!clientCertificateKeyPassword_.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_KEYPASSWD, clientCertificateKeyPassword_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 // NB: If no "clientKeyFile_" is provided, the key must be
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 // prepended to the certificate file
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 if (!clientCertificateKeyFile_.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEYTYPE, "PEM"));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLKEY, clientCertificateKeyFile_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 LOG(ERROR) << "This version of Orthanc is compiled without OpenSSL support, cannot use HTTPS client authentication";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 throw OrthancException(ErrorCode_InternalError);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 // Reset the parameters from previous calls to Apply()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->userHeaders_));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 if (redirectionFollowed_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 0L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 // Set timeouts
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 if (timeout_ <= 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, 10)); /* default: 10 seconds */
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, 10)); /* default: 10 seconds */
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, timeout_));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, timeout_));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 if (credentials_.size() != 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_USERPWD, credentials_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 if (proxy_.size() != 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, proxy_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547 switch (method_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 case HttpMethod_Get:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 case HttpMethod_Post:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 if (pimpl_->userHeaders_ == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563 case HttpMethod_Delete:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568 case HttpMethod_Put:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569 // http://stackoverflow.com/a/7570281/881731: Don't use
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 // CURLOPT_PUT if there is a body
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 if (pimpl_->userHeaders_ == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
579 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
580
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
581 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584 throw OrthancException(ErrorCode_InternalError);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 if (method_ == HttpMethod_Post ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 method_ == HttpMethod_Put)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 if (body_.size() > 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
595 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
596 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
597 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
598 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
599 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
600 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
601 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
602
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
603
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
604 // Do the actual request
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 CURLcode code;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 long status = 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 ChunkedBuffer buffer;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &buffer));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611 if (boost::starts_with(url_, "https://"))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
613 code = OrthancHttpClientPerformSSL(pimpl_->curl_, &status);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
615 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
616 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
617 code = GetHttpStatus(curl_easy_perform(pimpl_->curl_), pimpl_->curl_, &status);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620 CheckCode(code);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
622 if (status == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624 // This corresponds to a call to an inexistent host
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 lastStatus_ = HttpStatus_500_InternalServerError;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629 lastStatus_ = static_cast<HttpStatus>(status);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 bool success = (status >= 200 && status < 300);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 if (success)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 buffer.Flatten(answerBody);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 answerBody.clear();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 LOG(INFO) << "Error in HTTP request, received HTTP status " << status
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 << " (" << EnumerationToString(lastStatus_) << ")";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645 return success;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 bool HttpClient::ApplyInternal(Json::Value& answerBody,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650 HttpClient::HttpHeaders* answerHeaders)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 std::string s;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653 if (ApplyInternal(s, answerHeaders))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 Json::Reader reader;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656 return reader.parse(s, answerBody);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
658 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660 return false;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
664
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 void HttpClient::SetCredentials(const char* username,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
666 const char* password)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668 credentials_ = std::string(username) + ":" + std::string(password);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
669 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
671
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
672 void HttpClient::ConfigureSsl(bool httpsVerifyPeers,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
673 const std::string& httpsVerifyCertificates)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
675 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676 if (httpsVerifyPeers)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678 if (httpsVerifyCertificates.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
679 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
680 LOG(WARNING) << "No certificates are provided to validate peers, "
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
681 << "set \"HttpsCACertificates\" if you need to do HTTPS requests";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
682 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
683 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
684 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
685 LOG(WARNING) << "HTTPS will use the CA certificates from this file: " << httpsVerifyCertificates;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
686 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
687 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
688 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
689 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
690 LOG(WARNING) << "The verification of the peers in HTTPS requests is disabled";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
691 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
692 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
693
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
694 GlobalParameters::GetInstance().ConfigureSsl(httpsVerifyPeers, httpsVerifyCertificates);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
695 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
696
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
697
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
698 void HttpClient::GlobalInitialize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
699 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
700 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
701 CheckCode(curl_global_init(CURL_GLOBAL_ALL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
702 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
703 CheckCode(curl_global_init(CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
704 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
705 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
706
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
707
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
708 void HttpClient::GlobalFinalize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
709 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
710 curl_global_cleanup();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
711
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
712 #if ORTHANC_ENABLE_PKCS11 == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
713 Pkcs11::Finalize();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
714 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
715 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
716
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
717
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
718 void HttpClient::SetDefaultProxy(const std::string& proxy)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
719 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
720 GlobalParameters::GetInstance().SetDefaultProxy(proxy);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
721 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
722
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
723
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
724 void HttpClient::SetDefaultTimeout(long timeout)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
725 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
726 GlobalParameters::GetInstance().SetDefaultTimeout(timeout);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
727 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
728
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
729
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
730 void HttpClient::ApplyAndThrowException(std::string& answerBody)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
731 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
732 if (!Apply(answerBody))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
733 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
734 ThrowException(GetLastStatus());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
735 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
736 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
737
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
738
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
739 void HttpClient::ApplyAndThrowException(Json::Value& answerBody)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
740 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
741 if (!Apply(answerBody))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
742 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
743 ThrowException(GetLastStatus());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
744 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
745 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
746
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
747
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
748 void HttpClient::ApplyAndThrowException(std::string& answerBody,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
749 HttpHeaders& answerHeaders)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
750 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
751 if (!Apply(answerBody, answerHeaders))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
752 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
753 ThrowException(GetLastStatus());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
754 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
755 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
756
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
757
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
758 void HttpClient::ApplyAndThrowException(Json::Value& answerBody,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
759 HttpHeaders& answerHeaders)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
760 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
761 if (!Apply(answerBody, answerHeaders))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
762 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
763 ThrowException(GetLastStatus());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
764 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
765 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
766
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
767
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
768 void HttpClient::SetClientCertificate(const std::string& certificateFile,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
769 const std::string& certificateKeyFile,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
770 const std::string& certificateKeyPassword)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
771 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
772 if (certificateFile.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
773 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
774 throw OrthancException(ErrorCode_ParameterOutOfRange);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
775 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
776
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
777 if (!SystemToolbox::IsRegularFile(certificateFile))
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
778 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
779 LOG(ERROR) << "Cannot open certificate file: " << certificateFile;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
780 throw OrthancException(ErrorCode_InexistentFile);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
781 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
782
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
783 if (!certificateKeyFile.empty() &&
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
784 !SystemToolbox::IsRegularFile(certificateKeyFile))
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
785 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
786 LOG(ERROR) << "Cannot open key file: " << certificateKeyFile;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
787 throw OrthancException(ErrorCode_InexistentFile);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
788 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
789
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
790 clientCertificateFile_ = certificateFile;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
791 clientCertificateKeyFile_ = certificateKeyFile;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
792 clientCertificateKeyPassword_ = certificateKeyPassword;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
793 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
794
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
795
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
796 void HttpClient::InitializePkcs11(const std::string& module,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
797 const std::string& pin,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
798 bool verbose)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
799 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
800 #if ORTHANC_ENABLE_PKCS11 == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
801 LOG(INFO) << "Initializing PKCS#11 using " << module
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
802 << (pin.empty() ? " (no PIN provided)" : " (PIN is provided)");
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
803 GlobalParameters::GetInstance().InitializePkcs11(module, pin, verbose);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
804 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
805 LOG(ERROR) << "This version of Orthanc is compiled without support for PKCS#11";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
806 throw OrthancException(ErrorCode_InternalError);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
807 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
808 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
809
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
810
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
811 void HttpClient::InitializeOpenSsl()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
812 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
813 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
814 // https://wiki.openssl.org/index.php/Library_Initialization
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
815 SSL_library_init();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
816 SSL_load_error_strings();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
817 OpenSSL_add_all_algorithms();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
818 ERR_load_crypto_strings();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
819 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
820 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
821
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
822
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
823 void HttpClient::FinalizeOpenSsl()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
824 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 11
diff changeset
825 #if ORTHANC_ENABLE_SSL == 1
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
826 // Finalize OpenSSL
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
827 // https://wiki.openssl.org/index.php/Library_Initialization#Cleanup
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
828 FIPS_mode_set(0);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829 ENGINE_cleanup();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
830 CONF_modules_unload(1);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
831 EVP_cleanup();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
832 CRYPTO_cleanup_all_ex_data();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
833 ERR_remove_state(0);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
834 ERR_free_strings();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
835 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
836 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
837 }