diff --git a/gst-plugin-reqwest/src/reqwesthttpsrc.rs b/gst-plugin-reqwest/src/reqwesthttpsrc.rs index 4c9f58e2..d4769226 100644 --- a/gst-plugin-reqwest/src/reqwesthttpsrc.rs +++ b/gst-plugin-reqwest/src/reqwesthttpsrc.rs @@ -5,7 +5,6 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. - use bytes::Bytes; use futures::sync::oneshot; use futures::{Future, Stream}; @@ -14,6 +13,7 @@ use hyperx::header::{ RangeUnit, }; use reqwest::r#async::{Client, Decoder}; +use reqwest::StatusCode; use std::mem; use std::sync::Mutex; use std::u64; @@ -163,13 +163,33 @@ impl ReqwestHttpSrc { }) })?; - //I have to read statusCode and produce error accordingly if !response.status().is_success() { - gst_error!(cat, obj: src, "Request status failed: {:?}", response); - return Err(gst_error_msg!( - gst::ResourceError::NotFound, - ["Failed to fetch {}: {}", uri, response.status()] - )); + match response.status() { + StatusCode::NOT_FOUND => { + gst_error!(cat, obj: src, "Request status failed: {:?}", response); + return Err(gst_error_msg!( + gst::ResourceError::NotFound, + ["Request status failed for {}: {}", uri, response.status()] + )); + } + StatusCode::UNAUTHORIZED + | StatusCode::PAYMENT_REQUIRED + | StatusCode::FORBIDDEN + | StatusCode::PROXY_AUTHENTICATION_REQUIRED => { + gst_error!(cat, obj: src, "Request status failed: {:?}", response); + return Err(gst_error_msg!( + gst::ResourceError::NotAuthorized, + ["Request status failed for {}: {}", uri, response.status()] + )); + } + _ => { + gst_error!(cat, obj: src, "Request status failed: {:?}", response); + return Err(gst_error_msg!( + gst::ResourceError::OpenRead, + ["Request status failed for {}: {}", uri, response.status()] + )); + } + } } let headers = Headers::from(response.headers()); diff --git a/gst-plugin-reqwest/tests/reqwesthttpsrc.rs b/gst-plugin-reqwest/tests/reqwesthttpsrc.rs index b3f0e3b5..048139d5 100644 --- a/gst-plugin-reqwest/tests/reqwesthttpsrc.rs +++ b/gst-plugin-reqwest/tests/reqwesthttpsrc.rs @@ -136,7 +136,7 @@ impl Harness { fn wait_for_error(&mut self) -> glib::Error { loop { - match self.receiver.recv().unwrap() { + match self.receiver.as_mut().unwrap().recv().unwrap() { Message::ServerError(err) => { panic!("Got server error: {}", err); } @@ -145,7 +145,7 @@ impl Harness { match ev.view() { EventView::Eos(_) => { - panic!("Got EOS"); + panic!("Got EOS but expected error"); } _ => (), } @@ -160,8 +160,8 @@ impl Harness { _ => (), } } - Message::Buffer(buffer) => { - panic!("Got buffer {:?}", buffer); + Message::Buffer(_buffer) => { + panic!("Got buffer but expected error"); } } } @@ -282,13 +282,17 @@ fn test_basic_request() { #[test] fn test_404_error() { + use reqwest::StatusCode; init(); let mut h = Harness::new( |_req| { use hyper::{Body, Response}; - Response::builder().status(404).body(Body::empty()).unwrap() + Response::builder() + .status(StatusCode::NOT_FOUND.as_u16()) + .body(Body::empty()) + .unwrap() }, |_src| {}, ); @@ -297,15 +301,8 @@ fn test_404_error() { let _ = src.set_state(gst::State::Playing); }); - let expected_error = gst::ResourceError::NotFound; - let err_code = h.wait_for_error(); if let Some(err) = err_code.kind::() { - match err { - gst::ResourceError::NotFound => { - assert_eq!(err, expected_error); - } - _ => panic!("unexpected error : {:?}", err), - } + assert_eq!(err, gst::ResourceError::NotFound); } }