HTTPClient read until EOF fixes

This commit is contained in:
Fabio Alessandrelli 2019-02-20 03:06:02 +01:00
parent aa5b99821b
commit 10e4821260

View File

@ -504,6 +504,7 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
ERR_FAIL_COND_V(status != STATUS_BODY, PoolByteArray());
PoolByteArray ret;
Error err = OK;
if (chunked) {
@ -524,7 +525,7 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
if (chunk.size() > 32) {
ERR_PRINT("HTTP Invalid chunk hex len");
status = STATUS_CONNECTION_ERROR;
return PoolByteArray();
break;
}
if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') {
@ -542,14 +543,14 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
else {
ERR_PRINT("HTTP Chunk len not in hex!!");
status = STATUS_CONNECTION_ERROR;
return PoolByteArray();
break;
}
len <<= 4;
len |= v;
if (len > (1 << 24)) {
ERR_PRINT("HTTP Chunk too big!! >16mb");
status = STATUS_CONNECTION_ERROR;
return PoolByteArray();
break;
}
}
@ -557,7 +558,7 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
// End reached!
status = STATUS_CONNECTED;
chunk.clear();
return PoolByteArray();
break;
}
chunk_left = len + 2;
@ -577,18 +578,13 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') {
ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
status = STATUS_CONNECTION_ERROR;
return PoolByteArray();
break;
}
PoolByteArray ret;
ret.resize(chunk.size() - 2);
{
PoolByteArray::Write w = ret.write();
copymem(w.ptr(), chunk.ptr(), chunk.size() - 2);
}
PoolByteArray::Write w = ret.write();
copymem(w.ptr(), chunk.ptr(), chunk.size() - 2);
chunk.clear();
return ret;
}
break;
@ -598,45 +594,26 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
} else {
int to_read = !read_until_eof ? MIN(body_left, read_chunk_size) : read_chunk_size;
PoolByteArray ret;
ret.resize(to_read);
int _offset = 0;
while (read_until_eof || to_read > 0) {
while (to_read > 0) {
int rec = 0;
{
PoolByteArray::Write w = ret.write();
err = _get_http_data(w.ptr() + _offset, to_read, rec);
}
if (rec < 0) {
if (to_read > 0) // Ended up reading less
ret.resize(_offset);
if (rec <= 0) { // Ended up reading less
ret.resize(_offset);
break;
} else {
_offset += rec;
to_read -= rec;
if (!read_until_eof) {
body_left -= rec;
to_read -= rec;
} else {
if (rec < to_read) {
ret.resize(_offset);
err = ERR_FILE_EOF;
break;
}
ret.resize(_offset + to_read);
}
}
}
if (!read_until_eof) {
if (body_left == 0) {
status = STATUS_CONNECTED;
}
return ret;
} else {
if (err == ERR_FILE_EOF) {
err = OK; // EOF is expected here
close();
return ret;
}
if (err != OK)
break;
}
}
@ -651,12 +628,12 @@ PoolByteArray HTTPClient::read_response_body_chunk() {
status = STATUS_CONNECTION_ERROR;
}
} else if (body_left == 0 && !chunked) {
} else if (body_left == 0 && !chunked && !read_until_eof) {
status = STATUS_CONNECTED;
}
return PoolByteArray();
return ret;
}
HTTPClient::Status HTTPClient::get_status() const {