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/Http/Stream.h>
34#include <Pt/Signal.h>
35#include <Pt/NonCopyable.h>
36#include <string>
37#include <iosfwd>
38#include <cstddef>
39
40namespace Pt {
41
42namespace System {
43class EventLoop;
44}
45
46namespace Net {
47class Endpoint;
49}
50
51namespace Ssl {
52class Context;
53}
54
55namespace Http {
56
193class PT_HTTP_API Client : public Connectable
194 , private NonCopyable
195{
196 public:
200
203 explicit Client(const Net::Endpoint& ep);
204
208
212
216
220
224
227 void setTimeout(std::size_t timeout);
228
232
235 void setPeerName(const std::string& peer);
236
239 void setHost(const Net::Endpoint& ep);
240
243 void setHost(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts);
244
247 const Net::Endpoint& host() const;
248
252
255 const Request& request() const;
256
260
263 const Reply& reply() const;
264
269 void beginSend(bool finished = true);
270
274
278
282
286
290
291 // TODO: remove in later version
292 void cancel();
293
296 void close();
297
300 void send(bool finished = true);
301
304 std::istream& receive();
305
314
315 protected:
317 void onRequestSent(Request& r);
318
320 void onReplyReceived(Reply& r);
321
322 private:
324 void init();
325
326 private:
327 class ClientImpl* _impl;
328};
329
330} // namespace Http
331
332} // namespace Pt
333
334#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.
Stream & upgrade()
Returns the stream of a finished 101 reply.
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
One HTTP stream on a connection.
Definition Stream.h:90
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:46
SSL/TLS streams, certificates and contexts.
Definition Client.h:51
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33