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 use crate::h2::hpack::table::Header; 15 use crate::h2::PseudoHeaders; 16 use crate::headers::Headers; 17 18 /// HTTP2 HEADERS frame payload implementation. 19 #[derive(PartialEq, Eq, Clone)] 20 pub struct Parts { 21 pub(crate) pseudo: PseudoHeaders, 22 pub(crate) map: Headers, 23 } 24 25 impl Parts { 26 /// The constructor of `Parts` new() -> Self27 pub fn new() -> Self { 28 Self { 29 pseudo: PseudoHeaders::new(), 30 map: Headers::new(), 31 } 32 } 33 34 /// Sets pseudo headers for `Parts`. set_pseudo(&mut self, pseudo: PseudoHeaders)35 pub fn set_pseudo(&mut self, pseudo: PseudoHeaders) { 36 self.pseudo = pseudo; 37 } 38 39 /// Sets regular field lines for `Parts`. set_header_lines(&mut self, headers: Headers)40 pub fn set_header_lines(&mut self, headers: Headers) { 41 self.map = headers; 42 } 43 is_empty(&self) -> bool44 pub(crate) fn is_empty(&self) -> bool { 45 self.pseudo.is_empty() && self.map.is_empty() 46 } 47 update(&mut self, headers: Header, value: String)48 pub(crate) fn update(&mut self, headers: Header, value: String) { 49 match headers { 50 Header::Authority => self.pseudo.set_authority(Some(value)), 51 Header::Method => self.pseudo.set_method(Some(value)), 52 Header::Path => self.pseudo.set_path(Some(value)), 53 Header::Scheme => self.pseudo.set_scheme(Some(value)), 54 Header::Status => self.pseudo.set_status(Some(value)), 55 Header::Other(header) => self.map.append(header.as_str(), value.as_str()).unwrap(), 56 } 57 } 58 parts(&self) -> (&PseudoHeaders, &Headers)59 pub(crate) fn parts(&self) -> (&PseudoHeaders, &Headers) { 60 (&self.pseudo, &self.map) 61 } 62 into_parts(self) -> (PseudoHeaders, Headers)63 pub(crate) fn into_parts(self) -> (PseudoHeaders, Headers) { 64 (self.pseudo, self.map) 65 } 66 } 67 68 impl Default for Parts { default() -> Self69 fn default() -> Self { 70 Self::new() 71 } 72 } 73