3 // Copyright (C) 2011 James Turner <zakalawe@mac.com>
4 // Copyright (C) 2013 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef SG_HTTP_REQUEST_HXX
21 #define SG_HTTP_REQUEST_HXX
25 #include <simgear/structure/function_list.hxx>
26 #include <simgear/structure/map.hxx>
27 #include <simgear/structure/SGReferenced.hxx>
28 #include <simgear/structure/SGSharedPtr.hxx>
29 #include <simgear/math/sg_types.hxx>
31 #include <boost/bind.hpp>
41 * Base class for HTTP request (and answer).
47 typedef boost::function<void(Request*)> Callback;
64 StringMap& requestHeaders() { return _request_headers; }
65 const StringMap& requestHeaders() const { return _request_headers; }
66 std::string& requestHeader(const std::string& key)
67 { return _request_headers[key]; }
68 const std::string requestHeader(const std::string& key) const
69 { return _request_headers.get(key); }
72 * Add a handler to be called when the request successfully completes.
74 * @note If the request is already complete, the handler is called
77 Request* done(const Callback& cb);
80 Request* done(C* instance, void (C::*mem_func)(Request*))
82 return done(boost::bind(mem_func, instance, _1));
86 * Add a handler to be called when the request completes or aborts with an
89 * @note If the request has already failed, the handler is called
92 Request* fail(const Callback& cb);
95 Request* fail(C* instance, void (C::*mem_func)(Request*))
97 return fail(boost::bind(mem_func, instance, _1));
101 * Add a handler to be called when the request either successfully completes
104 * @note If the request is already complete or has already failed, the
105 * handler is called immediately.
107 Request* always(const Callback& cb);
110 Request* always(C* instance, void (C::*mem_func)(Request*))
112 return always(boost::bind(mem_func, instance, _1));
116 * Set the data for the body of the request. The request is automatically
117 * send using the POST method.
119 * @param data Body data
120 * @param type Media Type (aka MIME) of the body data
122 void setBodyData( const std::string& data,
123 const std::string& type = "text/plain" );
124 void setBodyData( const SGPropertyNode* data );
126 virtual void setUrl(const std::string& url);
128 virtual std::string method() const
130 virtual std::string url() const
133 virtual std::string scheme() const;
134 virtual std::string path() const;
135 virtual std::string host() const;
136 virtual std::string hostAndPort() const;
137 virtual unsigned short port() const;
138 virtual std::string query() const;
140 StringMap const& responseHeaders() const
141 { return _responseHeaders; }
143 std::string responseMime() const;
145 virtual int responseCode() const
146 { return _responseStatus; }
148 virtual std::string responseReason() const
149 { return _responseReason; }
151 void setResponseLength(unsigned int l);
152 virtual unsigned int responseLength() const;
155 * Check if request contains body data.
157 virtual bool hasBodyData() const;
160 * Retrieve the request body content type.
162 virtual std::string bodyType() const;
165 * Retrieve the size of the request body.
167 virtual size_t bodyLength() const;
170 * Retrieve the body data bytes. Will be passed the maximum body bytes
171 * to return in the buffer, and must return the actual number
174 virtual size_t getBodyData(char* s, size_t offset, size_t max_count) const;
177 * running total of body bytes received so far. Can be used
178 * to generate a completion percentage, if the response length is
181 unsigned int responseBytesReceived() const
182 { return _receivedBodyBytes; }
185 HTTP_VERSION_UNKNOWN = 0,
186 HTTP_0_x, // 0.9 or similar
191 HTTPVersion responseVersion() const
192 { return _responseVersion; }
194 ReadyState readyState() const { return _ready_state; }
197 * Request aborting this request.
202 * Request aborting this request and specify the reported reaseon for it.
204 void abort(const std::string& reason);
206 bool closeAfterComplete() const;
207 bool isComplete() const;
210 Request(const std::string& url, const std::string method = "GET");
212 virtual void requestStart();
213 virtual void responseStart(const std::string& r);
214 virtual void responseHeader(const std::string& key, const std::string& value);
215 virtual void responseHeadersComplete();
216 virtual void responseComplete();
217 virtual void gotBodyData(const char* s, int n);
219 virtual void onDone();
220 virtual void onFail();
221 virtual void onAlways();
223 void setFailure(int code, const std::string& reason);
227 friend class Connection;
228 friend class ContentDecoder;
230 Request(const Request&); // = delete;
231 Request& operator=(const Request&); // = delete;
233 void processBodyBytes(const char* s, int n);
234 void setReadyState(ReadyState state);
238 StringMap _request_headers;
239 std::string _request_data;
240 std::string _request_media_type;
242 HTTPVersion _responseVersion;
244 std::string _responseReason;
245 StringMap _responseHeaders;
246 unsigned int _responseLength;
247 unsigned int _receivedBodyBytes;
249 function_list<Callback> _cb_done,
253 ReadyState _ready_state;
257 typedef SGSharedPtr<Request> Request_ptr;
259 } // of namespace HTTP
260 } // of namespace simgear
262 #endif // of SG_HTTP_REQUEST_HXX