reqwesthttpsrc: Switch back to hyperx

It was updated to the new versions of its dependencies and provides
typed header types for more of the headers that we're using.
This commit is contained in:
Sebastian Dröge 2020-01-18 12:50:24 +02:00 committed by Sebastian Dröge
parent 676ae87aed
commit b84831c94f
2 changed files with 28 additions and 41 deletions

View file

@ -12,7 +12,7 @@ url = "2.1"
glib = { git = "https://github.com/gtk-rs/glib" } glib = { git = "https://github.com/gtk-rs/glib" }
reqwest = { version = "0.10", features = ["cookies", "gzip"] } reqwest = { version = "0.10", features = ["cookies", "gzip"] }
futures = "0.3" futures = "0.3"
headers = "0.3" hyperx = "1.0"
mime = "0.3" mime = "0.3"
gstreamer = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_10"] } gstreamer = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs", features = ["v1_10"] }
gstreamer-base = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" } gstreamer-base = { git = "https://gitlab.freedesktop.org/gstreamer/gstreamer-rs" }

View file

@ -353,10 +353,12 @@ impl ReqwestHttpSrc {
start: u64, start: u64,
stop: Option<u64>, stop: Option<u64>,
) -> Result<State, Option<gst::ErrorMessage>> { ) -> Result<State, Option<gst::ErrorMessage>> {
use headers::{ use hyperx::header::{
AcceptRanges, Connection, ContentLength, ContentRange, HeaderMapExt, Range, UserAgent, qitem, AcceptEncoding, AcceptRanges, ByteRangeSpec, Connection, ContentLength,
ContentRange, ContentRangeSpec, ContentType, Cookie, Encoding, Range, RangeUnit,
TypedHeaders, UserAgent,
}; };
use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use reqwest::header::HeaderMap;
gst_debug!(CAT, obj: src, "Creating new request for {}", uri); gst_debug!(CAT, obj: src, "Creating new request for {}", uri);
@ -369,38 +371,30 @@ impl ReqwestHttpSrc {
let mut headers = HeaderMap::new(); let mut headers = HeaderMap::new();
if settings.keep_alive { if settings.keep_alive {
headers.typed_insert(Connection::keep_alive()); headers.encode(&Connection::keep_alive());
} else { } else {
headers.typed_insert(Connection::close()); headers.encode(&Connection::close());
} }
match (start != 0, stop) { match (start != 0, stop) {
(false, None) => (), (false, None) => (),
(true, None) => { (true, None) => {
headers.typed_insert(Range::bytes(start..).unwrap()); headers.encode(&Range::Bytes(vec![ByteRangeSpec::AllFrom(start)]));
} }
(_, Some(stop)) => { (_, Some(stop)) => {
headers.typed_insert(Range::bytes(start..stop).unwrap()); headers.encode(&Range::Bytes(vec![ByteRangeSpec::FromTo(start, stop - 1)]));
} }
} }
if let Ok(user_agent) = settings.user_agent.parse::<UserAgent>() { headers.encode(&UserAgent::new(settings.user_agent));
headers.typed_insert(user_agent);
} else {
gst_warning!(
CAT,
obj: src,
"Failed to transform user-agent '{}' to header value",
settings.user_agent
);
}
if !settings.compress { if !settings.compress {
// Compression is the default // Compression is the default
headers.append("accept-encoding", "identity".parse().unwrap()); headers.encode(&AcceptEncoding(vec![qitem(Encoding::Identity)]));
}; };
if let Some(ref extra_headers) = settings.extra_headers { if let Some(ref extra_headers) = settings.extra_headers {
use reqwest::header::{HeaderName, HeaderValue};
use std::convert::TryFrom; use std::convert::TryFrom;
for (field, value) in extra_headers.iter() { for (field, value) in extra_headers.iter() {
@ -466,25 +460,16 @@ impl ReqwestHttpSrc {
} }
if !settings.cookies.is_empty() { if !settings.cookies.is_empty() {
let mut cookies = String::new(); let mut cookies = Cookie::new();
for cookie in settings.cookies { for cookie in settings.cookies {
let mut split = cookie.splitn(2, '='); let mut split = cookie.splitn(2, '=');
let key = split.next(); let key = split.next();
let value = split.next(); let value = split.next();
if let (Some(key), Some(value)) = (key, value) { if let (Some(key), Some(value)) = (key, value) {
if !cookies.is_empty() { cookies.append(String::from(key), String::from(value));
cookies.push_str("; ");
}
cookies.push_str(key);
cookies.push('=');
cookies.push_str(value);
} }
} }
if let Ok(cookies) = HeaderValue::from_str(&cookies) { headers.encode(&cookies);
headers.append("cookie", cookies);
} else {
gst_warning!(CAT, obj: src, "Failed to convert cookies into header value",);
}
} }
if settings.iradio_mode { if settings.iradio_mode {
@ -557,13 +542,19 @@ impl ReqwestHttpSrc {
} }
let headers = res.headers(); let headers = res.headers();
let size = headers.typed_get().map(|ContentLength(cl)| cl + start); let size = headers.decode().map(|ContentLength(cl)| cl + start).ok();
let accept_byte_ranges = headers.typed_get() == Some(AcceptRanges::bytes());
let accept_byte_ranges = if let Ok(AcceptRanges(ref ranges)) = headers.decode() {
ranges.iter().any(|u| *u == RangeUnit::Bytes)
} else {
false
};
let seekable = size.is_some() && accept_byte_ranges; let seekable = size.is_some() && accept_byte_ranges;
let position = if let Some((range_start, _range_end)) = headers let position = if let Ok(ContentRange(ContentRangeSpec::Bytes {
.typed_get() range: Some((range_start, _)),
.and_then(|h| ContentRange::bytes_range(&h)) ..
})) = headers.decode()
{ {
range_start range_start
} else { } else {
@ -587,11 +578,7 @@ impl ReqwestHttpSrc {
.build() .build()
}); });
if let Some(content_type) = headers if let Ok(ContentType(ref content_type)) = headers.decode() {
.get("content-type")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<mime::Mime>().ok())
{
gst_debug!(CAT, obj: src, "Got content type {}", content_type); gst_debug!(CAT, obj: src, "Got content type {}", content_type);
if let Some(ref mut caps) = caps { if let Some(ref mut caps) = caps {
let caps = caps.get_mut().unwrap(); let caps = caps.get_mut().unwrap();