Commit 1b138e99 authored by Paweł Wegner's avatar Paweł Wegner
Browse files

IHttp: refactored Complete callback.

parent f69a36fd
......@@ -33,9 +33,17 @@ namespace cloudstorage {
class IHttpRequest {
public:
struct Response {
int http_code_;
int content_length_;
std::shared_ptr<std::ostream> output_stream_;
std::shared_ptr<std::ostream> error_stream_;
};
using Pointer = std::shared_ptr<IHttpRequest>;
using CompleteCallback = std::function<void(
int, std::shared_ptr<std::ostream>, std::shared_ptr<std::ostream>)>;
using GetParameters = std::unordered_map<std::string, std::string>;
using HeaderParameters = std::unordered_map<std::string, std::string>;
using CompleteCallback = std::function<void(Response)>;
static constexpr int Ok = 200;
static constexpr int Partial = 206;
......@@ -100,16 +108,14 @@ class IHttpRequest {
*
* @return map of parameters
*/
virtual const std::unordered_map<std::string, std::string>& parameters()
const = 0;
virtual const GetParameters& parameters() const = 0;
/**
* Returns header parameters set with setHeaderParameter.
*
* @return header parameters
*/
virtual const std::unordered_map<std::string, std::string>& headerParameters()
const = 0;
virtual const HeaderParameters& headerParameters() const = 0;
/**
* @return url(without parameters set with setParameter)
......
......@@ -136,21 +136,26 @@ void CurlHttp::Worker::add(RequestData::Pointer r) {
void RequestData::done(int code) {
int ret = IHttpRequest::Unknown;
int content_length = 0;
if (code == CURLE_OK) {
long http_code = static_cast<long>(IHttpRequest::Unknown);
curl_easy_getinfo(handle_.get(), CURLINFO_RESPONSE_CODE, &http_code);
ret = http_code;
if (!follow_redirect_ && IHttpRequest::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);
*error_stream_ << data;
}
ret = http_code;
double curl_content_length;
curl_easy_getinfo(handle_.get(), CURLINFO_CONTENT_LENGTH_DOWNLOAD,
&curl_content_length);
content_length = (int)(curl_content_length + 0.5);
} else {
*error_stream_ << curl_easy_strerror(static_cast<CURLcode>(code));
ret = (code == CURLE_ABORTED_BY_CALLBACK) ? IHttpRequest::Aborted : -code;
}
complete_(ret, stream_, error_stream_);
complete_({ret, content_length, stream_, error_stream_});
}
CurlHttpRequest::CurlHttpRequest(const std::string& url,
......@@ -225,6 +230,8 @@ RequestData::Pointer CurlHttpRequest::prepare(
curl_easy_setopt(handle, CURLOPT_UPLOAD, static_cast<long>(true));
curl_easy_setopt(handle, CURLOPT_INFILESIZE,
static_cast<long>(stream_length(*data)));
} else if (method_ == "HEAD") {
curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
} else if (method_ != "GET") {
if (stream_length(*data) > 0)
curl_easy_setopt(handle, CURLOPT_UPLOAD, static_cast<long>(true));
......
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