rust_rcs_core/internet/headers/
cache_control.rs

1// Copyright 2023 宋昊文
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{internet::AsHeaderField, util::raw_string::ToInt};
16
17pub struct CacheControl {
18    pub no_cache: bool,
19    pub max_age: u32,
20}
21
22pub fn parse(s: &[u8]) -> CacheControl {
23    let mut cc = CacheControl {
24        no_cache: true,
25        max_age: 0,
26    };
27
28    let header_field = s.as_header_field();
29    let iter = header_field.get_parameter_iterator();
30
31    for param in iter {
32        if param.name.eq_ignore_ascii_case(b"no-cache") {
33            cc.no_cache = true;
34        } else if param.name.eq_ignore_ascii_case(b"max-age") {
35            if let Some(max_age) = param.value {
36                if let Ok(max_age) = max_age.to_int::<u32>() {
37                    cc.max_age = max_age;
38                }
39            }
40        }
41    }
42
43    cc
44}