feat: add encode_percent function for percent-encoding strings
This commit is contained in:
parent
68b6d569dc
commit
18047ea4b8
@ -3,5 +3,8 @@ use pyo3::prelude::*;
|
||||
#[pymodule]
|
||||
mod _httpx {
|
||||
#[pymodule_export]
|
||||
use crate::{urlparse::normalize_path, urls::QueryParams};
|
||||
use crate::{
|
||||
urlparse::{encode_percent, normalize_path},
|
||||
urls::QueryParams,
|
||||
};
|
||||
}
|
||||
|
||||
@ -23,3 +23,21 @@ pub fn normalize_path(path: &str) -> String {
|
||||
|
||||
normalized_components.join("/")
|
||||
}
|
||||
|
||||
const UNRESERVED_CHARS: &[u8] =
|
||||
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
||||
|
||||
#[pyfunction]
|
||||
pub fn encode_percent(string: &str, safe: &str) -> String {
|
||||
let safe = safe.as_bytes();
|
||||
string
|
||||
.bytes()
|
||||
.map(|b| {
|
||||
if UNRESERVED_CHARS.contains(&b) || safe.contains(&b) {
|
||||
(b as char).to_string()
|
||||
} else {
|
||||
format!("%{:02X}", b)
|
||||
}
|
||||
})
|
||||
.collect::<String>()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user