GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Revision:
12:daddfc44a0f5
Parent:
2:1cbb20dd1733
Child:
23:b57a47c7862a
diff -r 13176e7bd4c8 -r daddfc44a0f5 http.cpp
--- a/http.cpp	Tue Dec 23 21:28:40 2014 +0000
+++ b/http.cpp	Tue Jan 13 21:30:18 2015 +0000
@@ -89,13 +89,42 @@
     int receiveByteCount;
     string message;
     
+    bool headersSet = false;
+    int contentLength = -1;
+    string responseBody;
+    
+    string contentLengthNeedle = "Content-Length: ";
+    
     while (true)
     {
         receiveByteCount = sock.receive(buffer, sizeof(buffer)-1);//spare a byte for null termination byte
+        
+        //if the connection is closed by the remote client
         if (receiveByteCount <= 0)
             break;
+            
         buffer[receiveByteCount] = '\0';
         message += buffer;
+        
+        //if the response header has been received and includes the required headers
+        if (!headersSet && message.find("\r\n\r\n") != string::npos && message.find(contentLengthNeedle) != string::npos)
+        {
+            //headers have been fully received, ensure this check is not performed again
+            headersSet = true;
+            //end point of the "Content-Length: " string, beginning of the integer it specifies
+            int cl = message.find(contentLengthNeedle) + contentLengthNeedle.size();
+            //extract the content length from the header
+            string length = message.substr(cl, message.find_first_of("\r\n", cl) - cl);
+            contentLength = atoi(length.c_str());
+        }
+        
+        //if the headers have been set, extract the message body
+        if (headersSet)
+            responseBody = message.substr(message.find("\r\n\r\n"), contentLength);
+        
+        //if the response body is of the expected length, we're done
+        if (responseBody.size() >= contentLength)
+            break;
     }
     
     return message;