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 
40 namespace Pt {
41 
42 namespace JsonRpc {
43 
46 class 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 
81  void setSecure(Ssl::Context& ctx);
82 
85  void setPeerName(const std::string& peer);
86 
89  void setKeepAlive();
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
Core module.
Definition: Allocator.h:33
virtual void onCancel()
Cancels the remote procedure call.
void setServiceUrl(const char *url)
Sets the service URL.
void setActive(System::EventLoop &loop)
Sets the EventLoop to use for I/O.
HTTP request message.
Definition: Request.h:44
HTTP reply message.
Definition: Reply.h:44
A client for JSON-RPC 2.0 remote procedure calls via HTTP.
Definition: HttpClient.h:48
void setKeepAlive()
Enables HTTP keep-alive.
virtual ~HttpClient()
Destructor.
System::EventLoop * loop() const
Gets the used EventLoop.
void setTarget(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts, const std::string &url)
Sets target host and service URL.
const Net::Endpoint & host() const
Returns target host.
void setHost(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts)
Sets host to connect.
HttpClient()
Constructor.
void setPeerName(const std::string &peer)
Set expected SSL peer name.
HttpClient(System::EventLoop &loop)
Construct with EventLoop used for I/O.
void setServiceUrl(const std::string &url)
Sets the service URL.
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
void setTarget(const Net::Endpoint &ep, const std::string &url)
Sets target host and service URL.
Represents a Network Host.
Definition: Endpoint.h:47
virtual void onInvoke()
Perform a synchronous invocation.
void setHost(const Net::Endpoint &ep)
Sets host to connect.
An HTTP client.
Definition: Client.h:78
A client for JSON-RPC 2.0 remote procedure calls.
Definition: Client.h:56
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:83
HttpClient(const Net::Endpoint &ep, const std::string &url)
Construct with host and service URL.
virtual void onBeginInvoke()
Begin an asynchronous invocation.
HttpClient(System::EventLoop &loop, const Net::Endpoint &ep, const std::string &url)
Construct with host and service URL.
void close()
Closes the connection.
virtual bool isFailed() const
Indicates if the procedure has failed.
void setSecure(Ssl::Context &ctx)
Sets SSL context.
TCP socket options.
Definition: TcpSocket.h:49
virtual void onEndInvoke()
End an asynchronous invocation.
void setTimeout(std::size_t timeout)
Sets timeout for I/O operations.
Context for SSL connections.
Definition: Context.h:80