-
Notifications
You must be signed in to change notification settings - Fork 761
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
Add escape for management key #4363
Changes from all commits
7bb3f65
2d05699
4f4c3b2
5653309
0cfe168
5a813da
f7d2bff
1de96c6
2ef2e6b
be71026
56124a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// 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. | ||
|
||
use std::string::FromUtf8Error; | ||
|
||
/// Function that escapes special characters in a string. | ||
/// | ||
/// All characters except digit, alphabet and '_' are treated as special characters. | ||
/// A special character will be converted into "%num" where num is the hexadecimal form of the character. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// let key = "data_bend!!"; | ||
/// let new_key = escape_for_key(&key); | ||
/// assert_eq!(Ok("data_bend%21%21".to_string()), new_key); | ||
/// ``` | ||
pub fn escape_for_key(key: &str) -> Result<String, FromUtf8Error> { | ||
let mut new_key = Vec::with_capacity(key.len()); | ||
|
||
fn hex(num: u8) -> u8 { | ||
match num { | ||
0..=9 => b'0' + num, | ||
10..=15 => b'a' + (num - 10), | ||
unreachable => unreachable!("Unreachable branch num = {}", unreachable), | ||
} | ||
} | ||
|
||
for char in key.as_bytes() { | ||
match char { | ||
b'0'..=b'9' => new_key.push(*char), | ||
b'_' | b'a'..=b'z' | b'A'..=b'Z' => new_key.push(*char), | ||
_other => { | ||
new_key.push(b'%'); | ||
new_key.push(hex(*char / 16)); | ||
new_key.push(hex(*char % 16)); | ||
} | ||
} | ||
} | ||
|
||
String::from_utf8(new_key) | ||
} | ||
|
||
/// The reverse function of escape_for_key. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// let key = "data_bend%21%21"; | ||
/// let original_key = unescape_for_key(&key); | ||
/// assert_eq!(Ok("data_bend!!".to_string()), original_key); | ||
/// ``` | ||
pub fn unescape_for_key(key: &str) -> Result<String, FromUtf8Error> { | ||
let mut new_key = Vec::with_capacity(key.len()); | ||
|
||
fn unhex(num: u8) -> u8 { | ||
match num { | ||
b'0'..=b'9' => num - b'0', | ||
b'a'..=b'f' => num - b'a' + 10, | ||
unreachable => unreachable!("Unreachable branch num = {}", unreachable), | ||
} | ||
} | ||
|
||
let bytes = key.as_bytes(); | ||
|
||
let mut index = 0; | ||
while index < bytes.len() { | ||
match bytes[index] { | ||
b'%' => { | ||
// The last byte of the string won't be '%' | ||
let mut num = unhex(bytes[index + 1]) * 16; | ||
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. Should we check the index out of range if the index is already the last byte? If we can guarantee 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. For me I prefer to use iterator here so that we just call 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 function is only used with reverse case of escape_for_key. I think there is no need to check the input. I have leave a comment in the code.
In this function, the iterator sometimes needs to move 3 steps. Using index would be easier. |
||
num += unhex(bytes[index + 2]); | ||
new_key.push(num); | ||
index += 3; | ||
} | ||
other => { | ||
new_key.push(other); | ||
index += 1; | ||
} | ||
} | ||
} | ||
|
||
String::from_utf8(new_key) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
mod progress; | ||
mod runtime; | ||
mod stoppable; | ||
mod string_func; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// 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. | ||
|
||
use common_base::*; | ||
use common_exception::Result; | ||
|
||
#[test] | ||
fn test_progress() -> Result<()> { | ||
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. How about adding the slash case? 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. @BohuTANG 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. @BohuTANG Thanks for your advise. I have changed the test case. |
||
let original_key = "databend/test_user123!!"; | ||
let new_key = escape_for_key(original_key); | ||
assert_eq!(Ok("databend%2ftest_user123%21%21".to_string()), new_key); | ||
assert_eq!( | ||
Ok(original_key.to_string()), | ||
unescape_for_key(new_key.unwrap().as_str()) | ||
); | ||
Ok(()) | ||
} |
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.
Is it possible to add some comments about this escape?
Like the raw string is, now we escape to what?
Thanks.
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.
Also,some unit test cases for this
string_func
module is prefered.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.
Have add comment and unit test.