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/api.h>
33 #include <cxxtools/selectable.h>
34 #include <cxxtools/signal.h>
35 #include <cxxtools/delegate.h>
36 #include <cxxtools/noncopyable.h>
37 #include <string>
38 
39 namespace cxxtools
40 {
41 
42 class SelectorBase;
43 
44 namespace net
45 {
46 
47 class AddrInfo;
48 class Uri;
49 
50 }
51 
52 namespace http
53 {
54 
55 class ClientImpl;
56 class ReplyHeader;
57 class Request;
58 
60 {
61  ClientImpl* _impl;
62 
63  public:
64  Client();
65  Client(const std::string& host, unsigned short int port);
66  explicit Client(const net::AddrInfo& addr);
67  explicit Client(const net::Uri& uri);
68 
69  Client(SelectorBase& selector, const std::string& host, unsigned short int port);
70  Client(SelectorBase& selector, const net::AddrInfo& addrinfo);
71  Client(SelectorBase& selector, const net::Uri& uri);
72 
73  ~Client();
74 
75  // Sets the host and port. No actual network connect is done.
76  void connect(const net::AddrInfo& addrinfo);
77  void connect(const std::string& host, unsigned short int port);
78 
79  // Sends the passed request to the server and parses the headers.
80  // The body must be read with readBody.
81  // This method blocks or times out until the body is parsed.
82  const ReplyHeader& execute(const Request& request,
83  std::size_t timeout = Selectable::WaitInfinite);
84 
85  const ReplyHeader& header();
86 
87  // Reads the http body after header read with execute.
88  // This method blocks until the body is received.
89  void readBody(std::string& s);
90 
91  // Reads the http body after header read with execute.
92  // This method blocks until the body is received.
93  std::string readBody()
94  {
95  std::string ret;
96  readBody(ret);
97  return ret;
98  }
99 
100  // Combines the execute and readBody methods in one call.
101  // This method blocks until the reply is recieved.
102  std::string get(const std::string& url,
103  std::size_t timeout = Selectable::WaitInfinite);
104 
105  // Starts a new request.
106  // This method does not block. To actually process the request, the
107  // event loop must be executed. The state of the request is signaled
108  // with the corresponding signals and delegates.
109  // The delegate "bodyAvailable" must be connected, if a body is
110  // received.
111  void beginExecute(const Request& request);
112 
113  void endExecute();
114 
115  void setSelector(SelectorBase& selector);
116 
117  SelectorBase* selector();
118 
119  // Executes the underlying selector until a event occures or the
120  // specified timeout is reached.
121  bool wait(std::size_t msecs);
122 
123  // Returns the underlying stream, where the reply may be read from.
124  std::istream& in();
125 
126  const std::string& host() const;
127 
128  unsigned short int port() const;
129 
130  // Sets the username and password for all subsequent requests.
131  void auth(const std::string& username, const std::string& password);
132 
133  void clearAuth();
134 
135  void cancel();
136 
137  // Signals that the request is sent to the server.
138  Signal<Client&> requestSent;
139 
140  // Signals that the header is received.
141  Signal<Client&> headerReceived;
142 
143  // This delegate is called, when data is arrived while reading the
144  // body. The connected functor must return the number of bytes read.
145  cxxtools::Delegate<std::size_t, Client&> bodyAvailable;
146 
147  // Signals that the reply is completely processed.
148  Signal<Client&> replyFinished;
149 };
150 
151 } // namespace http
152 
153 } // namespace cxxtools
154 
155 #endif