1 #include "HTTPClient.hxx"
7 #include <boost/foreach.hpp>
8 #include <boost/algorithm/string/case_conv.hpp>
10 #include <simgear/io/sg_netChat.hxx>
11 #include <simgear/misc/strutils.hxx>
12 #include <simgear/compiler.h>
13 #include <simgear/debug/logstream.hxx>
14 #include <simgear/timing/timestamp.hxx>
16 #if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
19 # if !defined(SIMGEAR_VERSION)
20 # define SIMGEAR_VERSION "simgear-development"
25 using std::stringstream;
34 extern const int DEFAULT_HTTP_PORT = 80;
36 class Connection : public NetChat
39 Connection(Client* pr) :
42 port(DEFAULT_HTTP_PORT)
44 setTerminator("\r\n");
47 void setServer(const string& h, short p)
53 // socket-level errors
54 virtual void handleError(int error)
56 NetChat::handleError(error);
58 SG_LOG(SG_IO, SG_INFO, "HTTP socket error");
59 activeRequest->setFailure(error, "socket error");
63 state = STATE_SOCKET_ERROR;
66 virtual void handleClose()
68 if ((state == STATE_GETTING_BODY) && activeRequest) {
69 // force state here, so responseComplete can avoid closing the
76 NetChat::handleClose();
79 void queueRequest(const Request_ptr& r)
84 queuedRequests.push_back(r);
88 void startRequest(const Request_ptr& r)
90 if (state == STATE_CLOSED) {
91 if (!connectToHost()) {
100 bodyTransferSize = -1;
101 chunkedTransfer = false;
103 stringstream headerData;
104 string path = r->path();
105 if (!client->proxyHost().empty()) {
109 headerData << r->method() << " " << path << " HTTP/1.1\r\n";
110 headerData << "Host: " << r->hostAndPort() << "\r\n";
111 headerData << "User-Agent:" << client->userAgent() << "\r\n";
112 if (!client->proxyAuth().empty()) {
113 headerData << "Proxy-Authorization: " << client->proxyAuth() << "\r\n";
116 BOOST_FOREACH(string h, r->requestHeaders()) {
117 headerData << h << ": " << r->header(h) << "\r\n";
120 headerData << "\r\n"; // final CRLF to terminate the headers
122 // TODO - add request body support for PUT, etc operations
124 bool ok = push(headerData.str().c_str());
126 SG_LOG(SG_IO, SG_WARN, "HTTP writing to socket failed");
127 state = STATE_SOCKET_ERROR;
132 virtual void collectIncomingData(const char* s, int n)
134 if ((state == STATE_GETTING_BODY) || (state == STATE_GETTING_CHUNKED_BYTES)) {
135 activeRequest->processBodyBytes(s, n);
137 buffer += string(s, n);
141 virtual void foundTerminator(void)
145 activeRequest->responseStart(buffer);
146 state = STATE_GETTING_HEADERS;
150 case STATE_GETTING_HEADERS:
155 case STATE_GETTING_BODY:
159 case STATE_GETTING_CHUNKED:
160 processChunkHeader();
163 case STATE_GETTING_CHUNKED_BYTES:
164 setTerminator("\r\n");
165 state = STATE_GETTING_CHUNKED;
168 case STATE_GETTING_TRAILER:
178 bool hasIdleTimeout() const
180 if (state != STATE_IDLE) {
184 return idleTime.elapsedMSec() > 1000 * 10; // ten seconds
187 bool hasError() const
189 return (state == STATE_SOCKET_ERROR);
194 SG_LOG(SG_IO, SG_INFO, "HTTP connecting to " << host << ":" << port);
197 SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
201 if (connect(host.c_str(), port) != 0) {
211 string h = strutils::simplify(buffer);
212 if (h.empty()) { // blank line terminates headers
215 if (chunkedTransfer) {
216 state = STATE_GETTING_CHUNKED;
218 setByteCount(bodyTransferSize); // may be -1, that's fine
219 state = STATE_GETTING_BODY;
225 int colonPos = buffer.find(':');
227 SG_LOG(SG_IO, SG_WARN, "malformed HTTP response header:" << h);
231 string key = strutils::simplify(buffer.substr(0, colonPos));
232 string lkey = boost::to_lower_copy(key);
233 string value = strutils::strip(buffer.substr(colonPos + 1));
235 // only consider these if getting headers (as opposed to trailers
236 // of a chunked transfer)
237 if (state == STATE_GETTING_HEADERS) {
238 if (lkey == "content-length") {
239 int sz = strutils::to_int(value);
240 if (bodyTransferSize <= 0) {
241 bodyTransferSize = sz;
243 activeRequest->setResponseLength(sz);
244 } else if (lkey == "transfer-length") {
245 bodyTransferSize = strutils::to_int(value);
246 } else if (lkey == "transfer-encoding") {
247 processTransferEncoding(value);
251 activeRequest->responseHeader(lkey, value);
254 void processTransferEncoding(const string& te)
256 if (te == "chunked") {
257 chunkedTransfer = true;
259 SG_LOG(SG_IO, SG_WARN, "unsupported transfer encoding:" << te);
264 void processChunkHeader()
266 if (buffer.empty()) {
267 // blank line after chunk data
272 int semiPos = buffer.find(';');
274 // extensions ignored for the moment
275 chunkSize = strutils::to_int(buffer.substr(0, semiPos), 16);
277 chunkSize = strutils::to_int(buffer, 16);
281 if (chunkSize == 0) { // trailer start
282 state = STATE_GETTING_TRAILER;
286 state = STATE_GETTING_CHUNKED_BYTES;
287 setByteCount(chunkSize);
290 void processTrailer()
292 if (buffer.empty()) {
298 // process as a normal header
302 void headersComplete()
304 activeRequest->responseHeadersComplete();
307 void responseComplete()
309 activeRequest->responseComplete();
310 client->requestFinished(this);
312 bool doClose = activeRequest->closeAfterComplete();
313 activeRequest = NULL;
314 if (state == STATE_GETTING_BODY) {
317 // this will bring us into handleClose() above, which updates
318 // state to STATE_CLOSED
323 setTerminator("\r\n");
325 if (!queuedRequests.empty()) {
326 Request_ptr next = queuedRequests.front();
327 queuedRequests.pop_front();
334 enum ConnectionState {
336 STATE_GETTING_HEADERS,
338 STATE_GETTING_CHUNKED,
339 STATE_GETTING_CHUNKED_BYTES,
340 STATE_GETTING_TRAILER,
342 STATE_CLOSED ///< connection should be closed now
346 Request_ptr activeRequest;
347 ConnectionState state;
351 int bodyTransferSize;
352 SGTimeStamp idleTime;
353 bool chunkedTransfer;
355 std::list<Request_ptr> queuedRequests;
360 setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
363 void Client::update()
367 ConnectionDict::iterator it = _connections.begin();
368 for (; it != _connections.end(); ) {
369 if (it->second->hasIdleTimeout() || it->second->hasError()) {
370 // connection has been idle for a while, clean it up
371 // (or has an error condition, again clean it up)
372 SG_LOG(SG_IO, SG_INFO, "cleaning up " << it->second);
373 ConnectionDict::iterator del = it++;
375 _connections.erase(del);
379 } // of connecion iteration
382 void Client::makeRequest(const Request_ptr& r)
384 string host = r->host();
385 int port = r->port();
386 if (!_proxy.empty()) {
392 ss << host << "-" << port;
393 string connectionId = ss.str();
395 if (_connections.find(connectionId) == _connections.end()) {
396 Connection* con = new Connection(this);
397 con->setServer(host, port);
398 _connections[connectionId] = con;
401 _connections[connectionId]->queueRequest(r);
404 void Client::requestFinished(Connection* con)
409 void Client::setUserAgent(const string& ua)
414 void Client::setProxy(const string& proxy, int port, const string& auth)
421 } // of namespace HTTP
423 } // of namespace simgear