A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Files at this revision

API Documentation at this revision

Comitter:
vwochnik
Date:
Tue Apr 15 16:57:53 2014 +0000
Parent:
14:dc3f8dd5c02b
Child:
16:ee4f052c7570
Commit message:
fix

Changed in this revision

HTTPResponseFilter.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPResponseFilter.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
 }
--- a/HTTPResponseFilter.h	Mon Apr 14 11:23:50 2014 +0000
+++ b/HTTPResponseFilter.h	Tue Apr 15 16:57:53 2014 +0000
@@ -30,6 +30,7 @@
 private:
     AbstractDataSource& _source;
     uint8_t _state;
+    size_t _length, _read;
 };
 
 #endif
\ No newline at end of file