Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect socket handling for the HTTP client #5760

Merged
merged 1 commit into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/base/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ bool StreamReadContext::FillFromStream(const Stream::Ptr& stream, bool may_wait)
if (!Buffer)
throw std::bad_alloc();

if (stream->IsEof())
break;

size_t rc = stream->Read(Buffer + Size, 4096, true);

Size += rc;
Expand Down
39 changes: 24 additions & 15 deletions lib/remote/httpresponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,7 @@ bool HttpResponse::Parse(StreamReadContext& src, bool may_wait)

if (line == "") {
m_State = HttpResponseBody;

/* we're done if the request doesn't contain a message body */
if (!Headers->Contains("content-length") && !Headers->Contains("transfer-encoding"))
Complete = true;
else
m_Body = new FIFO();
m_Body = new FIFO();

return true;

Expand Down Expand Up @@ -204,27 +199,41 @@ bool HttpResponse::Parse(StreamReadContext& src, bool may_wait)
return true;
}
} else {
if (src.Eof)
bool hasLengthIndicator = false;
size_t lengthIndicator = 0;
Value contentLengthHeader;

if (Headers->Get("content-length", &contentLengthHeader)) {
hasLengthIndicator = true;
lengthIndicator = Convert::ToLong(contentLengthHeader);
}

if (hasLengthIndicator && src.Eof)
BOOST_THROW_EXCEPTION(std::invalid_argument("Unexpected EOF in HTTP body"));

if (src.MustRead) {
if (!src.FillFromStream(m_Stream, false)) {
if (!src.FillFromStream(m_Stream, may_wait))
src.Eof = true;
BOOST_THROW_EXCEPTION(std::invalid_argument("Unexpected EOF in HTTP body"));
}

src.MustRead = false;
}

size_t length_indicator = Convert::ToLong(Headers->Get("content-length"));
if (!hasLengthIndicator)
lengthIndicator = src.Size;

if (src.Size < length_indicator) {
if (src.Size < lengthIndicator) {
src.MustRead = true;
return false;
return may_wait;
}

m_Body->Write(src.Buffer, lengthIndicator);
src.DropData(lengthIndicator);

if (!hasLengthIndicator && !src.Eof) {
src.MustRead = true;
return may_wait;
}

m_Body->Write(src.Buffer, length_indicator);
src.DropData(length_indicator);
Complete = true;
return true;
}
Expand Down