httpclient.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 by 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 #ifndef CXXTOOLS_JSON_HTTPCLIENT_H
29 #define CXXTOOLS_JSON_HTTPCLIENT_H
30 
31 #include <cxxtools/remoteclient.h>
32 #include <string>
33 
34 namespace cxxtools
35 {
36 
37 class SelectorBase;
38 
39 namespace net
40 {
41  class Uri;
42  class AddrInfo;
43 }
44 
45 namespace json
46 {
47 
48  class HttpClientImpl;
49 
50  class HttpClient : public RemoteClient
51  {
52  public:
54  : _impl(0)
55  { }
56 
57  explicit HttpClient(SelectorBase& selector)
58  : _impl(0)
59  { setSelector(selector); }
60 
61  HttpClient(SelectorBase& selector, const std::string& addr,
62  unsigned short port, const std::string& url, bool ssl = false);
63 
64  HttpClient(SelectorBase& selector, const net::AddrInfo& addrinfo,
65  const std::string& url, bool ssl = false);
66 
67  HttpClient(SelectorBase& selector, const net::Uri& uri);
68 
69  HttpClient(const std::string& addr, unsigned short port, const std::string& url, bool ssl = false);
70 
71  HttpClient(const net::AddrInfo& addrinfo, const std::string& url, bool ssl = false);
72 
73  explicit HttpClient(const net::Uri& uri);
74 
75  HttpClient(const HttpClient&);
77 
78  virtual ~HttpClient();
79 
80  void prepareConnect(const net::AddrInfo& addrinfo, const std::string& url, bool ssl = false);
81 
82  void prepareConnect(const net::Uri& uri);
83 
84  void prepareConnect(const std::string& addr, unsigned short port,
85  const std::string& url, bool ssl = false);
86 
87  void connect();
88 
89  void connect(const net::AddrInfo& addrinfo, const std::string& url, bool ssl = false)
90  { prepareConnect(addrinfo, url, ssl); connect(); }
91 
92  void connect(const net::Uri& uri)
93  { prepareConnect(uri); connect(); }
94 
95  void connect(const std::string& addr, unsigned short port, const std::string& url, bool ssl = false)
96  { prepareConnect(addr, port, url, ssl); connect(); }
97 
98  void url(const std::string& url);
99  void auth(const std::string& username, const std::string& password);
100 
101  void clearAuth();
102 
103  void setSelector(SelectorBase& selector);
104 
105  void beginCall(IComposer& r, IRemoteProcedure& method, IDecomposer** argv, unsigned argc);
106 
107  void endCall();
108 
109  void call(IComposer& r, IRemoteProcedure& method, IDecomposer** argv, unsigned argc);
110 
111  Milliseconds timeout() const;
112  void timeout(Milliseconds t);
113 
116 
117  const std::string& url() const;
118 
119  const IRemoteProcedure* activeProcedure() const;
120 
121  void cancel();
122 
123  void wait(Milliseconds msecs = WaitInfinite);
124 
125  private:
126  HttpClientImpl* _impl;
127  HttpClientImpl* getImpl();
128  const HttpClientImpl* getImpl() const
129  { return const_cast<HttpClient*>(this)->getImpl(); }
130  };
131 
132 }
133 
134 }
135 
136 #endif