-
Notifications
You must be signed in to change notification settings - Fork 13k
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 fs char #10101
Merged
Merged
Fix fs char #10101
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e36668f
Add PathEncoder for OS to encode if dataId, ns, group contains illega…
Daydreamer-ia 8f1b498
Merge remote-tracking branch 'origin/develop' into fix_fs_char
Daydreamer-ia b168ca6
Add copyright
Daydreamer-ia e1ef5b3
Fix checkstyle
Daydreamer-ia b731c0a
Fix pmd
Daydreamer-ia ef94aeb
fix PathEncoderManager as singleton
Daydreamer-ia c43baf5
fix ut
Daydreamer-ia aa0084d
fix checkstyle
Daydreamer-ia 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
58 changes: 58 additions & 0 deletions
58
common/src/main/java/com/alibaba/nacos/common/pathencoder/PathEncoder.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,58 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.common.pathencoder; | ||
|
||
/** | ||
* To encode path if illegal,an os may have a PathEncoder. | ||
* | ||
* @author daydreamer-ia | ||
*/ | ||
public interface PathEncoder { | ||
|
||
/** | ||
* encode path. | ||
* | ||
* @param str origin | ||
* @param charset charset | ||
* @return new path | ||
*/ | ||
String encode(String str, String charset); | ||
|
||
/** | ||
* decode path. | ||
* | ||
* @param str new path | ||
* @param charset charset | ||
* @return origin path | ||
*/ | ||
String decode(String str, String charset); | ||
|
||
/** | ||
* return simple lowercase os name. | ||
* | ||
* @return simple lowercase os name | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* whether to encode. | ||
* | ||
* @param key key | ||
* @return whether to encode. | ||
*/ | ||
boolean needEncode(String key); | ||
} |
119 changes: 119 additions & 0 deletions
119
common/src/main/java/com/alibaba/nacos/common/pathencoder/PathEncoderManager.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,119 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.common.pathencoder; | ||
|
||
import com.alibaba.nacos.common.spi.NacosServiceLoader; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.Collection; | ||
|
||
/** | ||
* To expose interface from {@link PathEncoder}. | ||
* | ||
* @author daydreamer-ia | ||
*/ | ||
public class PathEncoderManager { | ||
|
||
/** | ||
* singleton. | ||
*/ | ||
private static final PathEncoderManager INSTANCE = new PathEncoderManager(); | ||
|
||
/** | ||
* encoder. | ||
*/ | ||
private PathEncoder targetEncoder = null; | ||
|
||
private PathEncoderManager() { | ||
// load path encoder | ||
Collection<PathEncoder> load = NacosServiceLoader.load(PathEncoder.class); | ||
if (!load.isEmpty()) { | ||
String currentOs = System.getProperty("os.name").toLowerCase(); | ||
for (PathEncoder pathEncoder : load) { | ||
// match first | ||
if (currentOs.contains(pathEncoder.name())) { | ||
targetEncoder = pathEncoder; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* encode path if necessary. | ||
* | ||
* @param path origin path | ||
* @param charset charset of origin path | ||
* @return encoded path | ||
*/ | ||
public String encode(String path, String charset) { | ||
if (path == null || charset == null) { | ||
return path; | ||
} | ||
if (targetEncoder != null && targetEncoder.needEncode(path)) { | ||
return targetEncoder.encode(path, charset); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* encode path if necessary. | ||
* | ||
* @param path origin path | ||
* @return encoded path | ||
*/ | ||
public String encode(String path) { | ||
return encode(path, Charset.defaultCharset().name()); | ||
} | ||
|
||
/** | ||
* decode path. | ||
* | ||
* @param path encoded path | ||
* @param charset charset of encoded path | ||
* @return origin path | ||
*/ | ||
public String decode(String path, String charset) { | ||
if (path == null || charset == null) { | ||
return path; | ||
} | ||
if (targetEncoder != null) { | ||
return targetEncoder.decode(path, charset); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* decode path. | ||
* | ||
* @param path encoded path | ||
* @return origin path | ||
*/ | ||
public String decode(String path) { | ||
return decode(path, Charset.defaultCharset().name()); | ||
} | ||
|
||
/** | ||
* get singleton. | ||
* | ||
* @return singleton. | ||
*/ | ||
public static PathEncoderManager getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
common/src/main/java/com/alibaba/nacos/common/pathencoder/impl/WindowsEncoder.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,95 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.common.pathencoder.impl; | ||
|
||
import com.alibaba.nacos.common.pathencoder.PathEncoder; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Encode path if illegal char reach in Windows. | ||
* | ||
* @author daydreamer-ia | ||
*/ | ||
public class WindowsEncoder implements PathEncoder { | ||
|
||
/** | ||
* 不应该含有 / : ? " < > | \. | ||
*/ | ||
private static final String PATTERN_EXP = "[^/:*?\"<>|\\\\]+"; | ||
|
||
private static final Map<String, String> REG_MAPPING = new HashMap<>(); | ||
|
||
private static final Map<String, String> CHAR_MAPPING = new HashMap<>(); | ||
|
||
private static final Pattern PATTERN = Pattern.compile(PATTERN_EXP); | ||
|
||
static { | ||
// reg | ||
REG_MAPPING.put("\\\\", "%A1%"); | ||
REG_MAPPING.put("/", "%A2%"); | ||
REG_MAPPING.put(":", "%A3%"); | ||
REG_MAPPING.put("\\*", "%A4%"); | ||
REG_MAPPING.put("\\?", "%A5%"); | ||
REG_MAPPING.put("\"", "%A6%"); | ||
REG_MAPPING.put("<", "%A7%"); | ||
REG_MAPPING.put(">", "%A8%"); | ||
REG_MAPPING.put("\\|", "%A9%"); | ||
|
||
// char | ||
CHAR_MAPPING.put("%A1%", "\\\\"); | ||
CHAR_MAPPING.put("%A2%", "/"); | ||
CHAR_MAPPING.put("%A3%", ":"); | ||
CHAR_MAPPING.put("%A4%", "*"); | ||
CHAR_MAPPING.put("%A5%", "?"); | ||
CHAR_MAPPING.put("%A6%", "\""); | ||
CHAR_MAPPING.put("%A7%", "<"); | ||
CHAR_MAPPING.put("%A8%", ">"); | ||
CHAR_MAPPING.put("%A9%", "|"); | ||
} | ||
|
||
@Override | ||
public String encode(String str, String charset) { | ||
for (Map.Entry<String, String> entry : REG_MAPPING.entrySet()) { | ||
str = str.replaceAll(entry.getKey(), entry.getValue()); | ||
} | ||
return str; | ||
} | ||
|
||
@Override | ||
public String decode(String str, String charset) { | ||
for (Map.Entry<String, String> entry : CHAR_MAPPING.entrySet()) { | ||
str = str.replaceAll(entry.getKey(), entry.getValue()); | ||
} | ||
return str; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "window"; | ||
} | ||
|
||
@Override | ||
public boolean needEncode(String key) { | ||
if (key == null) { | ||
key = ""; | ||
} | ||
return !PATTERN.matcher(key).matches(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/resources/META-INF/services/com.alibaba.nacos.common.pathencoder.PathEncoder
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,19 @@ | ||
# | ||
# | ||
# Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
# | ||
# 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. | ||
# | ||
# | ||
|
||
com.alibaba.nacos.common.pathencoder.impl.WindowsEncoder |
52 changes: 52 additions & 0 deletions
52
common/src/test/java/com/alibaba/nacos/common/pathencoder/PathEncoderManagerTest.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 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.common.pathencoder; | ||
|
||
import com.alibaba.nacos.common.pathencoder.impl.WindowsEncoder; | ||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class PathEncoderManagerTest extends TestCase { | ||
|
||
/** | ||
* test expose method. | ||
*/ | ||
public void test() throws Exception { | ||
// load static | ||
PathEncoderManager instance = PathEncoderManager.getInstance(); | ||
// remove windows impl | ||
Field targetEncoder = PathEncoderManager.class.getDeclaredField("targetEncoder"); | ||
targetEncoder.setAccessible(true); | ||
// remain old path encoder | ||
final Object origin = targetEncoder.get(instance); | ||
targetEncoder.set(instance, null); | ||
// try to encode, non windows | ||
String case1 = "aa||a"; | ||
Assert.assertEquals(PathEncoderManager.getInstance().encode(case1), case1); | ||
String case2 = "aa%A9%%A9%a"; | ||
Assert.assertEquals(PathEncoderManager.getInstance().decode(case2), case2); | ||
// try to encode if in windows | ||
targetEncoder.set(instance, new WindowsEncoder()); | ||
Assert.assertEquals(PathEncoderManager.getInstance().encode(case1), case2); | ||
Assert.assertEquals(PathEncoderManager.getInstance().decode(case2), case1); | ||
// set origin | ||
targetEncoder.set(instance, origin); | ||
} | ||
|
||
} |
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.
Manager换成单例实现会不会比较好。
全静态方法感觉是个
Utils
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.
done