HttpClient.h
1/*
2 * Copyright (C) 2020-2026 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * MA 02110-1301 USA
28 */
29
30#ifndef PT_JSONRPC_HTTPCLIENT_H
31#define PT_JSONRPC_HTTPCLIENT_H
32
33#include <Pt/JsonRpc/Api.h>
34#include <Pt/JsonRpc/Client.h>
35#include <Pt/Http/Client.h>
36#include <Pt/Connectable.h>
37#include <Pt/Types.h>
38#include <string>
39
40namespace Pt {
41
42namespace JsonRpc {
43
46class PT_JSONRPC_API HttpClient : public Client
47 , public Connectable
48{
49 public:
53
56 HttpClient(const Net::Endpoint& ep, const std::string& url);
57
61
65 const std::string& url);
66
69 virtual ~HttpClient();
70
74
78
82
85 void setPeerName(const std::string& peer);
86
90
93 void setTimeout(std::size_t timeout);
94
97 void setTarget(const Net::Endpoint& ep, const std::string& url);
98
101 void setTarget(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts,
102 const std::string& url);
103
106 void setHost(const Net::Endpoint& ep);
107
110 void setHost(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts);
111
114 void setServiceUrl(const std::string& url);
115
118 void setServiceUrl(const char* url);
119
122 const Net::Endpoint& host() const;
123
124 Pt::Http::Request& request();
125
126 Pt::Http::Reply& reply();
127
130 void close();
131
132 protected:
133 virtual bool isFailed() const;
134
135 // inheritdoc
136 virtual void onBeginInvoke();
137
138 // inheritdoc
139 virtual void onInvoke();
140
141 // inheritdoc
142 virtual void onEndInvoke();
143
144 // inheritdoc
145 virtual void onCancel();
146
147 private:
148 void setError(bool f = true)
149 { _error = f; }
150
152 void init();
153
155 void onRequest(Http::Client& client);
156
158 void onReply(Http::Client& client);
159
160 private:
161 Http::Client _client;
162 bool _error;
163 Pt::varint_t _r1;
164 Pt::varint_t _r2;
165};
166
167} // namespace JsonRpc
168
169} // namespace Pt
170
171#endif // PT_JSONRPC_HTTPCLIENT_H
Connectable()
Default constructor.
HTTP user agent.
Definition Client.h:189
HTTP reply message.
Definition Reply.h:64
HTTP request message.
Definition Request.h:65
Client()
Constructor.
void setSecure(Ssl::Context &ctx)
Sets SSL context.
const Net::Endpoint & host() const
Returns target host.
void setKeepAlive()
Enables HTTP keep-alive.
HttpClient(System::EventLoop &loop, const Net::Endpoint &ep, const std::string &url)
Construct with host and service URL.
void setTarget(const Net::Endpoint &ep, const std::string &url)
Sets target host and service URL.
HttpClient()
Constructor.
void setTarget(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts, const std::string &url)
Sets target host and service URL.
virtual void onCancel()
Cancels the remote procedure call.
HttpClient(System::EventLoop &loop)
Construct with EventLoop used for I/O.
void setTimeout(std::size_t timeout)
Sets timeout for I/O operations.
virtual void onBeginInvoke()
Begin an asynchronous invocation.
void close()
Closes the connection.
void setHost(const Net::Endpoint &ep)
Sets host to connect.
void setServiceUrl(const std::string &url)
Sets the service URL.
void setServiceUrl(const char *url)
Sets the service URL.
virtual bool isFailed() const
Indicates if the procedure has failed.
HttpClient(const Net::Endpoint &ep, const std::string &url)
Construct with host and service URL.
virtual void onInvoke()
Perform a synchronous invocation.
System::EventLoop * loop() const
Gets the used EventLoop.
void setActive(System::EventLoop &loop)
Sets the EventLoop to use for I/O.
virtual void onEndInvoke()
End an asynchronous invocation.
virtual ~HttpClient()
Destructor.
void setHost(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts)
Sets host to connect.
void setPeerName(const std::string &peer)
Set expected SSL peer name.
Host and service address.
Definition Endpoint.h:77
TCP socket connection options.
Definition TcpSocket.h:51
Shared configuration for SSL connections.
Definition Context.h:113
Event loop of a thread or process.
Definition EventLoop.h:83
JSON-RPC 2.0 services and clients.
Core module.
Definition Allocator.h:33