request.h
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
* Gary Wang <gary.wang@canonical.com>
*/
#ifndef CORE_NET_HTTP_REQUEST_H_
#define CORE_NET_HTTP_REQUEST_H_
#include <core/net/visibility.h>
#include <core/net/http/error.h>
#include <core/net/http/header.h>
#include <chrono>
#include <memory>
namespace core
{
namespace net
{
namespace http
{
class Response;
class CORE_NET_DLL_PUBLIC Request
{
public:
enum class State
{
ready,
active,
done
};
struct Errors
{
Errors() = delete;
struct AlreadyActive : public core::net::http::Error
{
AlreadyActive(const core::Location& loc);
};
};
struct Progress
{
enum class Next
{
continue_operation,
abort_operation
};
struct
{
double total{-1.};
double current{-1.};
} download{};
struct
{
double total{-1.};
double current{-1.};
} upload{};
};
typedef std::function<void(const core::net::Error&)> ErrorHandler;
typedef std::function<Progress::Next(const Progress&)> ProgressHandler;
typedef std::function<void(const Response&)> ResponseHandler;
class Handler
{
public:
Handler() = default;
const ProgressHandler& on_progress() const;
Handler& on_progress(const ProgressHandler& handler);
const ResponseHandler& on_response() const;
Handler& on_response(const ResponseHandler& handler);
const ErrorHandler& on_error() const;
Handler& on_error(const ErrorHandler& handler);
private:
ProgressHandler progress_handler{};
ResponseHandler response_handler{};
ErrorHandler error_handler{};
};
struct Credentials
{
std::string username;
std::string password;
};
typedef std::function<Credentials(const std::string&)> AuthenicationHandler;
struct Configuration
{
inline static Configuration from_uri_as_string(const std::string& uri)
{
Configuration result;
result.uri = uri;
return result;
}
std::string uri;
Header header;
ProgressHandler on_progress;
ResponseHandler on_response;
ErrorHandler on_error;
struct
{
bool verify_peer
{
true
};
bool verify_host
{
true
};
} ssl;
struct
{
AuthenicationHandler for_http;
AuthenicationHandler for_proxy;
} authentication_handler;
};
Request(const Request&) = delete;
virtual ~Request() = default;
Request& operator=(const Request&) = delete;
bool operator==(const Request&) const = delete;
virtual State state() = 0;
virtual void set_timeout(const std::chrono::milliseconds& timeout) = 0;
virtual Response execute(const ProgressHandler& ph) = 0;
virtual void async_execute(const Handler& handler) = 0;
virtual std::string url_escape(const std::string& s) = 0;
virtual std::string url_unescape(const std::string& s) = 0;
protected:
Request() = default;
};
}
}
}
#endif // CORE_NET_HTTP_REQUEST_H_
core::net::Error
Definition error.h:30
core::net::http::Error
Definition http/error.h:30
core::net::http::Header
The Header class encapsulates the headers of an HTTP request/response.
Definition header.h:39
core::net::http::Request::Handler
Encapsulates callbacks that can happen during request execution.
Definition request.h:120
core::net::http::Request::Handler::on_progress
const ProgressHandler & on_progress() const
Returns the currently set progress handler.
core::net::http::Request::Handler::on_response
Handler & on_response(const ResponseHandler &handler)
Adjusts the currently set response handler.
core::net::http::Request::Handler::on_error
const ErrorHandler & on_error() const
Returns the currently set error handler.
core::net::http::Request::Handler::on_error
Handler & on_error(const ErrorHandler &handler)
Adjusts the currently set error handler.
core::net::http::Request::Handler::Handler
Handler()=default
core::net::http::Request::Handler::on_response
const ResponseHandler & on_response() const
Returns the currently set response handler.
core::net::http::Request::Handler::on_progress
Handler & on_progress(const ProgressHandler &handler)
Adjusts the currently set progress handler.
core::net::http::Request::~Request
virtual ~Request()=default
core::net::http::Request::execute
virtual Response execute(const ProgressHandler &ph)=0
Synchronously executes the request.
core::net::http::Request::url_escape
virtual std::string url_escape(const std::string &s)=0
Returns the input string in URL-escaped format.
core::net::http::Request::state
virtual State state()=0
state queries the current state of the operation.
core::net::http::Request::Request
Request(const Request &)=delete
core::net::http::Request::AuthenicationHandler
std::function< Credentials(const std::string &)> AuthenicationHandler
Definition request.h:157
core::net::http::Request::url_unescape
virtual std::string url_unescape(const std::string &s)=0
Returns the input string in URL-unescaped format.
core::net::http::Request::ResponseHandler
std::function< void(const Response &)> ResponseHandler
ResponseHandler is invoked when a request completes.
Definition request.h:114
core::net::http::Request::State
State
The State enum describes the different states a request can be in.
Definition request.h:48
core::net::http::Request::async_execute
virtual void async_execute(const Handler &handler)=0
Asynchronously executes the request, reporting errors, progress and completion to the given handlers.
core::net::http::Request::operator==
bool operator==(const Request &) const =delete
core::net::http::Request::operator=
Request & operator=(const Request &)=delete
core::net::http::Request::ProgressHandler
std::function< Progress::Next(const Progress &)> ProgressHandler
ProgressHandler is invoked for progress updates while executing the request.
Definition request.h:109
core::net::http::Request::set_timeout
virtual void set_timeout(const std::chrono::milliseconds &timeout)=0
Adjusts the timeout of a State::ready request.
core::net::http::Request::ErrorHandler
std::function< void(const core::net::Error &)> ErrorHandler
ErrorHandler is invoked in case of errors arising while executing the request.
Definition request.h:104
header.h
error.h
core::net::http
Definition client.h:39
core::net
Definition error.h:28
core
Definition location.h:24
core::Location
Definition location.h:26
core::net::http::Request::Configuration
The Configuration struct encapsulates all options for creating requests.
Definition request.h:163
core::net::http::Request::Configuration::on_progress
ProgressHandler on_progress
Definition request.h:184
core::net::http::Request::Configuration::verify_host
bool verify_host
Definition request.h:203
core::net::http::Request::Configuration::header
Header header
Definition request.h:181
core::net::http::Request::Configuration::from_uri_as_string
static Configuration from_uri_as_string(const std::string &uri)
from_uri_as_string creates a new instance of Configuration for a url.
Definition request.h:169
core::net::http::Request::Configuration::for_http
AuthenicationHandler for_http
Definition request.h:212
core::net::http::Request::Configuration::for_proxy
AuthenicationHandler for_proxy
Definition request.h:214
core::net::http::Request::Configuration::verify_peer
bool verify_peer
Definition request.h:197
core::net::http::Request::Configuration::on_error
ErrorHandler on_error
Definition request.h:190
core::net::http::Request::Configuration::on_response
ResponseHandler on_response
Definition request.h:187
core::net::http::Request::Configuration::uri
std::string uri
Definition request.h:178
core::net::http::Request::Credentials
The Credentials struct encapsulates username and password for basic & digest authentication.
Definition request.h:151
core::net::http::Request::Credentials::password
std::string password
Definition request.h:153
core::net::http::Request::Credentials::username
std::string username
Definition request.h:152
core::net::http::Request::Errors::AlreadyActive::AlreadyActive
AlreadyActive(const core::Location &loc)
AlreadyActive creates a new instance with a location hint.
core::net::http::Request::Errors::Errors
Errors()=delete
core::net::http::Request::Progress
The Progress struct encapsulates progress information for web-resource requests.
Definition request.h:78
core::net::http::Request::Progress::current
double current
Current number of bytes already downloaded.
Definition request.h:91
core::net::http::Request::Progress::total
double total
Total number of bytes to be downloaded.
Definition request.h:90
core::net::http::Request::Progress::Next
Next
The Next enum summarizes the available return-types for the progress callback.
Definition request.h:83
core::net::http::Response
The Response struct models a response to a core::net::http::Request.
Definition response.h:40
visibility.h
CORE_NET_DLL_PUBLIC
#define CORE_NET_DLL_PUBLIC
Definition visibility.h:25