      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
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127

    #[cfg(feature = "http1")]
    use bytes::BytesMut;
    use http::header::CONTENT_LENGTH;
    use http::header::{HeaderValue, ValueIter};
    use http::HeaderMap;
    #[cfg(all(feature = "http2", feature = "client"))]
    use http::Method;

    #[cfg(feature = "http1")]
    pub(super) fn connection_keep_alive(value: &HeaderValue) -> bool {
        connection_has(value, "keep-alive")
    }

    #[cfg(feature = "http1")]
    pub(super) fn connection_close(value: &HeaderValue) -> bool {
        connection_has(value, "close")
    }

    #[cfg(feature = "http1")]
    fn connection_has(value: &HeaderValue, needle: &str) -> bool {
        if let Ok(s) = value.to_str() {
            for val in s.split(',') {
                if val.trim().eq_ignore_ascii_case(needle) {
                    return true;
                }
            }
        }
        false
    }

    #[cfg(all(feature = "http1", feature = "server"))]
    pub(super) fn content_length_parse(value: &HeaderValue) -> Option<u64> {
        value.to_str().ok().and_then(|s| s.parse().ok())
    }

    pub(super) fn content_length_parse_all(headers: &HeaderMap) -> Option<u64> {
        content_length_parse_all_values(headers.get_all(CONTENT_LENGTH).into_iter())
    }

    pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>) -> Option<u64> {
        // If multiple Content-Length headers were sent, everything can still
        // be alright if they all contain the same value, and all parse
        // correctly. If not, then it's an error.

        let folded = values.fold(None, |prev, line| match prev {
            Some(Ok(prev)) => Some(
                line.to_str()
                    .map_err(|_| ())
                    .and_then(|s| s.parse().map_err(|_| ()))
                    .and_then(|n| if prev == n { Ok(n) } else { Err(()) }),
            ),
            None => Some(
                line.to_str()
                    .map_err(|_| ())
                    .and_then(|s| s.parse().map_err(|_| ())),
            ),
            Some(Err(())) => Some(Err(())),
        });

        if let Some(Ok(n)) = folded {
            Some(n)
        } else {
            None
        }
    }

    #[cfg(all(feature = "http2", feature = "client"))]
    pub(super) fn method_has_defined_payload_semantics(method: &Method) -> bool {
        match *method {
            Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT => false,
            _ => true,
        }
    }

    #[cfg(feature = "http2")]
    pub(super) fn set_content_length_if_missing(headers: &mut HeaderMap, len: u64) {
        headers
            .entry(CONTENT_LENGTH)
            .or_insert_with(|| HeaderValue::from(len));
    }

    #[cfg(feature = "http1")]
    pub(super) fn transfer_encoding_is_chunked(headers: &HeaderMap) -> bool {
        is_chunked(headers.get_all(http::header::TRANSFER_ENCODING).into_iter())
    }

    #[cfg(feature = "http1")]
    pub(super) fn is_chunked(mut encodings: ValueIter<'_, HeaderValue>) -> bool {
        // chunked must always be the last encoding, according to spec
        if let Some(line) = encodings.next_back() {
            return is_chunked_(line);
        }

        false
    }

    #[cfg(feature = "http1")]
    pub(super) fn is_chunked_(value: &HeaderValue) -> bool {
        // chunked must always be the last encoding, according to spec
        if let Ok(s) = value.to_str() {
            if let Some(encoding) = s.rsplit(',').next() {
                return encoding.trim().eq_ignore_ascii_case("chunked");
            }
        }

        false
    }

    #[cfg(feature = "http1")]
    pub(super) fn add_chunked(mut entry: http::header::OccupiedEntry<'_, HeaderValue>) {
        const CHUNKED: &str = "chunked";

        if let Some(line) = entry.iter_mut().next_back() {
            // + 2 for ", "
            let new_cap = line.as_bytes().len() + CHUNKED.len() + 2;
            let mut buf = BytesMut::with_capacity(new_cap);
            buf.extend_from_slice(line.as_bytes());
            buf.extend_from_slice(b", ");
            buf.extend_from_slice(CHUNKED.as_bytes());

            *line = HeaderValue::from_maybe_shared(buf.freeze())
                .expect("original header value plus ascii is valid");
            return;
        }

        entry.insert(HeaderValue::from_static(CHUNKED));
    }