Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96dc9a1

Browse files
committed
http, bugfix: body will be lost when connection header of response is close.
1 parent 6db5998 commit 96dc9a1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

fibjs/src/http/HttpMessage.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ result_t HttpMessage::readFrom(Stream_base* stm, AsyncEvent* ac)
153153
: AsyncState(ac)
154154
, m_pThis(pThis)
155155
, m_stm(stm)
156-
, m_contentLength(0)
156+
, m_contentLength(-1)
157157
, m_bChunked(false)
158158
, m_headCount(0)
159159
{
@@ -173,7 +173,7 @@ result_t HttpMessage::readFrom(Stream_base* stm, AsyncEvent* ac)
173173

174174
if ((m_contentLength < 0)
175175
|| (m_pThis->m_maxBodySize >= 0
176-
&& m_contentLength > m_pThis->m_maxBodySize * 1024 * 1024))
176+
&& m_contentLength > m_pThis->m_maxBodySize * 1024 * 1024))
177177
return CHECK_ERROR(Runtime::setError("HttpMessage: body is too huge."));
178178
} else if (!qstricmp(m_strLine.c_str(),
179179
"transfer-encoding:", 18)) {
@@ -202,14 +202,15 @@ result_t HttpMessage::readFrom(Stream_base* stm, AsyncEvent* ac)
202202
if (m_pThis->m_maxBodySize == 0)
203203
return next();
204204

205-
if (m_contentLength)
205+
if (m_contentLength > 0)
206206
return CHECK_ERROR(CALL_E_INVALID_DATA);
207+
m_contentLength = 0;
207208

208209
m_pThis->get_body(m_body);
209210
return next(chunk_head);
210211
}
211212

212-
if (!m_pThis->m_bNoBody && m_contentLength > 0) {
213+
if (!m_pThis->m_bNoBody && (m_contentLength > 0 || (m_pThis->m_bResponse && !m_pThis->m_keepAlive && m_contentLength == -1))) {
213214
m_pThis->get_body(m_body);
214215
return m_stm->copyTo(m_body, m_contentLength, m_copySize, next(body));
215216
}
@@ -219,7 +220,7 @@ result_t HttpMessage::readFrom(Stream_base* stm, AsyncEvent* ac)
219220

220221
ON_STATE(asyncReadFrom, body)
221222
{
222-
if (!m_pThis->m_bNoBody && m_contentLength != m_copySize)
223+
if (!m_pThis->m_bNoBody && m_contentLength > 0 && m_contentLength != m_copySize)
223224
return CHECK_ERROR(Runtime::setError("HttpMessage: body is not complete."));
224225

225226
m_body->rewind();

test/http_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,24 @@ describe("http", () => {
510510
assert.equal(r.statusCode, 200);
511511
assert.equal(r.statusMessage, "ok");
512512
assert.equal(r.protocol, 'HTTP/1.0');
513+
514+
var r = get_response("HTTP/1.0 200 ok\r\n\r\n123456");
515+
assert.equal(r.statusCode, 200);
516+
assert.equal(r.statusMessage, "ok");
517+
assert.equal(r.protocol, 'HTTP/1.0');
518+
assert.equal('123456', r.body.read());
519+
520+
var r = get_response("HTTP/1.1 200 ok\r\n\r\n123456");
521+
assert.equal(r.statusCode, 200);
522+
assert.equal(r.statusMessage, "ok");
523+
assert.equal(r.protocol, 'HTTP/1.1');
524+
assert.isNull(r.body.read());
525+
526+
var r = get_response("HTTP/1.1 200 ok\r\nconnection: close\r\n\r\n123456");
527+
assert.equal(r.statusCode, 200);
528+
assert.equal(r.statusMessage, "ok");
529+
assert.equal(r.protocol, 'HTTP/1.1');
530+
assert.equal('123456', r.body.read());
513531
});
514532

515533
it("keep-alive", () => {

0 commit comments

Comments
 (0)