author | František Kučera <franta-hg@frantovo.cz> |
Mon, 04 Apr 2022 23:07:31 +0200 | |
branch | v_0 |
changeset 24 | 4f96098f7c57 |
parent 16 | 60688cf1f165 |
child 25 | dbeae485a3fd |
permissions | -rw-r--r-- |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
14
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
18 |
#include <string> |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <sstream> |
6
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
20 |
#include <iostream> |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <curl/curl.h> |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include "HTTPClient.h" |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
namespace relpipe { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
namespace tr { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
namespace http { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
class HTTPClient::HTTPClientImpl { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
public: |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
CURL* curl; |
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
34 |
char curlErrorBuffer[CURL_ERROR_SIZE]; |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
std::stringstream responseBody; |
6
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
36 |
std::stringstream responseHeaders; |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
HTTPClientImpl(CURL* curl) : curl(curl) { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
6
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
41 |
std::vector<std::string> getResponseHeaders() { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
42 |
std::vector<std::string> heathers; |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
43 |
std::stringstream name; |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
44 |
std::stringstream value; |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
45 |
std::stringstream* current = &name; |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
46 |
for (char ch = responseHeaders.get(); responseHeaders.good(); ch = responseHeaders.get()) { |
16
60688cf1f165
fix header parsing in getResponseHeaders()
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
47 |
if (current == &name && ch == ':') { |
6
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
48 |
current = &value; |
16
60688cf1f165
fix header parsing in getResponseHeaders()
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
49 |
while (responseHeaders.good() && responseHeaders.peek() == ' ') responseHeaders.get(); // skip spaces |
6
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
50 |
} else if (ch == '\n') { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
51 |
if (name.tellp() > 0 && current == &value) { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
52 |
heathers.push_back(name.str()); |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
53 |
heathers.push_back(value.str()); |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
54 |
} else if (name.tellp() > 0 && current == &value) { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
55 |
// TODO: usually "HTTP/1.1 200 OK" → extract HTTP version and message? |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
56 |
} |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
57 |
|
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
58 |
name = std::stringstream(); |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
59 |
value = std::stringstream(); |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
60 |
current = &name; |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
61 |
} else if (ch == '\r') { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
62 |
// ignore |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
63 |
} else { |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
64 |
current->put(ch); |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
65 |
} |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
66 |
} |
59c9ca066322
write response headers as a relation
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
67 |
return heathers; |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
}; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
7
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
72 |
class CurlList { |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
73 |
private: |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
74 |
curl_slist* list = nullptr; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
75 |
public: |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
76 |
CurlList() = default; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
77 |
CurlList(const HTTPClient&) = delete; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
78 |
CurlList& operator=(const CurlList&) = delete; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
79 |
|
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
80 |
virtual ~CurlList() { |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
81 |
curl_slist_free_all(list); |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
82 |
} |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
83 |
|
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
84 |
void append(std::string item) { |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
85 |
list = curl_slist_append(list, item.c_str()); |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
86 |
} |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
87 |
|
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
88 |
curl_slist* getList() { |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
89 |
return list; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
90 |
} |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
91 |
}; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
92 |
|
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
HTTPClient* HTTPClient::open() { |
10
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
94 |
HTTPClient::HTTPClientImpl* impl = new HTTPClient::HTTPClientImpl(curl_easy_init()); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
95 |
|
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
96 |
typedef size_t(*CurlWriteCallback)(char*, size_t, size_t, HTTPClient::HTTPClientImpl*); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
97 |
|
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
98 |
// set response body callback |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
99 |
curl_easy_setopt(impl->curl, CURLOPT_WRITEDATA, impl); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
100 |
curl_easy_setopt(impl->curl, CURLOPT_WRITEFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t { |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
101 |
size_t r = size * nmemb; |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
102 |
impl->responseBody.write(buffer, r); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
103 |
return r; |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
104 |
}); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
105 |
|
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
106 |
// set response headers callback |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
107 |
curl_easy_setopt(impl->curl, CURLOPT_HEADERDATA, impl); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
108 |
curl_easy_setopt(impl->curl, CURLOPT_HEADERFUNCTION, (CurlWriteCallback)[](char* buffer, size_t size, size_t nmemb, HTTPClient::HTTPClientImpl * impl)->size_t { |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
109 |
size_t r = size * nmemb; |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
110 |
impl->responseHeaders.write(buffer, r); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
111 |
return r; |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
112 |
}); |
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
113 |
|
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
114 |
// set the error buffer |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
115 |
curl_easy_setopt(impl->curl, CURLOPT_ERRORBUFFER, impl->curlErrorBuffer); |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
116 |
|
12
2f2d64333867
enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
117 |
// enable HTTP and HTTPS only |
2f2d64333867
enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
118 |
curl_easy_setopt(impl->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
2f2d64333867
enable HTTP and HTTPS protocols only
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
119 |
|
13
18f4f7e93c48
disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
120 |
// disable protocol guesswork |
18f4f7e93c48
disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
121 |
curl_easy_setopt(impl->curl, CURLOPT_DEFAULT_PROTOCOL, "no-default-protocol-specify-http-or-https-explicitly-in-url"); |
18f4f7e93c48
disable protocol guesswork (HTTP or HTTPS must be explicitly specified in the URL)
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
122 |
|
24
4f96098f7c57
TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
123 |
// TODO: automatic decompression of response bodies |
4f96098f7c57
TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
124 |
// curl_easy_setopt(impl->curl, CURLOPT_ACCEPT_ENCODING, ""); |
4f96098f7c57
TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
125 |
// curl_easy_setopt(impl->curl, CURLOPT_HTTP_CONTENT_DECODING, 1L); |
4f96098f7c57
TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
126 |
// curl_easy_setopt(impl->curl, CURLOPT_HTTP_TRANSFER_DECODING, 1L); |
4f96098f7c57
TODO: automatic decompression of response bodies
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
127 |
|
10
479557122717
initialize the CURL callbacks only once in the open() function
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
128 |
return new HTTPClient(impl); |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
129 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
130 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
131 |
HTTPClient::~HTTPClient() { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
132 |
curl_easy_cleanup(impl->curl); |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
133 |
delete impl; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
const HTTPClient::Response HTTPClient::exchange(const Request& request) { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
HTTPClient::Response response; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
|
14
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
139 |
// set request method |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
140 |
std::string method; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
141 |
if (request.method == Method::DELETE) method = "DELETE"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
142 |
else if (request.method == Method::GET) method = "GET"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
143 |
else if (request.method == Method::HEAD) method = "HEAD"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
144 |
else if (request.method == Method::PATCH) method = "PATCH"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
145 |
else if (request.method == Method::POST) method = "POST"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
146 |
else if (request.method == Method::PUT) method = "PUT"; |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
147 |
else throw std::invalid_argument("Unsupported HTTP method: " + std::to_string((int) request.method)); |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
148 |
curl_easy_setopt(impl->curl, CURLOPT_CUSTOMREQUEST, method.c_str()); |
e41abdd36ff1
request methods support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
149 |
if (request.method == Method::HEAD) curl_easy_setopt(impl->curl, CURLOPT_NOBODY, 1L); |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
150 |
|
7
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
151 |
// set URL |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
curl_easy_setopt(impl->curl, CURLOPT_URL, request.url.c_str()); |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
153 |
|
7
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
154 |
// set request headers |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
155 |
CurlList requestHeders; |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
156 |
for (size_t i = 0; i < request.headers.size(); i += 2) requestHeders.append(request.headers[i] + ": " + request.headers[i + 1]); // TODO: validate, no CR/LF... |
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
157 |
curl_easy_setopt(impl->curl, CURLOPT_HTTPHEADER, requestHeders.getList()); |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
|
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
159 |
// clear the error buffer |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
160 |
impl->curlErrorBuffer[0] = 0; |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
161 |
|
7
0b0374746e48
request headers support in HTTPClient
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
162 |
// do HTTP call |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
163 |
CURLcode result = curl_easy_perform(impl->curl); |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
164 |
|
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
165 |
if (result == CURLE_OK) { |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
166 |
|
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
167 |
// response code and fill the result object |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
168 |
curl_easy_getinfo(impl->curl, CURLINFO_RESPONSE_CODE, &response.responseCode); |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
169 |
response.headers = impl->getResponseHeaders(); |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
170 |
response.body = impl->responseBody.str(); |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
171 |
impl->responseBody = std::stringstream(); |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
172 |
|
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
173 |
return response; |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
174 |
} else { |
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
175 |
throw Exception(curl_easy_strerror(result), impl->curlErrorBuffer); |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
176 |
} |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
177 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
178 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
179 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
180 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
181 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
182 |
} |