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 spaces encoding inside DefaultUriBuilder #11439

Open
wants to merge 2 commits into
base: 4.8.x
Choose a base branch
from

Conversation

glorrian
Copy link
Contributor

@glorrian glorrian commented Dec 13, 2024

fix: #11434
I have implemented a hardcoded solution to address the current issue because I am concerned about completely rewriting the functionality of java.net.URLEncoder, but i think it should be own MK uri encoder like it implemented in Spring

@sdelamo sdelamo requested a review from yawkat December 13, 2024 13:51
@jamesdh
Copy link

jamesdh commented Dec 13, 2024

You might be better off just using Apache's URIBuilder rather than hardcoding a mini-workaround or completely reinventing the wheel. URIBuilder gives you the option to specify how to handle spaces, which could be made configurable in case someone prefers Micronauts existing behavior.

@glorrian
Copy link
Contributor Author

glorrian commented Dec 13, 2024

firstly, MK doesn't have a dependency on apache httpcore yet, and I'm not sure about its license to add this. also, I am not sure about the behavior of apache decoder in other tests and cases.

@glorrian
Copy link
Contributor Author

I would wait for the review, if the decision on the new encoder is approved, then I will be glad to implement your proposal

@jamesdh
Copy link

jamesdh commented Dec 13, 2024

Yea, I realized after commenting that micronaut-core doesn't directly have a dependency on anything apache, so that's probably not viable.

@@ -39,7 +39,8 @@ class ClientFormatSpec extends Specification {
given:
var cafe = new Cafe(name: "Pizza Garden", "address": "Home Street 2")
expect:
client.pipesFormattedObject(cafe) == "param=name|Pizza+Garden|address|Home+Street+2"
printf "client.pipesFormattedObject(cafe) = %s\n", client.pipesFormattedObject(cafe)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is unnecessary

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 missed it after debug, thanks ;)

@@ -415,6 +415,7 @@ private String expandOrEncode(String value, Map<String, ? super Object> values)
}

private String encode(String userInfo) {
return URLEncoder.encode(userInfo, StandardCharsets.UTF_8);
return URLEncoder.encode(userInfo, StandardCharsets.UTF_8)
.replaceAll("\\+", "%20"); // to match RFC3986
Copy link
Contributor

Choose a reason for hiding this comment

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

You can't just change the framework's behavior. I think the most reasonable solution is to add a setting - how to encode a space. Then you leave backward compatibility and whoever needs it will enable RFC support mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree with you Im gonna add it

Copy link

@jamesdh jamesdh Dec 24, 2024

Choose a reason for hiding this comment

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

It's not a good look for the framework to be handling this incorrectly when competing frameworks get it right. I'd argue it should be changed to the correct behavior, with an option to revert to the prior (incorrect) encoding behavior if needed. The Apache URIBuilder I mentioned in my earlier comment gets this right, defaulting to the correct encoding of %20 but providing an optional argument for encoding spaces as + if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yawkat need your final decision

Copy link
Member

Choose a reason for hiding this comment

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

Indeed we can't change it this way, at least not without a config option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

HttpClient incorrectly encodes query params containing spaces with + and not %20
4 participants