|
| 1 | +/** |
| 2 | + * @file connection.cpp |
| 3 | + * @brief implementation of the connection class |
| 4 | + * @author Daniel Schauenberg <[email protected]> |
| 5 | + */ |
| 6 | + |
| 7 | +#include "restclient-cpp/connection.h" |
| 8 | + |
| 9 | +#include <curl/curl.h> |
| 10 | + |
| 11 | +#include <cstring> |
| 12 | +#include <string> |
| 13 | +#include <iostream> |
| 14 | +#include <map> |
| 15 | +#include <stdexcept> |
| 16 | + |
| 17 | +#include "restclient-cpp/restclient.h" |
| 18 | +#include "restclient-cpp/helpers.h" |
| 19 | +#include "restclient-cpp/version.h" |
| 20 | + |
| 21 | +using namespace RestClient; |
| 22 | + |
| 23 | +Connection::Connection(const std::string baseUrl) { |
| 24 | + this->curlHandle = curl_easy_init(); |
| 25 | + if (!this->curlHandle) { |
| 26 | + throw std::runtime_error("Couldn't initialize curl handle"); |
| 27 | + } |
| 28 | + this->baseUrl = baseUrl; |
| 29 | + this->infoStruct = new Info(); |
| 30 | +} |
| 31 | + |
| 32 | +Connection::~Connection() { |
| 33 | + if (this->curlHandle) { |
| 34 | + curl_easy_cleanup(this->curlHandle); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// getters/setters |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief helper function to get called from the actual request methods to |
| 43 | + * prepare the curlHandle for transfer with generic options, perform the |
| 44 | + * request and record some stats from the last request and then reset the |
| 45 | + * handle with curl_easy_reset to its default state. This will keep things |
| 46 | + * like connections and session ID intact but makes sure you can change |
| 47 | + * parameters on the object for another request. |
| 48 | + * |
| 49 | + * @param uri URI to query |
| 50 | + * @param ret Reference to the Response struct that should be filled |
| 51 | + * |
| 52 | + * @return 0 on success and 1 on error |
| 53 | + */ |
| 54 | +int Connection::performCurlRequest(const std::string& uri, |
| 55 | + RestClient::Response& ret) { |
| 56 | + |
| 57 | + std::string url = std::string(this->baseUrl + uri); |
| 58 | + std::string headerString; |
| 59 | + CURLcode res = CURLE_OK; |
| 60 | + curl_slist* headerList = NULL; |
| 61 | + |
| 62 | + /** set query URL */ |
| 63 | + curl_easy_setopt(this->curlHandle, CURLOPT_URL, url.c_str()); |
| 64 | + /** set callback function */ |
| 65 | + curl_easy_setopt(this->curlHandle, CURLOPT_WRITEFUNCTION, Helpers::write_callback); |
| 66 | + /** set data object to pass to callback function */ |
| 67 | + curl_easy_setopt(this->curlHandle, CURLOPT_WRITEDATA, &ret); |
| 68 | + /** set the header callback function */ |
| 69 | + curl_easy_setopt(this->curlHandle, CURLOPT_HEADERFUNCTION, Helpers::header_callback); |
| 70 | + /** callback object for headers */ |
| 71 | + curl_easy_setopt(this->curlHandle, CURLOPT_HEADERDATA, &ret); |
| 72 | + /** set http headers */ |
| 73 | + for (HeaderFields::const_iterator it = this->headerFields.begin(); |
| 74 | + it != this->headerFields.end(); ++it) { |
| 75 | + headerString = it->first; |
| 76 | + headerString += ": "; |
| 77 | + headerString += it->second; |
| 78 | + headerList = curl_slist_append(headerList, headerString.c_str()); |
| 79 | + } |
| 80 | + curl_easy_setopt(this->curlHandle, CURLOPT_HTTPHEADER, headerList); |
| 81 | + /** set user agent */ |
| 82 | + curl_easy_setopt(this->curlHandle, CURLOPT_USERAGENT, this->GetUserAgent().c_str()); |
| 83 | + |
| 84 | + // set timeout |
| 85 | + if (this->timeout) { |
| 86 | + curl_easy_setopt(this->curlHandle, CURLOPT_TIMEOUT, this->timeout); |
| 87 | + // dont want to get a sig alarm on timeout |
| 88 | + curl_easy_setopt(this->curlHandle, CURLOPT_NOSIGNAL, 1); |
| 89 | + } |
| 90 | + res = curl_easy_perform(this->curlHandle); |
| 91 | + if (res != CURLE_OK) { |
| 92 | + if (res == CURLE_OPERATION_TIMEDOUT) { |
| 93 | + ret.code = res; |
| 94 | + ret.body = "Operation Timeout."; |
| 95 | + } |
| 96 | + |
| 97 | + ret.body = "Failed to query."; |
| 98 | + ret.code = -1; |
| 99 | + } |
| 100 | + int64_t http_code = 0; |
| 101 | + curl_easy_getinfo(this->curlHandle, CURLINFO_RESPONSE_CODE, &http_code); |
| 102 | + ret.code = static_cast<int>(http_code); |
| 103 | + |
| 104 | + // TODO: get metrics from curl handle |
| 105 | + // free header list |
| 106 | + curl_slist_free_all(headerList); |
| 107 | + // reset curl handle |
| 108 | + curl_easy_reset(this->curlHandle); |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief HTTP GET method |
| 114 | + * |
| 115 | + * @param url to query |
| 116 | + * |
| 117 | + * @return response struct |
| 118 | + */ |
| 119 | +Response Connection::get(const std::string& url) { |
| 120 | + /** create return struct */ |
| 121 | + Response ret = {}; |
| 122 | + CURLcode res = CURLE_OK; |
| 123 | + |
| 124 | + this->performCurlRequest(url, &ret); |
| 125 | + return ret; |
| 126 | +} |
| 127 | +/** |
| 128 | + * @brief HTTP POST method |
| 129 | + * |
| 130 | + * @param url to query |
| 131 | + * @param ctype content type as string |
| 132 | + * @param data HTTP POST body |
| 133 | + * |
| 134 | + * @return response struct |
| 135 | + */ |
| 136 | +Response Connection::post(const std::string& url, |
| 137 | + const std::string& data) { |
| 138 | + /** create return struct */ |
| 139 | + RestClient::Response ret = {}; |
| 140 | + |
| 141 | + /** Now specify we want to POST data */ |
| 142 | + curl_easy_setopt(this->curlHandle, CURLOPT_POST, 1L); |
| 143 | + /** set post fields */ |
| 144 | + curl_easy_setopt(this->curlHandle, CURLOPT_POSTFIELDS, data.c_str()); |
| 145 | + curl_easy_setopt(this->curlHandle, CURLOPT_POSTFIELDSIZE, data.size()); |
| 146 | + |
| 147 | + this->performCurlRequest(url, &ret); |
| 148 | + return ret; |
| 149 | +} |
| 150 | +/** |
| 151 | + * @brief HTTP PUT method |
| 152 | + * |
| 153 | + * @param url to query |
| 154 | + * @param ctype content type as string |
| 155 | + * @param data HTTP PUT body |
| 156 | + * |
| 157 | + * @return response struct |
| 158 | + */ |
| 159 | +RestClient::Response Connection::put(const std::string& url, |
| 160 | + const std::string& data) { |
| 161 | + /** create return struct */ |
| 162 | + RestClient::Response ret = {}; |
| 163 | + CURLcode res = CURLE_OK; |
| 164 | + |
| 165 | + /** initialize upload object */ |
| 166 | + RestClient::Helpers::UploadObject up_obj; |
| 167 | + up_obj.data = data.c_str(); |
| 168 | + up_obj.length = data.size(); |
| 169 | + |
| 170 | + /** Now specify we want to PUT data */ |
| 171 | + curl_easy_setopt(this->curlHandle, CURLOPT_PUT, 1L); |
| 172 | + curl_easy_setopt(this->curlHandle, CURLOPT_UPLOAD, 1L); |
| 173 | + /** set read callback function */ |
| 174 | + curl_easy_setopt(this->curlHandle, CURLOPT_READFUNCTION, |
| 175 | + RestClient::Helpers::read_callback); |
| 176 | + /** set data object to pass to callback function */ |
| 177 | + curl_easy_setopt(this->curlHandle, CURLOPT_READDATA, &up_obj); |
| 178 | + /** set data size */ |
| 179 | + curl_easy_setopt(this->curlHandle, CURLOPT_INFILESIZE, |
| 180 | + static_cast<int64_t>(up_obj.length)); |
| 181 | + |
| 182 | + this->performCurlRequest(url, ret); |
| 183 | + return ret; |
| 184 | +} |
| 185 | +/** |
| 186 | + * @brief HTTP DELETE method |
| 187 | + * |
| 188 | + * @param url to query |
| 189 | + * |
| 190 | + * @return response struct |
| 191 | + */ |
| 192 | +RestClient::Response Connection::del(const std::string& url) { |
| 193 | + /** create return struct */ |
| 194 | + RestClient::Response ret = {}; |
| 195 | + CURLcode res = CURLE_OK; |
| 196 | + |
| 197 | + /** we want HTTP DELETE */ |
| 198 | + const char* http_delete = "DELETE"; |
| 199 | + |
| 200 | + /** set HTTP DELETE METHOD */ |
| 201 | + curl_easy_setopt(this->curlHandle, CURLOPT_CUSTOMREQUEST, http_delete); |
| 202 | + |
| 203 | + this->performCurlRequest(url, ret); |
| 204 | + return ret; |
| 205 | +} |
| 206 | + |
0 commit comments