Skip to content

Commit

Permalink
Add a better message error on RoutingContextImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Cruz committed Nov 14, 2024
1 parent 2a661ad commit e32cd4b
Showing 1 changed file with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
import io.vertx.ext.web.handler.impl.UserHolder;

import java.nio.charset.Charset;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -77,35 +81,44 @@ public RoutingContextImpl(String mountPoint, RouterImpl router, HttpServerReques
void route() {
String path = request.path();
HostAndPort authority = request.authority();
if ((authority == null && request.version() != HttpVersion.HTTP_1_0) || path == null || path.isEmpty()) {
// Authority must be present (HTTP/1.x host header // HTTP/2 :authority pseudo header)
// HTTP paths must start with a '/'
fail(400);
} else if (path.charAt(0) != '/') {
// For compatiblity we return `Not Found` when a path does not start with `/`
if (authority == null && request.version() != HttpVersion.HTTP_1_0) {
String message = HttpVersion.HTTP_1_1 == request.version() ?
"For HTTP/1.x requests, the 'Host' header is required" :
"For HTTP/2 requests, the ':authority' pseudo-header is required";
fail(400, new VertxException(message));
return;
}

if (path == null || path.isEmpty()) {
fail(400, new VertxException("The request path must start with '/' and cannot be empty"));
return;
}

if (path.charAt(0) != '/') {
// For compatibility, we return `Not Found` when a path does not start with `/`
fail(404);
} else {
next();
}
}

private String ensureNotNull(String string){
private String ensureNotNull(String string) {
return string == null ? "" : string;
}

private void fillParsedHeaders(HttpServerRequest request) {
String accept = request.getHeader(HttpHeaders.ACCEPT);
String acceptCharset = request.getHeader (HttpHeaders.ACCEPT_CHARSET);
String acceptCharset = request.getHeader(HttpHeaders.ACCEPT_CHARSET);
String acceptEncoding = request.getHeader(HttpHeaders.ACCEPT_ENCODING);
String acceptLanguage = request.getHeader(HttpHeaders.ACCEPT_LANGUAGE);
String contentType = ensureNotNull(request.getHeader(HttpHeaders.CONTENT_TYPE));

parsedHeaders = new ParsableHeaderValuesContainer(
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(accept, ParsableMIMEValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptCharset, ParsableHeaderValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptEncoding, ParsableHeaderValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptLanguage, ParsableLanguageValue::new)),
new ParsableMIMEValue(contentType)
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(accept, ParsableMIMEValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptCharset, ParsableHeaderValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptEncoding, ParsableHeaderValue::new)),
HeaderParser.sort(HeaderParser.convertToParsedHeaderValues(acceptLanguage, ParsableLanguageValue::new)),
new ParsableMIMEValue(contentType)
);
}

Expand Down Expand Up @@ -315,7 +328,7 @@ public Session session() {
}

@Override
public boolean isSessionAccessed(){
public boolean isSessionAccessed() {
return isSessionAccessed;
}

Expand Down

0 comments on commit e32cd4b

Please sign in to comment.