GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Revision 12:daddfc44a0f5, committed 2015-01-13
- Comitter:
- Trumple
- Date:
- Tue Jan 13 21:30:18 2015 +0000
- Parent:
- 10:13176e7bd4c8
- Child:
- 13:344f559bf5ec
- Commit message:
- Fix HTTP library to prevent hanging after making HTTP requests
Changed in this revision
| http.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
