2018-01-28 07:03:03 +01:00
|
|
|
//! Http response
|
2018-06-25 05:08:28 +02:00
|
|
|
use std::cell::RefCell;
|
2017-12-16 01:24:15 +01:00
|
|
|
use std::collections::VecDeque;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::io::Write;
|
|
|
|
use std::{fmt, mem, str};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use bytes::{BufMut, Bytes, BytesMut};
|
2018-01-28 07:03:03 +01:00
|
|
|
use cookie::{Cookie, CookieJar};
|
2018-03-08 02:40:13 +01:00
|
|
|
use futures::Stream;
|
2017-10-10 08:07:32 +02:00
|
|
|
use http::header::{self, HeaderName, HeaderValue};
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::{Error as HttpError, HeaderMap, HttpTryFrom, StatusCode, Version};
|
2017-11-27 19:39:47 +01:00
|
|
|
use serde::Serialize;
|
2018-04-14 01:02:01 +02:00
|
|
|
use serde_json;
|
2017-12-08 01:40:29 +01:00
|
|
|
|
2017-10-24 08:25:32 +02:00
|
|
|
use body::Body;
|
2018-04-14 01:02:01 +02:00
|
|
|
use client::ClientResponse;
|
2017-11-25 18:03:44 +01:00
|
|
|
use error::Error;
|
2017-12-14 18:43:42 +01:00
|
|
|
use handler::Responder;
|
2018-04-14 01:02:01 +02:00
|
|
|
use header::{ContentEncoding, Header, IntoHeaderValue};
|
2018-03-22 04:04:35 +01:00
|
|
|
use httpmessage::HttpMessage;
|
2018-04-14 01:02:01 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2018-03-09 19:00:15 +01:00
|
|
|
/// max write buffer size 64k
|
|
|
|
pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536;
|
|
|
|
|
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
|
2018-06-25 06:10:02 +02:00
|
|
|
pub struct HttpResponse(Box<InnerHttpResponse>, &'static HttpResponsePool);
|
2017-12-16 01:24:15 +01:00
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
impl HttpResponse {
|
2018-06-25 05:08:28 +02:00
|
|
|
#[inline]
|
2017-12-16 01:24:15 +01:00
|
|
|
fn get_ref(&self) -> &InnerHttpResponse {
|
2018-06-25 05:08:28 +02:00
|
|
|
self.0.as_ref()
|
2017-12-16 01:24:15 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 05:08:28 +02:00
|
|
|
#[inline]
|
2017-12-16 01:24:15 +01:00
|
|
|
fn get_mut(&mut self) -> &mut InnerHttpResponse {
|
2018-06-25 05:08:28 +02:00
|
|
|
self.0.as_mut()
|
2017-12-16 01:24:15 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-23 05:14:57 +01:00
|
|
|
HttpResponsePool::get(status)
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 03:54:21 +01:00
|
|
|
/// Create http response builder
|
|
|
|
#[inline]
|
|
|
|
pub fn build_from<T: Into<HttpResponseBuilder>>(source: T) -> HttpResponseBuilder {
|
|
|
|
source.into()
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Constructs a response
|
2017-10-07 06:48:14 +02:00
|
|
|
#[inline]
|
2018-03-31 08:07:33 +02:00
|
|
|
pub fn new(status: StatusCode) -> HttpResponse {
|
|
|
|
HttpResponsePool::with_body(status, Body::Empty)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a response with body
|
|
|
|
#[inline]
|
|
|
|
pub fn with_body<B: Into<Body>>(status: StatusCode, body: B) -> HttpResponse {
|
|
|
|
HttpResponsePool::with_body(status, body.into())
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-05-29 18:11:10 +02:00
|
|
|
/// Constructs an error response
|
2017-11-25 18:03:44 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn from_error(error: Error) -> HttpResponse {
|
2018-06-07 18:53:27 +02:00
|
|
|
let mut resp = error.as_response_error().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
|
|
|
|
}
|
|
|
|
|
2018-03-30 23:30:24 +02:00
|
|
|
/// Convert `HttpResponse` to a `HttpResponseBuilder`
|
|
|
|
#[inline]
|
2018-06-25 05:08:28 +02:00
|
|
|
pub fn into_builder(self) -> HttpResponseBuilder {
|
2018-06-12 15:49:07 +02:00
|
|
|
// If this response has cookies, load them into a jar
|
|
|
|
let mut jar: Option<CookieJar> = None;
|
|
|
|
for c in self.cookies() {
|
|
|
|
if let Some(ref mut j) = jar {
|
|
|
|
j.add_original(c.into_owned());
|
|
|
|
} else {
|
|
|
|
let mut j = CookieJar::new();
|
|
|
|
j.add_original(c.into_owned());
|
|
|
|
jar = Some(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 23:30:24 +02:00
|
|
|
HttpResponseBuilder {
|
2018-06-25 06:10:02 +02:00
|
|
|
pool: self.1,
|
2018-06-25 05:08:28 +02:00
|
|
|
response: Some(self.0),
|
2018-03-30 23:30:24 +02:00
|
|
|
err: None,
|
2018-06-12 15:49:07 +02:00
|
|
|
cookies: jar,
|
2018-03-30 23:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 18:03:44 +01:00
|
|
|
/// 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-06-12 15:49:07 +02:00
|
|
|
/// Get an iterator for the cookies set by this response
|
|
|
|
#[inline]
|
|
|
|
pub fn cookies(&self) -> CookieIter {
|
|
|
|
CookieIter {
|
2018-06-14 08:37:19 +02:00
|
|
|
iter: self.get_ref().headers.get_all(header::SET_COOKIE).iter(),
|
2018-06-12 15:49:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a cookie to this response
|
|
|
|
#[inline]
|
2018-06-14 08:37:19 +02:00
|
|
|
pub fn add_cookie(&mut self, cookie: &Cookie) -> Result<(), HttpError> {
|
2018-06-12 15:49:07 +02:00
|
|
|
let h = &mut self.get_mut().headers;
|
|
|
|
HeaderValue::from_str(&cookie.to_string())
|
2018-06-14 08:37:19 +02:00
|
|
|
.map(|c| {
|
|
|
|
h.append(header::SET_COOKIE, c);
|
2018-08-23 18:48:01 +02:00
|
|
|
}).map_err(|e| e.into())
|
2018-06-12 15:49:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove all cookies with the given name from this response. Returns
|
|
|
|
/// the number of cookies removed.
|
|
|
|
#[inline]
|
|
|
|
pub fn del_cookie(&mut self, name: &str) -> usize {
|
|
|
|
let h = &mut self.get_mut().headers;
|
2018-06-14 08:37:19 +02:00
|
|
|
let vals: Vec<HeaderValue> = h
|
|
|
|
.get_all(header::SET_COOKIE)
|
2018-06-12 15:49:07 +02:00
|
|
|
.iter()
|
|
|
|
.map(|v| v.to_owned())
|
|
|
|
.collect();
|
|
|
|
h.remove(header::SET_COOKIE);
|
|
|
|
|
|
|
|
let mut count: usize = 0;
|
|
|
|
for v in vals {
|
|
|
|
if let Ok(s) = v.to_str() {
|
2018-07-17 07:38:18 +02:00
|
|
|
if let Ok(c) = Cookie::parse_encoded(s) {
|
2018-06-12 15:49:07 +02:00
|
|
|
if c.name() == name {
|
|
|
|
count += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.append(header::SET_COOKIE, v);
|
|
|
|
}
|
2018-06-14 08:37:19 +02:00
|
|
|
count
|
2018-06-12 15:49:07 +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 {
|
2018-04-29 18:09:08 +02: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
|
|
|
}
|
2018-03-09 19:00:15 +01:00
|
|
|
|
2018-10-06 07:16:12 +02:00
|
|
|
/// Get write buffer capacity
|
2018-03-09 19:00:15 +01:00
|
|
|
pub fn write_buffer_capacity(&self) -> usize {
|
|
|
|
self.get_ref().write_capacity
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set write buffer capacity
|
|
|
|
pub fn set_write_buffer_capacity(&mut self, cap: usize) {
|
|
|
|
self.get_mut().write_capacity = cap;
|
|
|
|
}
|
2018-06-12 03:52:54 +02:00
|
|
|
|
2018-06-25 06:10:02 +02:00
|
|
|
pub(crate) fn release(self) {
|
|
|
|
self.1.release(self.0);
|
|
|
|
}
|
|
|
|
|
2018-06-25 05:08:28 +02:00
|
|
|
pub(crate) fn into_parts(self) -> HttpResponseParts {
|
|
|
|
self.0.into_parts()
|
2018-06-12 03:52:54 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 06:45:24 +02:00
|
|
|
pub(crate) fn from_parts(parts: HttpResponseParts) -> HttpResponse {
|
2018-06-25 06:10:02 +02:00
|
|
|
HttpResponse(
|
|
|
|
Box::new(InnerHttpResponse::from_parts(parts)),
|
|
|
|
HttpResponsePool::get_pool(),
|
|
|
|
)
|
2018-06-12 03:52:54 +02: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 {
|
2018-04-14 01:02:01 +02:00
|
|
|
let res = writeln!(
|
|
|
|
f,
|
|
|
|
"\nHttpResponse {:?} {}{}",
|
|
|
|
self.get_ref().version,
|
|
|
|
self.get_ref().status,
|
|
|
|
self.get_ref().reason.unwrap_or("")
|
|
|
|
);
|
2018-04-09 23:25:30 +02:00
|
|
|
let _ = writeln!(f, " encoding: {:?}", self.get_ref().encoding);
|
|
|
|
let _ = writeln!(f, " headers:");
|
2018-03-08 00:41:46 +01:00
|
|
|
for (key, val) in self.get_ref().headers.iter() {
|
2018-04-09 23:25:30 +02:00
|
|
|
let _ = writeln!(f, " {:?}: {:?}", key, val);
|
2017-11-09 04:31:25 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 15:49:07 +02:00
|
|
|
pub struct CookieIter<'a> {
|
|
|
|
iter: header::ValueIter<'a, HeaderValue>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for CookieIter<'a> {
|
|
|
|
type Item = Cookie<'a>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Cookie<'a>> {
|
|
|
|
for v in self.iter.by_ref() {
|
2018-07-17 07:38:18 +02:00
|
|
|
if let Ok(c) = Cookie::parse_encoded(v.to_str().ok()?) {
|
2018-06-12 15:49:07 +02:00
|
|
|
return Some(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub struct HttpResponseBuilder {
|
2018-06-25 06:10:02 +02:00
|
|
|
pool: &'static HttpResponsePool,
|
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;
|
2018-06-01 18:37:14 +02:00
|
|
|
/// use actix_web::{http, HttpRequest, HttpResponse, Result};
|
2018-03-06 04:28:42 +01:00
|
|
|
///
|
|
|
|
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// Ok(HttpResponse::Ok()
|
2018-06-01 18:37:14 +02:00
|
|
|
/// .set(http::header::IfModifiedSince(
|
|
|
|
/// "Sun, 07 Nov 1994 08:48:37 GMT".parse()?,
|
|
|
|
/// ))
|
2018-03-31 08:07:33 +02:00
|
|
|
/// .finish())
|
2018-03-06 04:28:42 +01:00
|
|
|
/// }
|
|
|
|
/// fn main() {}
|
|
|
|
/// ```
|
|
|
|
#[doc(hidden)]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self {
|
2018-03-06 04:28:42 +01:00
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
match hdr.try_into() {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(value) => {
|
|
|
|
parts.headers.append(H::name(), value);
|
|
|
|
}
|
2018-03-06 04:28:42 +01:00
|
|
|
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 actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{http, HttpRequest, HttpResponse};
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
2018-03-31 08:07:33 +02:00
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
/// HttpResponse::Ok()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .header("X-TEST", "value")
|
2018-03-31 08:07:33 +02:00
|
|
|
/// .header(http::header::CONTENT_TYPE, "application/json")
|
|
|
|
/// .finish()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// }
|
|
|
|
/// fn main() {}
|
|
|
|
/// ```
|
2017-10-11 01:03:32 +02:00
|
|
|
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
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) {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(key) => match value.try_into() {
|
|
|
|
Ok(value) => {
|
|
|
|
parts.headers.append(key, value);
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
Err(e) => self.err = Some(e.into()),
|
2017-10-11 01:03:32 +02:00
|
|
|
},
|
|
|
|
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
|
2018-04-14 01:02:01 +02:00
|
|
|
/// negotiates content encoding based on request's `Accept-Encoding`
|
|
|
|
/// headers. 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
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderValue: HttpTryFrom<V>,
|
2017-10-14 19:01:53 +02:00
|
|
|
{
|
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) {
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(value) => {
|
|
|
|
parts.headers.insert(header::CONTENT_TYPE, value);
|
|
|
|
}
|
2017-10-14 19:01:53 +02:00
|
|
|
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;
|
2018-03-31 08:07:33 +02:00
|
|
|
/// use actix_web::{http, HttpRequest, HttpResponse, Result};
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
2018-03-31 08:07:33 +02:00
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
/// HttpResponse::Ok()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .cookie(
|
2018-03-31 02:31:18 +02:00
|
|
|
/// http::Cookie::build("name", "value")
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .domain("www.rust-lang.org")
|
|
|
|
/// .path("/")
|
|
|
|
/// .secure(true)
|
|
|
|
/// .http_only(true)
|
2018-06-01 18:37:14 +02:00
|
|
|
/// .finish(),
|
|
|
|
/// )
|
2018-03-31 08:07:33 +02:00
|
|
|
/// .finish()
|
2017-12-21 08:19:21 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
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 {
|
2018-05-16 01:41:46 +02:00
|
|
|
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
2017-10-14 19:40:58 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-05-24 20:36:17 +02:00
|
|
|
/// Remove cookie
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::{http, HttpRequest, HttpResponse, Result};
|
|
|
|
///
|
2018-06-25 06:58:04 +02:00
|
|
|
/// fn index(req: &HttpRequest) -> HttpResponse {
|
2018-05-24 20:36:17 +02:00
|
|
|
/// let mut builder = HttpResponse::Ok();
|
|
|
|
///
|
2018-06-25 06:58:04 +02:00
|
|
|
/// if let Some(ref cookie) = req.cookie("name") {
|
2018-05-24 20:36:17 +02:00
|
|
|
/// builder.del_cookie(cookie);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// builder.finish()
|
|
|
|
/// }
|
|
|
|
/// ```
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02: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-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut HttpResponseBuilder),
|
2017-10-29 22:51:02 +01:00
|
|
|
{
|
|
|
|
if value {
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02: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
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: FnOnce(T, &mut HttpResponseBuilder),
|
2018-01-10 08:55:42 +01:00
|
|
|
{
|
|
|
|
if let Some(val) = value {
|
|
|
|
f(val, self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:00:15 +01:00
|
|
|
/// Set write buffer capacity
|
|
|
|
///
|
|
|
|
/// This parameter makes sense only for streaming response
|
2018-04-14 01:02:01 +02:00
|
|
|
/// or actor. If write buffer reaches specified capacity, stream or actor
|
|
|
|
/// get paused.
|
2018-03-09 19:00:15 +01:00
|
|
|
///
|
|
|
|
/// Default write buffer capacity is 64kb
|
|
|
|
pub fn write_buffer_capacity(&mut self, cap: usize) -> &mut Self {
|
|
|
|
if let Some(parts) = parts(&mut self.response, &self.err) {
|
|
|
|
parts.write_capacity = cap;
|
|
|
|
}
|
|
|
|
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.
|
2018-03-31 08:07:33 +02:00
|
|
|
pub fn body<B: Into<Body>>(&mut self, body: B) -> HttpResponse {
|
2017-10-11 01:03:32 +02:00
|
|
|
if let Some(e) = self.err.take() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Error::from(e).into();
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
2018-05-16 01:41:46 +02:00
|
|
|
let mut response = self.response.take().expect("cannot reuse response builder");
|
2017-12-14 01:44:35 +01:00
|
|
|
if let Some(ref jar) = self.cookies {
|
2017-12-13 06:32:58 +01:00
|
|
|
for cookie in jar.delta() {
|
2018-03-31 08:07:33 +02:00
|
|
|
match HeaderValue::from_str(&cookie.to_string()) {
|
|
|
|
Ok(val) => response.headers.append(header::SET_COOKIE, val),
|
|
|
|
Err(e) => return Error::from(e).into(),
|
|
|
|
};
|
2017-12-13 06:32:58 +01:00
|
|
|
}
|
2017-10-14 19:40:58 +02:00
|
|
|
}
|
2017-12-14 01:44:35 +01:00
|
|
|
response.body = body.into();
|
2018-06-25 06:10:02 +02:00
|
|
|
HttpResponse(response, self.pool)
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
2017-10-24 08:52:20 +02:00
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
#[inline]
|
2018-03-08 02:40:13 +01:00
|
|
|
/// Set a streaming body and generate `HttpResponse`.
|
|
|
|
///
|
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
2018-03-31 08:07:33 +02:00
|
|
|
pub fn streaming<S, E>(&mut self, stream: S) -> HttpResponse
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
S: Stream<Item = Bytes, Error = E> + 'static,
|
|
|
|
E: Into<Error>,
|
2018-03-08 02:40:13 +01:00
|
|
|
{
|
2018-05-16 01:41:46 +02:00
|
|
|
self.body(Body::Streaming(Box::new(stream.map_err(|e| e.into()))))
|
2018-03-08 02:40:13 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2018-03-31 08:07:33 +02:00
|
|
|
pub fn json<T: Serialize>(&mut self, value: T) -> HttpResponse {
|
2018-08-08 19:11:15 +02:00
|
|
|
self.json2(&value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a json body and generate `HttpResponse`
|
|
|
|
///
|
|
|
|
/// `HttpResponseBuilder` can not be used after this call.
|
|
|
|
pub fn json2<T: Serialize>(&mut self, value: &T) -> HttpResponse {
|
|
|
|
match serde_json::to_string(value) {
|
2018-03-31 08:07:33 +02:00
|
|
|
Ok(body) => {
|
2018-04-14 01:02:01 +02:00
|
|
|
let contains = if let Some(parts) = parts(&mut self.response, &self.err)
|
|
|
|
{
|
|
|
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2018-03-31 08:07:33 +02:00
|
|
|
if !contains {
|
|
|
|
self.header(header::CONTENT_TYPE, "application/json");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.body(body)
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
|
|
|
Err(e) => Error::from(e).into(),
|
2017-11-27 19:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
#[inline]
|
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.
|
2018-03-31 08:07:33 +02:00
|
|
|
pub fn finish(&mut self) -> HttpResponse {
|
2017-10-24 08:52:20 +02:00
|
|
|
self.body(Body::Empty)
|
|
|
|
}
|
2018-01-01 02:26:32 +01:00
|
|
|
|
|
|
|
/// This method construct new `HttpResponseBuilder`
|
|
|
|
pub fn take(&mut self) -> HttpResponseBuilder {
|
|
|
|
HttpResponseBuilder {
|
2018-06-25 06:10:02 +02:00
|
|
|
pool: self.pool,
|
2018-01-01 02:26:32 +01:00
|
|
|
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]
|
2018-11-01 09:14:48 +01:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
|
2018-04-14 01:02:01 +02:00
|
|
|
fn parts<'a>(
|
2018-05-17 21:20:20 +02:00
|
|
|
parts: &'a mut Option<Box<InnerHttpResponse>>, err: &Option<HttpError>,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Option<&'a mut Box<InnerHttpResponse>> {
|
2017-10-11 01:03:32 +02:00
|
|
|
if err.is_some() {
|
2018-04-14 01:02:01 +02:00
|
|
|
return None;
|
2017-10-11 01:03:32 +02:00
|
|
|
}
|
|
|
|
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 {
|
2018-03-31 08:07:33 +02:00
|
|
|
builder.finish()
|
2017-11-29 18:17:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for HttpResponseBuilder {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
#[inline]
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(mut self, _: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-03-31 08:07:33 +02:00
|
|
|
Ok(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 {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(val)
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for &'static str {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for &'static [u8] {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(val)
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for String {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("text/plain; charset=utf-8")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for Bytes {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-29 18:09:08 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(val)
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for BytesMut {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = HttpResponse;
|
2018-03-22 04:15:52 +01:00
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, Error> {
|
2018-05-17 21:20:20 +02:00
|
|
|
Ok(req
|
|
|
|
.build_response(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("application/octet-stream")
|
|
|
|
.body(self))
|
2017-12-03 02:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 04:04:35 +01:00
|
|
|
/// Create `HttpResponseBuilder` from `ClientResponse`
|
|
|
|
///
|
|
|
|
/// It is useful for proxy response. This implementation
|
|
|
|
/// copies all responses's headers and status.
|
|
|
|
impl<'a> From<&'a ClientResponse> for HttpResponseBuilder {
|
|
|
|
fn from(resp: &'a ClientResponse) -> HttpResponseBuilder {
|
|
|
|
let mut builder = HttpResponse::build(resp.status());
|
|
|
|
for (key, value) in resp.headers() {
|
|
|
|
builder.header(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 05:14:57 +01:00
|
|
|
impl<'a, S> From<&'a HttpRequest<S>> for HttpResponseBuilder {
|
|
|
|
fn from(req: &'a HttpRequest<S>) -> HttpResponseBuilder {
|
2018-06-25 06:58:04 +02:00
|
|
|
req.request()
|
|
|
|
.server_settings()
|
|
|
|
.get_response_builder(StatusCode::OK)
|
2018-03-23 05:14:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
#[derive(Debug)]
|
2018-06-21 06:45:24 +02:00
|
|
|
struct InnerHttpResponse {
|
2017-12-16 05:00:12 +01:00
|
|
|
version: Option<Version>,
|
|
|
|
headers: HeaderMap,
|
|
|
|
status: StatusCode,
|
|
|
|
reason: Option<&'static str>,
|
2018-06-21 06:45:24 +02:00
|
|
|
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>,
|
2018-03-09 19:00:15 +01:00
|
|
|
write_capacity: usize,
|
2017-12-16 05:00:12 +01:00
|
|
|
response_size: u64,
|
|
|
|
error: Option<Error>,
|
|
|
|
}
|
|
|
|
|
2018-06-21 06:45:24 +02:00
|
|
|
pub(crate) struct HttpResponseParts {
|
|
|
|
version: Option<Version>,
|
|
|
|
headers: HeaderMap,
|
|
|
|
status: StatusCode,
|
|
|
|
reason: Option<&'static str>,
|
|
|
|
body: Option<Bytes>,
|
|
|
|
encoding: Option<ContentEncoding>,
|
|
|
|
connection_type: Option<ConnectionType>,
|
|
|
|
error: Option<Error>,
|
|
|
|
}
|
2018-06-12 03:52:54 +02:00
|
|
|
|
2017-12-16 05:00:12 +01:00
|
|
|
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,
|
2018-03-09 19:00:15 +01:00
|
|
|
write_capacity: MAX_WRITE_BUFFER_SIZE,
|
2017-12-16 05:00:12 +01:00
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 21:27:41 +02:00
|
|
|
|
|
|
|
/// This is for failure, we can not have Send + Sync on Streaming and Actor response
|
2018-06-21 06:45:24 +02:00
|
|
|
fn into_parts(mut self) -> HttpResponseParts {
|
|
|
|
let body = match mem::replace(&mut self.body, Body::Empty) {
|
|
|
|
Body::Empty => None,
|
|
|
|
Body::Binary(mut bin) => Some(bin.take()),
|
2018-06-19 21:27:41 +02:00
|
|
|
Body::Streaming(_) | Body::Actor(_) => {
|
|
|
|
error!("Streaming or Actor body is not support by error response");
|
2018-06-21 06:45:24 +02:00
|
|
|
None
|
2018-06-19 21:27:41 +02:00
|
|
|
}
|
2018-06-21 06:45:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
HttpResponseParts {
|
|
|
|
body,
|
|
|
|
version: self.version,
|
|
|
|
headers: self.headers,
|
|
|
|
status: self.status,
|
|
|
|
reason: self.reason,
|
|
|
|
encoding: self.encoding,
|
|
|
|
connection_type: self.connection_type,
|
|
|
|
error: self.error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_parts(parts: HttpResponseParts) -> InnerHttpResponse {
|
|
|
|
let body = if let Some(ref body) = parts.body {
|
|
|
|
Body::Binary(body.clone().into())
|
|
|
|
} else {
|
|
|
|
Body::Empty
|
|
|
|
};
|
|
|
|
|
|
|
|
InnerHttpResponse {
|
|
|
|
body,
|
|
|
|
status: parts.status,
|
|
|
|
version: parts.version,
|
|
|
|
headers: parts.headers,
|
|
|
|
reason: parts.reason,
|
|
|
|
chunked: None,
|
|
|
|
encoding: parts.encoding,
|
|
|
|
connection_type: parts.connection_type,
|
|
|
|
response_size: 0,
|
|
|
|
write_capacity: MAX_WRITE_BUFFER_SIZE,
|
|
|
|
error: parts.error,
|
2018-06-19 21:27:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-16 05:00:12 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 09:34:36 +02:00
|
|
|
/// Internal use only!
|
2018-06-25 05:08:28 +02:00
|
|
|
pub(crate) struct HttpResponsePool(RefCell<VecDeque<Box<InnerHttpResponse>>>);
|
2018-03-23 05:14:57 +01:00
|
|
|
|
2018-06-25 05:08:28 +02:00
|
|
|
thread_local!(static POOL: &'static HttpResponsePool = HttpResponsePool::pool());
|
2017-12-16 05:00:12 +01:00
|
|
|
|
2018-03-23 05:14:57 +01:00
|
|
|
impl HttpResponsePool {
|
2018-06-25 05:08:28 +02:00
|
|
|
fn pool() -> &'static HttpResponsePool {
|
|
|
|
let pool = HttpResponsePool(RefCell::new(VecDeque::with_capacity(128)));
|
|
|
|
Box::leak(Box::new(pool))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_pool() -> &'static HttpResponsePool {
|
|
|
|
POOL.with(|p| *p)
|
2018-03-23 05:14:57 +01:00
|
|
|
}
|
2017-12-16 05:00:12 +01:00
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn get_builder(
|
2018-06-25 05:08:28 +02:00
|
|
|
pool: &'static HttpResponsePool, status: StatusCode,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> HttpResponseBuilder {
|
2018-06-25 05:08:28 +02:00
|
|
|
if let Some(mut msg) = pool.0.borrow_mut().pop_front() {
|
2018-03-23 05:14:57 +01:00
|
|
|
msg.status = status;
|
|
|
|
HttpResponseBuilder {
|
2018-06-25 06:10:02 +02:00
|
|
|
pool,
|
2018-03-23 05:14:57 +01:00
|
|
|
response: Some(msg),
|
|
|
|
err: None,
|
2018-04-14 01:02:01 +02:00
|
|
|
cookies: None,
|
|
|
|
}
|
2018-03-23 05:14:57 +01:00
|
|
|
} else {
|
|
|
|
let msg = Box::new(InnerHttpResponse::new(status, Body::Empty));
|
|
|
|
HttpResponseBuilder {
|
2018-06-25 06:10:02 +02:00
|
|
|
pool,
|
2018-03-23 05:14:57 +01:00
|
|
|
response: Some(msg),
|
|
|
|
err: None,
|
2018-04-14 01:02:01 +02:00
|
|
|
cookies: None,
|
|
|
|
}
|
2018-03-23 05:14:57 +01:00
|
|
|
}
|
2017-12-16 05:00:12 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn get_response(
|
2018-06-25 05:08:28 +02:00
|
|
|
pool: &'static HttpResponsePool, status: StatusCode, body: Body,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> HttpResponse {
|
2018-06-25 05:08:28 +02:00
|
|
|
if let Some(mut msg) = pool.0.borrow_mut().pop_front() {
|
2018-03-23 05:14:57 +01:00
|
|
|
msg.status = status;
|
|
|
|
msg.body = body;
|
2018-06-25 06:10:02 +02:00
|
|
|
HttpResponse(msg, pool)
|
2018-03-23 05:14:57 +01:00
|
|
|
} else {
|
|
|
|
let msg = Box::new(InnerHttpResponse::new(status, body));
|
2018-06-25 06:10:02 +02:00
|
|
|
HttpResponse(msg, pool)
|
2018-03-23 05:14:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get(status: StatusCode) -> HttpResponseBuilder {
|
|
|
|
POOL.with(|pool| HttpResponsePool::get_builder(pool, status))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_body(status: StatusCode, body: Body) -> HttpResponse {
|
|
|
|
POOL.with(|pool| HttpResponsePool::get_response(pool, status, body))
|
2017-12-16 05:00:12 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 05:08:28 +02:00
|
|
|
#[inline]
|
2018-06-25 06:10:02 +02:00
|
|
|
fn release(&self, mut inner: Box<InnerHttpResponse>) {
|
|
|
|
let mut p = self.0.borrow_mut();
|
|
|
|
if p.len() < 128 {
|
|
|
|
inner.headers.clear();
|
|
|
|
inner.version = None;
|
|
|
|
inner.chunked = None;
|
|
|
|
inner.reason = None;
|
|
|
|
inner.encoding = None;
|
|
|
|
inner.connection_type = None;
|
|
|
|
inner.response_size = 0;
|
|
|
|
inner.error = None;
|
|
|
|
inner.write_capacity = MAX_WRITE_BUFFER_SIZE;
|
|
|
|
p.push_front(inner);
|
|
|
|
}
|
2017-12-16 05:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 07:54:11 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-11-28 22:52:53 +01:00
|
|
|
use body::Binary;
|
2018-03-31 08:07:33 +02:00
|
|
|
use http;
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
|
|
|
use time::Duration;
|
2017-12-13 20:16:26 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
use test::TestRequest;
|
|
|
|
|
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; "))
|
2018-03-31 08:07:33 +02:00
|
|
|
.finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
let dbg = format!("{:?}", resp);
|
|
|
|
assert!(dbg.contains("HttpResponse"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_response_cookies() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(COOKIE, "cookie1=value1")
|
|
|
|
.header(COOKIE, "cookie2=value2")
|
|
|
|
.finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
let cookies = req.cookies().unwrap();
|
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = HttpResponse::Ok()
|
2018-04-14 01:02:01 +02:00
|
|
|
.cookie(
|
|
|
|
http::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))
|
2018-04-14 01:02:01 +02:00
|
|
|
.finish(),
|
2018-08-23 18:48:01 +02:00
|
|
|
).del_cookie(&cookies[0])
|
2018-03-31 08:07:33 +02:00
|
|
|
.finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
let mut val: Vec<_> = resp
|
|
|
|
.headers()
|
2018-04-14 01:02:01 +02:00
|
|
|
.get_all("Set-Cookie")
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.to_str().unwrap().to_owned())
|
|
|
|
.collect();
|
2017-12-13 20:16:26 +01:00
|
|
|
val.sort();
|
2018-06-25 06:58:04 +02:00
|
|
|
assert!(val[0].starts_with("cookie1=; Max-Age=0;"));
|
2017-12-13 20:16:26 +01:00
|
|
|
assert_eq!(
|
2018-04-14 01:02:01 +02:00
|
|
|
val[1],
|
|
|
|
"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400"
|
|
|
|
);
|
2017-12-13 20:16:26 +01:00
|
|
|
}
|
2017-10-23 07:54:11 +02:00
|
|
|
|
2018-06-12 15:49:07 +02:00
|
|
|
#[test]
|
|
|
|
fn test_update_response_cookies() {
|
|
|
|
let mut r = HttpResponse::Ok()
|
|
|
|
.cookie(http::Cookie::new("original", "val100"))
|
|
|
|
.finish();
|
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
r.add_cookie(&http::Cookie::new("cookie2", "val200"))
|
|
|
|
.unwrap();
|
|
|
|
r.add_cookie(&http::Cookie::new("cookie2", "val250"))
|
|
|
|
.unwrap();
|
|
|
|
r.add_cookie(&http::Cookie::new("cookie3", "val300"))
|
|
|
|
.unwrap();
|
2018-06-12 15:49:07 +02:00
|
|
|
|
|
|
|
assert_eq!(r.cookies().count(), 4);
|
|
|
|
r.del_cookie("cookie2");
|
|
|
|
|
|
|
|
let mut iter = r.cookies();
|
|
|
|
let v = iter.next().unwrap();
|
|
|
|
assert_eq!((v.name(), v.value()), ("original", "val100"));
|
|
|
|
let v = iter.next().unwrap();
|
|
|
|
assert_eq!((v.name(), v.value()), ("cookie3", "val300"));
|
|
|
|
}
|
|
|
|
|
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)
|
2018-03-31 08:07:33 +02:00
|
|
|
.finish();
|
2017-11-28 23:29:22 +01:00
|
|
|
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() {
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK).upgrade().finish();
|
2017-10-23 07:54:11 +02:00
|
|
|
assert!(resp.upgrade())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_force_close() {
|
2018-05-16 01:41:46 +02:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK).force_close().finish();
|
2017-10-23 07:54:11 +02:00
|
|
|
assert!(!resp.keep_alive().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_content_type() {
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_type("text/plain")
|
|
|
|
.body(Body::Empty);
|
2018-05-16 01:41:46 +02: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() {
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK).finish();
|
2018-02-19 07:23:17 +01:00
|
|
|
assert_eq!(resp.content_encoding(), None);
|
2017-11-06 23:56:38 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
#[cfg(feature = "brotli")]
|
2018-03-21 16:03:21 +01:00
|
|
|
{
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_encoding(ContentEncoding::Br)
|
|
|
|
.finish();
|
2018-03-21 16:03:21 +01:00
|
|
|
assert_eq!(resp.content_encoding(), Some(ContentEncoding::Br));
|
|
|
|
}
|
|
|
|
|
2017-11-27 07:31:29 +01:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2018-04-14 01:02:01 +02:00
|
|
|
.content_encoding(ContentEncoding::Gzip)
|
|
|
|
.finish();
|
2018-03-21 16:03:21 +01:00
|
|
|
assert_eq!(resp.content_encoding(), Some(ContentEncoding::Gzip));
|
2017-11-07 01:23:58 +01:00
|
|
|
}
|
2017-11-27 19:39:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let resp = HttpResponse::build(StatusCode::OK).json(vec!["v1", "v2", "v3"]);
|
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"));
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
*resp.body(),
|
|
|
|
Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]"))
|
|
|
|
);
|
2017-11-27 19:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json_ct() {
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
2018-03-06 04:28:42 +01:00
|
|
|
.header(CONTENT_TYPE, "text/json")
|
2018-03-31 08:07:33 +02:00
|
|
|
.json(vec!["v1", "v2", "v3"]);
|
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"));
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
*resp.body(),
|
|
|
|
Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]"))
|
|
|
|
);
|
2017-11-27 19:39:47 +01:00
|
|
|
}
|
2017-11-28 22:52:53 +01:00
|
|
|
|
2018-08-08 19:58:56 +02:00
|
|
|
#[test]
|
|
|
|
fn test_json2() {
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK).json2(&vec!["v1", "v2", "v3"]);
|
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
|
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
|
|
|
assert_eq!(
|
|
|
|
*resp.body(),
|
|
|
|
Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json2_ct() {
|
|
|
|
let resp = HttpResponse::build(StatusCode::OK)
|
|
|
|
.header(CONTENT_TYPE, "text/json")
|
|
|
|
.json2(&vec!["v1", "v2", "v3"]);
|
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
|
|
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
|
|
|
assert_eq!(
|
|
|
|
*resp.body(),
|
|
|
|
Body::from(Bytes::from_static(b"[\"v1\",\"v2\",\"v3\"]"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
impl Body {
|
2018-05-16 01:41:46 +02:00
|
|
|
pub(crate) fn bin_ref(&self) -> &Binary {
|
2017-11-28 22:52:53 +01:00
|
|
|
match *self {
|
2018-05-16 01:41:46 +02:00
|
|
|
Body::Binary(ref bin) => bin,
|
|
|
|
_ => panic!(),
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().finish();
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = "test".into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from("test"));
|
2017-11-28 22:52:53 +01:00
|
|
|
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = "test".respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from("test"));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = b"test".as_ref().into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from(b"test".as_ref()));
|
2017-11-28 22:52:53 +01:00
|
|
|
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = b"test".as_ref().respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from(b"test".as_ref()));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = "test".to_owned().into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from("test".to_owned()));
|
2018-04-14 01:02:01 +02:00
|
|
|
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = "test".to_owned().respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from("test".to_owned()));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-11-28 22:52:53 +01:00
|
|
|
let resp: HttpResponse = (&"test".to_owned()).into();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from(&"test".to_owned()));
|
2018-04-14 01:02:01 +02:00
|
|
|
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = (&"test".to_owned()).respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &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-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
2018-05-16 01:41:46 +02:00
|
|
|
resp.body().bin_ref(),
|
2018-04-14 01:02:01 +02:00
|
|
|
&Binary::from(Bytes::from_static(b"test"))
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
let b = Bytes::from_static(b"test");
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = b.respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
2018-05-16 01:41:46 +02:00
|
|
|
resp.body().bin_ref(),
|
2018-04-14 01:02:01 +02:00
|
|
|
&Binary::from(Bytes::from_static(b"test"))
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
|
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-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 22:52:53 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from(BytesMut::from("test")));
|
2017-12-03 23:22:04 +01:00
|
|
|
|
|
|
|
let b = BytesMut::from("test");
|
2018-05-04 21:11:38 +02:00
|
|
|
let resp: HttpResponse = b.respond_to(&req).ok().unwrap();
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-12-03 23:22:04 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-05-16 01:41:46 +02:00
|
|
|
assert_eq!(resp.body().bin_ref(), &Binary::from(BytesMut::from("test")));
|
2017-11-28 22:52:53 +01:00
|
|
|
}
|
2018-03-30 23:30:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_builder() {
|
2018-06-12 15:49:07 +02:00
|
|
|
let mut resp: HttpResponse = "test".into();
|
2018-03-30 23:30:24 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
2018-06-14 08:37:19 +02:00
|
|
|
resp.add_cookie(&http::Cookie::new("cookie1", "val100"))
|
|
|
|
.unwrap();
|
2018-06-12 15:49:07 +02:00
|
|
|
|
2018-03-30 23:30:24 +02:00
|
|
|
let mut builder = resp.into_builder();
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = builder.status(StatusCode::BAD_REQUEST).finish();
|
2018-03-30 23:30:24 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-06-12 15:49:07 +02:00
|
|
|
|
|
|
|
let cookie = resp.cookies().next().unwrap();
|
|
|
|
assert_eq!((cookie.name(), cookie.value()), ("cookie1", "val100"));
|
2018-03-30 23:30:24 +02:00
|
|
|
}
|
2017-10-23 07:54:11 +02:00
|
|
|
}
|