Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
Diff: MbedClient.cpp
- Revision:
- 11:e1bee9a77652
- Parent:
- 8:3a4dba260b71
- Child:
- 12:6634f9814235
diff -r 97077cfa13fc -r e1bee9a77652 MbedClient.cpp --- a/MbedClient.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/MbedClient.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -47,6 +47,7 @@ _port(port), _tries(tries), _state(STATE_INIT), + _isStreamRequest(false), _filter(_source), _source(_sock), _sink(_sock), @@ -71,36 +72,36 @@ uint8_t MbedClient::beginRequest() { - uint8_t tries; - if (_state != STATE_INIT) return internalError(); - tries = _tries; - do { - MBCL_DBG("Connecting to %s:%u", _host, _port); - if (_sock.connect(_host, _port) >= 0) - break; - _sock.close(); - MBCL_DBG("Connection atempt failed."); - } while (--tries > 0); + MBCL_DBG("Beginning SmartREST request."); + _source.setTimeout(60000); + if (!connect()) + return connectionError(); - if (tries == 0) + if (!sendRequestHeader("/s")) return connectionError(); - MBCL_DBG("Sending request header."); + _state = STATE_IN_REQUEST; + return CLIENT_OK; +} - if ((!send("POST /s HTTP/1.0\r\n")) || - (!send("Host: ")) || - (!send(_host)) || - (!send("\r\n"))) +uint8_t MbedClient::beginStream(const char *uri) +{ + if (_state != STATE_INIT) + return internalError(); + + // set stream request flag to later set the timeout right + _isStreamRequest = true; + + MBCL_DBG("Beginning SmartREST request."); + _source.setTimeout(60000); + if (!connect()) return connectionError(); - - if ((_username != NULL) && (strlen(_username) > 0) && - (_password != NULL) && (strlen(_password) > 0)) { - if (!sendBasicAuth()) - return connectionError(); - } + + if (!sendRequestHeader(uri)) + return connectionError(); _state = STATE_IN_REQUEST; return CLIENT_OK; @@ -180,6 +181,10 @@ if ((status != 200) || (!_filter.skipHeaders())) return connectionError(); + + // set timeout to fifteen minutes if stream request flag set + if (_isStreamRequest) + _source.setTimeout(900000); _state = STATE_RECVD_RESPONSE; return CLIENT_OK; @@ -193,6 +198,7 @@ void MbedClient::stop() { MBCL_DBG("Resetting client."); + _isStreamRequest = false; _sock.close(); _source.reset(); _sink.reset(); @@ -200,16 +206,51 @@ _state = STATE_INIT; } +bool MbedClient::connect() +{ + uint8_t tries; + + tries = _tries; + do { + MBCL_DBG("Connecting to %s:%u", _host, _port); + if (_sock.connect(_host, _port) >= 0) + break; + _sock.close(); + MBCL_DBG("Connection atempt failed."); + } while (--tries > 0); + + return (tries > 0); +} + bool MbedClient::send(const char *str) { return (_sink.write(str) == strlen(str)); } +bool MbedClient::sendRequestHeader(const char *uri) +{ + MBCL_DBG("Sending request header."); + if ((!send("POST ")) || + (!send(uri)) || + (!send("HTTP/1.0\r\n")) || + (!send("Host: ")) || + (!send(_host)) || + (!send("\r\n"))) + return false; + + return sendBasicAuth(); +} + bool MbedClient::sendBasicAuth() { size_t ul, pl; unsigned char input[3]; unsigned char output[5]; int inputOffset = 0; + // no need to send authorization if not specified + if ((_username == NULL) || (strlen(_username) == 0) || + (_password == NULL) || (strlen(_password) == 0)) + return true; + if (!send("Authorization: Basic ")) return false;