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;
35 const char* CONTENT_TYPE_URL_ENCODED = "application/x-www-form-urlencoded";
37 class Connection : public NetChat
40 Connection(Client* pr) :
43 port(DEFAULT_HTTP_PORT)
48 void setServer(const string& h, short p)
54 // socket-level errors
55 virtual void handleError(int error)
57 NetChat::handleError(error);
59 SG_LOG(SG_IO, SG_INFO, "HTTP socket error");
60 activeRequest->setFailure(error, "socket error");
64 state = STATE_SOCKET_ERROR;
67 virtual void handleClose()
69 NetChat::handleClose();
71 if ((state == STATE_GETTING_BODY) && activeRequest) {
72 // force state here, so responseComplete can avoid closing the
81 void queueRequest(const Request_ptr& r)
86 queuedRequests.push_back(r);
90 void startRequest(const Request_ptr& r)
92 if (state == STATE_CLOSED) {
93 if (!connectToHost()) {
101 state = STATE_SENT_REQUEST;
102 bodyTransferSize = -1;
103 chunkedTransfer = false;
104 noMessageBody = (r->method() == "HEAD");
105 setTerminator("\r\n");
107 stringstream headerData;
108 string path = r->path();
109 string query = r->query();
112 if (!client->proxyHost().empty()) {
113 path = r->scheme() + "://" + r->host() + r->path();
116 if (r->method() == "POST") {
117 headerData << r->method() << " " << path << " HTTP/1.1\r\n";
118 bodyData = query.substr(1); // URL-encode, drop the leading '?'
119 headerData << "Content-Type:" << CONTENT_TYPE_URL_ENCODED << "\r\n";
120 headerData << "Content-Length:" << bodyData.size() << "\r\n";
122 headerData << r->method() << " " << path << query << " HTTP/1.1\r\n";
125 headerData << "Host: " << r->hostAndPort() << "\r\n";
126 headerData << "User-Agent:" << client->userAgent() << "\r\n";
127 if (!client->proxyAuth().empty()) {
128 headerData << "Proxy-Authorization: " << client->proxyAuth() << "\r\n";
131 BOOST_FOREACH(string h, r->requestHeaders()) {
132 headerData << h << ": " << r->header(h) << "\r\n";
135 headerData << "\r\n"; // final CRLF to terminate the headers
136 if (!bodyData.empty()) {
137 headerData << bodyData;
140 bool ok = push(headerData.str().c_str());
142 SG_LOG(SG_IO, SG_WARN, "HTTP writing to socket failed");
143 state = STATE_SOCKET_ERROR;
148 virtual void collectIncomingData(const char* s, int n)
150 if ((state == STATE_GETTING_BODY) || (state == STATE_GETTING_CHUNKED_BYTES)) {
151 activeRequest->processBodyBytes(s, n);
153 buffer += string(s, n);
157 virtual void foundTerminator(void)
160 case STATE_SENT_REQUEST:
161 activeRequest->responseStart(buffer);
162 state = STATE_GETTING_HEADERS;
164 if (activeRequest->responseCode() == 204) {
165 noMessageBody = true;
170 case STATE_GETTING_HEADERS:
175 case STATE_GETTING_BODY:
179 case STATE_GETTING_CHUNKED:
180 processChunkHeader();
183 case STATE_GETTING_CHUNKED_BYTES:
184 setTerminator("\r\n");
185 state = STATE_GETTING_CHUNKED;
189 case STATE_GETTING_TRAILER:
199 bool hasIdleTimeout() const
201 if (state != STATE_IDLE) {
205 return idleTime.elapsedMSec() > 1000 * 10; // ten seconds
208 bool hasError() const
210 return (state == STATE_SOCKET_ERROR);
213 bool shouldStartNext() const
215 return !activeRequest && !queuedRequests.empty() &&
216 ((state == STATE_CLOSED) || (state == STATE_IDLE));
221 Request_ptr next = queuedRequests.front();
222 queuedRequests.pop_front();
228 SG_LOG(SG_IO, SG_INFO, "HTTP connecting to " << host << ":" << port);
231 SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
235 if (connect(host.c_str(), port) != 0) {
245 string h = strutils::simplify(buffer);
246 if (h.empty()) { // blank line terminates headers
249 if (chunkedTransfer) {
250 state = STATE_GETTING_CHUNKED;
251 } else if (noMessageBody || (bodyTransferSize == 0)) {
252 // force the state to GETTING_BODY, to simplify logic in
253 // responseComplete and handleClose
254 state = STATE_GETTING_BODY;
257 setByteCount(bodyTransferSize); // may be -1, that's fine
258 state = STATE_GETTING_BODY;
264 int colonPos = buffer.find(':');
266 SG_LOG(SG_IO, SG_WARN, "malformed HTTP response header:" << h);
270 string key = strutils::simplify(buffer.substr(0, colonPos));
271 string lkey = boost::to_lower_copy(key);
272 string value = strutils::strip(buffer.substr(colonPos + 1));
274 // only consider these if getting headers (as opposed to trailers
275 // of a chunked transfer)
276 if (state == STATE_GETTING_HEADERS) {
277 if (lkey == "content-length") {
278 int sz = strutils::to_int(value);
279 if (bodyTransferSize <= 0) {
280 bodyTransferSize = sz;
282 activeRequest->setResponseLength(sz);
283 } else if (lkey == "transfer-length") {
284 bodyTransferSize = strutils::to_int(value);
285 } else if (lkey == "transfer-encoding") {
286 processTransferEncoding(value);
290 activeRequest->responseHeader(lkey, value);
293 void processTransferEncoding(const string& te)
295 if (te == "chunked") {
296 chunkedTransfer = true;
298 SG_LOG(SG_IO, SG_WARN, "unsupported transfer encoding:" << te);
303 void processChunkHeader()
305 if (buffer.empty()) {
306 // blank line after chunk data
311 int semiPos = buffer.find(';');
313 // extensions ignored for the moment
314 chunkSize = strutils::to_int(buffer.substr(0, semiPos), 16);
316 chunkSize = strutils::to_int(buffer, 16);
320 if (chunkSize == 0) { // trailer start
321 state = STATE_GETTING_TRAILER;
325 state = STATE_GETTING_CHUNKED_BYTES;
326 setByteCount(chunkSize);
329 void processTrailer()
331 if (buffer.empty()) {
337 // process as a normal header
341 void headersComplete()
343 activeRequest->responseHeadersComplete();
346 void responseComplete()
348 activeRequest->responseComplete();
349 client->requestFinished(this);
351 bool doClose = activeRequest->closeAfterComplete();
352 activeRequest = NULL;
353 if (state == STATE_GETTING_BODY) {
356 // this will bring us into handleClose() above, which updates
357 // state to STATE_CLOSED
362 setTerminator("\r\n");
364 // if we have more requests, and we're idle, can start the next
365 // request immediately. Note we cannot do this if we're in STATE_CLOSED,
366 // since NetChannel::close cleans up state after calling handleClose;
367 // instead we pick up waiting requests in update()
368 if (!queuedRequests.empty() && (state == STATE_IDLE)) {
375 enum ConnectionState {
378 STATE_GETTING_HEADERS,
380 STATE_GETTING_CHUNKED,
381 STATE_GETTING_CHUNKED_BYTES,
382 STATE_GETTING_TRAILER,
384 STATE_CLOSED ///< connection should be closed now
388 Request_ptr activeRequest;
389 ConnectionState state;
393 int bodyTransferSize;
394 SGTimeStamp idleTime;
395 bool chunkedTransfer;
398 std::list<Request_ptr> queuedRequests;
403 setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
406 void Client::update()
410 ConnectionDict::iterator it = _connections.begin();
411 for (; it != _connections.end(); ) {
412 if (it->second->hasIdleTimeout() || it->second->hasError()) {
413 // connection has been idle for a while, clean it up
414 // (or has an error condition, again clean it up)
415 SG_LOG(SG_IO, SG_INFO, "cleaning up " << it->second);
416 ConnectionDict::iterator del = it++;
418 _connections.erase(del);
420 if (it->second->shouldStartNext()) {
421 it->second->startNext();
426 } // of connecion iteration
429 void Client::makeRequest(const Request_ptr& r)
431 string host = r->host();
432 int port = r->port();
433 if (!_proxy.empty()) {
439 ss << host << "-" << port;
440 string connectionId = ss.str();
442 if (_connections.find(connectionId) == _connections.end()) {
443 Connection* con = new Connection(this);
444 con->setServer(host, port);
445 _connections[connectionId] = con;
448 _connections[connectionId]->queueRequest(r);
451 void Client::requestFinished(Connection* con)
456 void Client::setUserAgent(const string& ua)
461 void Client::setProxy(const string& proxy, int port, const string& auth)
468 } // of namespace HTTP
470 } // of namespace simgear