annotate Core/HttpClient.cpp @ 2111:64d0224595cc

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