A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Revision:
14:dc3f8dd5c02b
Child:
15:0ce90c525e7a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPResponseFilter.cpp	Mon Apr 14 11:23:50 2014 +0000
@@ -0,0 +1,110 @@
+#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;
+}