1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
//! Helpers for HTTP request handling and response generation

pub mod header;
pub mod request;
pub mod response;

use log::trace;
use percent_encoding::percent_decode;
use std;

/// Represents data that has been successfully percent decoded and is valid UTF-8
#[derive(Clone, PartialEq, Debug)]
pub struct PercentDecoded {
    val: String,
}

impl PercentDecoded {
    /// Attempt to decode data that has been provided in a perecent encoded format and ensure that
    /// the result is valid UTF-8.
    ///
    /// On success, the decoded data is returned as a `PercentDecoded` value, which allows a
    /// compile-time check that the decode has occurred in places where it's assumed to have
    /// occurred.
    pub(crate) fn new(raw: &str) -> Option<Self> {
        match percent_decode(raw.as_bytes()).decode_utf8() {
            Ok(pd) => {
                trace!(" percent_decode: {}, src: {}", pd, raw);
                Some(PercentDecoded {
                    val: pd.into_owned(),
                })
            }
            Err(_) => {
                trace!(" percent_decode: error, src: {}", raw);
                None
            }
        }
    }
}

impl AsRef<str> for PercentDecoded {
    fn as_ref(&self) -> &str {
        &self.val
    }
}

/// Decode form-urlencoded strings (e.g. query string, or request body with Content-Type:
/// application/x-www-form-urlencoded
fn form_url_decode(raw: &str) -> Result<String, std::str::Utf8Error> {
    match percent_decode(raw.replace("+", " ").as_bytes()).decode_utf8() {
        Ok(pd) => {
            trace!(" form_url_decode: {}, src: {}", pd, raw);
            Ok(pd.into_owned())
        }
        Err(e) => {
            trace!(" form_url_decode: error, src: {}", raw);
            Err(e)
        }
    }
}

/// Represents data that has been successfully decoded from a form-urlencoded source and is
/// valid UTF-8
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct FormUrlDecoded {
    val: String,
}

impl FormUrlDecoded {
    /// Attempt to decode data that has been provided in www-form-urlencoded format and ensure that
    /// the result is valid UTF-8.
    ///
    /// On success, the decoded data is returned as a `FormUrlDecoded` value, which allows a
    /// compile-time check that the decode has occurred in places where it's assumed to have
    /// occurred.
    pub(crate) fn new(raw: &str) -> Option<Self> {
        match form_url_decode(raw) {
            Ok(val) => Some(FormUrlDecoded { val }),
            Err(_) => None,
        }
    }
}

impl AsRef<str> for FormUrlDecoded {
    fn as_ref(&self) -> &str {
        &self.val
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ensure_valid_percent_decode() {
        let pd = PercentDecoded::new("%41+%42%2B%63%20%64").unwrap();
        assert_eq!("A+B+c d", pd.as_ref());
    }

    #[test]
    fn ensure_valid_www_form_url_encoded_value() {
        let f = FormUrlDecoded::new("%41+%42%2B%63%20%64").unwrap();
        assert_eq!("A B+c d", f.as_ref());
    }
}