2018-01-28 07:03:03 +01:00
|
|
|
//! Http response
|
2017-12-04 05:09:46 +01:00
|
|
|
use std::{mem, str, fmt};
|
2018-01-12 01:22:27 +01:00
|
|
|
use std::io::Write;
|
2017-12-16 01:24:15 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::VecDeque;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-01-28 07:03:03 +01:00
|
|
|
use cookie::{Cookie, CookieJar};
|
2018-01-12 01:22:27 +01:00
|
|
|
use bytes::{Bytes, BytesMut, BufMut};
|
2018-03-08 02:40:13 +01:00
|
|
|
use futures::Stream;
|
2017-10-22 18:13:29 +02:00
|
|
|
use http::{StatusCode, Version, HeaderMap, HttpTryFrom, Error as HttpError};
|
2017-10-10 08:07:32 +02:00
|
|
|
use http::header::{self, HeaderName, HeaderValue};
|
2017-11-27 19:39:47 +01:00
|
|
|
use serde_json;
|
|
|
|
use serde::Serialize;
|
2018-03-09 03:05:13 +01:00
|
|
|
use prost::Message;
|
2017-12-08 01:40:29 +01:00
|
|
|
|
2017-10-24 08:25:32 +02:00
|
|
|
use body::Body;
|
2017-11-25 18:03:44 +01:00
|
|
|
use error::Error;
|
2017-12-14 18:43:42 +01:00
|
|
|
use handler::Responder;
|
2018-03-06 04:28:42 +01:00
|
|
|
use header::{Header, IntoHeaderValue, ContentEncoding};
|
2017-12-03 02:14:55 +01:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-15 07:52:38 +02:00
|
|
|
|
|
|
|
/// Represents various types of connection
|
2017-10-08 06:48:00 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
|
|
pub enum ConnectionType {
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Close connection after response
|
2017-10-08 06:48:00 +02:00
|
|
|
Close,
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Keep connection alive after response
|
2017-10-08 06:48:00 +02:00
|
|
|
KeepAlive,
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Connection is upgraded to different type
|
2017-10-08 06:48:00 +02:00
|
|
|
Upgrade,
|
|
|
|
}
|
|
|
|
|
2017-12-16 01:24:15 +01:00
|
|
|
/// An HTTP Response
|
|
|
|
pub struct HttpResponse(Option<Box<InnerHttpResponse>>);
|
|
|
|
|
|
|
|
impl Drop for HttpResponse {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(inner) = self.0.take() {
|
|
|
|
Pool::release(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
impl HttpResponse {
|
2017-10-11 01:03:32 +02:00
|
|
|
|
2017-12-16 01:24:15 +01:00
|
|
|
#[inline(always)]
|
2017-12-16 05:00:12 +01:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
2017-12-16 01:24:15 +01:00
|
|
|
fn get_ref(&self) -> &InnerHttpResponse {
|
|
|
|
self.0.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2017-12-16 05:00:12 +01:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
2017-12-16 01:24:15 +01:00
|
|
|
fn get_mut(&mut self) -> &mut InnerHttpResponse {
|
|
|
|
self.0.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
2017-11-27 07:31:29 +01:00
|
|
|
/// Create http response builder with specific status.
|
2017-10-11 01:03:32 +02:00
|
|
|
#[inline]
|
2017-11-27 07:31:29 +01:00
|
|
|
pub fn build(status: StatusCode) -> HttpResponseBuilder {
|
2017-10-15 23:17:41 +02:00
|
|
|
HttpResponseBuilder {
|
2017-12-16 05:00:12 +01:00
|
|
|
response: Some(Pool::get(status)),
|
2017-10-11 01:03:32 +02:00
|
|
|
err: None,
|
2017-12-14 01:44:35 +01:00
|
|
|
cookies: None,
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Constructs a response
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn new(status: StatusCode, body: Body) -> HttpResponse {
|
2017-12-16 05:00:12 +01:00
|
|
|
HttpResponse(Some(Pool::with_body(status, body)))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 18:03:44 +01:00
|
|
|
/// Constructs a error response
|
|
|
|
#[inline]
|
|
|
|
pub fn from_error(error: Error) -> HttpResponse {
|
|
|
|
let mut resp = error.cause().error_response();
|
2017-12-16 01:24:15 +01:00
|
|
|
resp.get_mut().error = Some(error);
|
2017-11-25 18:03:44 +01:00
|
|
|
resp
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The source `error` for this response
|
|
|
|
#[inline]
|
|
|
|
pub fn error(&self) -> Option<&Error> {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().error.as_ref()
|
2017-11-25 18:03:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Get the HTTP version of this response
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2017-10-11 02:14:30 +02:00
|
|
|
pub fn version(&self) -> Option<Version> {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().version
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Get the headers from the response
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2017-10-10 08:07:32 +02:00
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2017-12-16 01:24:15 +01:00
|
|
|
&self.get_ref().headers
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Get a mutable reference to the headers
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2017-10-10 08:07:32 +02:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2017-12-16 01:24:15 +01:00
|
|
|
&mut self.get_mut().headers
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Get the response status code
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().status
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Set the `StatusCode` for this response
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn status_mut(&mut self) -> &mut StatusCode {
|
2017-12-16 01:24:15 +01:00
|
|
|
&mut self.get_mut().status
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Get custom reason for the response
|
2017-10-14 19:01:53 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn reason(&self) -> &str {
|
2017-12-16 01:24:15 +01:00
|
|
|
if let Some(reason) = self.get_ref().reason {
|
2017-10-14 19:01:53 +02:00
|
|
|
reason
|
|
|
|
} else {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().status.canonical_reason().unwrap_or("<unknown status code>")
|
2017-10-14 19:01:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 20:39:26 +01:00
|
|
|
/// Set the custom reason for the response
|
2017-10-08 06:48:00 +02:00
|
|
|
#[inline]
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn set_reason(&mut self, reason: &'static str) -> &mut Self {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_mut().reason = Some(reason);
|
2017-10-08 06:48:00 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set connection type
|
2017-10-14 19:01:53 +02:00
|
|
|
pub fn set_connection_type(&mut self, conn: ConnectionType) -> &mut Self {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_mut().connection_type = Some(conn);
|
2017-10-08 06:48:00 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Connection upgrade status
|
2018-01-01 02:26:32 +01:00
|
|
|
#[inline]
|
2017-10-08 06:48:00 +02:00
|
|
|
pub fn upgrade(&self) -> bool {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().connection_type == Some(ConnectionType::Upgrade)
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
/// Keep-alive status for this connection
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn keep_alive(&self) -> Option<bool> {
|
2017-12-16 01:24:15 +01:00
|
|
|
if let Some(ct) = self.get_ref().connection_type {
|
2017-10-23 07:54:11 +02:00
|
|
|
match ct {
|
|
|
|
ConnectionType::KeepAlive => Some(true),
|
|
|
|
ConnectionType::Close | ConnectionType::Upgrade => Some(false),
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-10-11 01:03:32 +02:00
|
|
|
None
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
/// is chunked encoding enabled
|
2018-01-01 02:26:32 +01:00
|
|
|
#[inline]
|
2018-01-14 01:17:33 +01:00
|
|
|
pub fn chunked(&self) -> Option<bool> {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().chunked
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 23:56:38 +01:00
|
|
|
/// Content encoding
|
2018-01-01 02:26:32 +01:00
|
|
|
#[inline]
|
2018-02-19 07:23:17 +01:00
|
|
|
pub fn content_encoding(&self) -> Option<ContentEncoding> {
|
2018-01-21 17:31:46 +01:00
|
|
|
self.get_ref().encoding
|
2017-11-06 23:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set content encoding
|
|
|
|
pub fn set_content_encoding(&mut self, enc: ContentEncoding) -> &mut Self {
|
2018-02-19 07:23:17 +01:00
|
|
|
self.get_mut().encoding = Some(enc);
|
2017-11-06 23:56:38 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Get body os this response
|
2018-01-01 02:26:32 +01:00
|
|
|
#[inline]
|
2017-10-07 06:48:14 +02:00
|
|
|
pub fn body(&self) -> &Body {
|
2017-12-16 01:24:15 +01:00
|
|
|
&self.get_ref().body
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 06:48:00 +02:00
|
|
|
/// Set a body
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn set_body<B: Into<Body>>(&mut self, body: B) {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_mut().body = body.into();
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Set a body and return previous body value
|
2017-10-08 06:48:00 +02:00
|
|
|
pub fn replace_body<B: Into<Body>>(&mut self, body: B) -> Body {
|
2017-12-16 01:24:15 +01:00
|
|
|
mem::replace(&mut self.get_mut().body, body.into())
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-11-10 21:29:54 +01:00
|
|
|
|
|
|
|
/// Size of response in bytes, excluding HTTP headers
|
|
|
|
pub fn response_size(&self) -> u64 {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().response_size
|
2017-11-10 21:29:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set content encoding
|
|
|
|
pub(crate) fn set_response_size(&mut self, size: u64) {
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_mut().response_size = size;
|
2017-11-10 21:29:54 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-10-11 01:03:32 +02:00
|
|
|
|
2017-11-09 04:31:25 +01:00
|
|
|
impl fmt::Debug for HttpResponse {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let res = write!(f, "\nHttpResponse {:?} {}{}\n",
|
2017-12-16 01:24:15 +01:00
|
|
|
self.get_ref().version, self.get_ref().status,
|
|
|
|
self.get_ref().reason.unwrap_or(""));
|
|
|
|
let _ = write!(f, " encoding: {:?}\n", self.get_ref().encoding);
|
2017-11-09 04:31:25 +01:00
|
|
|
let _ = write!(f, " headers:\n");
|
2018-03-08 00:41:46 +01:00
|
|
|
for (key, val) in self.get_ref().headers.iter() {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, val);
|
2017-11-09 04:31:25 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
/// An HTTP response builder
|
|
|
|
///
|
|
|
|
/// This type can be used to construct an instance of `HttpResponse` through a
|
|
|
|
/// builder-like pattern.
|
|
|
|
#[derive(Debug)]
|
2017-10-15 23:17:41 +02:00
|
|
|
pub struct HttpResponseBuilder {
|
2017-12-16 01:24:15 +01:00
|
|
|
response: Option<Box<InnerHttpResponse>>,
|
2017-10-22 18:13:29 +02:00
|
|
|
err: Option<HttpError>,
|
2017-12-14 01:44:35 +01:00
|
|
|
cookies: Option<CookieJar>,
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 23:17:41 +02:00
|
|
|
impl HttpResponseBuilder {
|
2018-03-06 09:43:25 +01:00
|
|
|
/// Set HTTP status code of this response.
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&mut self, status: StatusCode) -> &mut Self {
|
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
parts.status = status;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-21 08:19:21 +01:00
|
|
|
/// Set HTTP version of this response.
|
|
|
|
///
|
|
|
|
/// By default response's http version depends on request's version.
|
2017-10-11 01:03:32 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn version(&mut self, version: Version) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-10-11 02:14:30 +02:00
|
|
|
parts.version = Some(version);
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
/// Set a header.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// #
|
|
|
|
/// use actix_web::header;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
|
|
|
/// Ok(HttpOk.build()
|
|
|
|
/// .set(header::IfModifiedSince("Sun, 07 Nov 1994 08:48:37 GMT".parse()?))
|
|
|
|
/// .finish()?)
|
|
|
|
/// }
|
|
|
|
/// fn main() {}
|
|
|
|
/// ```
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self
|
|
|
|
{
|
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
match hdr.try_into() {
|
|
|
|
Ok(value) => { parts.headers.append(H::name(), value); }
|
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
/// Set a header.
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate http;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// #
|
|
|
|
/// use http::header;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
2018-03-02 04:12:59 +01:00
|
|
|
/// Ok(HttpOk.build()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .header("X-TEST", "value")
|
|
|
|
/// .header(header::CONTENT_TYPE, "application/json")
|
|
|
|
/// .finish()?)
|
|
|
|
/// }
|
|
|
|
/// fn main() {}
|
|
|
|
/// ```
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
|
|
where HeaderName: HttpTryFrom<K>,
|
2018-03-06 01:45:54 +01:00
|
|
|
V: IntoHeaderValue,
|
2017-10-11 01:03:32 +02:00
|
|
|
{
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-10-11 01:03:32 +02:00
|
|
|
match HeaderName::try_from(key) {
|
|
|
|
Ok(key) => {
|
2018-03-06 01:45:54 +01:00
|
|
|
match value.try_into() {
|
2017-10-11 01:03:32 +02:00
|
|
|
Ok(value) => { parts.headers.append(key, value); }
|
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the custom reason for the response.
|
|
|
|
#[inline]
|
|
|
|
pub fn reason(&mut self, reason: &'static str) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-10-11 01:03:32 +02:00
|
|
|
parts.reason = Some(reason);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-11-06 10:24:49 +01:00
|
|
|
/// Set content encoding.
|
|
|
|
///
|
|
|
|
/// By default `ContentEncoding::Auto` is used, which automatically
|
2017-11-06 23:56:38 +01:00
|
|
|
/// negotiates content encoding based on request's `Accept-Encoding` headers.
|
2017-11-30 16:42:02 +01:00
|
|
|
/// To enforce specific encoding, use specific ContentEncoding` value.
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-11-06 10:24:49 +01:00
|
|
|
pub fn content_encoding(&mut self, enc: ContentEncoding) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2018-02-19 07:23:17 +01:00
|
|
|
parts.encoding = Some(enc);
|
2017-11-06 10:24:49 +01:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
/// Set connection type
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-12-21 08:19:21 +01:00
|
|
|
#[doc(hidden)]
|
2017-10-14 19:01:53 +02:00
|
|
|
pub fn connection_type(&mut self, conn: ConnectionType) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-10-11 01:03:32 +02:00
|
|
|
parts.connection_type = Some(conn);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-14 19:01:53 +02:00
|
|
|
/// Set connection type to Upgrade
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-12-21 08:19:21 +01:00
|
|
|
#[doc(hidden)]
|
2017-10-14 19:01:53 +02:00
|
|
|
pub fn upgrade(&mut self) -> &mut Self {
|
|
|
|
self.connection_type(ConnectionType::Upgrade)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Force close connection, even if it is marked as keep-alive
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-10-14 19:01:53 +02:00
|
|
|
pub fn force_close(&mut self) -> &mut Self {
|
|
|
|
self.connection_type(ConnectionType::Close)
|
|
|
|
}
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
/// Enables automatic chunked transfer encoding
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-12-14 07:36:28 +01:00
|
|
|
pub fn chunked(&mut self) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2018-01-14 01:17:33 +01:00
|
|
|
parts.chunked = Some(true);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Force disable chunked encoding
|
|
|
|
#[inline]
|
|
|
|
pub fn no_chunking(&mut self) -> &mut Self {
|
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
parts.chunked = Some(false);
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-14 19:01:53 +02:00
|
|
|
/// Set response content type
|
2017-12-04 05:47:15 +01:00
|
|
|
#[inline]
|
2017-10-14 19:01:53 +02:00
|
|
|
pub fn content_type<V>(&mut self, value: V) -> &mut Self
|
|
|
|
where HeaderValue: HttpTryFrom<V>
|
|
|
|
{
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-10-14 19:01:53 +02:00
|
|
|
match HeaderValue::try_from(value) {
|
|
|
|
Ok(value) => { parts.headers.insert(header::CONTENT_TYPE, value); },
|
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-12 01:22:27 +01:00
|
|
|
/// Set content length
|
|
|
|
#[inline]
|
|
|
|
pub fn content_length(&mut self, len: u64) -> &mut Self {
|
|
|
|
let mut wrt = BytesMut::new().writer();
|
|
|
|
let _ = write!(wrt, "{}", len);
|
|
|
|
self.header(header::CONTENT_LENGTH, wrt.get_mut().take().freeze())
|
|
|
|
}
|
|
|
|
|
2017-10-14 19:40:58 +02:00
|
|
|
/// Set a cookie
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// #
|
|
|
|
/// use actix_web::headers::Cookie;
|
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
2018-03-02 04:12:59 +01:00
|
|
|
/// Ok(HttpOk.build()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .cookie(
|
|
|
|
/// Cookie::build("name", "value")
|
|
|
|
/// .domain("www.rust-lang.org")
|
|
|
|
/// .path("/")
|
|
|
|
/// .secure(true)
|
|
|
|
/// .http_only(true)
|
|
|
|
/// .finish())
|
|
|
|
/// .finish()?)
|
|
|
|
/// }
|
|
|
|
/// fn main() {}
|
|
|
|
/// ```
|
2017-10-14 19:40:58 +02:00
|
|
|
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
if self.cookies.is_none() {
|
|
|
|
let mut jar = CookieJar::new();
|
|
|
|
jar.add(cookie.into_owned());
|
|
|
|
self.cookies = Some(jar)
|
|
|
|
} else {
|
|
|
|
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
2017-10-14 19:40:58 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-21 08:19:21 +01:00
|
|
|
/// Remove cookie, cookie has to be cookie from `HttpRequest::cookies()` method.
|
2017-10-14 19:40:58 +02:00
|
|
|
pub fn del_cookie<'a>(&mut self, cookie: &Cookie<'a>) -> &mut Self {
|
2017-12-14 01:44:35 +01:00
|
|
|
{
|
|
|
|
if self.cookies.is_none() {
|
|
|
|
self.cookies = Some(CookieJar::new())
|
2017-12-13 06:32:58 +01:00
|
|
|
}
|
2017-12-14 01:44:35 +01:00
|
|
|
let jar = self.cookies.as_mut().unwrap();
|
2017-10-14 19:40:58 +02:00
|
|
|
let cookie = cookie.clone().into_owned();
|
2017-12-13 06:32:58 +01:00
|
|
|
jar.add_original(cookie.clone());
|
|
|
|
jar.remove(cookie);
|
2017-10-14 19:40:58 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-21 08:19:21 +01:00
|
|
|
/// This method calls provided closure with builder reference if value is true.
|
2017-10-29 22:51:02 +01:00
|
|
|
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
|
2018-01-10 22:41:33 +01:00
|
|
|
where F: FnOnce(&mut HttpResponseBuilder)
|
2017-10-29 22:51:02 +01:00
|
|
|
{
|
|
|
|
if value {
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-10 08:55:42 +01:00
|
|
|
/// This method calls provided closure with builder reference if value is Some.
|
2018-01-21 01:36:57 +01:00
|
|
|
pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Self
|
|
|
|
where F: FnOnce(T, &mut HttpResponseBuilder)
|
2018-01-10 08:55:42 +01:00
|
|
|
{
|
|
|
|
if let Some(val) = value {
|
|
|
|
f(val, self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:39:47 +01:00
|
|
|
/// Set a body and generate `HttpResponse`.
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
2017-11-27 19:39:47 +01:00
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
2017-10-22 18:13:29 +02:00
|
|
|
pub fn body<B: Into<Body>>(&mut self, body: B) -> Result<HttpResponse, HttpError> {
|
2017-10-11 01:03:32 +02:00
|
|
|
if let Some(e) = self.err.take() {
|
|
|
|
return Err(e)
|
|
|
|
}
|
2017-12-14 01:44:35 +01:00
|
|
|
let mut response = self.response.take().expect("cannot reuse response builder");
|
|
|
|
if let Some(ref jar) = self.cookies {
|
2017-12-13 06:32:58 +01:00
|
|
|
for cookie in jar.delta() {
|
2017-12-14 01:44:35 +01:00
|
|
|
response.headers.append(
|
2017-12-13 06:32:58 +01:00
|
|
|
header::SET_COOKIE,
|
|
|
|
HeaderValue::from_str(&cookie.to_string())?);
|
|
|
|
}
|
2017-10-14 19:40:58 +02:00
|
|
|
}
|
2017-12-14 01:44:35 +01:00
|
|
|
response.body = body.into();
|
2017-12-16 05:00:12 +01:00
|
|
|
Ok(HttpResponse(Some(response)))
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
2017-10-24 08:52:20 +02:00
|
|
|
|
2018-03-08 02:40:13 +01:00
|
|
|
/// Set a streaming body and generate `HttpResponse`.
|
|
|
|
///
|
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
|
|
|
pub fn streaming<S>(&mut self, stream: S) -> Result<HttpResponse, HttpError>
|
|
|
|
where S: Stream<Item=Bytes, Error=Error> + 'static,
|
|
|
|
{
|
|
|
|
self.body(Body::Streaming(Box::new(stream)))
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:39:47 +01:00
|
|
|
/// Set a json body and generate `HttpResponse`
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
2017-11-27 19:39:47 +01:00
|
|
|
pub fn json<T: Serialize>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
|
|
|
let body = serde_json::to_string(&value)?;
|
|
|
|
|
2017-12-14 01:44:35 +01:00
|
|
|
let contains = if let Some(parts) = parts(&mut self.response, &self.err) {
|
2017-11-27 19:39:47 +01:00
|
|
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
if !contains {
|
|
|
|
self.header(header::CONTENT_TYPE, "application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.body(body)?)
|
|
|
|
}
|
|
|
|
|
2018-03-09 03:05:13 +01:00
|
|
|
pub fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
|
|
|
let mut body = Vec::new();
|
|
|
|
value.encode(&mut body)?;
|
|
|
|
|
|
|
|
let contains = if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
if !contains {
|
|
|
|
self.header(header::CONTENT_TYPE, "application/protobuf");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.body(body)?)
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:39:47 +01:00
|
|
|
/// Set an empty body and generate `HttpResponse`
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
2017-10-24 08:52:20 +02:00
|
|
|
pub fn finish(&mut self) -> Result<HttpResponse, HttpError> {
|
|
|
|
self.body(Body::Empty)
|
|
|
|
}
|
2018-01-01 02:26:32 +01:00
|
|
|
|
|
|
|
/// This method construct new `HttpResponseBuilder`
|
|
|
|
pub fn take(&mut self) -> HttpResponseBuilder {
|
|
|
|
HttpResponseBuilder {
|
|
|
|
response: self.response.take(),
|
|
|
|
err: self.err.take(),
|
|
|
|
cookies: self.cookies.take(),
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
#[inline]
|
2017-12-16 01:24:15 +01:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
|
|
|
|
fn parts<'a>(parts: &'a mut Option<Box<InnerHttpResponse>>, err: &Option<HttpError>)
|
|
|
|
-> Option<&'a mut Box<InnerHttpResponse>>
|
2017-10-11 01:03:32 +02:00
|
|
|
{
|
|
|
|
if err.is_some() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
parts.as_mut()
|
|
|
|
}
|
2017-10-23 07:54:11 +02:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
/// Helper converters
|
|
|
|
impl<I: Into<HttpResponse>, E: Into<Error>> From<Result<I, E>> for HttpResponse {
|
|
|
|
fn from(res: Result<I, E>) -> Self {
|
|
|
|
match res {
|
|
|
|
Ok(val) => val.into(),
|
|
|
|
Err(err) => err.into().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 18:17:00 +01:00
|
|
|
impl From<HttpResponseBuilder> for HttpResponse {
|
|
|
|
fn from(mut builder: HttpResponseBuilder) -> Self {
|
|
|
|
builder.finish().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for HttpResponseBuilder {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
#[inline]
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(mut self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 23:22:04 +01:00
|
|
|
self.finish()
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl From<&'static str> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: &'static str) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for &'static str {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl From<&'static [u8]> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: &'static [u8]) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for &'static [u8] {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl From<String> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: String) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for String {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl<'a> From<&'a String> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: &'a String) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl<'a> Responder for &'a String {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl From<Bytes> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: Bytes) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for Bytes {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl From<BytesMut> for HttpResponse {
|
2017-11-29 18:17:00 +01:00
|
|
|
fn from(val: BytesMut) -> Self {
|
2017-11-28 22:52:53 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for BytesMut {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = HttpError;
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, HttpError> {
|
2017-12-03 02:14:55 +01:00
|
|
|
HttpResponse::build(StatusCode::OK)
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct InnerHttpResponse {
|
|
|
|
version: Option<Version>,
|
|
|
|
headers: HeaderMap,
|
|
|
|
status: StatusCode,
|
|
|
|
reason: Option<&'static str>,
|
|
|
|
body: Body,
|
2018-01-14 01:17:33 +01:00
|
|
|
chunked: Option<bool>,
|
2018-02-19 07:23:17 +01:00
|
|
|
encoding: Option<ContentEncoding>,
|
2017-12-16 05:00:12 +01:00
|
|
|
connection_type: Option<ConnectionType>,
|
|
|
|
response_size: u64,
|
|
|
|
error: Option<Error>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InnerHttpResponse {
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn new(status: StatusCode, body: Body) -> InnerHttpResponse {
|
|
|
|
InnerHttpResponse {
|
2018-02-26 23:33:56 +01:00
|
|
|
status,
|
|
|
|
body,
|
2017-12-16 05:00:12 +01:00
|
|
|
version: None,
|
2017-12-16 07:49:48 +01:00
|
|
|
headers: HeaderMap::with_capacity(16),
|
2017-12-16 05:00:12 +01:00
|
|
|
reason: None,
|
2018-01-14 01:17:33 +01:00
|
|
|
chunked: None,
|
2018-02-19 07:23:17 +01:00
|
|
|
encoding: None,
|
2017-12-16 05:00:12 +01:00
|
|
|
connection_type: None,
|
|
|
|
response_size: 0,
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal use only! unsafe
|
|
|
|
struct Pool(VecDeque<Box<InnerHttpResponse>>);
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
thread_local!(static POOL: RefCell<Pool> =
|
|
|
|
RefCell::new(Pool(VecDeque::with_capacity(128))));
|
2017-12-16 05:00:12 +01:00
|
|
|
|
|
|
|
impl Pool {
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-12-16 05:00:12 +01:00
|
|
|
fn get(status: StatusCode) -> Box<InnerHttpResponse> {
|
|
|
|
POOL.with(|pool| {
|
|
|
|
if let Some(mut resp) = pool.borrow_mut().0.pop_front() {
|
|
|
|
resp.body = Body::Empty;
|
|
|
|
resp.status = status;
|
|
|
|
resp
|
|
|
|
} else {
|
|
|
|
Box::new(InnerHttpResponse::new(status, Body::Empty))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-12-16 05:00:12 +01:00
|
|
|
fn with_body(status: StatusCode, body: Body) -> Box<InnerHttpResponse> {
|
|
|
|
POOL.with(|pool| {
|
|
|
|
if let Some(mut resp) = pool.borrow_mut().0.pop_front() {
|
|
|
|
resp.status = status;
|
2018-01-04 03:21:34 +01:00
|
|
|
resp.body = body;
|
2017-12-16 05:00:12 +01:00
|
|
|
resp
|
|
|
|
} else {
|
|
|
|
Box::new(InnerHttpResponse::new(status, body))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline(always)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(boxed_local, inline_always))]
|
2017-12-16 05:00:12 +01:00
|
|
|
fn release(mut inner: Box<InnerHttpResponse>) {
|
|
|
|
POOL.with(|pool| {
|
|
|
|
let v = &mut pool.borrow_mut().0;
|
|
|
|
if v.len() < 128 {
|
|
|
|
inner.headers.clear();
|
|
|
|
inner.version = None;
|
2018-01-14 01:17:33 +01:00
|
|
|
inner.chunked = None;
|
2017-12-16 05:00:12 +01:00
|
|
|
inner.reason = None;
|
2018-02-19 07:23:17 +01:00
|
|
|
inner.encoding = None;
|
2017-12-16 05:00:12 +01:00
|
|
|
inner.connection_type = None;
|
|
|
|
inner.response_size = 0;
|
|
|
|
inner.error = None;
|
|
|
|
v.push_front(inner);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 07:54:11 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-12-13 20:16:26 +01:00
|
|
|
use std::str::FromStr;
|
|
|
|
use time::Duration;
|
|
|
|
use http::{Method, Uri};
|
2018-03-06 09:43:25 +01:00
|
|
|
use http::header::{COOKIE, CONTENT_TYPE, HeaderValue};
|
2017-11-28 22:52:53 +01:00
|
|
|
use body::Binary;
|
2018-03-06 04:28:42 +01:00
|
|
|
use {header, httpcodes};
|
2017-12-13 20:16:26 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
2018-03-08 07:40:46 +01:00
|
|
|
let resp = HttpResponse::Ok()
|
|
|
|
.header(COOKIE, HeaderValue::from_static("cookie1=value1; "))
|
|
|
|
.header(COOKIE, HeaderValue::from_static("cookie2=value2; "))
|
|
|
|
.finish().unwrap();
|
2017-12-13 20:16:26 +01:00
|
|
|
let dbg = format!("{:?}", resp);
|
|
|
|
assert!(dbg.contains("HttpResponse"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_response_cookies() {
|
|
|
|
let mut headers = HeaderMap::new();
|
2018-03-08 20:16:54 +01:00
|
|
|
headers.insert(COOKIE, HeaderValue::from_static("cookie1=value1"));
|
|
|
|
headers.insert(COOKIE, HeaderValue::from_static("cookie2=value2"));
|
2017-12-13 20:16:26 +01:00
|
|
|
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
|
|
|
|
let cookies = req.cookies().unwrap();
|
|
|
|
|
2018-03-02 04:12:59 +01:00
|
|
|
let resp = httpcodes::HttpOk
|
2017-12-13 20:16:26 +01:00
|
|
|
.build()
|
2018-03-06 04:28:42 +01:00
|
|
|
.cookie(header::Cookie::build("name", "value")
|
2017-12-13 20:16:26 +01:00
|
|
|
.domain("www.rust-lang.org")
|
|
|
|
.path("/test")
|
|
|
|
.http_only(true)
|
|
|
|
.max_age(Duration::days(1))
|
|
|
|
.finish())
|
|
|
|
.del_cookie(&cookies[0])
|
|
|
|
.body(Body::Empty);
|
|
|
|
|
|
|
|
assert!(resp.is_ok());
|
|
|
|
let resp = resp.unwrap();
|
|
|
|
|
|
|
|
let mut val: Vec<_> = resp.headers().get_all("Set-Cookie")
|
|
|
|
.iter().map(|v| v.to_str().unwrap().to_owned()).collect();
|
|
|
|
val.sort();
|
2018-03-08 20:16:54 +01:00
|
|
|
assert!(val[0].starts_with("cookie2=; Max-Age=0;"));
|
2017-12-13 20:16:26 +01:00
|
|
|
assert_eq!(
|
|
|
|
val[1],"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400");
|
|
|
|
}
|
2017-10-23 07:54:11 +02:00
|
|
|
|
2017-11-28 23:29:22 +01:00
|
|
|
#[test]
|
|
|
|
fn test_basic_builder() {
|
|
|
|
let resp = HttpResponse::Ok()
|
2017-12-04 05:09:46 +01:00
|
|
|
.header("X-TEST", "value")
|
2017-11-28 23:29:22 +01:00
|
|
|
.version(Version::HTTP_10)
|
|
|
|
.finish().unwrap();
|
|
|
|
assert_eq!(resp.version(), Some(Version::HTTP_10));
|
2017-12-21 08:36:52 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2017-11-28 23:29:22 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 07:54:11 +02:00
|
|
|
#[test]
|
|
|
|
fn test_upgrade() {
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2017-10-23 07:54:11 +02:00
|
|
|
.upgrade().body(Body::Empty).unwrap();
|
|
|
|
assert!(resp.upgrade())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_force_close() {
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2017-10-23 07:54:11 +02:00
|
|
|
.force_close().body(Body::Empty).unwrap();
|
|
|
|
assert!(!resp.keep_alive().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_content_type() {
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2017-10-23 07:54:11 +02:00
|
|
|
.content_type("text/plain").body(Body::Empty).unwrap();
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
2017-10-23 07:54:11 +02:00
|
|
|
}
|
2017-11-06 23:56:38 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_content_encoding() {
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK).finish().unwrap();
|
2018-02-19 07:23:17 +01:00
|
|
|
assert_eq!(resp.content_encoding(), None);
|
2017-11-06 23:56:38 +01:00
|
|
|
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2017-11-06 23:56:38 +01:00
|
|
|
.content_encoding(ContentEncoding::Br).finish().unwrap();
|
2018-02-19 07:23:17 +01:00
|
|
|
assert_eq!(resp.content_encoding(), Some(ContentEncoding::Br));
|
2017-11-07 01:23:58 +01:00
|
|
|
}
|
2017-11-27 19:39:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json() {
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
|
|
|
.json(vec!["v1", "v2", "v3"]).unwrap();
|
2018-03-06 04:28:42 +01:00
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
2018-03-06 09:43:25 +01:00
|
|
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
2017-11-27 19:39:47 +01:00
|
|
|
assert_eq!(*resp.body(), Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json_ct() {
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2018-03-06 04:28:42 +01:00
|
|
|
.header(CONTENT_TYPE, "text/json")
|
2017-11-27 19:39:47 +01:00
|
|
|
.json(vec!["v1", "v2", "v3"]).unwrap();
|
2018-03-06 04:28:42 +01:00
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
2018-03-06 09:43:25 +01:00
|
|
|
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
2017-11-27 19:39:47 +01:00
|
|
|
assert_eq!(*resp.body(), Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]")));
|
|
|
|
}
|
2017-11-28 22:52:53 +01:00
|
|
|
|
|
|
|
impl Body {
|
|
|
|
pub(crate) fn binary(&self) -> Option<&Binary> {
|
|
|
|
match *self {
|
|
|
|
Body::Binary(ref bin) => Some(bin),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2017-12-03 23:22:04 +01:00
|
|
|
let req = HttpRequest::default();
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = "test".into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test"));
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = "test".respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test"));
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = b"test".as_ref().into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref()));
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = b"test".as_ref().respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref()));
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = "test".to_owned().into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned()));
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = "test".to_owned().respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned()));
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = (&"test".to_owned()).into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-01-26 06:50:28 +01:00
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned()));
|
2017-11-28 22:52:53 +01:00
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = (&"test".to_owned()).respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-01-26 06:50:28 +01:00
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(&"test".to_owned()));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let b = Bytes::from_static(b"test");
|
|
|
|
let resp: HttpResponse = b.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(Bytes::from_static(b"test")));
|
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
let b = Bytes::from_static(b"test");
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = b.respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(Bytes::from_static(b"test")));
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let b = BytesMut::from("test");
|
|
|
|
let resp: HttpResponse = b.into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test")));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
|
|
|
let b = BytesMut::from("test");
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp: HttpResponse = b.respond_to(req.clone()).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-03-06 04:28:42 +01:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(),
|
2018-03-06 09:43:25 +01:00
|
|
|
HeaderValue::from_static("application/octet-stream"));
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test")));
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
2017-10-23 07:54:11 +02:00
|
|
|
}
|