annotate Core/HttpClient.cpp @ 1606:31f4adefb88f

issuing HTTP requests from the plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 01 Sep 2015 17:37:26 +0200
parents ba0226474e22
children b1291df2f780
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
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1273
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1273
diff changeset
4 * Department, University Hospital of Liege, Belgium
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
6 * This program is free software: you can redistribute it and/or
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
7 * modify it under the terms of the GNU General Public License as
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
9 * License, or (at your option) any later version.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
11 * In addition, as a special exception, the copyright holders of this
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
12 * program give permission to link the code of its release with the
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
15 * the linked executables. You must obey the GNU General Public License
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
19 * you do not wish to do so, delete this exception statement from your
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
20 * version. If you delete this exception statement from all source files
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
21 * in the program, then also delete it here.
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
22 *
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
23 * This program is distributed in the hope that it will be useful, but
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
26 * General Public License for more details.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
493
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
28 * You should have received a copy of the GNU General Public License
ff34c51cd3dd fix licensing terms
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 477
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
33 #include "PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "HttpClient.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
1404
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
36 #include "Toolbox.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
37 #include "OrthancException.h"
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
38 #include "Logging.h"
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
39
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <curl/curl.h>
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
42 #include <boost/algorithm/string/predicate.hpp>
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
43
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
44
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
45 static std::string globalCACertificates_;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
46 static bool globalVerifyPeers_ = true;
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
47 static long globalTimeout_ = 0;
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
48
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
49 extern "C"
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
50 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
51 static CURLcode GetHttpStatus(CURLcode code, CURL* curl, long* status)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
52 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
53 if (code == CURLE_OK)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
54 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
55 code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
56 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
57 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
58 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
59 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
60 *status = 0;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
61 return code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
62 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
63 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
64
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
65 // This is a dummy wrapper function to suppress any OpenSSL-related
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
66 // problem in valgrind. Inlining is prevented.
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
67 #if defined(__GNUC__) || defined(__clang__)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
68 __attribute__((noinline))
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
69 #endif
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
70 static CURLcode OrthancHttpClientPerformSSL(CURL* curl, long* status)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
71 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
72 return GetHttpStatus(curl_easy_perform(curl), curl, status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
73 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
74 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
75
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
60
77aec9be0a51 renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
78 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 struct HttpClient::PImpl
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 CURL* curl_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 struct curl_slist *postHeaders_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
87 static void ThrowException(HttpStatus status)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
88 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
89 switch (status)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
90 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
91 case HttpStatus_400_BadRequest:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
92 throw OrthancException(ErrorCode_BadRequest);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
93
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
94 case HttpStatus_401_Unauthorized:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
95 throw OrthancException(ErrorCode_Unauthorized);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
96
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
97 case HttpStatus_404_NotFound:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
98 throw OrthancException(ErrorCode_InexistentItem);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
99
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
100 default:
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
101 throw OrthancException(ErrorCode_NetworkProtocol);
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
102 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
103 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
104
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
105
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
106
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 static CURLcode CheckCode(CURLcode code)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if (code != CURLE_OK)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
111 LOG(ERROR) << "libCURL error: " + std::string(curl_easy_strerror(code));
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
112 throw OrthancException(ErrorCode_NetworkProtocol);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 return code;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 static size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *payload)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 std::string& target = *(static_cast<std::string*>(payload));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 size_t length = size * nmemb;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 if (length == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 return 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 size_t pos = target.size();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 target.resize(pos + length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 memcpy(&target.at(pos), buffer, length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 return length;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
136 void HttpClient::Setup()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 pimpl_->postHeaders_ = NULL;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 {
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
141 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 pimpl_->curl_ = curl_easy_init();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 if (!pimpl_->curl_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 curl_slist_free_all(pimpl_->postHeaders_);
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
148 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
456
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
155 // This fixes the "longjmp causes uninitialized stack frame" crash
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
156 // that happens on modern Linux versions.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
157 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
158 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
159
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 url_ = "";
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
161 method_ = HttpMethod_Get;
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
162 lastStatus_ = HttpStatus_200_Ok;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 isVerbose_ = false;
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
164 timeout_ = globalTimeout_;
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
165 verifyPeers_ = globalVerifyPeers_;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
169 HttpClient::HttpClient() : pimpl_(new PImpl)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
170 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
171 Setup();
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
172 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
173
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
174
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
175 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
176 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
177 Setup();
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
178
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
179 if (other.IsVerbose())
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
180 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
181 SetVerbose(true);
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
182 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
183
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
184 if (other.credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
185 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
186 credentials_ = other.credentials_;
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
187 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
188 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
189
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
190
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 HttpClient::~HttpClient()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 curl_easy_cleanup(pimpl_->curl_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 curl_slist_free_all(pimpl_->postHeaders_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 void HttpClient::SetVerbose(bool isVerbose)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 isVerbose_ = isVerbose;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 if (isVerbose_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 bool HttpClient::Apply(std::string& answer)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 answer.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str()));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer));
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
218
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
219 // Setup HTTPS-related options
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
220 #if ORTHANC_SSL_ENABLED == 1
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
221 if (IsHttpsVerifyPeers())
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
222 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
223 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CAINFO, GetHttpsCACertificates().c_str()));
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
224 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
225 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
226 else
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
227 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
228 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
229 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
230 #endif
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
231
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
232 // Reset the parameters from previous calls to Apply()
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL));
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
234 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
235 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
236 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
237 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, NULL));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
238 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
239 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
1273
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
240 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PROXY, NULL));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
1186
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
242 // Set timeouts
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
243 if (timeout_ <= 0)
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
244 {
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
245 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_TIMEOUT, 10)); /* default: 10 seconds */
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
246 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CONNECTTIMEOUT, 10)); /* default: 10 seconds */
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
247 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
248 else
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
249 {
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
250 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
251 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
252 }
b17b6bd59747 timeouts for HTTP and DICOM
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1052
diff changeset
253
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
254 if (credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
255 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
256 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
257 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
258
1273
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
259 if (proxy_.size() != 0)
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
260 {
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
261 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
262 }
88010d8e12cf Support of HTTP proxy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1186
diff changeset
263
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 switch (method_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
266 case HttpMethod_Get:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
270 case HttpMethod_Post:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_));
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
273 break;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
275 case HttpMethod_Delete:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
276 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
277 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
278 break;
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
279
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
280 case HttpMethod_Put:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
281 // http://stackoverflow.com/a/7570281/881731: Don't use
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
282 // CURLOPT_PUT if there is a body
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
283
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
284 // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
285
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
286 curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
287 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_));
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
288 break;
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
289
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
290 default:
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
291 throw OrthancException(ErrorCode_InternalError);
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
292 }
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
293
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
294
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
295 if (method_ == HttpMethod_Post ||
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
296 method_ == HttpMethod_Put)
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
297 {
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
298 if (body_.size() > 0)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 {
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
300 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
301 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
1052
cc4ff680e2a0 http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
308 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 // Do the actual request
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
312 CURLcode code;
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
313 long status = 0;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
315 if (boost::starts_with(url_, "https://"))
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
316 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
317 code = OrthancHttpClientPerformSSL(pimpl_->curl_, &status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
318 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
319 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
320 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
321 code = GetHttpStatus(curl_easy_perform(pimpl_->curl_), pimpl_->curl_, &status);
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
322 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
323
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
324 CheckCode(code);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 if (status == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 // 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
329 lastStatus_ = HttpStatus_500_InternalServerError;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
333 lastStatus_ = static_cast<HttpStatus>(status);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 return (status >= 200 && status < 300);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 bool HttpClient::Apply(Json::Value& answer)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 std::string s;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 if (Apply(s))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 Json::Reader reader;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 return reader.parse(s, answer);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 }
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
353
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
354
468
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
355 void HttpClient::SetCredentials(const char* username,
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
356 const char* password)
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
357 {
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
358 credentials_ = std::string(username) + ":" + std::string(password);
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
359 }
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
360
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
361
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
362 const std::string& HttpClient::GetHttpsCACertificates() const
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
363 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
364 if (caCertificates_.empty())
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
365 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
366 return globalCACertificates_;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
367 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
368 else
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
369 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
370 return caCertificates_;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
371 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
372 }
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
373
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
374
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
375 void HttpClient::GlobalInitialize(bool httpsVerifyPeers,
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
376 const std::string& httpsVerifyCertificates)
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
377 {
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
378 globalVerifyPeers_ = httpsVerifyPeers;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
379 globalCACertificates_ = httpsVerifyCertificates;
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
380
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
381 #if ORTHANC_SSL_ENABLED == 1
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
382 if (httpsVerifyPeers)
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
383 {
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
384 if (globalCACertificates_.empty())
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
385 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
386 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
387 << "set \"HttpsCACertificates\" if you need to do HTTPS requests";
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
388 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
389 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
390 {
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
391 LOG(WARNING) << "HTTPS will use the CA certificates from this file: " << globalCACertificates_;
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
392 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
393 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
394 else
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
395 {
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
396 LOG(WARNING) << "The verification of the peers in HTTPS requests is disabled!";
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
397 }
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
398 #endif
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
399
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
400 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT));
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
401 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1404
diff changeset
402
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
403
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
404 void HttpClient::GlobalFinalize()
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
405 {
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
406 curl_global_cleanup();
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
407 }
1590
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
408
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
409
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
410 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
411 {
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
412 LOG(INFO) << "Setting the default timeout for HTTP client connections: " << timeout << " seconds";
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
413 globalTimeout_ = timeout;
ba0226474e22 New configuration option: "HttpTimeout" to set the default timeout for HTTP requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1534
diff changeset
414 }
1606
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
415
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
416
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
417 void HttpClient::ApplyAndThrowException(std::string& answer)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
418 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
419 if (!Apply(answer))
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
420 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
421 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
422 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
423 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
424
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
425 void HttpClient::ApplyAndThrowException(Json::Value& answer)
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
426 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
427 if (!Apply(answer))
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
428 {
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
429 ThrowException(GetLastStatus());
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
430 }
31f4adefb88f issuing HTTP requests from the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1590
diff changeset
431 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 }