annotate Core/HttpClient.cpp @ 3386:af9432e46c07

HttpClient::IBodyStream
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Jun 2019 14:40:14 +0200
parents 974e12006b7d
children a48d652f1500
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
3060
4e43e67f8ecf preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
7 * 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
8 * 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
9 * 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
10 * License, or (at your option) any later version.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
12 * 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
13 * 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
14 * 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
15 * 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
16 * 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
17 * 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
18 * 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
19 * 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
20 * 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
21 * 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
22 * in the program, then also delete it here.
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
23 *
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
24 * 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
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
27 * General Public License for more details.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
29 * 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
30 * 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
31 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
34 #include "PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "HttpClient.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
1404
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
37 #include "Toolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
38 #include "OrthancException.h"
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
39 #include "Logging.h"
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
40 #include "ChunkedBuffer.h"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2140
diff changeset
41 #include "SystemToolbox.h"
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
42
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include <curl/curl.h>
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
45 #include <boost/algorithm/string/predicate.hpp>
1989
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1987
diff changeset
46 #include <boost/thread/mutex.hpp>
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
47
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
48
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
49 #if ORTHANC_ENABLE_PKCS11 == 1
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
50 # 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
51 #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
52
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
53
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
54 extern "C"
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
55 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
56 static CURLcode GetHttpStatus(CURLcode code, CURL* curl, long* status)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
57 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
58 if (code == CURLE_OK)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
59 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
60 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
61 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
62 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
63 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
64 {
3122
df4f977c2f88 trying to reproduce isse 32 in C++
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3114
diff changeset
65 LOG(ERROR) << "Error code " << static_cast<int>(code)
df4f977c2f88 trying to reproduce isse 32 in C++
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3114
diff changeset
66 << " in libcurl: " << curl_easy_strerror(code);
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
67 *status = 0;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
68 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
69 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
70 }
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
71 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
72
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
73 // This is a dummy wrapper function to suppress any OpenSSL-related
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
74 // problem in valgrind. Inlining is prevented.
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
75 #if defined(__GNUC__) || defined(__clang__)
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
76 __attribute__((noinline))
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
77 #endif
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
78 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status)
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
79 {
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
80 #if ORTHANC_ENABLE_SSL == 1
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
81 return GetHttpStatus(curl_easy_perform(curl), curl, status);
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
82 #else
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
83 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
84 "Orthanc was compiled without SSL support, "
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
85 "cannot make HTTPS request");
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
86 #endif
1533
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 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
93 static CURLcode CheckCode(CURLcode code)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
94 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
95 if (code == CURLE_NOT_BUILT_IN)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
96 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
97 throw OrthancException(ErrorCode_InternalError,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
98 "Your libcurl does not contain a required feature, "
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
99 "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
100 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
101
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
102 if (code != CURLE_OK)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
103 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
104 throw OrthancException(ErrorCode_NetworkProtocol,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
105 "libCURL error: " + std::string(curl_easy_strerror(code)));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
106 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
107
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
108 return code;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
109 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
110
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
111
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
112 // RAII pattern around a "curl_slist"
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
113 class HttpClient::CurlHeaders : public boost::noncopyable
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
114 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
115 private:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
116 struct curl_slist *content_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
117 bool isChunkedTransfer_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
118 bool hasExpect_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
119
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
120 public:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
121 CurlHeaders() :
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
122 content_(NULL),
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
123 isChunkedTransfer_(false),
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
124 hasExpect_(false)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
125 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
126 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
127
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
128 CurlHeaders(const HttpClient::HttpHeaders& headers)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
129 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
130 for (HttpClient::HttpHeaders::const_iterator
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
131 it = headers.begin(); it != headers.end(); ++it)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
132 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
133 AddHeader(it->first, it->second);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
134 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
135 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
136
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
137 ~CurlHeaders()
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
138 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
139 Clear();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
140 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
141
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
142 bool IsEmpty() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
143 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
144 return content_ == NULL;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
145 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
146
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
147 void Clear()
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
148 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
149 if (content_ != NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
150 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
151 curl_slist_free_all(content_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
152 content_ = NULL;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
153 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
154
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
155 isChunkedTransfer_ = false;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
156 hasExpect_ = false;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
157 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
158
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
159 void AddHeader(const std::string& key,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
160 const std::string& value)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
161 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
162 if (boost::iequals(key, "Expect"))
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
163 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
164 hasExpect_ = true;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
165 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
166
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
167 if (boost::iequals(key, "Transfer-Encoding") &&
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
168 value == "chunked")
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
169 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
170 isChunkedTransfer_ = true;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
171 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
172
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
173 std::string item = key + ": " + value;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
174
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
175 struct curl_slist *tmp = curl_slist_append(content_, item.c_str());
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
176
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
177 if (tmp == NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
178 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
179 throw OrthancException(ErrorCode_NotEnoughMemory);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
180 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
181 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
182 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
183 content_ = tmp;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
184 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
185 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
186
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
187 void Assign(CURL* curl) const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
188 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
189 CheckCode(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, content_));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
190 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
191
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
192 bool HasExpect() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
193 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
194 return hasExpect_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
195 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
196
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
197 bool IsChunkedTransfer() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
198 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
199 return isChunkedTransfer_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
200 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
201 };
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
202
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
203
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
204 class HttpClient::CurlBodyStream : public boost::noncopyable
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
205 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
206 private:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
207 HttpClient::IBodyStream* stream_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
208 std::string buffer_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
209
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
210 size_t CallbackInternal(char* curlBuffer,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
211 size_t curlBufferSize)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
212 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
213 if (stream_ == NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
214 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
215 throw OrthancException(ErrorCode_BadSequenceOfCalls);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
216 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
217
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
218 if (curlBufferSize == 0)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
219 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
220 throw OrthancException(ErrorCode_InternalError);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
221 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
222
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
223 // Read chunks from the stream so as to fill the target buffer
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
224 std::string chunk;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
225
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
226 while (buffer_.size() < curlBufferSize &&
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
227 stream_->ReadNextChunk(chunk))
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
228 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
229 buffer_ += chunk;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
230 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
231
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
232 size_t s = std::min(buffer_.size(), curlBufferSize);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
233
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
234 if (s != 0)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
235 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
236 memcpy(curlBuffer, buffer_.c_str(), s);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
237
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
238 // Remove the bytes that were actually sent from the buffer
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
239 buffer_.erase(0, s);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
240 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
241
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
242 return s;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
243 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
244
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
245 public:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
246 CurlBodyStream() :
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
247 stream_(NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
248 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
249 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
250
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
251 void SetStream(HttpClient::IBodyStream& stream)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
252 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
253 stream_ = &stream;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
254 buffer_.clear();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
255 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
256
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
257 void Clear()
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
258 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
259 stream_ = NULL;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
260 buffer_.clear();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
261 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
262
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
263 bool IsValid() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
264 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
265 return stream_ != NULL;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
266 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
267
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
268 static size_t Callback(char *buffer,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
269 size_t size,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
270 size_t nitems,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
271 void *userdata)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
272 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
273 try
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
274 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
275 HttpClient::CurlBodyStream* stream = reinterpret_cast<HttpClient::CurlBodyStream*>(userdata);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
276
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
277 if (stream == NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
278 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
279 throw OrthancException(ErrorCode_NullPointer);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
280 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
281 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
282 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
283 return stream->CallbackInternal(buffer, size * nitems);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
284 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
285 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
286 catch (OrthancException& e)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
287 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
288 LOG(ERROR) << "Exception while streaming HTTP body: " << e.What();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
289 return CURL_READFUNC_ABORT;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
290 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
291 catch (...)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
292 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
293 LOG(ERROR) << "Native exception while streaming HTTP body";
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
294 return CURL_READFUNC_ABORT;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
295 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
296 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
297 };
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
298
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
299
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
300 class HttpClient::GlobalParameters
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
301 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
302 private:
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
303 boost::mutex mutex_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
304 bool httpsVerifyPeers_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
305 std::string httpsCACertificates_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
306 std::string proxy_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
307 long timeout_;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
308 bool verbose_;
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
309
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
310 GlobalParameters() :
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
311 httpsVerifyPeers_(true),
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
312 timeout_(0),
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
313 verbose_(false)
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
314 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
315 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
316
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
317 public:
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
318 // Singleton pattern
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
319 static GlobalParameters& GetInstance()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
320 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
321 static GlobalParameters parameters;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
322 return parameters;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
323 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
324
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
325 void ConfigureSsl(bool httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
326 const std::string& httpsCACertificates)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
327 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
328 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
329 httpsVerifyPeers_ = httpsVerifyPeers;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
330 httpsCACertificates_ = httpsCACertificates;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
331 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
332
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
333 void GetSslConfiguration(bool& httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
334 std::string& httpsCACertificates)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
335 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
336 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
337 httpsVerifyPeers = httpsVerifyPeers_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
338 httpsCACertificates = httpsCACertificates_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
339 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
340
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
341 void SetDefaultProxy(const std::string& proxy)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
342 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
343 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
344
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
345 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
346 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
347 proxy_ = proxy;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
348 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
349 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
350
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
351 void GetDefaultProxy(std::string& target)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
352 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
353 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
354 target = proxy_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
355 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
356
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
357 void SetDefaultTimeout(long seconds)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
358 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
359 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
360
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
361 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
362 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
363 timeout_ = seconds;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
364 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
365 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
366
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
367 long GetDefaultTimeout()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
368 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
369 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
370 return timeout_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
371 }
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
372
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
373 #if ORTHANC_ENABLE_PKCS11 == 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
374 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
375 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
376 boost::mutex::scoped_lock lock(mutex_);
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
377 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
378 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
379
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
380 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
381 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
382 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
383 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
384 boost::mutex::scoped_lock lock(mutex_);
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
385 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
386 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
387 #endif
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
388
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
389 bool IsDefaultVerbose() const
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
390 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
391 return verbose_;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
392 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
393
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
394 void SetDefaultVerbose(bool verbose)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
395 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
396 verbose_ = verbose;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
397 }
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
398 };
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
399
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
400
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 struct HttpClient::PImpl
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 CURL* curl_;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
404 CurlHeaders defaultPostHeaders_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
405 CurlHeaders defaultChunkedHeaders_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
406 CurlHeaders userHeaders_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
407 CurlBodyStream bodyStream_;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410
2787
ad2c32082653 Fix OrthancPluginHttpClient() to return the HTTP status on errors
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2677
diff changeset
411 void HttpClient::ThrowException(HttpStatus status)
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
412 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
413 switch (status)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
414 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
415 case HttpStatus_400_BadRequest:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
416 throw OrthancException(ErrorCode_BadRequest);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
417
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
418 case HttpStatus_401_Unauthorized:
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
419 case HttpStatus_403_Forbidden:
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
420 throw OrthancException(ErrorCode_Unauthorized);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
421
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
422 case HttpStatus_404_NotFound:
2044
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2041
diff changeset
423 throw OrthancException(ErrorCode_UnknownResource);
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
424
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
425 default:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
426 throw OrthancException(ErrorCode_NetworkProtocol);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
427 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
428 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
429
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
430
2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2022
diff changeset
431 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
432 {
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
433 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
434
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
435 size_t length = size * nmemb;
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
436 if (length == 0)
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
437 {
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
438 return 0;
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
439 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
440 else
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
441 {
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
442 target.AddChunk(buffer, length);
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
443 return length;
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
444 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
445 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
446
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
447
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
448 /*static int CurlDebugCallback(CURL *handle,
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
449 curl_infotype type,
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
450 char *data,
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
451 size_t size,
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
452 void *userptr)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
453 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
454 switch (type)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
455 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
456 case CURLINFO_TEXT:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
457 case CURLINFO_HEADER_IN:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
458 case CURLINFO_HEADER_OUT:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
459 case CURLINFO_SSL_DATA_IN:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
460 case CURLINFO_SSL_DATA_OUT:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
461 case CURLINFO_END:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
462 case CURLINFO_DATA_IN:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
463 case CURLINFO_DATA_OUT:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
464 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
465 std::string s(data, size);
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
466 LOG(INFO) << "libcurl: " << s;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
467 break;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
468 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
469
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
470 default:
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
471 break;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
472 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
473
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
474 return 0;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
475 }*/
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
476
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
477
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
478 struct CurlHeaderParameters
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
479 {
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
480 bool lowerCase_;
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
481 HttpClient::HttpHeaders* headers_;
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
482 };
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
483
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
484
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
485 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
486 {
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
487 CurlHeaderParameters& parameters = *(static_cast<CurlHeaderParameters*>(payload));
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
488 assert(parameters.headers_ != NULL);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 size_t length = size * nmemb;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 if (length == 0)
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
492 {
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 return 0;
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
494 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
495 else
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
496 {
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
497 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
498 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
499 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
500 if (colon != std::string::npos &&
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
501 eol != std::string::npos)
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
502 {
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
503 std::string tmp(s.substr(0, colon));
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
504
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
505 if (parameters.lowerCase_)
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
506 {
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
507 Toolbox::ToLowerCase(tmp);
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
508 }
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
509
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
510 std::string key = Toolbox::StripSpaces(tmp);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
512 if (!key.empty())
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
513 {
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
514 std::string value = Toolbox::StripSpaces(s.substr(colon + 1, eol));
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
515 (*parameters.headers_) [key] = value;
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
516 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
517 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
519 return length;
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
520 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
524 void HttpClient::Setup()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
526 pimpl_->defaultPostHeaders_.AddHeader("Expect", "");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
527 pimpl_->defaultChunkedHeaders_.AddHeader("Expect", "");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
528 pimpl_->defaultChunkedHeaders_.AddHeader("Transfer-Encoding", "chunked");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 pimpl_->curl_ = curl_easy_init();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531
2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2022
diff changeset
532 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlBodyCallback));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535
456
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
536 // This fixes the "longjmp causes uninitialized stack frame" crash
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
537 // that happens on modern Linux versions.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
538 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
539 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
540
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 url_ = "";
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
542 method_ = HttpMethod_Get;
3360
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3122
diff changeset
543 lastStatus_ = HttpStatus_None;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
544 SetVerbose(GlobalParameters::GetInstance().IsDefaultVerbose());
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
545 timeout_ = GlobalParameters::GetInstance().GetDefaultTimeout();
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
546 GlobalParameters::GetInstance().GetDefaultProxy(proxy_);
2219
262fcab24024 fix for HttpsCACertificates
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
547 GlobalParameters::GetInstance().GetSslConfiguration(verifyPeers_, caCertificates_);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
551 HttpClient::HttpClient() :
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
552 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
553 verifyPeers_(true),
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
554 pkcs11Enabled_(false),
2107
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
555 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
556 redirectionFollowed_(true)
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
557 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
558 Setup();
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
559 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
560
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
561
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
562 HttpClient::HttpClient(const WebServiceParameters& service,
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
563 const std::string& uri) :
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
564 pimpl_(new PImpl),
2049
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2045
diff changeset
565 verifyPeers_(true),
2107
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
566 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
567 redirectionFollowed_(true)
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
568 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
569 Setup();
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
570
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
571 if (service.GetUsername().size() != 0 &&
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
572 service.GetPassword().size() != 0)
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
573 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
574 SetCredentials(service.GetUsername().c_str(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
575 service.GetPassword().c_str());
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
576 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
577
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
578 if (!service.GetCertificateFile().empty())
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
579 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
580 SetClientCertificate(service.GetCertificateFile(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
581 service.GetCertificateKeyFile(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
582 service.GetCertificateKeyPassword());
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
583 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
584
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
585 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
586
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
587 SetUrl(service.GetUrl() + uri);
2800
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
588
2823
807169f85ba9 OrthancPluginGetPeerUserProperty()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2800
diff changeset
589 for (WebServiceParameters::Dictionary::const_iterator
2800
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
590 it = service.GetHttpHeaders().begin();
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
591 it != service.GetHttpHeaders().end(); ++it)
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
592 {
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
593 AddHeader(it->first, it->second);
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
594 }
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
595 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
596
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
597
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
598 HttpClient::~HttpClient()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
599 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
600 curl_easy_cleanup(pimpl_->curl_);
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
601 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
602
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
603
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
604 void HttpClient::SetBody(const std::string& data)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
605 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
606 body_ = data;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
607 pimpl_->bodyStream_.Clear();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
608 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
609
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
610
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
611 void HttpClient::SetBodyStream(IBodyStream& stream)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
612 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
613 pimpl_->bodyStream_.SetStream(stream);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
614 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
615
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
616
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
617 void HttpClient::ClearBodyStream()
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
618 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
619 pimpl_->bodyStream_.Clear();
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
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 void HttpClient::SetVerbose(bool isVerbose)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 isVerbose_ = isVerbose;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 if (isVerbose_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1));
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
630 //CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_DEBUGFUNCTION, &CurlDebugCallback));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
639 void HttpClient::AddHeader(const std::string& key,
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
640 const std::string& value)
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
641 {
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
642 if (key.empty())
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
643 {
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
644 throw OrthancException(ErrorCode_ParameterOutOfRange);
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
645 }
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
646 else
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
647 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
648 pimpl_->userHeaders_.AddHeader(key, value);
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
649 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
650 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
651
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
652
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
653 void HttpClient::ClearHeaders()
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
654 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
655 pimpl_->userHeaders_.Clear();
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
656 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
657
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
658
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
659 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
660 HttpHeaders* answerHeaders)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
662 answerBody.clear();
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663 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
664
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
665 CurlHeaderParameters headerParameters;
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
666
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
667 if (answerHeaders == NULL)
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
668 {
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
669 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
670 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
671 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
672 else
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
673 {
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
674 headerParameters.lowerCase_ = headersToLowerCase_;
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
675 headerParameters.headers_ = answerHeaders;
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
676 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERFUNCTION, &CurlHeaderCallback));
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
677 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
678 }
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
679
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
680 #if ORTHANC_ENABLE_SSL == 1
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
681 // 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
682
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
683 if (verifyPeers_)
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
684 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
685 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
686 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
687 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
688 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
689 else
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
690 {
1973
7bd4cb780feb Option "HttpsVerifyPeers" can be used to connect against self-signed HTTPS certificates
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
691 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
692 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
693 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
694 #endif
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
695
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
696 // 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
697 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
698 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
699 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
700 throw OrthancException(ErrorCode_ParameterOutOfRange,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
701 "Cannot enable both client certificates and PKCS#11 authentication");
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
702 }
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
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
704 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
705 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
706 #if ORTHANC_ENABLE_PKCS11 == 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
707 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
708 {
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
709 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
710 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
711 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
712 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
713 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
714 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
715 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
716 "Cannot use PKCS#11 for a HTTPS request, "
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
717 "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
718 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
719 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
720 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
721 "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
722 #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
723 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
724 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
725 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
726 #if ORTHANC_ENABLE_SSL == 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
727 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
728 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
729
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
730 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
731 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
732 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
733 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
734
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
735 // 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
736 // 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
737 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
738 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
739 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
740 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
741 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
742 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
743 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
744 "This version of Orthanc is compiled without OpenSSL support, "
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
745 "cannot use HTTPS client authentication");
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
746 #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
747 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
748
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
749 // Reset the parameters from previous calls to Apply()
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
750 pimpl_->userHeaders_.Assign(pimpl_->curl_);
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
751 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
752 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
753 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
754 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
755 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
2104
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
756 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
757 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, NULL));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
758
2104
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
759 if (redirectionFollowed_)
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
760 {
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
761 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1L));
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
762 }
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
763 else
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
764 {
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
765 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 0L));
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
766 }
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
767
1186
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
768 // Set timeouts
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
769 if (timeout_ <= 0)
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
770 {
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
771 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
772 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
773 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
774 else
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
775 {
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
776 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
777 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
778 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
779
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
780 if (credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
781 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
782 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
783 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
784
1273
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
785 if (proxy_.size() != 0)
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
786 {
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
787 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
788 }
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
789
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
790 switch (method_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
791 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
792 case HttpMethod_Get:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
793 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
794 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
795
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
796 case HttpMethod_Post:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
797 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
798
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
799 break;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
800
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
801 case HttpMethod_Delete:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
802 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
803 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
804 break;
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
805
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
806 case HttpMethod_Put:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
807 // http://stackoverflow.com/a/7570281/881731: Don't use
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
808 // CURLOPT_PUT if there is a body
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
809
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
810 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
811
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
812 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
813 break;
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
814
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
815 default:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
816 throw OrthancException(ErrorCode_InternalError);
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
817 }
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
818
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
819 if (method_ == HttpMethod_Post ||
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
820 method_ == HttpMethod_Put)
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
821 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
822 if (!pimpl_->userHeaders_.IsEmpty() &&
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
823 !pimpl_->userHeaders_.HasExpect())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
824 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
825 LOG(INFO) << "For performance, the HTTP header \"Expect\" should be set to empty string in POST/PUT requests";
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
826 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
827
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
828 if (pimpl_->bodyStream_.IsValid())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
830 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READFUNCTION, CurlBodyStream::Callback));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
831 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READDATA, &pimpl_->bodyStream_));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
832 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
833 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, -1L));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
834
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
835 if (pimpl_->userHeaders_.IsEmpty())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
836 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
837 pimpl_->defaultChunkedHeaders_.Assign(pimpl_->curl_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
838 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
839 else if (!pimpl_->userHeaders_.IsChunkedTransfer())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
840 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
841 LOG(WARNING) << "The HTTP header \"Transfer-Encoding\" must be set to \"chunked\" in streamed POST/PUT requests";
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
842 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
843 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
844 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
845 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
846 // Disable possible previous stream transfers
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
847 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READFUNCTION, NULL));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
848 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_UPLOAD, 0));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
849
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
850 if (pimpl_->userHeaders_.IsEmpty())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
851 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
852 pimpl_->defaultPostHeaders_.Assign(pimpl_->curl_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
853 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
854
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
855 if (body_.size() > 0)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
856 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
857 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
858 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
859 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
860 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
861 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
862 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
863 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
864 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
865 }
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
866 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
867
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
868
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
869 // Do the actual request
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
870 CURLcode code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
871 long status = 0;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
872
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
873 ChunkedBuffer buffer;
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
874 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
875
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
876 if (boost::starts_with(url_, "https://"))
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
877 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
878 code = OrthancHttpClientPerformSSL(pimpl_->curl_, &status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
879 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
880 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
881 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
882 code = GetHttpStatus(curl_easy_perform(pimpl_->curl_), pimpl_->curl_, &status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
883 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
884
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
885 LOG(INFO) << "HTTP status code " << status << " after "
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
886 << EnumerationToString(method_) << " request on: " << url_;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
887
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
888 if (isVerbose_)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
889 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
890 LOG(INFO) << "cURL status code: " << code;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
891 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
892
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
893 CheckCode(code);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
894
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
895 if (status == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
896 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
897 // 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
898 lastStatus_ = HttpStatus_500_InternalServerError;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
899 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
900 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
901 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
902 lastStatus_ = static_cast<HttpStatus>(status);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
903 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
904
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
905 bool success = (status >= 200 && status < 300);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
906
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
907 if (success)
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
908 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
909 buffer.Flatten(answerBody);
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
910 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
911 else
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
912 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
913 answerBody.clear();
3122
df4f977c2f88 trying to reproduce isse 32 in C++
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3114
diff changeset
914 LOG(ERROR) << "Error in HTTP request, received HTTP status " << status
df4f977c2f88 trying to reproduce isse 32 in C++
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3114
diff changeset
915 << " (" << EnumerationToString(lastStatus_) << ")";
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
916 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
917
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
918 return success;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
919 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
920
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
921
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
922 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
923 HttpClient::HttpHeaders* answerHeaders)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
924 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
925 std::string s;
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
926 if (ApplyInternal(s, answerHeaders))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
927 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
928 Json::Reader reader;
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
929 return reader.parse(s, answerBody);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
930 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
931 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
932 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
933 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
934 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
935 }
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
936
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
937
468
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
938 void HttpClient::SetCredentials(const char* username,
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
939 const char* password)
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
940 {
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
941 credentials_ = std::string(username) + ":" + std::string(password);
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
942 }
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
943
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
944
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
945 void HttpClient::ConfigureSsl(bool httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
946 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
947 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
948 #if ORTHANC_ENABLE_SSL == 1
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
949 if (httpsVerifyPeers)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
950 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
951 if (httpsVerifyCertificates.empty())
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
952 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
953 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
954 << "set \"HttpsCACertificates\" if you need to do HTTPS requests";
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
955 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
956 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
957 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
958 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
959 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
960 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
961 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
962 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
963 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
964 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
965 #endif
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
966
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
967 GlobalParameters::GetInstance().ConfigureSsl(httpsVerifyPeers, httpsVerifyCertificates);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
968 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
969
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
970
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
971 void HttpClient::GlobalInitialize()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
972 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
973 #if ORTHANC_ENABLE_SSL == 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
974 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
975 #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
976 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
977 #endif
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
978 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
979
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
980
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
981 void HttpClient::GlobalFinalize()
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
982 {
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
983 curl_global_cleanup();
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
984
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
985 #if ORTHANC_ENABLE_PKCS11 == 1
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
986 Pkcs11::Finalize();
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
987 #endif
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
988 }
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
989
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
990
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
991 void HttpClient::SetDefaultVerbose(bool verbose)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
992 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
993 GlobalParameters::GetInstance().SetDefaultVerbose(verbose);
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
994 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
995
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
996
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
997 void HttpClient::SetDefaultProxy(const std::string& proxy)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
998 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
999 GlobalParameters::GetInstance().SetDefaultProxy(proxy);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1000 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1001
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1002
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
1003 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
1004 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1005 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
1006 }
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1007
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1008
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1009 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
1010 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1011 if (!Apply(answerBody))
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1012 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1013 ThrowException(GetLastStatus());
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1014 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1015 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1016
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1017
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1018 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
1019 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1020 if (!Apply(answerBody))
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1021 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1022 ThrowException(GetLastStatus());
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1023 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1024 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1025
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1026
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1027 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
1028 HttpHeaders& answerHeaders)
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1029 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1030 if (!Apply(answerBody, answerHeaders))
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1031 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1032 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1033 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1034 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1035
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1036
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1037 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
1038 HttpHeaders& answerHeaders)
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1039 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1040 if (!Apply(answerBody, answerHeaders))
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1041 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1042 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1043 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1044 }
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
1045
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
1046
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
1047 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
1048 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
1049 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
1050 {
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
1051 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
1052 {
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
1053 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
1054 }
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
1055
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2136
diff changeset
1056 if (!SystemToolbox::IsRegularFile(certificateFile))
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
1057 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1058 throw OrthancException(ErrorCode_InexistentFile,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1059 "Cannot open certificate file: " + certificateFile);
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
1060 }
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
1061
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
1062 if (!certificateKeyFile.empty() &&
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2136
diff changeset
1063 !SystemToolbox::IsRegularFile(certificateKeyFile))
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
1064 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1065 throw OrthancException(ErrorCode_InexistentFile,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1066 "Cannot open key file: " + certificateKeyFile);
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
1067 }
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
1068
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
1069 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
1070 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
1071 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
1072 }
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
1073
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
1074
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
1075 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
1076 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
1077 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
1078 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
1079 #if ORTHANC_ENABLE_PKCS11 == 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
1080 LOG(INFO) << "Initializing PKCS#11 using " << module
2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2023
diff changeset
1081 << (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
1082 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
1083 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1084 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1085 "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
1086 #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
1087 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1088 }