-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #853 print an explicit error message when the URI is too lon…
…g (414)
- Loading branch information
1 parent
835daf2
commit 43f3dd1
Showing
7 changed files
with
214 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
server/src/main/java/io/kroki/server/error/ErrorContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.kroki.server.error; | ||
|
||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.ext.web.MIMEHeader; | ||
import io.vertx.ext.web.impl.ParsableMIMEValue; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ErrorContext { | ||
|
||
private final List<MIMEHeader> acceptableMimes; | ||
private final HttpServerRequest request; | ||
private final HttpServerResponse response; | ||
private final ErrorInfo errorInfo; | ||
private final String statusMessage; | ||
|
||
public ErrorContext(HttpServerRequest request, HttpServerResponse response, String statusMessage, ErrorInfo errorInfo) { | ||
this.acceptableMimes = request.headers().getAll("accept").stream().map(ParsableMIMEValue::new).collect(Collectors.toList()); | ||
this.request = request; | ||
this.response = response; | ||
// no new lines are allowed in the status message | ||
this.statusMessage = statusMessage.replaceAll("[\\r\\n]", " "); | ||
this.errorInfo = errorInfo; | ||
} | ||
|
||
public List<MIMEHeader> getAcceptableMimes() { | ||
return acceptableMimes; | ||
} | ||
|
||
public HttpServerRequest getRequest() { | ||
return request; | ||
} | ||
|
||
public HttpServerResponse getResponse() { | ||
return response; | ||
} | ||
|
||
public String getStatusMessage() { | ||
return statusMessage; | ||
} | ||
|
||
public ErrorInfo getErrorInfo() { | ||
return errorInfo; | ||
} | ||
|
||
|
||
public Throwable getFailure() { | ||
return errorInfo.getFailure(); | ||
} | ||
|
||
public int getErrorCode() { | ||
return errorInfo.getCode(); | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorInfo.getMessage(); | ||
} | ||
|
||
public String getHtmlErrorMessage() { | ||
return errorInfo.getHtmlMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.kroki.server.error; | ||
|
||
public class ErrorInfo { | ||
|
||
private final Throwable failure; | ||
private final int code; | ||
private final String message; | ||
private final String htmlMessage; | ||
|
||
public ErrorInfo(Throwable failure, int code, String message, String htmlMessage) { | ||
this.failure = failure; | ||
this.code = code; | ||
this.message = message; | ||
this.htmlMessage = htmlMessage; | ||
} | ||
|
||
public Throwable getFailure() { | ||
return failure; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public String getHtmlMessage() { | ||
if (htmlMessage == null) { | ||
return message; | ||
} | ||
return htmlMessage; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
server/src/main/java/io/kroki/server/error/InvalidRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.kroki.server.error; | ||
|
||
import io.netty.handler.codec.DecoderResult; | ||
import io.netty.handler.codec.TooLongFrameException; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.http.HttpVersion; | ||
|
||
public class InvalidRequestHandler implements Handler<HttpServerRequest> { | ||
|
||
private final ErrorHandler errorHandler; | ||
private final int maxInitialLineLength; | ||
|
||
public InvalidRequestHandler(ErrorHandler errorHandler, int maxInitialLineLength) { | ||
this.errorHandler = errorHandler; | ||
this.maxInitialLineLength = maxInitialLineLength; | ||
} | ||
|
||
/** | ||
* Copied and adapted from {@link HttpServerRequest#DEFAULT_INVALID_REQUEST_HANDLER} to print an explicit error message when the URL is too long. | ||
*/ | ||
@Override | ||
public void handle(HttpServerRequest httpServerRequest) { | ||
DecoderResult result = httpServerRequest.decoderResult(); | ||
Throwable cause = result.cause(); | ||
HttpResponseStatus status = null; | ||
if (cause instanceof TooLongFrameException) { | ||
HttpServerResponse response = httpServerRequest.response(); | ||
String causeMsg = cause.getMessage(); | ||
if (causeMsg.startsWith("An HTTP line is larger than")) { | ||
HttpResponseStatus responseStatus = HttpResponseStatus.REQUEST_URI_TOO_LONG; | ||
this.errorHandler.handleError(new ErrorContext( | ||
httpServerRequest, | ||
response, | ||
responseStatus.reasonPhrase(), | ||
new ErrorInfo( | ||
cause, | ||
responseStatus.code(), | ||
"The request URI's length exceeds " + maxInitialLineLength + ". You can update this value by setting KROKI_MAX_URI_LENGTH environment variable. Please read: https://docs.kroki.io/kroki/setup/configuration/#_max_uri_length for more information.", | ||
"" | ||
) | ||
)); | ||
return; | ||
} else if (causeMsg.startsWith("HTTP header is larger than")) { | ||
status = HttpResponseStatus.REQUEST_HEADER_FIELDS_TOO_LARGE; | ||
} | ||
response.setStatusMessage(causeMsg.replaceAll("[\\r\\n]", " ")); | ||
} | ||
if (status == null && HttpMethod.GET == httpServerRequest.method() && | ||
HttpVersion.HTTP_1_0 == httpServerRequest.version() && "/bad-request".equals(httpServerRequest.uri())) { | ||
// Matches Netty's specific HttpRequest for invalid messages | ||
status = HttpResponseStatus.BAD_REQUEST; | ||
} | ||
if (status != null) { | ||
httpServerRequest.response().setStatusCode(status.code()).end(); | ||
} else { | ||
httpServerRequest.connection().close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.