reqwesthttpsrc: Report correct error messages based on HTTP error status codes

404 is mapped to ResourceError::NotFound
401,402,403,407 are mapped to ResourceError::NotAuthorized
other error status codes are mapped to ResourceError::OpenRead
This mirrors the behavior in souphttpsrc
This commit is contained in:
ahamedsajeer.15 2019-07-05 00:47:22 +05:30
parent e20a1ce947
commit ce1ed81922
2 changed files with 37 additions and 20 deletions

View file

@ -5,7 +5,6 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use bytes::Bytes; use bytes::Bytes;
use futures::sync::oneshot; use futures::sync::oneshot;
use futures::{Future, Stream}; use futures::{Future, Stream};
@ -14,6 +13,7 @@ use hyperx::header::{
RangeUnit, RangeUnit,
}; };
use reqwest::r#async::{Client, Decoder}; use reqwest::r#async::{Client, Decoder};
use reqwest::StatusCode;
use std::mem; use std::mem;
use std::sync::Mutex; use std::sync::Mutex;
use std::u64; use std::u64;
@ -163,13 +163,33 @@ impl ReqwestHttpSrc {
}) })
})?; })?;
//I have to read statusCode and produce error accordingly
if !response.status().is_success() { if !response.status().is_success() {
gst_error!(cat, obj: src, "Request status failed: {:?}", response); match response.status() {
return Err(gst_error_msg!( StatusCode::NOT_FOUND => {
gst::ResourceError::NotFound, gst_error!(cat, obj: src, "Request status failed: {:?}", response);
["Failed to fetch {}: {}", uri, response.status()] 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()); let headers = Headers::from(response.headers());

View file

@ -136,7 +136,7 @@ impl Harness {
fn wait_for_error(&mut self) -> glib::Error { fn wait_for_error(&mut self) -> glib::Error {
loop { loop {
match self.receiver.recv().unwrap() { match self.receiver.as_mut().unwrap().recv().unwrap() {
Message::ServerError(err) => { Message::ServerError(err) => {
panic!("Got server error: {}", err); panic!("Got server error: {}", err);
} }
@ -145,7 +145,7 @@ impl Harness {
match ev.view() { match ev.view() {
EventView::Eos(_) => { EventView::Eos(_) => {
panic!("Got EOS"); panic!("Got EOS but expected error");
} }
_ => (), _ => (),
} }
@ -160,8 +160,8 @@ impl Harness {
_ => (), _ => (),
} }
} }
Message::Buffer(buffer) => { Message::Buffer(_buffer) => {
panic!("Got buffer {:?}", buffer); panic!("Got buffer but expected error");
} }
} }
} }
@ -282,13 +282,17 @@ fn test_basic_request() {
#[test] #[test]
fn test_404_error() { fn test_404_error() {
use reqwest::StatusCode;
init(); init();
let mut h = Harness::new( let mut h = Harness::new(
|_req| { |_req| {
use hyper::{Body, Response}; 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| {}, |_src| {},
); );
@ -297,15 +301,8 @@ fn test_404_error() {
let _ = src.set_state(gst::State::Playing); let _ = src.set_state(gst::State::Playing);
}); });
let expected_error = gst::ResourceError::NotFound;
let err_code = h.wait_for_error(); let err_code = h.wait_for_error();
if let Some(err) = err_code.kind::<gst::ResourceError>() { if let Some(err) = err_code.kind::<gst::ResourceError>() {
match err { assert_eq!(err, gst::ResourceError::NotFound);
gst::ResourceError::NotFound => {
assert_eq!(err, expected_error);
}
_ => panic!("unexpected error : {:?}", err),
}
} }
} }