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/function.hpp>
24 typedef boost::function<void(Request*)> Callback;
41 StringMap& requestHeaders() { return _request_headers; }
42 const StringMap& requestHeaders() const { return _request_headers; }
43 std::string& requestHeader(const std::string& key)
44 { return _request_headers[key]; }
45 const std::string requestHeader(const std::string& key) const
46 { return _request_headers.get(key); }
49 * Set the handler to be called when the request successfully completes.
51 * @note If the request is already complete, the handler is called
54 Request* done(const Callback& cb);
57 * Set the handler to be called when the request completes or aborts with an
60 * @note If the request has already failed, the handler is called
63 Request* fail(const Callback& cb);
66 * Set the handler to be called when the request either successfully
69 * @note If the request is already complete or has already failed, the
70 * handler is called immediately.
72 Request* always(const Callback& cb);
75 * Set the data for the body of the request. The request is automatically
76 * send using the POST method.
78 * @param data Body data
79 * @param type Media Type (aka MIME) of the body data
81 void setBodyData( const std::string& data,
82 const std::string& type = "text/plain" );
83 void setBodyData( const SGPropertyNode* data );
85 virtual void setUrl(const std::string& url);
87 virtual std::string method() const
89 virtual std::string url() const
92 virtual std::string scheme() const;
93 virtual std::string path() const;
94 virtual std::string host() const;
95 virtual std::string hostAndPort() const;
96 virtual unsigned short port() const;
97 virtual std::string query() const;
99 StringMap const& responseHeaders() const
100 { return _responseHeaders; }
102 virtual int responseCode() const
103 { return _responseStatus; }
105 virtual std::string responseReason() const
106 { return _responseReason; }
108 void setResponseLength(unsigned int l);
109 virtual unsigned int responseLength() const;
112 * Check if request contains body data.
114 virtual bool hasBodyData() const;
117 * Retrieve the request body content type.
119 virtual std::string bodyType() const;
122 * Retrieve the size of the request body.
124 virtual size_t bodyLength() const;
127 * Retrieve the body data bytes. Will be passed the maximum body bytes
128 * to return in the buffer, and must return the actual number
131 virtual size_t getBodyData(char* s, size_t offset, size_t max_count) const;
134 * running total of body bytes received so far. Can be used
135 * to generate a completion percentage, if the response length is
138 unsigned int responseBytesReceived() const
139 { return _receivedBodyBytes; }
142 HTTP_VERSION_UNKNOWN = 0,
143 HTTP_0_x, // 0.9 or similar
148 HTTPVersion responseVersion() const
149 { return _responseVersion; }
151 ReadyState readyState() const { return _ready_state; }
154 * Request aborting this request.
159 * Request aborting this request and specify the reported reaseon for it.
161 void abort(const std::string& reason);
163 bool closeAfterComplete() const;
164 bool isComplete() const;
167 Request(const std::string& url, const std::string method = "GET");
169 virtual void requestStart();
170 virtual void responseStart(const std::string& r);
171 virtual void responseHeader(const std::string& key, const std::string& value);
172 virtual void responseHeadersComplete();
173 virtual void responseComplete();
174 virtual void gotBodyData(const char* s, int n);
176 virtual void onDone();
177 virtual void onFail();
178 virtual void onAlways();
182 friend class Connection;
183 friend class ContentDecoder;
185 Request(const Request&); // = delete;
186 Request& operator=(const Request&); // = delete;
188 void processBodyBytes(const char* s, int n);
189 void setFailure(int code, const std::string& reason);
190 void setReadyState(ReadyState state);
194 StringMap _request_headers;
195 std::string _request_data;
196 std::string _request_media_type;
198 HTTPVersion _responseVersion;
200 std::string _responseReason;
201 StringMap _responseHeaders;
202 unsigned int _responseLength;
203 unsigned int _receivedBodyBytes;
209 ReadyState _ready_state;
213 typedef SGSharedPtr<Request> Request_ptr;
215 } // of namespace HTTP
216 } // of namespace simgear
218 #endif // of SG_HTTP_REQUEST_HXX