2 * \file HTTPClient.cxx - simple HTTP client engine for SimHear
5 // Written by James Turner
7 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Library General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "HTTPClient.hxx"
28 #include <cstdlib> // rand()
34 #include <boost/foreach.hpp>
35 #include <boost/algorithm/string/case_conv.hpp>
37 #include <simgear/io/sg_netChat.hxx>
38 #include <simgear/io/HTTPContentDecode.hxx>
39 #include <simgear/misc/strutils.hxx>
40 #include <simgear/compiler.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/timing/timestamp.hxx>
43 #include <simgear/structure/exception.hxx>
45 #if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
48 # if !defined(SIMGEAR_VERSION)
49 # define SIMGEAR_VERSION "simgear-development"
54 using std::stringstream;
63 extern const int DEFAULT_HTTP_PORT = 80;
64 const char* CONTENT_TYPE_URL_ENCODED = "application/x-www-form-urlencoded";
65 const unsigned int MAX_INFLIGHT_REQUESTS = 32;
68 typedef std::multimap<std::string, Connection*> ConnectionDict;
69 typedef std::list<Request_ptr> RequestList;
71 class Client::ClientPrivate
74 std::string userAgent;
77 std::string proxyAuth;
78 NetChannelPoller poller;
79 unsigned int maxConnections;
81 RequestList pendingRequests;
83 // connections by host (potentially more than one)
84 ConnectionDict connections;
86 SGTimeStamp timeTransferSample;
87 unsigned int bytesTransferred;
88 unsigned int lastTransferRate;
91 class Connection : public NetChat
94 Connection(Client* pr) :
97 port(DEFAULT_HTTP_PORT)
101 virtual ~Connection()
105 void setServer(const string& h, short p)
111 // socket-level errors
112 virtual void handleError(int error)
114 if (error == ENOENT) {
115 // name lookup failure
116 // we won't have an active request yet, so the logic below won't
117 // fire to actually call setFailure. Let's fail all of the requests
118 BOOST_FOREACH(Request_ptr req, sentRequests) {
119 req->setFailure(error, "hostname lookup failure");
122 BOOST_FOREACH(Request_ptr req, queuedRequests) {
123 req->setFailure(error, "hostname lookup failure");
126 // name lookup failure, abandon all requests on this connection
127 sentRequests.clear();
128 queuedRequests.clear();
131 NetChat::handleError(error);
133 SG_LOG(SG_IO, SG_INFO, "HTTP socket error");
134 activeRequest->setFailure(error, "socket error");
135 activeRequest = NULL;
136 _contentDecoder.reset();
139 state = STATE_SOCKET_ERROR;
142 virtual void handleClose()
144 NetChat::handleClose();
146 // closing of the connection from the server side when getting the body,
147 bool canCloseState = (state == STATE_GETTING_BODY);
148 if (canCloseState && activeRequest) {
149 // force state here, so responseComplete can avoid closing the
151 state = STATE_CLOSED;
155 activeRequest->setFailure(500, "server closed connection");
156 // remove the failed request from sentRequests, so it does
158 RequestList::iterator it = std::find(sentRequests.begin(),
159 sentRequests.end(), activeRequest);
160 if (it != sentRequests.end()) {
161 sentRequests.erase(it);
163 activeRequest = NULL;
164 _contentDecoder.reset();
167 state = STATE_CLOSED;
170 if (sentRequests.empty()) {
174 // restore sent requests to the queue, so they will be re-sent
175 // when the connection opens again
176 queuedRequests.insert(queuedRequests.begin(),
177 sentRequests.begin(), sentRequests.end());
178 sentRequests.clear();
183 NetChat::handleError(ETIMEDOUT);
185 SG_LOG(SG_IO, SG_DEBUG, "HTTP socket timeout");
186 activeRequest->setFailure(ETIMEDOUT, "socket timeout");
187 activeRequest = NULL;
188 _contentDecoder.reset();
191 state = STATE_SOCKET_ERROR;
194 void queueRequest(const Request_ptr& r)
196 queuedRequests.push_back(r);
197 tryStartNextRequest();
202 assert(!sentRequests.empty());
203 assert(state == STATE_WAITING_FOR_RESPONSE);
205 activeRequest = sentRequests.front();
207 activeRequest->responseStart(buffer);
208 state = STATE_GETTING_HEADERS;
210 if (activeRequest->responseCode() == 204) {
211 noMessageBody = true;
212 } else if (activeRequest->method() == "HEAD") {
213 noMessageBody = true;
215 noMessageBody = false;
218 bodyTransferSize = -1;
219 chunkedTransfer = false;
220 _contentDecoder.reset();
223 void tryStartNextRequest()
225 if (queuedRequests.empty()) {
230 if (sentRequests.size() > MAX_INFLIGHT_REQUESTS) {
234 if (state == STATE_CLOSED) {
235 if (!connectToHost()) {
239 setTerminator("\r\n");
243 Request_ptr r = queuedRequests.front();
245 requestBodyBytesToSend = r->requestBodyLength();
247 stringstream headerData;
248 string path = r->path();
249 assert(!path.empty());
250 string query = r->query();
253 if (!client->proxyHost().empty()) {
254 path = r->scheme() + "://" + r->host() + r->path();
257 if (r->requestBodyType() == CONTENT_TYPE_URL_ENCODED) {
258 headerData << r->method() << " " << path << " HTTP/1.1\r\n";
259 bodyData = query.substr(1); // URL-encode, drop the leading '?'
260 headerData << "Content-Type:" << CONTENT_TYPE_URL_ENCODED << "\r\n";
261 headerData << "Content-Length:" << bodyData.size() << "\r\n";
263 headerData << r->method() << " " << path << query << " HTTP/1.1\r\n";
264 if (requestBodyBytesToSend >= 0) {
265 headerData << "Content-Length:" << requestBodyBytesToSend << "\r\n";
266 headerData << "Content-Type:" << r->requestBodyType() << "\r\n";
270 headerData << "Host: " << r->hostAndPort() << "\r\n";
271 headerData << "User-Agent:" << client->userAgent() << "\r\n";
272 headerData << "Accept-Encoding: deflate, gzip\r\n";
273 if (!client->proxyAuth().empty()) {
274 headerData << "Proxy-Authorization: " << client->proxyAuth() << "\r\n";
277 BOOST_FOREACH(string h, r->requestHeaders()) {
278 headerData << h << ": " << r->header(h) << "\r\n";
281 headerData << "\r\n"; // final CRLF to terminate the headers
282 if (!bodyData.empty()) {
283 headerData << bodyData;
286 bool ok = push(headerData.str().c_str());
288 SG_LOG(SG_IO, SG_WARN, "HTTPClient: over-stuffed the socket");
289 // we've over-stuffed the socket, give up for now, let things
290 // drain down before trying to start any more requests.
294 while (requestBodyBytesToSend > 0) {
296 int len = r->getBodyData(buf, 4096);
298 requestBodyBytesToSend -= len;
299 if (!bufferSend(buf, len)) {
300 SG_LOG(SG_IO, SG_WARN, "overflow the HTTP::Connection output buffer");
301 state = STATE_SOCKET_ERROR;
304 // SG_LOG(SG_IO, SG_INFO, "sent body:\n" << string(buf, len) << "\n%%%%%%%%%");
306 SG_LOG(SG_IO, SG_WARN, "HTTP asynchronous request body generation is unsupported");
311 // SG_LOG(SG_IO, SG_INFO, "did start request:" << r->url() <<
312 // "\n\t @ " << reinterpret_cast<void*>(r.ptr()) <<
313 // "\n\t on connection " << this);
314 // successfully sent, remove from queue, and maybe send the next
315 queuedRequests.pop_front();
316 sentRequests.push_back(r);
317 state = STATE_WAITING_FOR_RESPONSE;
319 // pipelining, let's maybe send the next request right away
320 tryStartNextRequest();
323 virtual void collectIncomingData(const char* s, int n)
326 client->receivedBytes(static_cast<unsigned int>(n));
328 if ((state == STATE_GETTING_BODY) || (state == STATE_GETTING_CHUNKED_BYTES)) {
329 _contentDecoder.receivedBytes(s, n);
331 buffer += string(s, n);
335 virtual void foundTerminator(void)
339 case STATE_WAITING_FOR_RESPONSE:
343 case STATE_GETTING_HEADERS:
348 case STATE_GETTING_BODY:
352 case STATE_GETTING_CHUNKED:
353 processChunkHeader();
356 case STATE_GETTING_CHUNKED_BYTES:
357 setTerminator("\r\n");
358 state = STATE_GETTING_CHUNKED;
363 case STATE_GETTING_TRAILER:
369 SG_LOG(SG_IO, SG_WARN, "HTTP got data in IDLE state, bad server?");
376 bool hasIdleTimeout() const
378 if (state != STATE_IDLE) {
382 assert(sentRequests.empty());
383 return idleTime.elapsedMSec() > 1000 * 10; // ten seconds
386 bool hasErrorTimeout() const
388 if (state == STATE_IDLE) {
392 return idleTime.elapsedMSec() > (1000 * 30); // 30 seconds
395 bool hasError() const
397 return (state == STATE_SOCKET_ERROR);
400 bool shouldStartNext() const
402 return !queuedRequests.empty() && (sentRequests.size() < MAX_INFLIGHT_REQUESTS);
405 bool isActive() const
407 return !queuedRequests.empty() || !sentRequests.empty();
412 SG_LOG(SG_IO, SG_DEBUG, "HTTP connecting to " << host << ":" << port);
415 SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
419 if (connect(host.c_str(), port) != 0) {
429 string h = strutils::simplify(buffer);
430 if (h.empty()) { // blank line terminates headers
435 int colonPos = buffer.find(':');
437 SG_LOG(SG_IO, SG_WARN, "malformed HTTP response header:" << h);
441 string key = strutils::simplify(buffer.substr(0, colonPos));
442 string lkey = boost::to_lower_copy(key);
443 string value = strutils::strip(buffer.substr(colonPos + 1));
445 // only consider these if getting headers (as opposed to trailers
446 // of a chunked transfer)
447 if (state == STATE_GETTING_HEADERS) {
448 if (lkey == "content-length") {
450 int sz = strutils::to_int(value);
451 if (bodyTransferSize <= 0) {
452 bodyTransferSize = sz;
454 activeRequest->setResponseLength(sz);
455 } else if (lkey == "transfer-length") {
456 bodyTransferSize = strutils::to_int(value);
457 } else if (lkey == "transfer-encoding") {
458 processTransferEncoding(value);
459 } else if (lkey == "content-encoding") {
460 _contentDecoder.setEncoding(value);
464 activeRequest->responseHeader(lkey, value);
467 void processTransferEncoding(const string& te)
469 if (te == "chunked") {
470 chunkedTransfer = true;
472 SG_LOG(SG_IO, SG_WARN, "unsupported transfer encoding:" << te);
477 void processChunkHeader()
479 if (buffer.empty()) {
480 // blank line after chunk data
485 int semiPos = buffer.find(';');
487 // extensions ignored for the moment
488 chunkSize = strutils::to_int(buffer.substr(0, semiPos), 16);
490 chunkSize = strutils::to_int(buffer, 16);
494 if (chunkSize == 0) { // trailer start
495 state = STATE_GETTING_TRAILER;
499 state = STATE_GETTING_CHUNKED_BYTES;
500 setByteCount(chunkSize);
503 void processTrailer()
505 if (buffer.empty()) {
511 // process as a normal header
515 void headersComplete()
517 activeRequest->responseHeadersComplete();
518 _contentDecoder.initWithRequest(activeRequest);
520 if (chunkedTransfer) {
521 state = STATE_GETTING_CHUNKED;
522 } else if (noMessageBody || (bodyTransferSize == 0)) {
523 // force the state to GETTING_BODY, to simplify logic in
524 // responseComplete and handleClose
525 state = STATE_GETTING_BODY;
528 setByteCount(bodyTransferSize); // may be -1, that's fine
529 state = STATE_GETTING_BODY;
533 void responseComplete()
535 Request_ptr completedRequest = activeRequest;
536 _contentDecoder.finish();
538 assert(sentRequests.front() == activeRequest);
539 sentRequests.pop_front();
540 bool doClose = activeRequest->closeAfterComplete();
541 activeRequest = NULL;
543 if ((state == STATE_GETTING_BODY) || (state == STATE_GETTING_TRAILER)) {
545 // this will bring us into handleClose() above, which updates
546 // state to STATE_CLOSED
549 // if we have additional requests waiting, try to start them now
550 tryStartNextRequest();
554 if (state != STATE_CLOSED) {
555 state = sentRequests.empty() ? STATE_IDLE : STATE_WAITING_FOR_RESPONSE;
558 // notify request after we change state, so this connection is idle
559 // if completion triggers other requests (which is likely)
560 // SG_LOG(SG_IO, SG_INFO, "*** responseComplete:" << activeRequest->url());
561 completedRequest->responseComplete();
562 client->requestFinished(this);
564 setTerminator("\r\n");
567 enum ConnectionState {
569 STATE_WAITING_FOR_RESPONSE,
570 STATE_GETTING_HEADERS,
572 STATE_GETTING_CHUNKED,
573 STATE_GETTING_CHUNKED_BYTES,
574 STATE_GETTING_TRAILER,
576 STATE_CLOSED ///< connection should be closed now
580 Request_ptr activeRequest;
581 ConnectionState state;
585 int bodyTransferSize;
586 SGTimeStamp idleTime;
587 bool chunkedTransfer;
589 int requestBodyBytesToSend;
591 RequestList queuedRequests;
592 RequestList sentRequests;
594 ContentDecoder _contentDecoder;
601 d->maxConnections = 4;
602 d->bytesTransferred = 0;
603 d->lastTransferRate = 0;
604 d->timeTransferSample.stamp();
606 setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
613 void Client::setMaxConnections(unsigned int maxCon)
616 throw sg_range_exception("illegal HTTP::Client::setMaxConnections value");
619 d->maxConnections = maxCon;
622 void Client::update(int waitTimeout)
624 d->poller.poll(waitTimeout);
625 bool waitingRequests = !d->pendingRequests.empty();
627 ConnectionDict::iterator it = d->connections.begin();
628 for (; it != d->connections.end(); ) {
629 Connection* con = it->second;
630 if (con->hasIdleTimeout() ||
632 con->hasErrorTimeout() ||
633 (!con->isActive() && waitingRequests))
635 if (con->hasErrorTimeout()) {
636 // tell the connection we're timing it out
637 con->handleTimeout();
640 // connection has been idle for a while, clean it up
641 // (or if we have requests waiting for a different host,
642 // or an error condition
643 ConnectionDict::iterator del = it++;
645 d->connections.erase(del);
647 if (it->second->shouldStartNext()) {
648 it->second->tryStartNextRequest();
652 } // of connection iteration
654 if (waitingRequests && (d->connections.size() < d->maxConnections)) {
655 RequestList waiting(d->pendingRequests);
656 d->pendingRequests.clear();
658 // re-submit all waiting requests in order; this takes care of
659 // finding multiple pending items targetted to the same (new)
661 BOOST_FOREACH(Request_ptr req, waiting) {
667 void Client::makeRequest(const Request_ptr& r)
669 string host = r->host();
670 int port = r->port();
671 if (!d->proxy.empty()) {
676 Connection* con = NULL;
678 ss << host << "-" << port;
679 string connectionId = ss.str();
680 bool havePending = !d->pendingRequests.empty();
681 bool atConnectionsLimit = d->connections.size() >= d->maxConnections;
682 ConnectionDict::iterator consEnd = d->connections.end();
684 // assign request to an existing Connection.
685 // various options exist here, examined in order
686 ConnectionDict::iterator it = d->connections.find(connectionId);
687 if (atConnectionsLimit && (it == consEnd)) {
688 // maximum number of connections active, queue this request
689 // when a connection goes inactive, we'll start this one
690 d->pendingRequests.push_back(r);
694 // scan for an idle Connection to the same host (likely if we're
695 // retrieving multiple resources from the same host in quick succession)
696 // if we have pending requests (waiting for a free Connection), then
697 // force new requests on this id to always use the first Connection
698 // (instead of the random selection below). This ensures that when
699 // there's pressure on the number of connections to keep alive, one
700 // host can't DoS every other.
702 for (; (it != consEnd) && (it->first == connectionId); ++it, ++count) {
703 if (havePending || !it->second->isActive()) {
709 if (!con && atConnectionsLimit) {
710 // all current connections are busy (active), and we don't
711 // have free connections to allocate, so let's assign to
712 // an existing one randomly. Ideally we'd used whichever one will
713 // complete first but we don't have that info.
714 int index = rand() % count;
715 for (it = d->connections.find(connectionId); index > 0; --index) { ; }
719 // allocate a new connection object
721 con = new Connection(this);
722 con->setServer(host, port);
723 d->poller.addChannel(con);
724 d->connections.insert(d->connections.end(),
725 ConnectionDict::value_type(connectionId, con));
728 con->queueRequest(r);
731 void Client::requestFinished(Connection* con)
736 void Client::setUserAgent(const string& ua)
741 const std::string& Client::userAgent() const
746 const std::string& Client::proxyHost() const
751 const std::string& Client::proxyAuth() const
756 void Client::setProxy(const string& proxy, int port, const string& auth)
763 bool Client::hasActiveRequests() const
765 ConnectionDict::const_iterator it = d->connections.begin();
766 for (; it != d->connections.end(); ++it) {
767 if (it->second->isActive()) return true;
773 void Client::receivedBytes(unsigned int count)
775 d->bytesTransferred += count;
778 unsigned int Client::transferRateBytesPerSec() const
780 unsigned int e = d->timeTransferSample.elapsedMSec();
782 // if called too frequently, return cahced value, to smooth out
783 // < 1 sec changes in flow
784 return d->lastTransferRate;
787 unsigned int ratio = (d->bytesTransferred * 1000) / e;
788 d->timeTransferSample.stamp();
789 d->bytesTransferred = 0;
790 d->lastTransferRate = ratio;
794 } // of namespace HTTP
796 } // of namespace simgear