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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
use super::features::*;
use super::*;
/// `Require` header ([RFC 7826 section 18.43](https://tools.ietf.org/html/rfc7826#section-18.43)).
#[derive(Debug, Clone)]
pub struct Require(Vec<String>);
impl std::ops::Deref for Require {
type Target = Vec<String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Require {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl AsRef<Vec<String>> for Require {
fn as_ref(&self) -> &Vec<String> {
&self.0
}
}
impl AsMut<Vec<String>> for Require {
fn as_mut(&mut self) -> &mut Vec<String> {
&mut self.0
}
}
impl From<Vec<String>> for Require {
fn from(v: Vec<String>) -> Self {
Require(v)
}
}
impl<'a> From<&'a [String]> for Require {
fn from(v: &'a [String]) -> Self {
Require(v.to_vec())
}
}
impl<'a> From<&'a [&'a &str]> for Require {
fn from(v: &'a [&'a &str]) -> Self {
Require(v.iter().map(|s| String::from(**s)).collect())
}
}
impl Require {
/// Creates a new `Require` header builder.
pub fn builder() -> RequireBuilder {
RequireBuilder(Vec::new())
}
/// Check if the "play.basic" feature is required.
///
/// See [RFC 7826 section 11.1](https://tools.ietf.org/html/rfc7826#section-11.1).
pub fn contains_play_basic(&self) -> bool {
self.0.iter().any(|f| f == PLAY_BASIC)
}
/// Check if the "play.scale" feature is required.
///
/// See [RFC 7826 section 18.46](https://tools.ietf.org/html/rfc7826#section-18.46).
pub fn contains_play_scale(&self) -> bool {
self.0.iter().any(|f| f == PLAY_SCALE)
}
/// Check if the "play.speed" feature is required.
///
/// See [RFC 7826 section 18.50](https://tools.ietf.org/html/rfc7826#section-18.50).
pub fn contains_play_speed(&self) -> bool {
self.0.iter().any(|f| f == PLAY_SPEED)
}
/// Check if the "setup.rtp.rtcp.mux" feature is required.
///
/// See [RFC 7826 Appendix C.1.6.4](https://tools.ietf.org/html/rfc7826#appendix-C.1.6.4).
pub fn contains_setup_rtp_rtcp_mux(&self) -> bool {
self.0.iter().any(|f| f == SETUP_RTP_RTCP_MUX)
}
}
/// Builder for the 'Require' header.
#[derive(Debug, Clone)]
pub struct RequireBuilder(Vec<String>);
impl RequireBuilder {
/// Add the provided feature to the `Require` header.
pub fn feature<S: Into<String>>(mut self, feature: S) -> Self {
self.0.push(feature.into());
self
}
/// Add the "play.basic" feature to the `Require` header.
///
/// See [RFC 7826 section 11.1](https://tools.ietf.org/html/rfc7826#section-11.1).
pub fn play_basic(self) -> Self {
self.feature(PLAY_BASIC)
}
/// Add the "play.scale" feature to the `Require` header.
///
/// See [RFC 7826 section 18.46](https://tools.ietf.org/html/rfc7826#section-18.46).
pub fn play_scale(self) -> Self {
self.feature(PLAY_SCALE)
}
/// Add the "play.speed" feature to the `Require` header.
///
/// See [RFC 7826 section 18.50](https://tools.ietf.org/html/rfc7826#section-18.50).
pub fn play_speed(self) -> Self {
self.feature(PLAY_SPEED)
}
/// Add the "setup.rtp.rtcp.mux" feature to the `Require` header.
///
/// See [RFC 7826 Appendix C.1.6.4](https://tools.ietf.org/html/rfc7826#appendix-C.1.6.4).
pub fn setup_rtp_rtcp_mux(self) -> Self {
self.feature(SETUP_RTP_RTCP_MUX)
}
/// Build the `Require` header.
pub fn build(self) -> Require {
Require(self.0)
}
}
impl super::TypedHeader for Require {
fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>, HeaderParseError> {
let headers = headers.as_ref();
let header = match headers.get(&REQUIRE) {
None => return Ok(None),
Some(header) => header,
};
let mut require = Vec::new();
for feature in header.as_str().split(',') {
let feature = feature.trim();
require.push(feature.into());
}
Ok(Some(Require(require)))
}
fn insert_into(&self, mut headers: impl AsMut<Headers>) {
let headers = headers.as_mut();
let mut require = String::new();
for feature in &self.0 {
if !require.is_empty() {
require.push_str(", ");
}
require.push_str(feature);
}
headers.insert(REQUIRE, require);
}
}
impl super::TypedAppendableHeader for Require {
fn append_to(&self, mut headers: impl AsMut<Headers>) {
let headers = headers.as_mut();
let mut require = String::new();
for feature in &self.0 {
if !require.is_empty() {
require.push_str(", ");
}
require.push_str(feature);
}
headers.append(REQUIRE, require);
}
}