-
Notifications
You must be signed in to change notification settings - Fork 79
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 URL parsing issue (#391) #393
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
44 changes: 44 additions & 0 deletions
44
components/server/src/main/java/com/hotels/styx/server/netty/codec/UrlDecoder.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,44 @@ | ||
/* | ||
Copyright (C) 2013-2019 Expedia Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.hotels.styx.server.netty.codec; | ||
|
||
import com.hotels.styx.api.Url; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
|
||
import java.net.URI; | ||
|
||
import static com.hotels.styx.api.HttpHeaderNames.HOST; | ||
import static com.hotels.styx.api.Url.Builder.url; | ||
|
||
final class UrlDecoder { | ||
private UrlDecoder() { | ||
} | ||
|
||
static Url decodeUrl(UnwiseCharsEncoder unwiseCharEncoder, HttpRequest request) { | ||
String host = request.headers().get(HOST); | ||
|
||
if (request.uri().startsWith("/") && host != null) { | ||
URI uri = URI.create("http://" + host + unwiseCharEncoder.encode(request.uri())); | ||
return new Url.Builder() | ||
.path(uri.getRawPath()) | ||
.rawQuery(uri.getRawQuery()) | ||
.fragment(uri.getFragment()) | ||
.build(); | ||
} else { | ||
return url(unwiseCharEncoder.encode(request.uri())).build(); | ||
} | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
components/server/src/test/java/com/hotels/styx/server/netty/codec/UrlDecoderTest.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,102 @@ | ||
/* | ||
Copyright (C) 2013-2019 Expedia Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package com.hotels.styx.server.netty.codec; | ||
|
||
import com.hotels.styx.api.Url; | ||
import io.netty.handler.codec.http.DefaultFullHttpRequest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.netty.handler.codec.http.HttpHeaders.Names.HOST; | ||
import static io.netty.handler.codec.http.HttpMethod.GET; | ||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class UrlDecoderTest { | ||
@Test | ||
public void decodesOriginForm() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made some changes to the test files. Please have another look. |
||
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/foo"); | ||
request.headers().add(HOST, "example.com"); | ||
|
||
Url url = UrlDecoder.decodeUrl(x -> x, request); | ||
|
||
assertThat(url.authority(), is(Optional.empty())); | ||
assertThat(url.path(), is("/foo")); | ||
assertThat(url.encodedUri(), is("/foo")); | ||
assertThat(url.isAbsolute(), is(false)); | ||
assertThat(url.isRelative(), is(true)); | ||
assertThat(url.host(), is(Optional.empty())); | ||
assertThat(url.query(), is(Optional.empty())); | ||
assertThat(url.scheme(), is("")); | ||
} | ||
|
||
// From GitHub issue #391. | ||
@Test | ||
public void exposesPathComponentsWithDoubleSlashSeparators() { | ||
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "//www.abc.com//abc123Z"); | ||
request.headers().add(HOST, "example.com"); | ||
|
||
Url url = UrlDecoder.decodeUrl(x -> x, request); | ||
|
||
assertThat(url.authority(), is(Optional.empty())); | ||
assertThat(url.path(), is("//www.abc.com//abc123Z")); | ||
assertThat(url.encodedUri(), is("//www.abc.com//abc123Z")); | ||
assertThat(url.isAbsolute(), is(false)); | ||
assertThat(url.isRelative(), is(true)); | ||
assertThat(url.host(), is(Optional.empty())); | ||
assertThat(url.query(), is(Optional.empty())); | ||
assertThat(url.scheme(), is("")); | ||
} | ||
|
||
// Demonstrates that Host header handling is case insensitive. | ||
@Test | ||
public void decodesOriginFormWithUppercaseHostHeader() { | ||
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/foo"); | ||
request.headers().add("HOST", "example.com"); | ||
|
||
Url url = UrlDecoder.decodeUrl(x -> x, request); | ||
|
||
assertThat(url.authority(), is(Optional.empty())); | ||
assertThat(url.path(), is("/foo")); | ||
assertThat(url.encodedUri(), is("/foo")); | ||
assertThat(url.isAbsolute(), is(false)); | ||
assertThat(url.isRelative(), is(true)); | ||
assertThat(url.host(), is(Optional.empty())); | ||
assertThat(url.query(), is(Optional.empty())); | ||
assertThat(url.scheme(), is("")); | ||
} | ||
|
||
|
||
@Test | ||
public void decodesAbsoluteForm() { | ||
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "http://example.com/foo"); | ||
|
||
Url url = UrlDecoder.decodeUrl(x -> x, request); | ||
|
||
assertThat(url.authority().isPresent(), is(true)); | ||
assertThat(url.path(), is("/foo")); | ||
assertThat(url.encodedUri(), is("http://example.com/foo")); | ||
assertThat(url.isAbsolute(), is(true)); | ||
assertThat(url.isRelative(), is(false)); | ||
assertThat(url.host(), is(Optional.of("example.com"))); | ||
assertThat(url.query(), is(Optional.empty())); | ||
assertThat(url.scheme(), is("http")); | ||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this method used for? We have changed from it's an absolute path to i_t's an absolute URI_ . What's the real purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can only guess. I believe the original intention was to determine if the request is in origin-form or absolute-form. My gut feeling is that we don't really need this method in the URL class.
As to why this change? It is because every path component always starts with
/
therefore it always returned true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would at least mark it as deprecated then. I think from previous discussions our goal could be to completely remove Url from the API.