client.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 by Marc Boris Duerner, Tommi Maekitalo
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 cxxtools_Http_Client_h
30 #define cxxtools_Http_Client_h
31 
32 #include <cxxtools/http/reply.h>
33 #include <cxxtools/query_params.h>
34 #include <cxxtools/net/uri.h>
35 #include <cxxtools/net/addrinfo.h>
36 #include <cxxtools/selectable.h>
37 #include <cxxtools/signal.h>
38 #include <cxxtools/delegate.h>
39 #include <string>
40 
41 namespace cxxtools
42 {
43 
44 class SelectorBase;
45 
46 namespace http
47 {
48 
49 class ClientImpl;
50 class ReplyHeader;
51 class Request;
52 
70 class Client
71 {
72  ClientImpl* _impl;
73  ClientImpl* getImpl();
74  const ClientImpl* getImpl() const
75  { return const_cast<Client*>(this)->getImpl(); }
76 
77  public:
80  : _impl(0)
81  { }
82 
85  explicit Client(const net::AddrInfo& addr, bool ssl = false);
86  Client(const std::string& host, unsigned short int port, bool ssl = false);
88 
90  : _impl(0)
91  { setSelector(selector); }
92 
104  explicit Client(const net::Uri& uri);
105 
108 
109  Client(SelectorBase& selector, const std::string& host, unsigned short int port, bool ssl = false);
110  Client(SelectorBase& selector, const net::AddrInfo& addrinfo, bool ssl = false);
111  Client(SelectorBase& selector, const net::Uri& uri);
112 
114 
120  Client(const Client& other);
121  Client& operator= (const Client& other);
122 
123  ~Client();
124 
132  void prepareConnect(const net::AddrInfo& addrinfo, bool ssl = false);
133  void prepareConnect(const std::string& host, unsigned short int port, bool ssl = false);
134  void prepareConnect(const net::Uri& uri);
135 
141  void connect();
142 
148  void close();
149 
152  void connect(const net::AddrInfo& addrinfo)
153  { prepareConnect(addrinfo); connect(); }
154 
155  void connect(const std::string& host, unsigned short int port)
156  { prepareConnect(host, port); connect(); }
157 
158  void connect(const net::Uri& uri)
159  { prepareConnect(uri); connect(); }
160 
168  const ReplyHeader& execute(const Request& request,
170  Milliseconds connectTimeout = Selectable::WaitInfinite);
171 
176  const Reply& readBody();
177 
180  const Reply& reply() const;
181  Reply& reply();
182 
187  const ReplyHeader& header() const
188  { return reply().header(); }
189 
194  std::string body() const
195  { return reply().body(); }
196 
203  const Reply& get(const std::string& url,
204  const QueryParams& qparams,
206  Milliseconds connectTimeout = Selectable::WaitInfinite);
207 
214  const Reply& get(const std::string& url,
216  Milliseconds connectTimeout = Selectable::WaitInfinite)
217  { return get(url, QueryParams(), timeout, connectTimeout); }
218 
225  const Reply& post(const std::string& url,
226  const QueryParams& qparams,
228  Milliseconds connectTimeout = Selectable::WaitInfinite);
229 
236  const Reply& post(const std::string& url,
238  Milliseconds connectTimeout = Selectable::WaitInfinite)
239  { return post(url, QueryParams(), timeout, connectTimeout); }
240 
249  void beginExecute(const Request& request);
250 
256  void endExecute();
257 
260 
263 
267  bool wait(Milliseconds msecs);
268 
271  std::istream& in();
272 
273  const std::string& host() const;
274 
275  unsigned short int port() const;
276 
281  void auth(const std::string& username, const std::string& password);
282 
285  void clearAuth();
286 
287  void cancel();
288 
290  Signal<Client&> requestSent;
291 
293  Signal<Client&> headerReceived;
294 
297  cxxtools::Delegate<std::size_t, Client&> bodyAvailable;
298 
300  Signal<Client&> replyFinished;
301 };
302 
303 } // namespace http
304 
305 } // namespace cxxtools
306 
307 #endif