Commit 57899a2a authored by Paweł Wegner's avatar Paweł Wegner
Browse files

HttpRequest: added redirect options.

parent 7d29a51d
......@@ -23,8 +23,11 @@
#include "HttpRequest.h"
#include <array>
#include <sstream>
const uint32_t MAX_URL_LENGTH = 1024;
namespace cloudstorage {
namespace {
......@@ -92,7 +95,10 @@ std::ios::pos_type stream_length(std::istream& data) {
} // namespace
HttpRequest::HttpRequest(const std::string& url, Type t)
: handle_(curl_easy_init(), CurlDeleter()), url_(url), type_(t) {
: handle_(curl_easy_init(), CurlDeleter()),
url_(url),
type_(t),
follow_redirect_(true) {
curl_easy_setopt(handle_.get(), CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(handle_.get(), CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(handle_.get(), CURLOPT_SSL_VERIFYPEER,
......@@ -113,6 +119,13 @@ void HttpRequest::setHeaderParameter(const std::string& parameter,
header_parameters_[parameter] = value;
}
bool HttpRequest::follow_redirect() const { return follow_redirect_; }
void HttpRequest::set_follow_redirect(bool e) {
follow_redirect_ = e;
curl_easy_setopt(handle_.get(), CURLOPT_FOLLOWLOCATION, static_cast<long>(e));
}
const std::string& HttpRequest::url() const { return url_; }
void HttpRequest::set_url(const std::string& url) { url_ = url; }
......@@ -175,6 +188,12 @@ int HttpRequest::send(std::istream& data, std::ostream& response,
else if (status == CURLE_OK) {
long http_code = static_cast<long>(Unknown);
curl_easy_getinfo(handle_.get(), CURLINFO_RESPONSE_CODE, &http_code);
if (!follow_redirect() && isRedirect(http_code)) {
std::array<char, MAX_URL_LENGTH> redirect_url;
char* data = redirect_url.data();
curl_easy_getinfo(handle_.get(), CURLINFO_REDIRECT_URL, &data);
response << data;
}
return static_cast<int>(http_code);
} else
throw HttpException(status);
......@@ -185,7 +204,11 @@ void HttpRequest::resetParameters() {
header_parameters_.clear();
}
bool HttpRequest::isSuccess(int code) { return (code / 100) == 2; }
bool HttpRequest::isSuccess(int code) {
return (code / 100) == 2 || (code / 100) == 3;
}
bool HttpRequest::isRedirect(int code) { return (code / 100) == 3; }
bool HttpRequest::isClientError(int code) {
return (code / 100) == 4 || (code / 100) == 5;
......
......@@ -71,6 +71,9 @@ class HttpRequest {
void setHeaderParameter(const std::string& parameter,
const std::string& value);
bool follow_redirect() const;
void set_follow_redirect(bool);
const std::string& url() const;
void set_url(const std::string&);
......@@ -87,6 +90,7 @@ class HttpRequest {
void resetParameters();
static bool isSuccess(int code);
static bool isRedirect(int code);
static bool isClientError(int code);
static bool isAuthorizationError(int code);
static bool isCurlError(int code);
......@@ -104,6 +108,7 @@ class HttpRequest {
std::unordered_map<std::string, std::string> parameters_;
std::unordered_map<std::string, std::string> header_parameters_;
Type type_;
bool follow_redirect_;
};
} // namespace cloudstorage
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment