annotate Core/HttpClient.cpp @ 833:30efdc8fac70

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 02 Jun 2014 12:17:47 +0200
parents a811bdf8b8eb
children cc4ff680e2a0
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
689
2d0a347e8cfc switch to 2014
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 493
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
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
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
36 #include "../Core/Toolbox.h"
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
37 #include "../Core/OrthancException.h"
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
38
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <curl/curl.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
60
77aec9be0a51 renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
43 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 struct HttpClient::PImpl
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 CURL* curl_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 struct curl_slist *postHeaders_;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 static CURLcode CheckCode(CURLcode code)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if (code != CURLE_OK)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
56 throw OrthancException("libCURL error: " + std::string(curl_easy_strerror(code)));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 return code;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 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
64 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 std::string& target = *(static_cast<std::string*>(payload));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 size_t length = size * nmemb;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if (length == 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 return 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 size_t pos = target.size();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 target.resize(pos + length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 memcpy(&target.at(pos), buffer, length);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 return length;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
80 void HttpClient::Setup()
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 pimpl_->postHeaders_ = NULL;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
85 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 pimpl_->curl_ = curl_easy_init();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (!pimpl_->curl_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 curl_slist_free_all(pimpl_->postHeaders_);
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
92 throw OrthancException(ErrorCode_NotEnoughMemory);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEFUNCTION, &CurlCallback));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HEADER, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_FOLLOWLOCATION, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
60
77aec9be0a51 renaming of cppclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
99 #if ORTHANC_SSL_ENABLED == 1
26
6ba765ecf3db compiling openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
100 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_SSL_VERIFYPEER, 0));
6ba765ecf3db compiling openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
101 #endif
6ba765ecf3db compiling openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
102
456
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
103 // This fixes the "longjmp causes uninitialized stack frame" crash
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
104 // that happens on modern Linux versions.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
105 // http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
106 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOSIGNAL, 1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 400
diff changeset
107
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 url_ = "";
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
109 method_ = HttpMethod_Get;
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
110 lastStatus_ = HttpStatus_200_Ok;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 isVerbose_ = false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
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
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
115 HttpClient::HttpClient() : pimpl_(new PImpl)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
116 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
117 Setup();
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
118 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
119
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
120
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
121 HttpClient::HttpClient(const HttpClient& other) : pimpl_(new PImpl)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
122 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
123 Setup();
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
124
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
125 if (other.IsVerbose())
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
126 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
127 SetVerbose(true);
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
128 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
129
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
130 if (other.credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
131 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
132 credentials_ = other.credentials_;
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
133 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
134 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
135
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
136
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 HttpClient::~HttpClient()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 curl_easy_cleanup(pimpl_->curl_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 curl_slist_free_all(pimpl_->postHeaders_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
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 void HttpClient::SetVerbose(bool isVerbose)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 isVerbose_ = isVerbose;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 if (isVerbose_)
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 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 1));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_VERBOSE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 bool HttpClient::Apply(std::string& answer)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 answer.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_URL, url_.c_str()));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_WRITEDATA, &answer));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
166 if (credentials_.size() != 0)
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
167 {
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
168 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
169 }
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
170
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 switch (method_)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
173 case HttpMethod_Get:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
177 case HttpMethod_Post:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 if (postData_.size() > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str()));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size()));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
194 case HttpMethod_Delete:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "DELETE"));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
199 case HttpMethod_Put:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 default:
475
72cca077abf8 removal of HttpException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 473
diff changeset
204 throw OrthancException(ErrorCode_InternalError);
0
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 // Do the actual request
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 CheckCode(curl_easy_perform(pimpl_->curl_));
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 long status;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 CheckCode(curl_easy_getinfo(pimpl_->curl_, CURLINFO_RESPONSE_CODE, &status));
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 if (status == 0)
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 // 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
216 lastStatus_ = HttpStatus_500_InternalServerError;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 469
diff changeset
220 lastStatus_ = static_cast<HttpStatus>(status);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 return (status >= 200 && status < 300);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 bool HttpClient::Apply(Json::Value& answer)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 std::string s;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 if (Apply(s))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 Json::Reader reader;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 return reader.parse(s, answer);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
240
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
241
468
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
242 void HttpClient::SetCredentials(const char* username,
456b9d2e9af4 rename methods for clarity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 457
diff changeset
243 const char* password)
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
244 {
469
a6fe16a31615 transmitting credentials by copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 468
diff changeset
245 credentials_ = std::string(username) + ":" + std::string(password);
144
aa6c8a942952 http client password
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 60
diff changeset
246 }
457
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
247
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
248
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
249 void HttpClient::GlobalInitialize()
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
250 {
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
251 CheckCode(curl_global_init(CURL_GLOBAL_DEFAULT));
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
252 }
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
253
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
254 void HttpClient::GlobalFinalize()
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
255 {
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
256 curl_global_cleanup();
997282a61ff8 cleaner names
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 456
diff changeset
257 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }