1 #ifndef SG_HTTP_REQUEST_HXX
2 #define SG_HTTP_REQUEST_HXX
6 #include <simgear/structure/map.hxx>
7 #include <simgear/structure/SGReferenced.hxx>
8 #include <simgear/structure/SGSharedPtr.hxx>
9 #include <simgear/math/sg_types.hxx>
11 #include <boost/bind.hpp>
12 #include <boost/function.hpp>
25 typedef boost::function<void(Request*)> Callback;
42 StringMap& requestHeaders() { return _request_headers; }
43 const StringMap& requestHeaders() const { return _request_headers; }
44 std::string& requestHeader(const std::string& key)
45 { return _request_headers[key]; }
46 const std::string requestHeader(const std::string& key) const
47 { return _request_headers.get(key); }
50 * Set the handler to be called when the request successfully completes.
52 * @note If the request is already complete, the handler is called
55 Request* done(const Callback& cb);
58 Request* done(C* instance, void (C::*mem_func)(Request*))
60 return done(boost::bind(mem_func, instance, _1));
64 * Set the handler to be called when the request completes or aborts with an
67 * @note If the request has already failed, the handler is called
70 Request* fail(const Callback& cb);
73 Request* fail(C* instance, void (C::*mem_func)(Request*))
75 return fail(boost::bind(mem_func, instance, _1));
79 * Set the handler to be called when the request either successfully
82 * @note If the request is already complete or has already failed, the
83 * handler is called immediately.
85 Request* always(const Callback& cb);
88 Request* always(C* instance, void (C::*mem_func)(Request*))
90 return always(boost::bind(mem_func, instance, _1));
94 * Set the data for the body of the request. The request is automatically
95 * send using the POST method.
97 * @param data Body data
98 * @param type Media Type (aka MIME) of the body data
100 void setBodyData( const std::string& data,
101 const std::string& type = "text/plain" );
102 void setBodyData( const SGPropertyNode* data );
104 virtual void setUrl(const std::string& url);
106 virtual std::string method() const
108 virtual std::string url() const
111 virtual std::string scheme() const;
112 virtual std::string path() const;
113 virtual std::string host() const;
114 virtual std::string hostAndPort() const;
115 virtual unsigned short port() const;
116 virtual std::string query() const;
118 StringMap const& responseHeaders() const
119 { return _responseHeaders; }
121 virtual int responseCode() const
122 { return _responseStatus; }
124 virtual std::string responseReason() const
125 { return _responseReason; }
127 void setResponseLength(unsigned int l);
128 virtual unsigned int responseLength() const;
131 * Check if request contains body data.
133 virtual bool hasBodyData() const;
136 * Retrieve the request body content type.
138 virtual std::string bodyType() const;
141 * Retrieve the size of the request body.
143 virtual size_t bodyLength() const;
146 * Retrieve the body data bytes. Will be passed the maximum body bytes
147 * to return in the buffer, and must return the actual number
150 virtual size_t getBodyData(char* s, size_t offset, size_t max_count) const;
153 * running total of body bytes received so far. Can be used
154 * to generate a completion percentage, if the response length is
157 unsigned int responseBytesReceived() const
158 { return _receivedBodyBytes; }
161 HTTP_VERSION_UNKNOWN = 0,
162 HTTP_0_x, // 0.9 or similar
167 HTTPVersion responseVersion() const
168 { return _responseVersion; }
170 ReadyState readyState() const { return _ready_state; }
173 * Request aborting this request.
178 * Request aborting this request and specify the reported reaseon for it.
180 void abort(const std::string& reason);
182 bool closeAfterComplete() const;
183 bool isComplete() const;
186 Request(const std::string& url, const std::string method = "GET");
188 virtual void requestStart();
189 virtual void responseStart(const std::string& r);
190 virtual void responseHeader(const std::string& key, const std::string& value);
191 virtual void responseHeadersComplete();
192 virtual void responseComplete();
193 virtual void gotBodyData(const char* s, int n);
195 virtual void onDone();
196 virtual void onFail();
197 virtual void onAlways();
199 void setFailure(int code, const std::string& reason);
203 friend class Connection;
204 friend class ContentDecoder;
206 Request(const Request&); // = delete;
207 Request& operator=(const Request&); // = delete;
209 void processBodyBytes(const char* s, int n);
210 void setReadyState(ReadyState state);
214 StringMap _request_headers;
215 std::string _request_data;
216 std::string _request_media_type;
218 HTTPVersion _responseVersion;
220 std::string _responseReason;
221 StringMap _responseHeaders;
222 unsigned int _responseLength;
223 unsigned int _receivedBodyBytes;
229 ReadyState _ready_state;
233 typedef SGSharedPtr<Request> Request_ptr;
235 } // of namespace HTTP
236 } // of namespace simgear
238 #endif // of SG_HTTP_REQUEST_HXX