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 <cstddef>
36 
37 namespace Pt {
38 
39 namespace System {
40 class EventLoop;
41 }
42 
43 namespace Net {
44 class Endpoint;
45 class TcpSocketOptions;
46 }
47 
48 namespace Ssl {
49 class Context;
50 }
51 
52 namespace Http {
53 
74 class PT_HTTP_API Client : public Connectable
75  , private NonCopyable
76 {
77  public:
80  Client();
81 
84  explicit Client(const Net::Endpoint& ep);
85 
88  explicit Client(System::EventLoop& loop);
89 
92  Client(System::EventLoop& loop, const Net::Endpoint& ep);
93 
96  ~Client();
97 
100  System::EventLoop* loop() const;
101 
104  void setActive(System::EventLoop& loop);
105 
108  void setTimeout(std::size_t timeout);
109 
112  void setSecure(Ssl::Context& ctx);
113 
116  void setPeerName(const std::string& peer);
117 
120  void setHost(const Net::Endpoint& ep);
121 
124  void setHost(const Net::Endpoint& ep, const Net::TcpSocketOptions& opts);
125 
128  const Net::Endpoint& host() const;
129 
132  Request& request();
133 
136  const Request& request() const;
137 
140  Reply& reply();
141 
144  const Reply& reply() const;
145 
148  void beginSend(bool finished = true);
149 
152  MessageProgress endSend();
153 
156  Signal<Client&>& requestSent();
157 
160  void beginReceive();
161 
164  MessageProgress endReceive();
165 
168  Signal<Client&>& replyReceived();
169 
170  // TODO: remove in later version
171  void cancel();
172 
175  void close();
176 
179  void send(bool finished = true);
180 
183  std::istream& receive();
184 
185  protected:
187  void onRequestSent(Request& r);
188 
190  void onReplyReceived(Reply& r);
191 
192  private:
194  void init();
195 
196  private:
197  class ClientImpl* _impl;
198 };
199 
200 } // namespace Http
201 
202 } // namespace Pt
203 
204 #endif // Pt_Http_Client_h
TCP socket options.
Definition: TcpSocket.h:48
HTTP message progress.
Definition: Message.h:277
HTTP request message.
Definition: Request.h:43
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
An HTTP client.
Definition: Client.h:74
HTTP reply message.
Definition: Reply.h:43
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Context for SSL connections.
Definition: Context.h:76
Represents a Network Host.
Definition: Endpoint.h:46
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72