Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isOriginForm and isAsteriskForm #12568

Merged
merged 2 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,6 +31,7 @@
import io.netty.util.internal.UnstableApi;

import static io.netty.util.internal.StringUtil.COMMA;
import static io.netty.util.internal.StringUtil.isNullOrEmpty;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;

/**
Expand All @@ -49,19 +50,15 @@ private HttpUtil() { }
* <a href="https://tools.ietf.org/html/rfc7230#section-5.3">rfc7230, 5.3</a>.
*/
public static boolean isOriginForm(URI uri) {
return uri.getScheme() == null && uri.getSchemeSpecificPart() == null &&
uri.getHost() == null && uri.getAuthority() == null;
return isNullOrEmpty(uri.getScheme()) && isNullOrEmpty(uri.getAuthority()) && uri.toString().startsWith("/");
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Determine if a uri is in asterisk-form according to
* <a href="https://tools.ietf.org/html/rfc7230#section-5.3">rfc7230, 5.3</a>.
*/
public static boolean isAsteriskForm(URI uri) {
return "*".equals(uri.getPath()) &&
uri.getScheme() == null && uri.getSchemeSpecificPart() == null &&
uri.getHost() == null && uri.getAuthority() == null && uri.getQuery() == null &&
uri.getFragment() == null;
return "*".equals(uri.toString());
}

/**
Expand Down
Expand Up @@ -22,6 +22,7 @@

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,6 +39,29 @@

public class HttpUtilTest {

@Test
public void testRecognizesOriginForm() {
// Origin form: https://tools.ietf.org/html/rfc7230#section-5.3.1
assertTrue(HttpUtil.isOriginForm(URI.create("/where?q=now")));
// Absolute form: https://tools.ietf.org/html/rfc7230#section-5.3.2
assertFalse(HttpUtil.isOriginForm(URI.create("http://www.example.org/pub/WWW/TheProject.html")));
// Authority form: https://tools.ietf.org/html/rfc7230#section-5.3.3
assertFalse(HttpUtil.isOriginForm(URI.create("www.example.com:80")));
// Asterisk form: https://tools.ietf.org/html/rfc7230#section-5.3.4
assertFalse(HttpUtil.isOriginForm(URI.create("*")));
}

@Test public void testRecognizesAsteriskForm() {
// Asterisk form: https://tools.ietf.org/html/rfc7230#section-5.3.4
assertTrue(HttpUtil.isAsteriskForm(URI.create("*")));
// Origin form: https://tools.ietf.org/html/rfc7230#section-5.3.1
assertFalse(HttpUtil.isAsteriskForm(URI.create("/where?q=now")));
// Absolute form: https://tools.ietf.org/html/rfc7230#section-5.3.2
assertFalse(HttpUtil.isAsteriskForm(URI.create("http://www.example.org/pub/WWW/TheProject.html")));
// Authority form: https://tools.ietf.org/html/rfc7230#section-5.3.3
assertFalse(HttpUtil.isAsteriskForm(URI.create("www.example.com:80")));
}

@Test
public void testRemoveTransferEncodingIgnoreCase() {
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
Expand Down