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 
39 namespace Pt {
40 
41 namespace System {
42 class EventLoop;
43 }
44 
45 namespace Net {
46 class Endpoint;
47 class TcpSocketOptions;
48 }
49 
50 namespace Ssl {
51 class Context;
52 }
53 
54 namespace Http {
55 
76 class PT_HTTP_API Client : public Connectable
77  , private NonCopyable
78 {
79  public:
82  Client();
83 
86  explicit Client(const Net::Endpoint& ep);
87 
90  explicit Client(System::EventLoop& loop);
91 
95 
99 
103 
107 
110  void setTimeout(std::size_t timeout);
111 
115 
118  void setPeerName(const std::string& peer);
119 
122  void setHost(const Net::Endpoint& ep);
123 
126  void setHost(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts);
127 
130  const Net::Endpoint& host() const;
131 
135 
138  const Request& request() const;
139 
143 
146  const Reply& reply() const;
147 
150  void beginSend(bool finished = true);
151 
155 
159 
162  void beginReceive();
163 
167 
171 
172  // TODO: remove in later version
173  void cancel();
174 
177  void close();
178 
181  void send(bool finished = true);
182 
185  std::istream& receive();
186 
187  protected:
189  void onRequestSent(Request& r);
190 
192  void onReplyReceived(Reply& r);
193 
194  private:
196  void init();
197 
198  private:
199  class ClientImpl* _impl;
200 };
201 
202 } // namespace Http
203 
204 } // namespace Pt
205 
206 #endif // Pt_Http_Client_h
Core module.
Definition: pt-gfx-images.dox:14
~Client()
Destructor.
void setActive(System::EventLoop &loop)
Sets the event loop to use.
Request & request()
Returns the request to send.
Client(System::EventLoop &loop, const Net::Endpoint &ep)
Construct with loop and host to connect to.
HTTP request message.
Definition: Request.h:44
void setPeerName(const std::string &peer)
Set expected SSL peer name.
void setHost(const Net::Endpoint &ep)
Set host to connect to.
void close()
Closes the connection and cancels all operations.
HTTP reply message.
Definition: Reply.h:44
Reply & reply()
Returns the received reply.
void send(bool finished=true)
Blocks until request is sent.
Multicast Signal to call multiple slots.
Definition: Signal.h:110
MessageProgress endSend()
End sending the request.
const Net::Endpoint & host() const
Returns the host to connect to.
const Request & request() const
Returns the request to send.
std::istream & receive()
Blocks until reply is received.
void beginReceive()
Begin receiving the reply.
const Reply & reply() const
Returns the received reply.
void setSecure(Ssl::Context &ctx)
Enable to use HTTPS.
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
HTTP message progress.
Definition: Message.h:282
Represents a Network Host.
Definition: Endpoint.h:47
void setHost(const Net::Endpoint &ep, const Net::TcpSocketOptions &opts)
Set host to connect to.
An HTTP client.
Definition: Client.h:78
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
MessageProgress endReceive()
End receiving the reply.
void setTimeout(std::size_t timeout)
Set timeout for I/O operations.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Client(const Net::Endpoint &ep)
Construct with host to connect to.
Signal< Client & > & replyReceived()
Signals that a part of the reply was received.
Client(System::EventLoop &loop)
Construct with event loop.
Client()
Default Constructor.
System::EventLoop * loop() const
Returns the used event loop.
TCP socket options.
Definition: TcpSocket.h:49
void beginSend(bool finished=true)
Begin sending the request.
Signal< Client & > & requestSent()
Signals that a part of the request was sent.
Context for SSL connections.
Definition: Context.h:80