A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Revision:
15:0ce90c525e7a
Parent:
14:dc3f8dd5c02b
Child:
16:ee4f052c7570
--- a/HTTPResponseFilter.cpp	Mon Apr 14 11:23:50 2014 +0000
+++ b/HTTPResponseFilter.cpp	Tue Apr 15 16:57:53 2014 +0000
@@ -5,9 +5,11 @@
 #include <stdio.h>
 
 const char *cExpectedStatus = "HTTP/1.* ";
+const char *cContentLength = "Content-Length: ";
 
 HTTPResponseFilter::HTTPResponseFilter(AbstractDataSource& source) : _source(source), _state(RESPF_STATE_INIT)
 {
+    _length = _read = 0;
 }
 
 HTTPResponseFilter::~HTTPResponseFilter()
@@ -72,27 +74,47 @@
     if (_state != RESPF_STATE_READ_STATUS)
         return false;
 
-    while ((state < 3) && (((c = _source.read()) > 0) || ((status = _source.status()) == DS_STATUS_OK))) {
+    while ((state < 5) && (((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
+            if (offset == 0) {
+                if (cContentLength[0] == c)
                     state = 1;
+                if (c == '\r')
+                    state = 4;
             } else {
-                offset++;
+                if (c == '\r')
+                    state = 3;
             }
+            offset++;
             break;
         case 1:
+            if (offset == strlen(cContentLength)
+                state = 2;
+            else if (c == '\r')
+                state = 3;
+            else if (cContentLength[offset] != c)
+                state = 0;
+            offset++;
+            break;
+        case 2:
+            if (isdigit(c))
+                _length = (_length * 10) + (c - '0');
+            else if (c == '\r')
+                state = 3;
+            else
+                state = 0;
+            offset++;
+            break;
+        case 3:
             if (c == '\n') {
                 state = 0;
                 offset = 0;
             }
             break;
-        case 2:
+        case 4:
             if (c == '\n')
-                state = 3;
+                state = 5;
             break;
         }
     }
@@ -107,4 +129,5 @@
 void HTTPResponseFilter::reset()
 {
     _state = RESPF_STATE_INIT;
+    _length = _read = 0;
 }