1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 #![rustfmt::skip] 15 16 use crate::h3::qpack::table::Field; 17 use crate::h3::pseudo::PseudoHeaders; 18 use crate::headers::Headers; 19 20 /// HTTP2 HEADERS frame payload implementation. 21 #[derive(PartialEq, Eq, Clone)] 22 pub struct Parts { 23 pub(crate) pseudo: PseudoHeaders, 24 pub(crate) map: Headers, 25 } 26 27 impl Parts { 28 /// The constructor of `Parts` new() -> Self29 pub fn new() -> Self { 30 Self { 31 pseudo: PseudoHeaders::new(), 32 map: Headers::new(), 33 } 34 } 35 36 /// Sets pseudo headers for `Parts`. set_pseudo(&mut self, pseudo: PseudoHeaders)37 pub fn set_pseudo(&mut self, pseudo: PseudoHeaders) { 38 self.pseudo = pseudo; 39 } 40 41 /// Sets regular field lines for `Parts`. set_header_lines(&mut self, headers: Headers)42 pub fn set_header_lines(&mut self, headers: Headers) { 43 self.map = headers; 44 } 45 is_empty(&self) -> bool46 pub(crate) fn is_empty(&self) -> bool { 47 self.pseudo.is_empty() && self.map.is_empty() 48 } 49 update(&mut self, headers: Field, value: String)50 pub(crate) fn update(&mut self, headers: Field, value: String) { 51 match headers { 52 Field::Authority => self.pseudo.set_authority(Some(value)), 53 Field::Method => self.pseudo.set_method(Some(value)), 54 Field::Path => self.pseudo.set_path(Some(value)), 55 Field::Scheme => self.pseudo.set_scheme(Some(value)), 56 Field::Status => self.pseudo.set_status(Some(value)), 57 Field::Other(header) => self.map.append(header.as_str(), value.as_str()).unwrap(), 58 } 59 } 60 parts(&self) -> (&PseudoHeaders, &Headers)61 pub(crate) fn parts(&self) -> (&PseudoHeaders, &Headers) { 62 (&self.pseudo, &self.map) 63 } 64 into_parts(self) -> (PseudoHeaders, Headers)65 pub(crate) fn into_parts(self) -> (PseudoHeaders, Headers) { 66 (self.pseudo, self.map) 67 } 68 } 69 70 impl Default for Parts { default() -> Self71 fn default() -> Self { 72 Self::new() 73 } 74 } 75