libfilezilla 0.59.0
A cross-platform C++ library for asynchronous and networked applications
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_HTTP_CLIENT_HEADER
2#define LIBFILEZILLA_HTTP_CLIENT_HEADER
3
7
8#include "../string.hpp"
9#include "client_request.hpp"
10#include "client_response.hpp"
11
12#include <memory>
13
14namespace fz {
15
16class event_handler;
19
20namespace http::client {
21
23class FZ_PUBLIC_SYMBOL request_response_interface
24{
25public:
26 virtual ~request_response_interface() = default;
27
28 virtual request & req() = 0;
29 virtual response & res() = 0;
30
31 uint64_t request_id_{}; // Gets updated by add client::add_request()
32};
33
34template<class Request, class Response>
36{
37public:
38 virtual request & req() override { return request_; }
39 virtual response & res() override { return response_; }
40
46 void set_on_header(std::function<continuation(std::shared_ptr<request_response_holder<Request, Response>> const&)> cb)
47 {
48 if (cb) {
49 response_.on_header_ = [cb = std::move(cb)](shared_request_response const& srr) {
50 auto p = std::static_pointer_cast<request_response_holder<Request, Response>>(srr);
51 return cb(p);
52 };
53 }
54 else {
55 response_.on_header_ = nullptr;
56 }
57 }
58
64 void set_body_sending_progress(std::function<void(std::shared_ptr<request_response_holder<Request, Response>> const&, size_t)> cb)
65 {
66 if (cb) {
67 request_.on_body_sending_progress_ = [cb = std::move(cb)](shared_request_response const& srr, size_t c) {
68 auto p = std::static_pointer_cast<request_response_holder<Request, Response>>(srr);
69 cb(p, c);
70 };
71 }
72 else {
73 request_.on_body_sending_progress_ = nullptr;
74 }
75 }
76
77 Request request_;
78 Response response_;
79};
80typedef std::shared_ptr<request_response_interface> shared_request_response;
81
83struct done_event_type;
84
88
94class FZ_PUBLIC_SYMBOL client
95{
96public:
98 client(event_handler & handler, aio_buffer_pool & buffer_pool, logger_interface & logger, std::string user_agent);
99 client(event_handler & handler, logger_interface & logger, std::string user_agent);
100 virtual ~client();
101
102 bool add_request(shared_request_response const& srr);
103
110 void stop(bool keep_alive);
111
118 void destroy();
119
120protected:
125 virtual void on_alive() {}
126
128 virtual socket_interface* create_socket(native_string const& host, unsigned short port, bool tls) = 0;
129
131 virtual void destroy_socket() = 0;
132
133private:
134 class impl;
135 std::unique_ptr<impl> impl_;
136};
137
138}
139}
140
141#endif
A buffer pool for use with async readers/writers.
Definition aio.hpp:107
Definition event_handler.hpp:63
void stop(bool keep_alive)
Stops the client, deletes all requests.
client(event_handler &handler, aio_buffer_pool &buffer_pool, logger_interface &logger, std::string user_agent)
A buffer pool is optional. If no pool is given, writers cannot be used in responses.
virtual socket_interface * create_socket(native_string const &host, unsigned short port, bool tls)=0
Called when the client requests a new socket.
virtual void destroy_socket()=0
Called when the client no longer needs the socket.
virtual void on_alive()
Definition client.hpp:125
void set_on_header(std::function< continuation(std::shared_ptr< request_response_holder< Request, Response > > const &)> cb)
Definition client.hpp:46
void set_body_sending_progress(std::function< void(std::shared_ptr< request_response_holder< Request, Response > > const &, size_t)> cb)
Definition client.hpp:64
Used for type erasure, so the internals don't need to know the full request/response.
Definition client.hpp:24
A single HTTP request to be sent by the client.
Definition client_request.hpp:19
A single HTTP response received by the client.
Definition client_response.hpp:22
Abstract interface for logging strings.
Definition logger.hpp:51
This is the recommended event class.
Definition event.hpp:68
Interface for sockets.
Definition socket.hpp:375
simple_event< done_event_type, uint64_t, bool > done_event
Definition client.hpp:87
HTTP requests, client-side view.
HTTP responses, client-side view.
The namespace used by libfilezilla.
Definition apply.hpp:17
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:69
String types and assorted functions.