Add content disposition for OK responses (#2545)

This commit is contained in:
Mark Renten 2019-02-11 13:54:21 -05:00 committed by Ignotus Peverell
parent 200f87b8bc
commit 3b74a9ba3f
3 changed files with 20 additions and 9 deletions

View file

@ -56,7 +56,7 @@ pub struct ChainValidationHandler {
impl Handler for ChainValidationHandler { impl Handler for ChainValidationHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture { fn get(&self, _req: Request<Body>) -> ResponseFuture {
match w(&self.chain).validate(true) { match w(&self.chain).validate(true) {
Ok(_) => response(StatusCode::OK, ""), Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response( Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
format!("validate failed: {}", e), format!("validate failed: {}", e),
@ -75,7 +75,7 @@ pub struct ChainCompactHandler {
impl Handler for ChainCompactHandler { impl Handler for ChainCompactHandler {
fn post(&self, _req: Request<Body>) -> ResponseFuture { fn post(&self, _req: Request<Body>) -> ResponseFuture {
match w(&self.chain).compact() { match w(&self.chain).compact() {
Ok(_) => response(StatusCode::OK, ""), Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response( Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
format!("compact failed: {}", e), format!("compact failed: {}", e),

View file

@ -97,6 +97,6 @@ impl Handler for PeerHandler {
_ => return response(StatusCode::BAD_REQUEST, "invalid command"), _ => return response(StatusCode::BAD_REQUEST, "invalid command"),
}; };
response(StatusCode::OK, "") response(StatusCode::OK, "{}")
} }
} }

View file

@ -487,11 +487,11 @@ where
), ),
"cancel_tx" => Box::new( "cancel_tx" => Box::new(
self.cancel_tx(req, api) self.cancel_tx(req, api)
.and_then(|_| ok(response(StatusCode::OK, ""))), .and_then(|_| ok(response(StatusCode::OK, "{}"))),
), ),
"post_tx" => Box::new( "post_tx" => Box::new(
self.post_tx(req, api) self.post_tx(req, api)
.and_then(|_| ok(response(StatusCode::OK, ""))), .and_then(|_| ok(response(StatusCode::OK, "{}"))),
), ),
_ => Box::new(err(ErrorKind::GenericError( _ => Box::new(err(ErrorKind::GenericError(
"Unknown error handling post request".to_owned(), "Unknown error handling post request".to_owned(),
@ -677,20 +677,31 @@ fn create_ok_response(json: &str) -> Response<Body> {
"access-control-allow-headers", "access-control-allow-headers",
"Content-Type, Authorization", "Content-Type, Authorization",
) )
.header(hyper::header::CONTENT_TYPE, "application/json")
.body(json.to_string().into()) .body(json.to_string().into())
.unwrap() .unwrap()
} }
/// Build a new hyper Response with the status code and body provided.
///
/// Whenever the status code is `StatusCode::OK` the text parameter should be
/// valid JSON as the content type header will be set to `application/json'
fn response<T: Into<Body>>(status: StatusCode, text: T) -> Response<Body> { fn response<T: Into<Body>>(status: StatusCode, text: T) -> Response<Body> {
Response::builder() let mut builder = &mut Response::builder();
builder = builder
.status(status) .status(status)
.header("access-control-allow-origin", "*") .header("access-control-allow-origin", "*")
.header( .header(
"access-control-allow-headers", "access-control-allow-headers",
"Content-Type, Authorization", "Content-Type, Authorization",
) );
.body(text.into())
.unwrap() if status == StatusCode::OK {
builder = builder.header(hyper::header::CONTENT_TYPE, "application/json");
}
builder.body(text.into()).unwrap()
} }
fn parse_params(req: &Request<Body>) -> HashMap<String, Vec<String>> { fn parse_params(req: &Request<Body>) -> HashMap<String, Vec<String>> {