urlencoding-1.0.0/.gitignore01006440000000000000000000000022131043572730014177 0ustar0000000000000000target Cargo.lock urlencoding-1.0.0/Cargo.toml01006440000000000000000000001466131043575720014156 0ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "urlencoding" version = "1.0.0" authors = ["Bertram Truong "] description = "A Rust library for doing URL percentage encoding." keywords = ["url", "encoding", "urlencoding"] license = "MIT" repository = "https://github.com/bt/rust_urlencoding" [dependencies] urlencoding-1.0.0/Cargo.toml.orig01006440000000000000000000000452131043575720015107 0ustar0000000000000000[package] name = "urlencoding" version = "1.0.0" authors = ["Bertram Truong "] license = "MIT" description = "A Rust library for doing URL percentage encoding." repository = "https://github.com/bt/rust_urlencoding" keywords = ["url", "encoding", "urlencoding"] [dependencies] urlencoding-1.0.0/LICENSE01006440000000000000000000002042131043572730013220 0ustar0000000000000000Copyright (c) 2016 Bertram Truong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. urlencoding-1.0.0/README.md01006440000000000000000000001776131043575720013511 0ustar0000000000000000# urlencoding [![Latest Version](https://img.shields.io/crates/v/urlencoding.svg)](https://crates.io/crates/urlencoding) A Rust library for doing URL percentage encoding. Installation ============ This crate can be downloaded through Cargo. To do so, add the following line to your `Cargo.toml` file, under `dependencies`: ```toml urlencoding = "1.0.0" ``` Usage ===== To encode a string, do the following: ```rust extern crate urlencoding; use urlencoding::encode; fn main() { let encoded = encode("This string will be URL encoded."); println!("{}", encoded); // This%20string%20will%20be%20URL%20encoded. } ``` To decode a string, it's only slightly different: ```rust extern crate urlencoding; use urlencoding::decode; fn main() { let decoded = decode("%F0%9F%91%BE%20Exterminate%21"); println!("{}", decoded.unwrap()); // 👾 Exterminate! } ``` License ======= This project is licensed under the MIT license, Copyright (c) 2017 Bertram Truong. For more information see the `LICENSE` file. urlencoding-1.0.0/src/lib.rs01006440000000000000000000012234131043572730014122 0ustar0000000000000000use std::str; use std::string::FromUtf8Error; pub fn encode(data: &str) -> String { let mut escaped = String::new(); for b in data.as_bytes().iter() { match *b as char { // Accepted characters 'A'...'Z' | 'a'...'z' | '0'...'9' | '-' | '_' | '.' | '~' => escaped.push(*b as char), // Everything else is percent-encoded b => escaped.push_str(format!("%{:02X}", b as u32).as_str()), }; } return escaped; } pub fn decode(data: &str) -> Result { validate_urlencoded_str(data)?; let mut unescaped_bytes: Vec = Vec::new(); let mut bytes = data.bytes(); // If validate_urlencoded_str returned Ok, then we know // every '%' is followed by 2 hex characters while let Some(b) = bytes.next() { match b as char { '%' => { let bytes_to_decode = &[bytes.next().unwrap(), bytes.next().unwrap()]; let hex_str = str::from_utf8(bytes_to_decode).unwrap(); unescaped_bytes.push(u8::from_str_radix(hex_str, 16).unwrap()); }, _ => { // Assume whoever did the encoding intended what we got unescaped_bytes.push(b); } } } String::from_utf8(unescaped_bytes).or_else(|e| Err(FromUrlEncodingError::Utf8CharacterError { error: e, })) } // Validates every '%' character is followed by exactly 2 hex // digits. fn validate_urlencoded_str(data: &str) -> Result<(), FromUrlEncodingError> { let mut iter = data.char_indices(); while let Some((idx, chr)) = iter.next() { match chr { '%' => { validate_percent_encoding(&mut iter, idx)?; }, _ => continue, } } Ok(()) } // Validates the next two characters returned by the provided iterator are // hexadecimal digits. fn validate_percent_encoding(iter: &mut str::CharIndices, current_idx: usize) -> Result<(), FromUrlEncodingError> { for _ in 0..2 { match iter.next() { // Only hex digits are valid Some((_, c)) if c.is_digit(16) => { continue }, Some((i, c)) => return Err(FromUrlEncodingError::UriCharacterError { character: c, index: i, }), // We got a '%' without 2 characters after it, so mark the '%' as bad None => return Err(FromUrlEncodingError::UriCharacterError { character: '%', index: current_idx, }), } } Ok(()) } #[derive(Debug)] pub enum FromUrlEncodingError { UriCharacterError { character: char, index: usize }, Utf8CharacterError { error: FromUtf8Error }, } #[cfg(test)] mod tests { use super::encode; use super::decode; use super::FromUrlEncodingError; #[test] fn it_encodes_successfully() { let expected = "this%20that"; assert_eq!(expected, encode("this that")); } #[test] fn it_encodes_successfully_emoji() { let emoji_string = "👾 Exterminate!"; let expected = "%F0%9F%91%BE%20Exterminate%21"; assert_eq!(expected, encode(emoji_string)); } #[test] fn it_decodes_successfully() { let expected = String::from("this that"); let encoded = "this%20that"; assert_eq!(expected, decode(encoded).unwrap()); } #[test] fn it_decodes_successfully_emoji() { let expected = String::from("👾 Exterminate!"); let encoded = "%F0%9F%91%BE%20Exterminate%21"; assert_eq!(expected, decode(encoded).unwrap()); } #[test] fn it_decodes_unsuccessfully_emoji() { let bad_encoded_string = "👾 Exterminate!"; assert_eq!(bad_encoded_string, decode(bad_encoded_string).unwrap()); } #[test] fn it_decodes_unsuccessfuly_bad_percent_01() { let bad_encoded_string = "this%2that"; let expected_idx = 6; let expected_char = 't'; match decode(bad_encoded_string).unwrap_err() { FromUrlEncodingError::UriCharacterError { index: i, character: c } => { assert_eq!(expected_idx, i); assert_eq!(expected_char, c) }, _ => panic!() } } #[test] fn it_decodes_unsuccessfuly_bad_percent_02() { let bad_encoded_string = "this%20that%"; let expected_idx = 11; let expected_char = '%'; match decode(bad_encoded_string).unwrap_err() { FromUrlEncodingError::UriCharacterError { index: i, character: c } => { assert_eq!(expected_idx, i); assert_eq!(expected_char, c) }, _ => panic!() } } #[test] fn it_decodes_unsuccessfuly_bad_percent_03() { let bad_encoded_string = "this%20that%2"; let expected_idx = 11; let expected_char = '%'; match decode(bad_encoded_string).unwrap_err() { FromUrlEncodingError::UriCharacterError { index: i, character: c } => { assert_eq!(expected_idx, i); assert_eq!(expected_char, c) }, _ => panic!() } } }