mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2025-01-02 23:38:45 +00:00
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:
parent
e20a1ce947
commit
ce1ed81922
2 changed files with 37 additions and 20 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue