Skip to content

Commit

Permalink
fixes networknt#753 handle text/plain and content type missing in Bod…
Browse files Browse the repository at this point in the history
…yHandler (networknt#755)
  • Loading branch information
stevehu committed Aug 25, 2020
1 parent f45b529 commit d1828e3
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions body/src/main/java/com/networknt/body/BodyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public BodyHandler() {
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// parse the body to map or list if content type is application/json
String contentType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE);
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
exchange.startBlocking();
if (contentType != null) {
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
exchange.startBlocking();
try {
if (contentType.startsWith("application/json")) {
InputStream inputStream = exchange.getInputStream();
Expand All @@ -103,6 +103,10 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
}
// attach the parsed request body into exchange if the body parser is enabled
attachJsonBody(exchange, unparsedRequestBody);
} else if (contentType.startsWith("text/plain")) {
InputStream inputStream = exchange.getInputStream();
String unparsedRequestBody = StringUtils.inputStreamToString(inputStream, StandardCharsets.UTF_8);
exchange.putAttachment(REQUEST_BODY, unparsedRequestBody);
} else if (contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded")) {
// attach the parsed request body into exchange if the body parser is enabled
attachFormDataBody(exchange);
Expand All @@ -115,6 +119,10 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
setExchangeStatus(exchange, CONTENT_TYPE_MISMATCH, contentType);
return;
}
} else {
// attach the stream to the exchange if the content type is missing.
InputStream inputStream = exchange.getInputStream();
exchange.putAttachment(REQUEST_BODY, inputStream);
}
Handler.next(exchange, next);
}
Expand Down

0 comments on commit d1828e3

Please sign in to comment.