| 58 | HttpClient::~HttpClient() { curl_easy_cleanup(curl); } |
| 59 | |
| 60 | void HttpClient::SendRPCMessage(const std::string &message, std::string &result) { |
| 61 | |
| 62 | curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); |
| 63 | curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str()); |
| 64 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 65 | |
| 66 | CURLcode res; |
| 67 | |
| 68 | struct string s; |
| 69 | init_string(&s); |
| 70 | |
| 71 | struct curl_slist *headers = NULL; |
| 72 | |
| 73 | for (std::map<std::string, std::string>::iterator header = this->headers.begin(); header != this->headers.end(); ++header) { |
| 74 | headers = curl_slist_append(headers, (header->first + ": " + header->second).c_str()); |
| 75 | } |
| 76 | |
| 77 | headers = curl_slist_append(headers, "Content-Type: application/json"); |
| 78 | headers = curl_slist_append(headers, "charsets: utf-8"); |
| 79 | |
| 80 | curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message.c_str()); |
| 81 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 82 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 83 | curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); |
| 84 | |
| 85 | res = curl_easy_perform(curl); |
| 86 | |
| 87 | result = s.ptr; |
| 88 | free(s.ptr); |
| 89 | curl_slist_free_all(headers); |
| 90 | if (res != CURLE_OK) { |
| 91 | std::stringstream str; |
| 92 | str << "libcurl error: " << res; |
| 93 | |
| 94 | if (res == 7) |
| 95 | str << " -> Could not connect to " << this->url; |
| 96 | else if (res == 28) |
| 97 | str << " -> Operation timed out"; |
| 98 | throw JsonRpcException(Errors::ERROR_CLIENT_CONNECTOR, str.str()); |
| 99 | } |
| 100 | |
| 101 | long http_code = 0; |
| 102 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); |
| 103 | |
| 104 | if (http_code / 100 != 2) { |
| 105 | throw JsonRpcException(Errors::ERROR_RPC_INTERNAL_ERROR, result); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void HttpClient::SetUrl(const std::string &url) { this->url = url; } |
| 110 |
no test coverage detected