cookie_rs/jar/
parse.rs

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
use std::borrow::Cow;

use crate::cookie::parse::ParseError;
use crate::{Cookie, StringPrison};

use super::CookieJar;

impl<'a> CookieJar<'a> {
    pub fn parse<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError> {
        Self::inner_parse(value.into(), false)
    }

    pub fn parse_strict<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError> {
        Self::inner_parse(value.into(), true)
    }

    fn inner_parse(value: Cow<'a, str>, strict: bool) -> Result<Self, ParseError> {
        let prison = StringPrison::new(value);

        // SAFETY: prison and slice owned by the same struct
        let str = unsafe { prison.get() };

        let mut jar = parse_jar(str, strict)?;
        jar.prison = Some(prison);

        Ok(jar)
    }
}

fn parse_jar(str: &str, strict: bool) -> Result<CookieJar<'_>, ParseError> {
    let mut jar = CookieJar::default();
    let cookie = str.split(';').map(|p| p.trim()).filter(|p| !p.is_empty());

    for pair in cookie {
        jar.cookie.insert(Cookie::inner_parse(pair.into(), strict)?);
    }

    Ok(jar)
}