Commit ce1ed819 authored by Sajeer Ahamed's avatar Sajeer Ahamed

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
parent e20a1ce9
Pipeline #46901 passed with stages
in 41 minutes and 41 seconds
......@@ -5,7 +5,6 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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());
......
......@@ -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::<gst::ResourceError>() {
match err {
gst::ResourceError::NotFound => {
assert_eq!(err, expected_error);
}
_ => panic!("unexpected error : {:?}", err),
}
assert_eq!(err, gst::ResourceError::NotFound);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment