Client.h
1/*
2 * Copyright (C) 2012 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, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Http_Client_h
30#define Pt_Http_Client_h
31
32#include <Pt/Http/Api.h>
33#include <Pt/Signal.h>
34#include <Pt/NonCopyable.h>
35#include <string>
36#include <iosfwd>
37#include <cstddef>
38
39namespace Pt {
40
41namespace System {
42class EventLoop;
43}
44
45namespace Net {
46class Endpoint;
48}
49
50namespace Ssl {
51class Context;
52}
53
54namespace Http {
55
187class PT_HTTP_API Client : public Connectable
188 , private NonCopyable
189{
190 public:
194
197 explicit Client(const Net::Endpoint& ep);
198
202
206
210
214
218
221 void setTimeout(std::size_t timeout);
222
226
229 void setPeerName(const std::string& peer);
230
233 void setHost(const Net::Endpoint& ep);
234
237 void setHost(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts);
238
241 const Net::Endpoint& host() const;
242
246
249 const Request& request() const;
250
254
257 const Reply& reply() const;
258
263 void beginSend(bool finished = true);
264
268
272
276
280
284
285 // TODO: remove in later version
286 void cancel();
287
290 void close();
291
294 void send(bool finished = true);
295
298 std::istream& receive();
299
300 protected:
302 void onRequestSent(Request& r);
303
305 void onReplyReceived(Reply& r);
306
307 private:
309 void init();
310
311 private:
312 class ClientImpl* _impl;
313};
314
315} // namespace Http
316
317} // namespace Pt
318
319#endif // Pt_Http_Client_h
Connectable()
Default constructor.
const Reply & reply() const
Returns the received reply.
Reply & reply()
Returns the received reply.
void setSecure(Ssl::Context &ctx)
Enables HTTPS with ctx.
const Net::Endpoint & host() const
Returns the host to connect to.
~Client()
Destructor.
Request & request()
Returns the request to send.
void setTimeout(std::size_t timeout)
Set timeout for I/O operations.
void beginSend(bool finished=true)
Begins sending the request.
const Request & request() const
Returns the request to send.
void close()
Closes the connection and cancels all operations.
Signal< Client & > & requestSent()
Signals that a part of the request was sent.
void setHost(const Net::Endpoint &ep)
Sets the host to connect to.
Client(const Net::Endpoint &ep)
Construct with host to connect to.
void beginReceive()
Begin receiving the reply.
System::EventLoop * loop() const
Returns the used event loop.
void setActive(System::EventLoop &loop)
Sets the event loop to use.
Client(System::EventLoop &loop)
Construct with event loop.
MessageProgress endReceive()
End receiving the reply.
Client()
Default Constructor.
Signal< Client & > & replyReceived()
Signals that a part of the reply was received.
void send(bool finished=true)
Blocks until request is sent.
std::istream & receive()
Blocks until reply is received.
MessageProgress endSend()
End sending the request.
Client(System::EventLoop &loop, const Net::Endpoint &ep)
Construct with loop and host to connect to.
void setHost(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts)
Sets the host to connect to.
void setPeerName(const std::string &peer)
Sets the expected SSL peer name.
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
HTTP reply message.
Definition Reply.h:64
HTTP request message.
Definition Request.h:65
Host and service address.
Definition Endpoint.h:77
TCP socket connection options.
Definition TcpSocket.h:51
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Multicast Signal to call multiple slots.
Definition Signal.h:190
Shared configuration for SSL connections.
Definition Context.h:113
Event loop of a thread or process.
Definition EventLoop.h:83
HTTP clients and servers.
TCP and UDP network sockets.
Definition Client.h:45
SSL/TLS streams, certificates and contexts.
Definition Client.h:50
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33