GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Revision 13:344f559bf5ec, committed 2015-01-13
- Comitter:
- Trumple
- Date:
- Tue Jan 13 21:34:08 2015 +0000
- Parent:
- 11:190d07f37ec0
- Parent:
- 12:daddfc44a0f5
- Child:
- 14:bc0e2fa25f94
- Child:
- 15:1d98f45717bc
- Commit message:
- Merge master branch
Changed in this revision
--- a/http.cpp Sun Jan 11 10:25:19 2015 +0000
+++ b/http.cpp Tue Jan 13 21:34:08 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;
