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 std::string responseMime() const;
123 virtual int responseCode() const
124 { return _responseStatus; }
126 virtual std::string responseReason() const
127 { return _responseReason; }
129 void setResponseLength(unsigned int l);
130 virtual unsigned int responseLength() const;
133 * Check if request contains body data.
135 virtual bool hasBodyData() const;
138 * Retrieve the request body content type.
140 virtual std::string bodyType() const;
143 * Retrieve the size of the request body.
145 virtual size_t bodyLength() const;
148 * Retrieve the body data bytes. Will be passed the maximum body bytes
149 * to return in the buffer, and must return the actual number
152 virtual size_t getBodyData(char* s, size_t offset, size_t max_count) const;
155 * running total of body bytes received so far. Can be used
156 * to generate a completion percentage, if the response length is
159 unsigned int responseBytesReceived() const
160 { return _receivedBodyBytes; }
163 HTTP_VERSION_UNKNOWN = 0,
164 HTTP_0_x, // 0.9 or similar
169 HTTPVersion responseVersion() const
170 { return _responseVersion; }
172 ReadyState readyState() const { return _ready_state; }
175 * Request aborting this request.
180 * Request aborting this request and specify the reported reaseon for it.
182 void abort(const std::string& reason);
184 bool closeAfterComplete() const;
185 bool isComplete() const;
188 Request(const std::string& url, const std::string method = "GET");
190 virtual void requestStart();
191 virtual void responseStart(const std::string& r);
192 virtual void responseHeader(const std::string& key, const std::string& value);
193 virtual void responseHeadersComplete();
194 virtual void responseComplete();
195 virtual void gotBodyData(const char* s, int n);
197 virtual void onDone();
198 virtual void onFail();
199 virtual void onAlways();
201 void setFailure(int code, const std::string& reason);
205 friend class Connection;
206 friend class ContentDecoder;
208 Request(const Request&); // = delete;
209 Request& operator=(const Request&); // = delete;
211 void processBodyBytes(const char* s, int n);
212 void setReadyState(ReadyState state);
216 StringMap _request_headers;
217 std::string _request_data;
218 std::string _request_media_type;
220 HTTPVersion _responseVersion;
222 std::string _responseReason;
223 StringMap _responseHeaders;
224 unsigned int _responseLength;
225 unsigned int _receivedBodyBytes;
231 ReadyState _ready_state;
235 typedef SGSharedPtr<Request> Request_ptr;
237 } // of namespace HTTP
238 } // of namespace simgear
240 #endif // of SG_HTTP_REQUEST_HXX