Skip to content

Commit

Permalink
bugfix:fix the bug that middleware will return illegal http body when…
Browse files Browse the repository at this point in the history
… http body is empty (#737)

If the http body is empty. The httpBody function will return HTTP header content from index 3. This is unexpected.
Function caller also cannot tell whether the httpbody is empty or not.
  • Loading branch information
gavinshark authored Jun 22, 2020
1 parent 9a4eb03 commit f364a39
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ function deleteHttpHeader(payload, name) {
}

function httpBody(payload) {
return payload.slice(payload.indexOf("\r\n\r\n") + 4, payload.length);
let bodyIndex = payload.indexOf("\r\n\r\n");
if (-1 != bodyIndex){
return payload.slice(bodyIndex + 4, payload.length);
} else {
return null;
}
}

function setHttpBody(payload, newBody) {
Expand Down Expand Up @@ -710,6 +715,12 @@ function TEST_httpBody() {
if (body != "hello") {
fail(`'${body}' != 'hello'`)
}

const exampleInvalidPayload = "Invalid HTTP Response by Network issue";
let invalidBody = httpBody(Buffer.from(exampleInvalidPayload));
if (invalidBody != null) {
fail(`'${invalidBody}' != 'null'`)
}
}

function TEST_setHttpBody() {
Expand Down

0 comments on commit f364a39

Please sign in to comment.