annotate OrthancFramework/Sources/HttpClient.cpp @ 5853:4d932683049d get-scu tip

very first implementation of C-Get SCU
author Alain Mazy <am@orthanc.team>
date Tue, 29 Oct 2024 17:25:49 +0100
parents f7adfb22e20e
children
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
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
9 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
12 * the License, or (at your option) any later version.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
14 * 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
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * Lesser General Public License for more details.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
20 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
21 * <http://www.gnu.org/licenses/>.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
25 #include "PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "HttpClient.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
1404
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
28 #include "Toolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
29 #include "OrthancException.h"
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
30 #include "Logging.h"
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
31 #include "ChunkedBuffer.h"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2140
diff changeset
32 #include "SystemToolbox.h"
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
33
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <curl/curl.h>
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
36 #include <boost/algorithm/string/predicate.hpp>
1989
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1987
diff changeset
37 #include <boost/thread/mutex.hpp>
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
38
3428
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
39 // Default timeout = 60 seconds (in Orthanc <= 1.5.6, it was 10 seconds)
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
40 static const unsigned int DEFAULT_HTTP_TIMEOUT = 60;
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
41
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
42
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
43 #if ORTHANC_ENABLE_PKCS11 == 1
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
44 # 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
45 #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
46
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
47
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
48 extern "C"
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
49 {
5095
b52fe770aec0 more logs in HttpClient
Alain Mazy <am@osimis.io>
parents: 5021
diff changeset
50 static CURLcode GetHttpStatus(CURLcode code, CURL* curl, long* status, const std::string& url)
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
51 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
52 if (code == CURLE_OK)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
53 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
54 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
55 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
56 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
57 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
58 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
59 *status = 0;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
60 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
61 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
62 }
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
63 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
64
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
65 // 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
66 // problem in valgrind. Inlining is prevented.
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
67 #if defined(__GNUC__) || defined(__clang__)
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
68 __attribute__((noinline))
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
69 #endif
5095
b52fe770aec0 more logs in HttpClient
Alain Mazy <am@osimis.io>
parents: 5021
diff changeset
70 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status, const std::string& url)
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
71 {
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
72 #if ORTHANC_ENABLE_SSL == 1
5095
b52fe770aec0 more logs in HttpClient
Alain Mazy <am@osimis.io>
parents: 5021
diff changeset
73 return GetHttpStatus(curl_easy_perform(curl), curl, status, url);
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
74 #else
3379
974e12006b7d Removed extern "C" qualifier around function that is internally linked
Benjamin Golinvaux <bgo@osimis.io>
parents: 3360
diff changeset
75 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
76 "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
77 "cannot make HTTPS request");
2473
0f463989f7e3 warning if no SSL support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
78 #endif
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
79 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
80
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
60
77aec9be0a51 renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
83 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
85 static CURLcode CheckCode(CURLcode code)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
86 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
87 if (code == CURLE_NOT_BUILT_IN)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
88 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
89 throw OrthancException(ErrorCode_InternalError,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
90 "Your libcurl does not contain a required feature, "
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
91 "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
92 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
93
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
94 if (code != CURLE_OK)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
95 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
96 throw OrthancException(ErrorCode_NetworkProtocol,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
97 "libCURL error: " + std::string(curl_easy_strerror(code)));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
98 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
99
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
100 return code;
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
5100
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
103 static CURLcode CheckCode(CURLcode code, const std::string& url)
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
104 {
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
105 if (code == CURLE_NOT_BUILT_IN)
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
106 {
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
107 throw OrthancException(ErrorCode_InternalError,
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
108 "Your libcurl does not contain a required feature, "
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
109 "please recompile Orthanc with -DUSE_SYSTEM_CURL=OFF");
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
110 }
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
111
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
112 if (code != CURLE_OK)
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
113 {
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
114 throw OrthancException(ErrorCode_NetworkProtocol,
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
115 "libCURL error: " + std::string(curl_easy_strerror(code)) + " while accessing " + url);
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
116 }
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
117
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
118 return code;
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
119 }
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
120
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
121
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
122 // RAII pattern around a "curl_slist"
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
123 class HttpClient::CurlHeaders : public boost::noncopyable
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
124 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
125 private:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
126 struct curl_slist *content_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
127 bool isChunkedTransfer_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
128 bool hasExpect_;
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 public:
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
131 CurlHeaders() :
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
132 content_(NULL),
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
133 isChunkedTransfer_(false),
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
134 hasExpect_(false)
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
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
138 explicit CurlHeaders(const HttpClient::HttpHeaders& headers)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
139 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
140 for (HttpClient::HttpHeaders::const_iterator
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
141 it = headers.begin(); it != headers.end(); ++it)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
142 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
143 AddHeader(it->first, it->second);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
144 }
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 ~CurlHeaders()
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 Clear();
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
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
152 bool IsEmpty() const
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 return content_ == NULL;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
155 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
156
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
157 void Clear()
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 if (content_ != NULL)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
160 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
161 curl_slist_free_all(content_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
162 content_ = NULL;
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
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
165 isChunkedTransfer_ = false;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
166 hasExpect_ = false;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
167 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
168
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
169 void AddHeader(const std::string& key,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
170 const std::string& value)
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 if (boost::iequals(key, "Expect"))
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
173 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
174 hasExpect_ = true;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
175 }
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 (boost::iequals(key, "Transfer-Encoding") &&
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
178 value == "chunked")
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
179 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
180 isChunkedTransfer_ = true;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
181 }
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 std::string item = key + ": " + value;
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 struct curl_slist *tmp = curl_slist_append(content_, item.c_str());
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 if (tmp == NULL)
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 throw OrthancException(ErrorCode_NotEnoughMemory);
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 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
192 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
193 content_ = tmp;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
194 }
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 void Assign(CURL* curl) 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 CheckCode(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, content_));
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 bool HasExpect() const
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 return hasExpect_;
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
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
207 bool IsChunkedTransfer() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
208 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
209 return isChunkedTransfer_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
210 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
211 };
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
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
214 class HttpClient::CurlRequestBody : public boost::noncopyable
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
215 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
216 private:
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
217 HttpClient::IRequestBody* body_;
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
218 std::string pending_;
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
219 size_t pendingPos_;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
220
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
221 size_t CallbackInternal(char* curlBuffer,
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
222 size_t curlBufferSize)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
223 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
224 if (body_ == NULL)
3386
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 throw OrthancException(ErrorCode_BadSequenceOfCalls);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
227 }
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 if (curlBufferSize == 0)
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 throw OrthancException(ErrorCode_InternalError);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
232 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
233
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
234 if (pendingPos_ + curlBufferSize <= pending_.size())
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
235 {
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
236 assert(sizeof(char) == 1);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
237 memcpy(curlBuffer, &pending_[pendingPos_], curlBufferSize);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
238 pendingPos_ += curlBufferSize;
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
239 return curlBufferSize;
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
240 }
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
241 else
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
242 {
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
243 ChunkedBuffer buffer;
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
244 buffer.SetPendingBufferSize(curlBufferSize);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
245
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
246 if (pendingPos_ < pending_.size())
3590
d5740d3b1d67 fix for issue 156: Chunked Dicom-web transfer uses 100% CPU
Alain Mazy <alain@mazy.be>
parents: 3516
diff changeset
247 {
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
248 buffer.AddChunk(&pending_[pendingPos_], pending_.size() - pendingPos_);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
249 }
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
250
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
251 // Read chunks from the body stream so as to fill the target buffer
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
252 std::string chunk;
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
253
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
254 while (buffer.GetNumBytes() < curlBufferSize &&
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
255 body_->ReadNextChunk(chunk))
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
256 {
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
257 buffer.AddChunk(chunk);
3590
d5740d3b1d67 fix for issue 156: Chunked Dicom-web transfer uses 100% CPU
Alain Mazy <alain@mazy.be>
parents: 3516
diff changeset
258 }
d5740d3b1d67 fix for issue 156: Chunked Dicom-web transfer uses 100% CPU
Alain Mazy <alain@mazy.be>
parents: 3516
diff changeset
259
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
260 buffer.Flatten(pending_);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
261 pendingPos_ = std::min(pending_.size(), curlBufferSize);
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
262
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
263 if (pendingPos_ != 0)
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
264 {
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
265 memcpy(curlBuffer, pending_.c_str(), pendingPos_);
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
266 }
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
267
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
268 return pendingPos_;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
269 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
270 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
271
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
272 public:
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
273 CurlRequestBody() :
3590
d5740d3b1d67 fix for issue 156: Chunked Dicom-web transfer uses 100% CPU
Alain Mazy <alain@mazy.be>
parents: 3516
diff changeset
274 body_(NULL),
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
275 pendingPos_(0)
3386
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 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
278
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
279 void SetBody(HttpClient::IRequestBody& body)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
280 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
281 body_ = &body;
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
282 pending_.clear();
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
283 pendingPos_ = 0;
3386
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 void Clear()
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
287 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
288 body_ = NULL;
4151
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
289 pending_.clear();
8c559dd5034b Fix possible crash in HttpClient if sending multipart body (can occur in STOW-RS)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
290 pendingPos_ = 0;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
291 }
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 bool IsValid() const
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
294 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
295 return body_ != NULL;
3386
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
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
298 static size_t Callback(char *buffer, size_t size, size_t nitems, void *userdata)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
299 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
300 try
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
301 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
302 assert(userdata != NULL);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
303 return reinterpret_cast<HttpClient::CurlRequestBody*>(userdata)->
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
304 CallbackInternal(buffer, size * nitems);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
305 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
306 catch (OrthancException& e)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
307 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
308 LOG(ERROR) << "Exception while streaming HTTP body: " << e.What();
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
309 return CURL_READFUNC_ABORT;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
310 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
311 catch (...)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
312 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
313 LOG(ERROR) << "Native exception while streaming HTTP body";
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
314 return CURL_READFUNC_ABORT;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
315 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
316 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
317 };
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
318
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
319
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
320 class HttpClient::CurlAnswer : public boost::noncopyable
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
321 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
322 private:
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
323 HttpClient::IAnswer& answer_;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
324 bool headersLowerCase_;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
325
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
326 public:
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
327 CurlAnswer(HttpClient::IAnswer& answer,
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
328 bool headersLowerCase) :
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
329 answer_(answer),
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
330 headersLowerCase_(headersLowerCase)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
331 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
332 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
333
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
334 static size_t HeaderCallback(void *buffer, size_t size, size_t nmemb, void *userdata)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
335 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
336 try
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
337 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
338 assert(userdata != NULL);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
339
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
340 size_t length = size * nmemb;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
341 if (length == 0)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
342 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
343 return 0;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
344 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
345 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
346 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
347 std::string s(reinterpret_cast<const char*>(buffer), length);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
348 std::size_t colon = s.find(':');
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
349 std::size_t eol = s.find("\r\n");
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
350 if (colon != std::string::npos &&
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
351 eol != std::string::npos)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
352 {
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
353 CurlAnswer& that = *(static_cast<CurlAnswer*>(userdata));
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
354 std::string tmp(s.substr(0, colon));
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
355
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
356 if (that.headersLowerCase_)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
357 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
358 Toolbox::ToLowerCase(tmp);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
359 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
360
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
361 std::string key = Toolbox::StripSpaces(tmp);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
362
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
363 if (!key.empty())
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
364 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
365 std::string value = Toolbox::StripSpaces(s.substr(colon + 1, eol));
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
366
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
367 that.answer_.AddHeader(key, value);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
368 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
369 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
370
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
371 return length;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
372 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
373 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
374 catch (OrthancException& e)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
375 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
376 LOG(ERROR) << "Exception while streaming HTTP body: " << e.What();
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
377 return CURL_READFUNC_ABORT;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
378 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
379 catch (...)
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
380 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
381 LOG(ERROR) << "Native exception while streaming HTTP body";
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
382 return CURL_READFUNC_ABORT;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
383 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
384 }
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
385
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
386 static size_t BodyCallback(void *buffer, size_t size, size_t nmemb, void *userdata)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
387 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
388 try
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
389 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
390 assert(userdata != NULL);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
391
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
392 size_t length = size * nmemb;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
393 if (length == 0)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
394 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
395 return 0;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
396 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
397 else
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
398 {
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
399 CurlAnswer& that = *(static_cast<CurlAnswer*>(userdata));
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
400 that.answer_.AddChunk(buffer, length);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
401 return length;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
402 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
403 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
404 catch (OrthancException& e)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
405 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
406 LOG(ERROR) << "Exception while streaming HTTP body: " << e.What();
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
407 return CURL_READFUNC_ABORT;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
408 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
409 catch (...)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
410 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
411 LOG(ERROR) << "Native exception while streaming HTTP body";
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
412 return CURL_READFUNC_ABORT;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
413 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
414 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
415 };
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
416
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
417
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
418 class HttpClient::DefaultAnswer : public HttpClient::IAnswer
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
419 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
420 private:
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
421 ChunkedBuffer answer_;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
422 HttpHeaders* headers_;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
423
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
424 public:
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
425 DefaultAnswer() : headers_(NULL)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
426 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
427 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
428
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
429 void SetHeaders(HttpHeaders& headers)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
430 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
431 headers_ = &headers;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
432 headers_->clear();
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
433 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
434
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
435 void FlattenBody(std::string& target)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
436 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
437 answer_.Flatten(target);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
438 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
439
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
440 virtual void AddHeader(const std::string& key,
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
441 const std::string& value) ORTHANC_OVERRIDE
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
442 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
443 if (headers_ != NULL)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
444 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
445 (*headers_) [key] = value;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
446 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
447 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
448
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
449 virtual void AddChunk(const void* data,
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4195
diff changeset
450 size_t size) ORTHANC_OVERRIDE
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
451 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
452 answer_.AddChunk(data, size);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
453 }
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
454 };
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
455
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
456
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
457 class HttpClient::GlobalParameters
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
458 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
459 private:
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
460 boost::mutex mutex_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
461 bool httpsVerifyPeers_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
462 std::string httpsCACertificates_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
463 std::string proxy_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
464 long timeout_;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
465 bool verbose_;
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
466
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
467 GlobalParameters() :
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
468 httpsVerifyPeers_(true),
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
469 timeout_(0),
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
470 verbose_(false)
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
471 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
472 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
473
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
474 public:
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
475 // Singleton pattern
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
476 static GlobalParameters& GetInstance()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
477 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
478 static GlobalParameters parameters;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
479 return parameters;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
480 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
481
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
482 void ConfigureSsl(bool httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
483 const std::string& httpsCACertificates)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
484 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
485 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
486 httpsVerifyPeers_ = httpsVerifyPeers;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
487 httpsCACertificates_ = httpsCACertificates;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
488 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
489
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
490 void GetSslConfiguration(bool& httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
491 std::string& httpsCACertificates)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
492 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
493 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
494 httpsVerifyPeers = httpsVerifyPeers_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
495 httpsCACertificates = httpsCACertificates_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
496 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
497
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
498 void SetDefaultProxy(const std::string& proxy)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
499 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
500 CLOG(INFO, HTTP) << "Setting the default proxy for HTTP client connections: " << proxy;
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
501
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
502 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
503 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
504 proxy_ = proxy;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
505 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
506 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
507
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
508 void GetDefaultProxy(std::string& target)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
509 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
510 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
511 target = proxy_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
512 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
513
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
514 void SetDefaultTimeout(long seconds)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
515 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
516 CLOG(INFO, HTTP) << "Setting the default timeout for HTTP client connections: " << seconds << " seconds";
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
517
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
518 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
519 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
520 timeout_ = seconds;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
521 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
522 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
523
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
524 long GetDefaultTimeout()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
525 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
526 boost::mutex::scoped_lock lock(mutex_);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
527 return timeout_;
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
528 }
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
529
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
530 #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
531 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
532 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
533 boost::mutex::scoped_lock lock(mutex_);
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
534 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
535 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
536
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
537 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
538 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
539 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
540 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
541 boost::mutex::scoped_lock lock(mutex_);
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
542 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
543 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
544 #endif
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
545
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
546 bool IsDefaultVerbose() const
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
547 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
548 return verbose_;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
549 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
550
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
551 void SetDefaultVerbose(bool verbose)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
552 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
553 verbose_ = verbose;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
554 }
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
555 };
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
556
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
557
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 struct HttpClient::PImpl
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 CURL* curl_;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
561 CurlHeaders defaultPostHeaders_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
562 CurlHeaders defaultChunkedHeaders_;
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
563 CurlHeaders userHeaders_;
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
564 CurlRequestBody requestBody_;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567
2787
ad2c32082653 Fix OrthancPluginHttpClient() to return the HTTP status on errors
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2677
diff changeset
568 void HttpClient::ThrowException(HttpStatus status)
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
569 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
570 switch (status)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
571 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
572 case HttpStatus_400_BadRequest:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
573 throw OrthancException(ErrorCode_BadRequest);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
574
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
575 case HttpStatus_401_Unauthorized:
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
576 case HttpStatus_403_Forbidden:
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
577 throw OrthancException(ErrorCode_Unauthorized);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
578
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
579 case HttpStatus_404_NotFound:
2044
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2041
diff changeset
580 throw OrthancException(ErrorCode_UnknownResource);
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
581
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
582 default:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
583 throw OrthancException(ErrorCode_NetworkProtocol);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
584 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
585 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
586
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
587
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
588 /*static int CurlDebugCallback(CURL *handle,
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
589 curl_infotype type,
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
590 char *data,
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
591 size_t size,
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
592 void *userptr)
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
593 {
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
594 switch (type)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
595 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
596 case CURLINFO_TEXT:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
597 case CURLINFO_HEADER_IN:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
598 case CURLINFO_HEADER_OUT:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
599 case CURLINFO_SSL_DATA_IN:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
600 case CURLINFO_SSL_DATA_OUT:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
601 case CURLINFO_END:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
602 case CURLINFO_DATA_IN:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
603 case CURLINFO_DATA_OUT:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
604 {
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
605 std::string s(data, size);
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
606 CLOG(INFO, INFO) << "libcurl: " << s;
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
607 break;
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
608 }
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
609
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
610 default:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
611 break;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
612 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
613
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
614 return 0;
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
615 }*/
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
616
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
617
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
618 void HttpClient::Setup()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
620 pimpl_->defaultPostHeaders_.AddHeader("Expect", "");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
621 pimpl_->defaultChunkedHeaders_.AddHeader("Expect", "");
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
622 pimpl_->defaultChunkedHeaders_.AddHeader("Transfer-Encoding", "chunked");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624 pimpl_->curl_ = curl_easy_init();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
626 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERFUNCTION, &CurlAnswer::HeaderCallback));
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
627 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlAnswer::BodyCallback));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630
456
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
631 // This fixes the "longjmp causes uninitialized stack frame" crash
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
632 // that happens on modern Linux versions.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
633 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
634 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
635
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 url_ = "";
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
637 method_ = HttpMethod_Get;
3360
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3122
diff changeset
638 lastStatus_ = HttpStatus_None;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
639 SetVerbose(GlobalParameters::GetInstance().IsDefaultVerbose());
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
640 timeout_ = GlobalParameters::GetInstance().GetDefaultTimeout();
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
641 GlobalParameters::GetInstance().GetDefaultProxy(proxy_);
2219
262fcab24024 fix for HttpsCACertificates
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
642 GlobalParameters::GetInstance().GetSslConfiguration(verifyPeers_, caCertificates_);
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
643
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
644 hasExternalBody_ = false;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
645 externalBodyData_ = NULL;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
646 externalBodySize_ = 0;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
650 HttpClient::HttpClient() :
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
651 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
652 verifyPeers_(true),
2045
21a8ca9ad928 HttpClient::SetConvertHeadersToLowerCase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2044
diff changeset
653 pkcs11Enabled_(false),
2107
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
654 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
655 redirectionFollowed_(true)
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
656 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
657 Setup();
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
658 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
659
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
660
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
661 HttpClient::HttpClient(const WebServiceParameters& service,
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
662 const std::string& uri) :
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
663 pimpl_(new PImpl),
2049
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2045
diff changeset
664 verifyPeers_(true),
2107
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
665 headersToLowerCase_(true),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2104
diff changeset
666 redirectionFollowed_(true)
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
667 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
668 Setup();
2020
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
669
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
670 if (service.GetUsername().size() != 0 &&
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
671 service.GetPassword().size() != 0)
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
672 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
673 SetCredentials(service.GetUsername().c_str(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
674 service.GetPassword().c_str());
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
675 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
676
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
677 if (!service.GetCertificateFile().empty())
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
678 {
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
679 SetClientCertificate(service.GetCertificateFile(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
680 service.GetCertificateKeyFile(),
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
681 service.GetCertificateKeyPassword());
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
682 }
a0bd8cd55da7 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2019
diff changeset
683
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
684 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
685
5143
95d8e0540219 avoid double '/' in HttpClient uri
Alain Mazy <am@osimis.io>
parents: 5100
diff changeset
686 SetUrl(Toolbox::JoinUri(service.GetUrl(), uri));
2800
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
687
2823
807169f85ba9 OrthancPluginGetPeerUserProperty()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2800
diff changeset
688 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
689 it = service.GetHttpHeaders().begin();
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
690 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
691 {
dc7330089736 "OrthancPeers" configuration option now allows to specify HTTP headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2788
diff changeset
692 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
693 }
4522
017ab543e6ef added field "Timeout" in "OrthancPeers" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4521
diff changeset
694
017ab543e6ef added field "Timeout" in "OrthancPeers" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4521
diff changeset
695 if (service.HasTimeout())
017ab543e6ef added field "Timeout" in "OrthancPeers" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4521
diff changeset
696 {
017ab543e6ef added field "Timeout" in "OrthancPeers" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4521
diff changeset
697 SetTimeout(service.GetTimeout());
017ab543e6ef added field "Timeout" in "OrthancPeers" configuration option
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4521
diff changeset
698 }
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
699 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
700
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
701
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
702 HttpClient::~HttpClient()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
703 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
704 curl_easy_cleanup(pimpl_->curl_);
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
705 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
706
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
707 void HttpClient::SetUrl(const char *url)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
708 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
709 url_ = std::string(url);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
710 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
711
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
712 void HttpClient::SetUrl(const std::string &url)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
713 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
714 url_ = url;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
715 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
716
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
717 const std::string &HttpClient::GetUrl() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
718 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
719 return url_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
720 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
721
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
722 void HttpClient::SetMethod(HttpMethod method)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
723 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
724 method_ = method;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
725 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
726
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
727 HttpMethod HttpClient::GetMethod() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
728 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
729 return method_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
730 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
731
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
732 void HttpClient::SetTimeout(long seconds)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
733 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
734 timeout_ = seconds;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
735 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
736
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
737 long HttpClient::GetTimeout() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
738 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
739 return timeout_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
740 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
741
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
742
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
743 void HttpClient::AssignBody(const std::string& data)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
744 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
745 body_ = data;
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
746 pimpl_->requestBody_.Clear();
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
747 hasExternalBody_ = false;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
748 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
749
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
750
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
751 void HttpClient::AssignBody(const void* data,
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
752 size_t size)
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
753 {
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
754 if (size != 0 &&
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
755 data == NULL)
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
756 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
757 throw OrthancException(ErrorCode_NullPointer);
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
758 }
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
759 else
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
760 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
761 body_.assign(reinterpret_cast<const char*>(data), size);
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
762 pimpl_->requestBody_.Clear();
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
763 hasExternalBody_ = false;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
764 }
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
765 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
766
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
767
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
768 void HttpClient::SetBody(IRequestBody& body)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
769 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
770 body_.clear();
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
771 pimpl_->requestBody_.SetBody(body);
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
772 hasExternalBody_ = false;
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
773 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
774
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
775
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
776 void HttpClient::SetExternalBody(const void* data,
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
777 size_t size)
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
778 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
779 if (size != 0 &&
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
780 data == NULL)
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
781 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
782 throw OrthancException(ErrorCode_NullPointer);
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
783 }
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
784 else
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
785 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
786 body_.clear();
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
787 pimpl_->requestBody_.Clear();
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
788 hasExternalBody_ = true;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
789 externalBodyData_ = data;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
790 externalBodySize_ = size;
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
791 }
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
792 }
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
793
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
794
4657
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
795 void HttpClient::SetExternalBody(const std::string& data)
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
796 {
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
797 SetExternalBody(data.empty() ? NULL : data.c_str(), data.size());
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
798 }
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
799
e8967149d87a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4650
diff changeset
800
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
801 void HttpClient::ClearBody()
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
802 {
3387
a48d652f1500 new function OrthancPluginHttpClientChunkedBody(), new class OrthancPlugins::HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3386
diff changeset
803 body_.clear();
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
804 pimpl_->requestBody_.Clear();
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
805 hasExternalBody_ = false;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
806 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
807
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
808
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
809 void HttpClient::SetVerbose(bool isVerbose)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
810 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
811 isVerbose_ = isVerbose;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
812
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
813 if (isVerbose_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
814 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
815 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
816 //CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_DEBUGFUNCTION, &CurlDebugCallback));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
817 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
818 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
819 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
820 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
821 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
822 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
823
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
824 bool HttpClient::IsVerbose() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
825 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
826 return isVerbose_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
827 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
828
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
830 void HttpClient::AddHeader(const std::string& key,
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
831 const std::string& value)
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
832 {
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
833 if (key.empty())
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
834 {
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
835 throw OrthancException(ErrorCode_ParameterOutOfRange);
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
836 }
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
837 else
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
838 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
839 pimpl_->userHeaders_.AddHeader(key, value);
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
840 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
841 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
842
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
843
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
844 void HttpClient::ClearHeaders()
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
845 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
846 pimpl_->userHeaders_.Clear();
1986
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
847 }
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
848
99b249867052 HTTP headers in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1973
diff changeset
849
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
850 bool HttpClient::ApplyInternal(CurlAnswer& answer)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
851 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
852 CLOG(INFO, HTTP) << "New HTTP request to: " << url_ << " (timeout: "
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
853 << boost::lexical_cast<std::string>(timeout_ <= 0 ? DEFAULT_HTTP_TIMEOUT : timeout_) << "s)";
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
854
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
855 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str()));
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
856 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADERDATA, &answer));
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
857
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
858 #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
859 // 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
860
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
861 if (verifyPeers_)
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
862 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
863 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
864 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
865 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
866 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
867 else
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
868 {
1973
7bd4cb780feb Option "HttpsVerifyPeers" can be used to connect against self-signed HTTPS certificates
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
869 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
870 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
871 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
872 #endif
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
873
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
874 // 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
875 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
876 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
877 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
878 throw OrthancException(ErrorCode_ParameterOutOfRange,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
879 "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
880 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
881
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
882 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
883 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
884 #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
885 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
886 {
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
887 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
888 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
889 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
890 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
891 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
892 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
893 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
894 "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
895 "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
896 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
897 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
898 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
899 "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
900 #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
901 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
902 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
903 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
904 #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
905 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
906 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSLCERT, clientCertificateFile_.c_str()));
4195
Alain Mazy <alain@mazy.be>
parents: 4151
diff changeset
907 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_KEYPASSWD, clientCertificateKeyPassword_.c_str()));
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
908
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
909 // 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
910 // 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
911 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
912 {
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
913 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
914 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
915 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
916 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
917 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
918 "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
919 "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
920 #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
921 }
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
922
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
923 // Reset the parameters from previous calls to Apply()
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
924 pimpl_->userHeaders_.Assign(pimpl_->curl_);
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
925 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
926 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
927 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
928 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
929 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
2104
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
930 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
931 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, NULL));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
932
2104
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
933 if (redirectionFollowed_)
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
934 {
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
935 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1L));
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
936 }
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
937 else
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
938 {
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
939 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 0L));
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
940 }
58a0ee0b4be1 HttpClient::SetRedirectionFollowed
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2099
diff changeset
941
1186
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
942 // Set timeouts
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
943 if (timeout_ <= 0)
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
944 {
3428
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
945 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, DEFAULT_HTTP_TIMEOUT));
6add197274b1 default value of HttpTimeout is now 60 seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3393
diff changeset
946 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, DEFAULT_HTTP_TIMEOUT));
1186
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
947 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
948 else
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
949 {
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
950 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
951 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
952 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
953
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
954 if (credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
955 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
956 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
957 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
958
1273
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
959 if (proxy_.size() != 0)
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
960 {
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
961 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
962 }
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
963
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
964 switch (method_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
965 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
966 case HttpMethod_Get:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
967 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
968 break;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
969
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
970 case HttpMethod_Post:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
971 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
972
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
973 break;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
974
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
975 case HttpMethod_Delete:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
976 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
977 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
978 break;
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
979
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
980 case HttpMethod_Put:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
981 // http://stackoverflow.com/a/7570281/881731: Don't use
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
982 // CURLOPT_PUT if there is a body
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
983
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
984 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
985
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
986 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
987 break;
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
988
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
989 default:
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
990 throw OrthancException(ErrorCode_InternalError);
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
991 }
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
992
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
993 if (method_ == HttpMethod_Post ||
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
994 method_ == HttpMethod_Put)
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
995 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
996 if (!pimpl_->userHeaders_.IsEmpty() &&
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
997 !pimpl_->userHeaders_.HasExpect())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
998 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
999 CLOG(INFO, HTTP) << "For performance, the HTTP header \"Expect\" should be set to empty string in POST/PUT requests";
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1000 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1001
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1002 if (pimpl_->requestBody_.IsValid())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1003 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1004 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READFUNCTION, CurlRequestBody::Callback));
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1005 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READDATA, &pimpl_->requestBody_));
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1006 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1007 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, -1L));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1008
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1009 if (pimpl_->userHeaders_.IsEmpty())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1010 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1011 pimpl_->defaultChunkedHeaders_.Assign(pimpl_->curl_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1012 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1013 else if (!pimpl_->userHeaders_.IsChunkedTransfer())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1014 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1015 LOG(WARNING) << "The HTTP header \"Transfer-Encoding\" must be set to \"chunked\" "
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1016 << "if streaming a chunked body in POST/PUT requests";
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1017 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1018 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1019 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1020 {
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1021 // Disable possible previous stream transfers
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1022 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_READFUNCTION, NULL));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1023 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_UPLOAD, 0));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1024
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1025 if (pimpl_->userHeaders_.IsChunkedTransfer())
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1026 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1027 LOG(WARNING) << "The HTTP header \"Transfer-Encoding\" must only be set "
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1028 << "if streaming a chunked body in POST/PUT requests";
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1029 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1030
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1031 if (pimpl_->userHeaders_.IsEmpty())
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1032 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1033 pimpl_->defaultPostHeaders_.Assign(pimpl_->curl_);
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1034 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1035
4650
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1036 if (hasExternalBody_)
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1037 {
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1038 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, externalBodyData_));
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1039 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, externalBodySize_));
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1040 }
9804d6490872 Reduced memory consumption of HTTP/REST plugins calls on POST/PUT if chunked transfer is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4522
diff changeset
1041 else if (body_.size() > 0)
3386
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1042 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1043 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1044 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1045 }
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1046 else
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1047 {
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1048 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1049 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
af9432e46c07 HttpClient::IBodyStream
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3379
diff changeset
1050 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1051 }
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
1052 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1053
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1054
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1055 // Do the actual request
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1056 CURLcode code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1057 long status = 0;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1058
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1059 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer));
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
1060
3516
96780208dbd7 reporting timings in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3428
diff changeset
1061 const boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
96780208dbd7 reporting timings in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3428
diff changeset
1062
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1063 if (boost::starts_with(url_, "https://"))
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1064 {
5095
b52fe770aec0 more logs in HttpClient
Alain Mazy <am@osimis.io>
parents: 5021
diff changeset
1065 code = OrthancHttpClientPerformSSL(pimpl_->curl_, &status, url_);
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1066 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1067 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1068 {
5095
b52fe770aec0 more logs in HttpClient
Alain Mazy <am@osimis.io>
parents: 5021
diff changeset
1069 code = GetHttpStatus(curl_easy_perform(pimpl_->curl_), pimpl_->curl_, &status, url_);
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1070 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1071
3516
96780208dbd7 reporting timings in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3428
diff changeset
1072 const boost::posix_time::ptime end = boost::posix_time::microsec_clock::universal_time();
96780208dbd7 reporting timings in HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3428
diff changeset
1073
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1074 CLOG(INFO, HTTP) << "HTTP status code " << status << " in "
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1075 << ((end - start).total_milliseconds()) << " ms after "
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1076 << EnumerationToString(method_) << " request on: " << url_;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1077
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1078 if (isVerbose_)
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1079 {
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1080 CLOG(INFO, HTTP) << "cURL status code: " << code;
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1081 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1082
5100
9d51c000e91a more verbose HTTPClient errors + avoid duplicate logging of same error
Alain Mazy <am@osimis.io>
parents: 5095
diff changeset
1083 CheckCode(code, url_); // throws on HTTP error
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1084
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1085 if (status == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1086 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1087 // 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
1088 lastStatus_ = HttpStatus_500_InternalServerError;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1089 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1090 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1091 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
1092 lastStatus_ = static_cast<HttpStatus>(status);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1093 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1094
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1095 if (status >= 200 && status < 300)
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1096 {
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1097 return true; // Success
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
1098 }
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
1099 else
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
1100 {
3122
df4f977c2f88 trying to reproduce isse 32 in C++
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3114
diff changeset
1101 LOG(ERROR) << "Error in HTTP request, received HTTP status " << status
5021
559b35d18ef7 Improved HttpClient error logging (add method + url)
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
1102 << " (" << EnumerationToString(lastStatus_) << ") after "
559b35d18ef7 Improved HttpClient error logging (add method + url)
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
1103 << EnumerationToString(method_) << " request on: " << url_;
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1104 return false;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1105 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1106 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1107
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1108
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1109 bool HttpClient::ApplyInternal(std::string& answerBody,
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1110 HttpHeaders* answerHeaders)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1111 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1112 answerBody.clear();
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1113
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1114 DefaultAnswer answer;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1115
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1116 if (answerHeaders != NULL)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1117 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1118 answer.SetHeaders(*answerHeaders);
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1119 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1120
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1121 CurlAnswer wrapper(answer, headersToLowerCase_);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1122
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1123 if (ApplyInternal(wrapper))
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1124 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1125 answer.FlattenBody(answerBody);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1126 return true;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1127 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1128 else
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1129 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1130 return false;
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1131 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1132 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1133
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1134
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1135 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
1136 HttpClient::HttpHeaders* answerHeaders)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1137 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1138 std::string s;
2040
6ea2e264ca50 retrieval of HTTP headers in answers within HttpClient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2025
diff changeset
1139 if (ApplyInternal(s, answerHeaders))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1140 {
4392
3af1d763763a confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1141 return Toolbox::ReadJson(answerBody, s);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1142 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1143 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1144 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1145 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1146 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1147 }
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
1148
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
1149
468
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
1150 void HttpClient::SetCredentials(const char* username,
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
1151 const char* password)
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
1152 {
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
1153 credentials_ = std::string(username) + ":" + std::string(password);
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
1154 }
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1155
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1156 void HttpClient::SetProxy(const std::string &proxy)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1157 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1158 proxy_ = proxy;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1159 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1160
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1161 void HttpClient::SetHttpsVerifyPeers(bool verify)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1162 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1163 verifyPeers_ = verify;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1164 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1165
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1166 bool HttpClient::IsHttpsVerifyPeers() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1167 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1168 return verifyPeers_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1169 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1170
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1171 void HttpClient::SetHttpsCACertificates(const std::string &certificates)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1172 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1173 caCertificates_ = certificates;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1174 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1175
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1176 const std::string &HttpClient::GetHttpsCACertificates() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1177 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1178 return caCertificates_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1179 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1180
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1181
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1182 void HttpClient::ConfigureSsl(bool httpsVerifyPeers,
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1183 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
1184 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
1185 #if ORTHANC_ENABLE_SSL == 1
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1186 if (httpsVerifyPeers)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1187 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1188 if (httpsVerifyCertificates.empty())
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1189 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1190 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
1191 << "set \"HttpsCACertificates\" if you need to do HTTPS requests";
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1192 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1193 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1194 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1195 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
1196 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1197 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1198 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1199 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1200 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
1201 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1202 #endif
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1203
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1204 GlobalParameters::GetInstance().ConfigureSsl(httpsVerifyPeers, httpsVerifyCertificates);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1205 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1206
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1207
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1208 void HttpClient::GlobalInitialize()
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1209 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
1210 #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
1211 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
1212 #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
1213 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
1214 #endif
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1215 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
1216
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1217
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1218 void HttpClient::GlobalFinalize()
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1219 {
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1220 curl_global_cleanup();
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
1221
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
1222 #if ORTHANC_ENABLE_PKCS11 == 1
2025
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
1223 Pkcs11::Finalize();
e7e1858d9504 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2024
diff changeset
1224 #endif
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
1225 }
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1226
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
1227
2788
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1228 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
1229 {
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1230 GlobalParameters::GetInstance().SetDefaultVerbose(verbose);
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1231 }
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1232
959bd8857eb5 New configuration option: "HttpVerbose" to debug outgoing HTTP connections
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2787
diff changeset
1233
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1234 void HttpClient::SetDefaultProxy(const std::string& proxy)
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1235 {
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1236 GlobalParameters::GetInstance().SetDefaultProxy(proxy);
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1237 }
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1238
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1239
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
1240 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
1241 {
1987
ce90d109bb64 new plugin functions: OrthancPluginHttpClient and OrthancPluginGenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1986
diff changeset
1242 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
1243 }
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1244
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1245
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1246 bool HttpClient::Apply(IAnswer& answer)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1247 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1248 CurlAnswer wrapper(answer, headersToLowerCase_);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1249 return ApplyInternal(wrapper);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1250 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1251
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1252 bool HttpClient::Apply(std::string &answerBody)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1253 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1254 return ApplyInternal(answerBody, NULL);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1255 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1256
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1257 bool HttpClient::Apply(Json::Value &answerBody)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1258 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1259 return ApplyInternal(answerBody, NULL);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1260 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1261
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1262 bool HttpClient::Apply(std::string &answerBody,
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1263 HttpClient::HttpHeaders &answerHeaders)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1264 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1265 return ApplyInternal(answerBody, &answerHeaders);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1266 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1267
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1268 bool HttpClient::Apply(Json::Value &answerBody,
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1269 HttpClient::HttpHeaders &answerHeaders)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1270 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1271 return ApplyInternal(answerBody, &answerHeaders);
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1272 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1273
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1274 HttpStatus HttpClient::GetLastStatus() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1275 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1276 return lastStatus_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1277 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1278
3393
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1279
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1280 void HttpClient::ApplyAndThrowException(IAnswer& answer)
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1281 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1282 CurlAnswer wrapper(answer, headersToLowerCase_);
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1283
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1284 if (!ApplyInternal(wrapper))
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1285 {
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1286 ThrowException(GetLastStatus());
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1287 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1288 }
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1289
2cd0369a156f support of chunked answers in HttpClient and in SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3392
diff changeset
1290
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1291 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
1292 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1293 if (!Apply(answerBody))
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1294 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1295 ThrowException(GetLastStatus());
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1296 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1297 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1298
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1299
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1300 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
1301 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1302 if (!Apply(answerBody))
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1303 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1304 ThrowException(GetLastStatus());
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1305 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1306 }
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1307
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1308
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1309 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
1310 HttpHeaders& answerHeaders)
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1311 {
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1312 if (!Apply(answerBody, answerHeaders))
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1313 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1314 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1315 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1316 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1317
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1318
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1319 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
1320 HttpHeaders& answerHeaders)
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1321 {
2041
9f61ca1e3eb3 OrthancPluginHttpClient can return the HTTP headers of the answer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2040
diff changeset
1322 if (!Apply(answerBody, answerHeaders))
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1323 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1324 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1325 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
1326 }
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
1327
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
1328
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
1329 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
1330 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
1331 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
1332 {
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
1333 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
1334 {
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
1335 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
1336 }
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
1337
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2136
diff changeset
1338 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
1339 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1340 throw OrthancException(ErrorCode_InexistentFile,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1341 "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
1342 }
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
1343
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
1344 if (!certificateKeyFile.empty() &&
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2136
diff changeset
1345 !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
1346 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1347 throw OrthancException(ErrorCode_InexistentFile,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1348 "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
1349 }
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
1350
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
1351 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
1352 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
1353 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
1354 }
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
1355
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1356 void HttpClient::SetPkcs11Enabled(bool enabled)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1357 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1358 pkcs11Enabled_ = enabled;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1359 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1360
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1361 bool HttpClient::IsPkcs11Enabled() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1362 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1363 return pkcs11Enabled_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1364 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1365
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1366 const std::string &HttpClient::GetClientCertificateFile() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1367 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1368 return clientCertificateFile_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1369 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1370
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1371 const std::string &HttpClient::GetClientCertificateKeyFile() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1372 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1373 return clientCertificateKeyFile_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1374 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1375
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1376 const std::string &HttpClient::GetClientCertificateKeyPassword() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1377 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1378 return clientCertificateKeyPassword_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1379 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1380
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1381 void HttpClient::SetConvertHeadersToLowerCase(bool lowerCase)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1382 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1383 headersToLowerCase_ = lowerCase;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1384 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1385
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1386 bool HttpClient::IsConvertHeadersToLowerCase() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1387 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1388 return headersToLowerCase_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1389 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1390
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1391 void HttpClient::SetRedirectionFollowed(bool follow)
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1392 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1393 redirectionFollowed_ = follow;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1394 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1395
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1396 bool HttpClient::IsRedirectionFollowed() const
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1397 {
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1398 return redirectionFollowed_;
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1399 }
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
1400
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
1401
fefbe71c2272 Possibility to use PKCS#11 authentication for hardware security modules with Orthanc peers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2020
diff changeset
1402 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
1403 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
1404 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
1405 {
2136
dd609a99d39a uniformization of the macro naming
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2107
diff changeset
1406 #if ORTHANC_ENABLE_PKCS11 == 1
4521
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1407 CLOG(INFO, HTTP) << "Initializing PKCS#11 using " << module
60e4f94ec30f using CLOG(INFO, HTTP) in HttpClient instead of LOG(INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
1408 << (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
1409 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
1410 #else
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1411 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2823
diff changeset
1412 "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
1413 #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
1414 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1415 }