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
Url repository should respect repo.path for file urls #11687
Merged
imotov
merged 1 commit into
elastic:master
from
imotov:url-repository-should-use-repo-path
Jul 14, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
98 changes: 98 additions & 0 deletions
98
core/src/main/java/org/elasticsearch/common/util/URIPattern.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,98 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.util; | ||
|
||
import org.elasticsearch.common.regex.Regex; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* URI Pattern matcher | ||
* | ||
* The pattern is URI in which authority, path, query and fragment can be replace with simple pattern. | ||
* | ||
* For example: foobar://*.local/some_path/*?*#* will match all uris with schema foobar in local domain | ||
* with any port, with path that starts some_path and with any query and fragment. | ||
*/ | ||
public class URIPattern { | ||
private final URI uriPattern; | ||
|
||
/** | ||
* Constructs uri pattern | ||
* @param pattern | ||
*/ | ||
public URIPattern(String pattern) { | ||
try { | ||
uriPattern = new URI(pattern); | ||
} catch (URISyntaxException ex) { | ||
throw new IllegalArgumentException("cannot parse URI pattern [" + pattern + "]"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the given uri matches the pattern | ||
*/ | ||
public boolean match(URI uri) { | ||
return matchNormalized(uri.normalize()); | ||
} | ||
|
||
public static boolean match(URIPattern[] patterns, URI uri) { | ||
URI normalized = uri.normalize(); | ||
for (URIPattern pattern : patterns) { | ||
if (pattern.matchNormalized(normalized)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean matchNormalized(URI uri) { | ||
if(uriPattern.isOpaque()) { | ||
// This url only has scheme, scheme-specific part and fragment | ||
return uri.isOpaque() && | ||
match(uriPattern.getScheme(), uri.getScheme()) && | ||
match(uriPattern.getSchemeSpecificPart(), uri.getSchemeSpecificPart()) && | ||
match(uriPattern.getFragment(), uri.getFragment()); | ||
|
||
} else { | ||
return match(uriPattern.getScheme(), uri.getScheme()) && | ||
match(uriPattern.getAuthority(), uri.getAuthority()) && | ||
match(uriPattern.getQuery(), uri.getQuery()) && | ||
match(uriPattern.getPath(), uri.getPath()) && | ||
match(uriPattern.getFragment(), uri.getFragment()); | ||
} | ||
} | ||
|
||
private boolean match(String pattern, String value) { | ||
if (value == null) { | ||
// If the pattern is empty or matches anything - it's a match | ||
if (pattern == null || Regex.isMatchAllPattern(pattern)) { | ||
return true; | ||
} | ||
} | ||
return Regex.simpleMatch(pattern, value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return uriPattern.toString(); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
core/src/test/java/org/elasticsearch/common/util/URIPatternTests.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,52 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.common.util; | ||
|
||
import org.elasticsearch.test.ElasticsearchTestCase; | ||
import org.junit.Test; | ||
|
||
import java.net.URI; | ||
|
||
public class URIPatternTests extends ElasticsearchTestCase { | ||
|
||
@Test | ||
public void testURIPattern() throws Exception { | ||
assertTrue(new URIPattern("http://test.local/").match(new URI("http://test.local/"))); | ||
assertFalse(new URIPattern("http://test.local/somepath").match(new URI("http://test.local/"))); | ||
assertTrue(new URIPattern("http://test.local/somepath").match(new URI("http://test.local/somepath"))); | ||
assertFalse(new URIPattern("http://test.local/somepath").match(new URI("http://test.local/somepath/more"))); | ||
assertTrue(new URIPattern("http://test.local/somepath/*").match(new URI("http://test.local/somepath/more"))); | ||
assertTrue(new URIPattern("http://test.local/somepath/*").match(new URI("http://test.local/somepath/more/andmore"))); | ||
assertTrue(new URIPattern("http://test.local/somepath/*").match(new URI("http://test.local/somepath/more/andmore/../bitmore"))); | ||
assertFalse(new URIPattern("http://test.local/somepath/*").match(new URI("http://test.local/somepath/../more"))); | ||
assertFalse(new URIPattern("http://test.local/somepath/*").match(new URI("http://test.local/"))); | ||
assertFalse(new URIPattern("http://test.local/somepath/*").match(new URI("https://test.local/somepath/more"))); | ||
assertFalse(new URIPattern("http://test.local:1234/somepath/*").match(new URI("http://test.local/somepath/more"))); | ||
assertFalse(new URIPattern("http://test.local:1234/somepath/*").match(new URI("http://test.local/somepath/more"))); | ||
assertTrue(new URIPattern("http://test.local:1234/somepath/*").match(new URI("http://test.local:1234/somepath/more"))); | ||
assertTrue(new URIPattern("http://*.local:1234/somepath/*").match(new URI("http://foobar.local:1234/somepath/more"))); | ||
assertFalse(new URIPattern("http://*.local:1234/somepath/*").match(new URI("http://foobar.local:2345/somepath/more"))); | ||
assertTrue(new URIPattern("http://*.local:*/somepath/*").match(new URI("http://foobar.local:2345/somepath/more"))); | ||
assertFalse(new URIPattern("http://*.local:*/somepath/*").match(new URI("http://foobar.local:2345/somepath/more?par=val"))); | ||
assertTrue(new URIPattern("http://*.local:*/somepath/*?*").match(new URI("http://foobar.local:2345/somepath/more?par=val"))); | ||
assertFalse(new URIPattern("http://*.local:*/somepath/*?*").match(new URI("http://foobar.local:2345/somepath/more?par=val#frag"))); | ||
assertTrue(new URIPattern("http://*.local:*/somepath/*?*#*").match(new URI("http://foobar.local:2345/somepath/more?par=val#frag"))); | ||
assertTrue(new URIPattern("http://*.local/somepath/*?*#*").match(new URI("http://foobar.local/somepath/more"))); | ||
} | ||
} |
Oops, something went wrong.
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.
this looks good. i forgot how crazy URL was here with null versus empty strings for different pieces...