diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index d337038d6..ada042bb2 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -64,8 +64,8 @@ extern crate actix; extern crate actix_web; use actix_web::prelude::*; -fn index(req: HttpRequest) -> Result { - Ok(httpcodes::HTTPOk.with_body("Hello world!")) +fn index(req: HttpRequest) -> &'static str { + "Hello world!" } fn main() { diff --git a/guide/src/qs_3.md b/guide/src/qs_3.md index 0e650edc1..64419071f 100644 --- a/guide/src/qs_3.md +++ b/guide/src/qs_3.md @@ -47,11 +47,11 @@ struct AppState { counter: Cell, } -fn index(req: HttpRequest) -> HttpResponse { +fn index(req: HttpRequest) -> String { let count = req.state().counter.get() + 1; // <- get count req.state().counter.set(count); // <- store new count in state - httpcodes::HTTPOk.with_body( // <- response with count - format!("Num of requests: {}", count)) + + format!("Request number: {}", count) // <- response with count } fn main() { diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 91b2b1350..d8d0cf6fe 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -3,6 +3,7 @@ use std::{io, mem, str, fmt}; use std::convert::Into; use cookie::CookieJar; +use bytes::{Bytes, BytesMut}; use http::{StatusCode, Version, HeaderMap, HttpTryFrom, Error as HttpError}; use http::header::{self, HeaderName, HeaderValue}; use serde_json; @@ -204,16 +205,6 @@ impl HttpResponse { } } -/// Helper conversion implementation -impl, E: Into> From> for HttpResponse { - fn from(res: Result) -> Self { - match res { - Ok(val) => val.into(), - Err(err) => err.into().into(), - } - } -} - impl From for Frame { fn from(resp: HttpResponse) -> Frame { Frame::Message(resp) @@ -458,10 +449,74 @@ fn parts<'a>(parts: &'a mut Option, err: &Option) -> Option<&' parts.as_mut() } +/// Helper converters +impl, E: Into> From> for HttpResponse { + fn from(res: Result) -> Self { + match res { + Ok(val) => val.into(), + Err(err) => err.into().into(), + } + } +} + +impl From<&'static str> for HttpResponse { + fn from(val: &'static str) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(val) + .into() + } +} + +impl From<&'static [u8]> for HttpResponse { + fn from(val: &'static [u8]) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(val) + .into() + } +} + +impl From for HttpResponse { + fn from(val: String) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(val) + .into() + } +} + +impl<'a> From<&'a String> for HttpResponse { + fn from(val: &'a String) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("text/plain; charset=utf-8") + .body(val) + .into() + } +} + +impl From for HttpResponse { + fn from(val: Bytes) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(val) + .into() + } +} + +impl From for HttpResponse { + fn from(val: BytesMut) -> HttpResponse { + HttpResponse::build(StatusCode::OK) + .content_type("application/octet-stream") + .body(val) + .into() + } +} + #[cfg(test)] mod tests { use super::*; - use bytes::Bytes; + use body::Binary; #[test] fn test_body() { @@ -518,4 +573,60 @@ mod tests { assert_eq!(ct, header::HeaderValue::from_static("text/json")); assert_eq!(*resp.body(), Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]"))); } + + impl Body { + pub(crate) fn binary(&self) -> Option<&Binary> { + match *self { + Body::Binary(ref bin) => Some(bin), + _ => None, + } + } + } + + #[test] + fn test_into_response() { + let resp: HttpResponse = "test".into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("text/plain; charset=utf-8")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from("test")); + + let resp: HttpResponse = b"test".as_ref().into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("application/octet-stream")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref())); + + let resp: HttpResponse = "test".to_owned().into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("text/plain; charset=utf-8")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned())); + + let resp: HttpResponse = (&"test".to_owned()).into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("text/plain; charset=utf-8")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from((&"test".to_owned()))); + + let b = Bytes::from_static(b"test"); + let resp: HttpResponse = b.into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("application/octet-stream")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from(Bytes::from_static(b"test"))); + + let b = BytesMut::from("test"); + let resp: HttpResponse = b.into(); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), + header::HeaderValue::from_static("application/octet-stream")); + assert_eq!(resp.status(), StatusCode::OK); + assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test"))); + } }