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

Introduce rawPath in RequestTarget and ServiceRequestContext #5932

Merged
merged 3 commits into from
Nov 11, 2024

Conversation

yzfeng2020
Copy link
Contributor

@yzfeng2020 yzfeng2020 commented Oct 2, 2024

Motivation:
It may be useful if we have access to the original request path unmodified. For example we have use cases where the decoding rules are different from the ones at decodedPath.

Modifications:

  • Added rawPath method in RequestTarget to store the rawPath.
  • The rawPath is from the unmodified ':path' header at the server side; the client side target, it's always null.
  • Added rawPath method in ServiceRequestContext, it's always non null for server-side targets.

Result:

@yzfeng2020 yzfeng2020 changed the title add rawPath to RequestTarget Introduce rawPath in RequestTarget Oct 2, 2024
@yzfeng2020
Copy link
Contributor Author

hoping to get some feedbacks!

@yzfeng2020 yzfeng2020 marked this pull request as ready for review October 2, 2024 09:26
@yzfeng2020
Copy link
Contributor Author

@ikhoon I hope to get some feedbacks thanks!

@ikhoon ikhoon added this to the 1.31.0 milestone Oct 8, 2024
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks nice.

@ikhoon
Copy link
Contributor

ikhoon commented Oct 8, 2024

Please address the line failure.

Error: eckstyle] [ERROR] /home/runner/work/armeria/armeria/core/src/test/java/com/linecorp/armeria/internal/common/DefaultRequestTargetTest.java:660:16: Variable 'pathInTarget' should be declared final. [FinalLocalVariable]

@yzfeng2020 yzfeng2020 force-pushed the add-raw-path branch 4 times, most recently from 9d460f4 to 5a3bfcf Compare October 22, 2024 04:37
/**
* Returns the server-side raw path of this {@link RequestTarget} from the ":path" header.
* Unlike {@link #path()}, the returned string is the original path without any normalization.
* For client-side target it always returns {@code null}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would returning a non-null make sense on the client-side? By doing so, this method could be always non-null, which removes the need for null check on the server side as well (i.e. no complaints from NullAway and other linters)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that as well, it's not very clear to me what's the semantic, because we need to consider the case for absolute or not, and its transformation along the lines.

Another idea I have is to expose it only in ServiceRequestContext, and keep the one in RequestTarget internal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing it only in ServiceRequestContext sounds good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

took a closer look, b/c DefaultRequestTarget and DefaultServiceRequestContext don't live within the same package, it doesn't seems to be very easy, I might be missing something though

Copy link
Member

@trustin trustin Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep RequestTarget.rawPath() public and just access it from DefaultServiceRequestContext, asserting it is non-null before returning it? i.e.

public interface ServiceRequestContext {
  ...
  // Note no @Nullable
  String rawPath();
}

public class DefaultServiceRequestContext {
  ...
  @Override
  String rawPath() {
    final String rawPath = requestTarget().rawPath();
    assert rawPath != null;
    return rawPath;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should work! (I was thinking about keeping rawPath package private but java doesn't seem to work as scala), thanks for you idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

TEST_URLS.add("/%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29*%2B%2C%3B%3D");
}

@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use @ParameterizedTest with @CsvSource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg, will update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

@yzfeng2020 yzfeng2020 changed the title Introduce rawPath in RequestTarget Introduce rawPath in RequestTarget and ServiceRequestContext Oct 23, 2024
assertThat(res.requestTarget.path()).isEqualTo(expectedPath);
assertThat(res.requestTarget.query()).isEqualTo(expectedQuery);
assertThat(res.requestTarget.fragment()).isEqualTo(expectedFragment);
assertThat(res.requestTarget.rawPath()).isEqualTo(res.rawPath);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check rawPath for all test cases.

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@yzfeng2020
Copy link
Contributor Author

@jrhee17 @ikhoon @trustin if you could take a look, thanks a lot!

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @yzfeng2020! 👍

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for a late review. Looks great! Thanks, @yzfeng2020 🙇

@trustin trustin merged commit 95ed9e1 into line:main Nov 11, 2024
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce rawPath in RequestTarget
5 participants