1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 09:49:31 +02:00

Fix lyrics retrieval

This commit is contained in:
Roderick van Domburg 2021-11-27 08:30:51 +01:00
parent d19fd24074
commit e1b273b8a1
No known key found for this signature in database
GPG key ID: FE2585E713F9F30A
2 changed files with 45 additions and 21 deletions

View file

@ -1,3 +1,7 @@
use bytes::Bytes;
use http::header::HeaderValue;
use http::uri::InvalidUri;
use hyper::header::InvalidHeaderValue;
use hyper::{Body, Client, Request, Response, StatusCode};
use hyper_proxy::{Intercept, Proxy, ProxyConnector};
use hyper_rustls::HttpsConnector;
@ -11,7 +15,7 @@ pub struct HttpClient {
#[derive(Error, Debug)]
pub enum HttpClientError {
#[error("could not parse request: {0}")]
Parsing(#[from] http::uri::InvalidUri),
Parsing(#[from] http::Error),
#[error("could not send request: {0}")]
Request(hyper::Error),
#[error("could not read response: {0}")]
@ -20,6 +24,18 @@ pub enum HttpClientError {
ProxyBuilder(#[from] std::io::Error),
}
impl From<InvalidHeaderValue> for HttpClientError {
fn from(err: InvalidHeaderValue) -> Self {
Self::Parsing(err.into())
}
}
impl From<InvalidUri> for HttpClientError {
fn from(err: InvalidUri) -> Self {
Self::Parsing(err.into())
}
}
impl HttpClient {
pub fn new(proxy: Option<&Url>) -> Self {
Self {
@ -27,10 +43,17 @@ impl HttpClient {
}
}
pub async fn request(&self, req: Request<Body>) -> Result<Response<Body>, HttpClientError> {
pub async fn request(&self, mut req: Request<Body>) -> Result<Response<Body>, HttpClientError> {
let connector = HttpsConnector::with_native_roots();
let uri = req.uri().clone();
let headers_mut = req.headers_mut();
headers_mut.insert(
"User-Agent",
// Some features like lyrics are version-gated and require a "real" version string.
HeaderValue::from_str("Spotify/8.6.80 iOS/13.5 (iPhone11,2)")?,
);
let response = if let Some(url) = &self.proxy {
let uri = url.to_string().parse()?;
let proxy = Proxy::new(Intercept::All, uri);
@ -58,7 +81,7 @@ impl HttpClient {
response
}
pub async fn request_body(&self, req: Request<Body>) -> Result<bytes::Bytes, HttpClientError> {
pub async fn request_body(&self, req: Request<Body>) -> Result<Bytes, HttpClientError> {
let response = self.request(req).await?;
hyper::body::to_bytes(response.into_body())
.await