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