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;
29 //using namespace std;
37 extern const int DEFAULT_HTTP_PORT = 80;
39 class Connection : public NetChat
42 Connection(Client* pr) :
45 port(DEFAULT_HTTP_PORT)
50 void setServer(const string& h, short p)
56 // socket-level errors
57 virtual void handleError(int error)
59 NetChat::handleError(error);
61 SG_LOG(SG_IO, SG_INFO, "HTTP socket error");
62 activeRequest->setFailure(error, "socket error");
66 state = STATE_SOCKET_ERROR;
69 virtual void handleClose()
71 NetChat::handleClose();
73 if ((state == STATE_GETTING_BODY) && activeRequest) {
74 // force state here, so responseComplete can avoid closing the
83 void queueRequest(const Request_ptr& r)
88 queuedRequests.push_back(r);
92 void startRequest(const Request_ptr& r)
94 if (state == STATE_CLOSED) {
95 if (!connectToHost()) {
103 state = STATE_SENT_REQUEST;
104 bodyTransferSize = -1;
105 chunkedTransfer = false;
106 setTerminator("\r\n");
108 stringstream headerData;
109 string path = r->path();
110 if (!client->proxyHost().empty()) {
114 headerData << r->method() << " " << path << " HTTP/1.1\r\n";
115 headerData << "Host: " << r->hostAndPort() << "\r\n";
116 headerData << "User-Agent:" << client->userAgent() << "\r\n";
117 if (!client->proxyAuth().empty()) {
118 headerData << "Proxy-Authorization: " << client->proxyAuth() << "\r\n";
121 BOOST_FOREACH(string h, r->requestHeaders()) {
122 headerData << h << ": " << r->header(h) << "\r\n";
125 headerData << "\r\n"; // final CRLF to terminate the headers
127 // TODO - add request body support for PUT, etc operations
129 bool ok = push(headerData.str().c_str());
131 SG_LOG(SG_IO, SG_WARN, "HTTP writing to socket failed");
132 state = STATE_SOCKET_ERROR;
137 virtual void collectIncomingData(const char* s, int n)
139 if ((state == STATE_GETTING_BODY) || (state == STATE_GETTING_CHUNKED_BYTES)) {
140 activeRequest->processBodyBytes(s, n);
142 buffer += string(s, n);
146 virtual void foundTerminator(void)
149 case STATE_SENT_REQUEST:
150 activeRequest->responseStart(buffer);
151 state = STATE_GETTING_HEADERS;
155 case STATE_GETTING_HEADERS:
160 case STATE_GETTING_BODY:
164 case STATE_GETTING_CHUNKED:
165 processChunkHeader();
168 case STATE_GETTING_CHUNKED_BYTES:
169 setTerminator("\r\n");
170 state = STATE_GETTING_CHUNKED;
174 case STATE_GETTING_TRAILER:
184 bool hasIdleTimeout() const
186 if (state != STATE_IDLE) {
190 return idleTime.elapsedMSec() > 1000 * 10; // ten seconds
193 bool hasError() const
195 return (state == STATE_SOCKET_ERROR);
198 bool shouldStartNext() const
200 return !activeRequest && !queuedRequests.empty() &&
201 ((state == STATE_CLOSED) || (state == STATE_IDLE));
206 Request_ptr next = queuedRequests.front();
207 queuedRequests.pop_front();
213 SG_LOG(SG_IO, SG_INFO, "HTTP connecting to " << host << ":" << port);
216 SG_LOG(SG_ALL, SG_WARN, "HTTP::Connection: connectToHost: open() failed");
220 if (connect(host.c_str(), port) != 0) {
230 string h = strutils::simplify(buffer);
231 if (h.empty()) { // blank line terminates headers
234 if (chunkedTransfer) {
235 state = STATE_GETTING_CHUNKED;
237 setByteCount(bodyTransferSize); // may be -1, that's fine
238 state = STATE_GETTING_BODY;
244 int colonPos = buffer.find(':');
246 SG_LOG(SG_IO, SG_WARN, "malformed HTTP response header:" << h);
250 string key = strutils::simplify(buffer.substr(0, colonPos));
251 string lkey = boost::to_lower_copy(key);
252 string value = strutils::strip(buffer.substr(colonPos + 1));
254 // only consider these if getting headers (as opposed to trailers
255 // of a chunked transfer)
256 if (state == STATE_GETTING_HEADERS) {
257 if (lkey == "content-length") {
258 int sz = strutils::to_int(value);
259 if (bodyTransferSize <= 0) {
260 bodyTransferSize = sz;
262 activeRequest->setResponseLength(sz);
263 } else if (lkey == "transfer-length") {
264 bodyTransferSize = strutils::to_int(value);
265 } else if (lkey == "transfer-encoding") {
266 processTransferEncoding(value);
270 activeRequest->responseHeader(lkey, value);
273 void processTransferEncoding(const string& te)
275 if (te == "chunked") {
276 chunkedTransfer = true;
278 SG_LOG(SG_IO, SG_WARN, "unsupported transfer encoding:" << te);
283 void processChunkHeader()
285 if (buffer.empty()) {
286 // blank line after chunk data
291 int semiPos = buffer.find(';');
293 // extensions ignored for the moment
294 chunkSize = strutils::to_int(buffer.substr(0, semiPos), 16);
296 chunkSize = strutils::to_int(buffer, 16);
300 if (chunkSize == 0) { // trailer start
301 state = STATE_GETTING_TRAILER;
305 state = STATE_GETTING_CHUNKED_BYTES;
306 setByteCount(chunkSize);
309 void processTrailer()
311 if (buffer.empty()) {
317 // process as a normal header
321 void headersComplete()
323 activeRequest->responseHeadersComplete();
326 void responseComplete()
328 activeRequest->responseComplete();
329 client->requestFinished(this);
330 //cout << "response complete: " << activeRequest->url() << endl;
332 bool doClose = activeRequest->closeAfterComplete();
333 activeRequest = NULL;
334 if (state == STATE_GETTING_BODY) {
337 // this will bring us into handleClose() above, which updates
338 // state to STATE_CLOSED
343 setTerminator("\r\n");
345 // if we have more requests, and we're idle, can start the next
346 // request immediately. Note we cannot do this if we're in STATE_CLOSED,
347 // since NetChannel::close cleans up state after calling handleClose;
348 // instead we pick up waiting requests in update()
349 if (!queuedRequests.empty() && (state == STATE_IDLE)) {
356 enum ConnectionState {
359 STATE_GETTING_HEADERS,
361 STATE_GETTING_CHUNKED,
362 STATE_GETTING_CHUNKED_BYTES,
363 STATE_GETTING_TRAILER,
365 STATE_CLOSED ///< connection should be closed now
369 Request_ptr activeRequest;
370 ConnectionState state;
374 int bodyTransferSize;
375 SGTimeStamp idleTime;
376 bool chunkedTransfer;
378 std::list<Request_ptr> queuedRequests;
383 setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
386 void Client::update()
390 ConnectionDict::iterator it = _connections.begin();
391 for (; it != _connections.end(); ) {
392 if (it->second->hasIdleTimeout() || it->second->hasError()) {
393 // connection has been idle for a while, clean it up
394 // (or has an error condition, again clean it up)
395 SG_LOG(SG_IO, SG_INFO, "cleaning up " << it->second);
396 ConnectionDict::iterator del = it++;
398 _connections.erase(del);
400 if (it->second->shouldStartNext()) {
401 it->second->startNext();
406 } // of connecion iteration
409 void Client::makeRequest(const Request_ptr& r)
411 string host = r->host();
412 int port = r->port();
413 if (!_proxy.empty()) {
419 ss << host << "-" << port;
420 string connectionId = ss.str();
422 if (_connections.find(connectionId) == _connections.end()) {
423 Connection* con = new Connection(this);
424 con->setServer(host, port);
425 _connections[connectionId] = con;
428 _connections[connectionId]->queueRequest(r);
431 void Client::requestFinished(Connection* con)
436 void Client::setUserAgent(const string& ua)
441 void Client::setProxy(const string& proxy, int port, const string& auth)
448 } // of namespace HTTP
450 } // of namespace simgear