A client for the SmartREST protocol from Cumulocity.
Fork of MbedSmartRest by
HTTPResponseFilter.cpp
- Committer:
- vwochnik
- Date:
- 2014-04-14
- Revision:
- 14:dc3f8dd5c02b
- Child:
- 15:0ce90c525e7a
File content as of revision 14:dc3f8dd5c02b:
#include "HTTPResponseFilter.h" #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdio.h> const char *cExpectedStatus = "HTTP/1.* "; HTTPResponseFilter::HTTPResponseFilter(AbstractDataSource& source) : _source(source), _state(RESPF_STATE_INIT) { } HTTPResponseFilter::~HTTPResponseFilter() { } char HTTPResponseFilter::read() { if (_state != RESPF_STATE_READ_HEADERS) return 0; return _source.read(); } uint8_t HTTPResponseFilter::status() { if (_state != RESPF_STATE_READ_HEADERS) return DS_STATUS_ERROR; return _source.status(); } uint16_t HTTPResponseFilter::readStatus() { uint16_t res = 0; uint8_t state = 0; char c; size_t offset = 0; uint8_t status; if (_state != RESPF_STATE_INIT) return 0; while ((state < 3) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) { switch (state) { case 0: // read expected status line if ((cExpectedStatus[offset] != c) && (cExpectedStatus[offset] != '*')) state = 3; offset++; if (offset == strlen(cExpectedStatus)) { state = 1; } break; case 1: if (isspace(c)) state = 2; if (isdigit(c)) res = (res * 10) + (c - '0'); break; case 2: if (c == '\n') state = 3; break; } } if ((status != DS_STATUS_OK) || (state != 3)) return 0; _state = RESPF_STATE_READ_STATUS; return res; } bool HTTPResponseFilter::skipHeaders() { uint16_t res = 0; uint8_t state = 0; char c; size_t offset = 0; uint8_t status; if (_state != RESPF_STATE_READ_STATUS) return false; while ((state < 3) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) { switch (state) { case 0: // start of line if (c == '\r') { if (offset == 0) state = 2; else state = 1; } else { offset++; } break; case 1: if (c == '\n') { state = 0; offset = 0; } break; case 2: if (c == '\n') state = 3; break; } } if ((status != DS_STATUS_OK) || (state != 3)) return false; _state = RESPF_STATE_READ_HEADERS; return true; } void HTTPResponseFilter::reset() { _state = RESPF_STATE_INIT; }