2021-01-15 02:11:10 +00:00
|
|
|
//! HTTP responses.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefMut},
|
|
|
|
convert::TryInto,
|
|
|
|
fmt,
|
|
|
|
future::Future,
|
2021-01-18 03:14:29 -09:00
|
|
|
ops,
|
2021-01-15 02:11:10 +00:00
|
|
|
pin::Pin,
|
|
|
|
str,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2017-10-06 21:48:14 -07:00
|
|
|
|
2019-12-05 23:35:43 +06:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-12-13 11:24:57 +06:00
|
|
|
use futures_core::Stream;
|
2017-11-27 10:39:47 -08:00
|
|
|
use serde::Serialize;
|
2017-12-07 16:40:29 -08:00
|
|
|
|
2018-12-06 14:32:52 -08:00
|
|
|
use crate::body::{Body, BodyStream, MessageBody, ResponseBody};
|
2019-03-29 21:13:39 -07:00
|
|
|
use crate::cookie::{Cookie, CookieJar};
|
2018-12-06 14:32:52 -08:00
|
|
|
use crate::error::Error;
|
2019-03-27 11:29:31 -07:00
|
|
|
use crate::extensions::Extensions;
|
2021-01-15 02:11:10 +00:00
|
|
|
use crate::header::{IntoHeaderPair, IntoHeaderValue};
|
2019-04-06 15:02:02 -07:00
|
|
|
use crate::http::header::{self, HeaderName, HeaderValue};
|
2019-12-05 23:35:43 +06:00
|
|
|
use crate::http::{Error as HttpError, HeaderMap, StatusCode};
|
2019-04-06 15:02:02 -07:00
|
|
|
use crate::message::{BoxedResponseHead, ConnectionType, ResponseHead};
|
2018-03-09 10:00:15 -08:00
|
|
|
|
2017-12-15 16:24:15 -08:00
|
|
|
/// An HTTP Response
|
2019-02-20 21:02:23 -08:00
|
|
|
pub struct Response<B = Body> {
|
2019-04-06 15:02:02 -07:00
|
|
|
head: BoxedResponseHead,
|
2019-02-07 21:16:46 -08:00
|
|
|
body: ResponseBody<B>,
|
|
|
|
error: Option<Error>,
|
|
|
|
}
|
2017-12-15 16:24:15 -08:00
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl Response<Body> {
|
2021-02-11 22:39:54 +00:00
|
|
|
/// Create HTTP response builder with specific status.
|
2017-10-10 16:03:32 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
pub fn build(status: StatusCode) -> ResponseBuilder {
|
2019-02-07 21:16:46 -08:00
|
|
|
ResponseBuilder::new(status)
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
2021-02-11 22:39:54 +00:00
|
|
|
/// Create HTTP response builder
|
2018-03-21 19:54:21 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
pub fn build_from<T: Into<ResponseBuilder>>(source: T) -> ResponseBuilder {
|
2018-03-21 19:54:21 -07:00
|
|
|
source.into()
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:14:13 -07:00
|
|
|
/// Constructs a response
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
pub fn new(status: StatusCode) -> Response {
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
2019-04-07 23:06:21 -07:00
|
|
|
head: BoxedResponseHead::new(status),
|
2019-02-07 21:16:46 -08:00
|
|
|
body: ResponseBody::Body(Body::Empty),
|
|
|
|
error: None,
|
|
|
|
}
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 18:11:10 +02:00
|
|
|
/// Constructs an error response
|
2017-11-25 09:03:44 -08:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
pub fn from_error(error: Error) -> Response {
|
2019-11-26 16:07:39 +06:00
|
|
|
let mut resp = error.as_response_error().error_response();
|
2019-07-18 11:31:18 +03:00
|
|
|
if resp.head.status == StatusCode::INTERNAL_SERVER_ERROR {
|
|
|
|
error!("Internal Server Error: {:?}", error);
|
|
|
|
}
|
2019-02-07 21:16:46 -08:00
|
|
|
resp.error = Some(error);
|
2019-04-10 12:43:31 -07:00
|
|
|
resp
|
2017-11-25 09:03:44 -08:00
|
|
|
}
|
|
|
|
|
2018-11-21 07:49:24 -08:00
|
|
|
/// Convert response to response with body
|
2019-03-05 17:24:24 -08:00
|
|
|
pub fn into_body<B>(self) -> Response<B> {
|
2019-02-07 21:16:46 -08:00
|
|
|
let b = match self.body {
|
2018-11-21 07:49:24 -08:00
|
|
|
ResponseBody::Body(b) => b,
|
|
|
|
ResponseBody::Other(b) => b,
|
|
|
|
};
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
error: self.error,
|
|
|
|
body: ResponseBody::Other(b),
|
|
|
|
}
|
2018-11-21 07:49:24 -08:00
|
|
|
}
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:05:37 -08:00
|
|
|
impl<B> Response<B> {
|
2019-04-07 23:06:21 -07:00
|
|
|
/// Constructs a response with body
|
|
|
|
#[inline]
|
|
|
|
pub fn with_body(status: StatusCode, body: B) -> Response<B> {
|
|
|
|
Response {
|
|
|
|
head: BoxedResponseHead::new(status),
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 20:21:28 -08:00
|
|
|
#[inline]
|
2018-11-19 14:57:12 -08:00
|
|
|
/// Http message part of the response
|
|
|
|
pub fn head(&self) -> &ResponseHead {
|
2019-02-07 21:16:46 -08:00
|
|
|
&*self.head
|
2018-11-19 14:57:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-02-11 22:39:54 +00:00
|
|
|
/// Mutable reference to a HTTP message part of the response
|
2018-11-19 14:57:12 -08:00
|
|
|
pub fn head_mut(&mut self) -> &mut ResponseHead {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut *self.head
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2017-11-25 09:03:44 -08:00
|
|
|
/// The source `error` for this response
|
|
|
|
#[inline]
|
|
|
|
pub fn error(&self) -> Option<&Error> {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.error.as_ref()
|
2017-11-25 09:03:44 -08:00
|
|
|
}
|
|
|
|
|
2018-11-17 20:21:28 -08:00
|
|
|
/// Get the response status code
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.status
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the `StatusCode` for this response
|
|
|
|
#[inline]
|
|
|
|
pub fn status_mut(&mut self) -> &mut StatusCode {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut self.head.status
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2018-01-29 11:39:26 -08:00
|
|
|
/// Get the headers from the response
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2017-10-09 23:07:32 -07:00
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2019-02-07 21:16:46 -08:00
|
|
|
&self.head.headers
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2018-01-29 11:39:26 -08:00
|
|
|
/// Get a mutable reference to the headers
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2017-10-09 23:07:32 -07:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut self.head.headers
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2018-06-12 13:49:07 +00:00
|
|
|
/// Get an iterator for the cookies set by this response
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn cookies(&self) -> CookieIter<'_> {
|
2018-06-12 13:49:07 +00:00
|
|
|
CookieIter {
|
2019-04-06 15:02:02 -07:00
|
|
|
iter: self.head.headers.get_all(header::SET_COOKIE),
|
2018-06-12 13:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a cookie to this response
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn add_cookie(&mut self, cookie: &Cookie<'_>) -> Result<(), HttpError> {
|
2019-02-07 21:16:46 -08:00
|
|
|
let h = &mut self.head.headers;
|
2018-06-12 13:49:07 +00:00
|
|
|
HeaderValue::from_str(&cookie.to_string())
|
2018-06-13 23:37:19 -07:00
|
|
|
.map(|c| {
|
|
|
|
h.append(header::SET_COOKIE, c);
|
2018-12-06 14:32:52 -08:00
|
|
|
})
|
|
|
|
.map_err(|e| e.into())
|
2018-06-12 13:49:07 +00: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 {
|
2019-02-07 21:16:46 -08:00
|
|
|
let h = &mut self.head.headers;
|
2018-06-13 23:37:19 -07:00
|
|
|
let vals: Vec<HeaderValue> = h
|
|
|
|
.get_all(header::SET_COOKIE)
|
2018-06-12 13:49:07 +00:00
|
|
|
.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 08:38:18 +03:00
|
|
|
if let Ok(c) = Cookie::parse_encoded(s) {
|
2018-06-12 13:49:07 +00:00
|
|
|
if c.name() == name {
|
|
|
|
count += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.append(header::SET_COOKIE, v);
|
|
|
|
}
|
2018-06-13 23:37:19 -07:00
|
|
|
count
|
2018-06-12 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:48:00 -07:00
|
|
|
/// Connection upgrade status
|
2017-12-31 17:26:32 -08:00
|
|
|
#[inline]
|
2017-10-07 21:48:00 -07:00
|
|
|
pub fn upgrade(&self) -> bool {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.upgrade()
|
2017-10-07 21:48:00 -07:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:48:14 -07:00
|
|
|
/// Keep-alive status for this connection
|
2018-11-18 17:52:56 -08:00
|
|
|
pub fn keep_alive(&self) -> bool {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.keep_alive()
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
2017-10-07 21:48:00 -07:00
|
|
|
|
2019-03-30 02:29:11 +03:00
|
|
|
/// Responses extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-30 02:29:11 +03:00
|
|
|
self.head.extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the response's extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
2019-03-30 02:29:11 +03:00
|
|
|
self.head.extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:11:55 -04:00
|
|
|
/// Get body of this response
|
2017-12-31 17:26:32 -08:00
|
|
|
#[inline]
|
2019-02-07 21:16:46 -08:00
|
|
|
pub fn body(&self) -> &ResponseBody<B> {
|
|
|
|
&self.body
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:48:00 -07:00
|
|
|
/// Set a body
|
2019-04-05 11:29:42 -07:00
|
|
|
pub fn set_body<B2>(self, body: B2) -> Response<B2> {
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: None,
|
|
|
|
}
|
2017-10-07 21:48:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-10 12:24:17 -07:00
|
|
|
/// Split response and body
|
|
|
|
pub fn into_parts(self) -> (Response<()>, ResponseBody<B>) {
|
|
|
|
(
|
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(()),
|
|
|
|
error: self.error,
|
|
|
|
},
|
|
|
|
self.body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
/// Drop request's body
|
2019-04-05 11:29:42 -07:00
|
|
|
pub fn drop_body(self) -> Response<()> {
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(()),
|
|
|
|
error: None,
|
|
|
|
}
|
2018-11-18 13:48:42 -08:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:14:13 -07:00
|
|
|
/// Set a body and return previous body value
|
2019-03-05 18:14:30 -08:00
|
|
|
pub(crate) fn replace_body<B2>(self, body: B2) -> (Response<B2>, ResponseBody<B>) {
|
2019-02-07 21:16:46 -08:00
|
|
|
(
|
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: self.error,
|
|
|
|
},
|
|
|
|
self.body,
|
|
|
|
)
|
2018-06-25 10:10:02 +06:00
|
|
|
}
|
2019-02-18 17:01:35 -08:00
|
|
|
|
|
|
|
/// Set a body and return previous body value
|
2019-03-05 18:14:30 -08:00
|
|
|
pub fn map_body<F, B2>(mut self, f: F) -> Response<B2>
|
2019-02-18 17:01:35 -08:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
|
|
|
{
|
|
|
|
let body = f(&mut self.head, self.body);
|
|
|
|
|
|
|
|
Response {
|
2019-03-29 21:13:39 -07:00
|
|
|
body,
|
2019-02-18 17:01:35 -08:00
|
|
|
head: self.head,
|
|
|
|
error: self.error,
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 21:15:18 -08:00
|
|
|
|
|
|
|
/// Extract response body
|
|
|
|
pub fn take_body(&mut self) -> ResponseBody<B> {
|
|
|
|
self.body.take_body()
|
|
|
|
}
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
2017-10-10 16:03:32 -07:00
|
|
|
|
2018-11-18 17:52:56 -08:00
|
|
|
impl<B: MessageBody> fmt::Debug for Response<B> {
|
2019-12-08 00:46:51 +06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-13 16:02:01 -07:00
|
|
|
let res = writeln!(
|
|
|
|
f,
|
2018-10-05 11:04:59 -07:00
|
|
|
"\nResponse {:?} {}{}",
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.version,
|
|
|
|
self.head.status,
|
|
|
|
self.head.reason.unwrap_or(""),
|
2018-04-13 16:02:01 -07:00
|
|
|
);
|
2018-04-09 14:25:30 -07:00
|
|
|
let _ = writeln!(f, " headers:");
|
2019-02-07 21:16:46 -08:00
|
|
|
for (key, val) in self.head.headers.iter() {
|
2018-04-09 14:25:30 -07:00
|
|
|
let _ = writeln!(f, " {:?}: {:?}", key, val);
|
2017-11-08 19:31:25 -08:00
|
|
|
}
|
2019-04-10 12:24:17 -07:00
|
|
|
let _ = writeln!(f, " body: {:?}", self.body.size());
|
2017-11-08 19:31:25 -08:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:34:04 +06:00
|
|
|
impl Future for Response {
|
|
|
|
type Output = Result<Response, Error>;
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2019-12-08 00:46:51 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-21 21:34:04 +06:00
|
|
|
Poll::Ready(Ok(Response {
|
2019-11-22 11:49:35 +06:00
|
|
|
head: self.head.take(),
|
2019-11-21 21:34:04 +06:00
|
|
|
body: self.body.take_body(),
|
|
|
|
error: self.error.take(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2018-06-12 13:49:07 +00:00
|
|
|
pub struct CookieIter<'a> {
|
2019-04-06 15:02:02 -07:00
|
|
|
iter: header::GetAll<'a>,
|
2018-06-12 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 08:38:18 +03:00
|
|
|
if let Ok(c) = Cookie::parse_encoded(v.to_str().ok()?) {
|
2018-06-12 13:49:07 +00:00
|
|
|
return Some(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:03:32 -07:00
|
|
|
/// An HTTP response builder
|
|
|
|
///
|
2018-10-05 11:04:59 -07:00
|
|
|
/// This type can be used to construct an instance of `Response` through a
|
2017-10-10 16:03:32 -07:00
|
|
|
/// builder-like pattern.
|
2018-10-05 11:04:59 -07:00
|
|
|
pub struct ResponseBuilder {
|
2019-04-06 15:02:02 -07:00
|
|
|
head: Option<BoxedResponseHead>,
|
2017-10-22 09:13:29 -07:00
|
|
|
err: Option<HttpError>,
|
2017-12-13 16:44:35 -08:00
|
|
|
cookies: Option<CookieJar>,
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
impl ResponseBuilder {
|
2019-04-07 23:06:21 -07:00
|
|
|
#[inline]
|
2019-02-07 21:16:46 -08:00
|
|
|
/// Create response builder
|
|
|
|
pub fn new(status: StatusCode) -> Self {
|
|
|
|
ResponseBuilder {
|
2019-04-07 23:06:21 -07:00
|
|
|
head: Some(BoxedResponseHead::new(status)),
|
2019-02-07 21:16:46 -08:00
|
|
|
err: None,
|
|
|
|
cookies: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 00:43:25 -08:00
|
|
|
/// Set HTTP status code of this response.
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&mut self, status: StatusCode) -> &mut Self {
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.status = status;
|
2018-03-06 00:43:25 -08:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
/// Insert a header, replacing any that were set with an equivalent field name.
|
2018-03-05 19:28:42 -08:00
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// ```rust
|
2021-01-15 02:11:10 +00:00
|
|
|
/// # use actix_http::Response;
|
|
|
|
/// use actix_http::http::header::ContentType;
|
2018-03-05 19:28:42 -08:00
|
|
|
///
|
2021-01-15 02:11:10 +00:00
|
|
|
/// Response::Ok()
|
|
|
|
/// .insert_header(ContentType(mime::APPLICATION_JSON))
|
|
|
|
/// .insert_header(("X-TEST", "value"))
|
|
|
|
/// .finish();
|
2018-03-05 19:28:42 -08:00
|
|
|
/// ```
|
2021-01-15 02:11:10 +00:00
|
|
|
pub fn insert_header<H>(&mut self, header: H) -> &mut Self
|
|
|
|
where
|
|
|
|
H: IntoHeaderPair,
|
|
|
|
{
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
2021-01-15 02:11:10 +00:00
|
|
|
match header.try_into_header_pair() {
|
2021-02-09 22:59:17 +00:00
|
|
|
Ok((key, value)) => {
|
|
|
|
parts.headers.insert(key, value);
|
|
|
|
}
|
2018-03-05 19:28:42 -08:00
|
|
|
Err(e) => self.err = Some(e.into()),
|
2021-01-15 02:11:10 +00:00
|
|
|
};
|
2018-03-05 19:28:42 -08:00
|
|
|
}
|
2021-01-15 02:11:10 +00:00
|
|
|
|
2018-03-05 19:28:42 -08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
/// Append a header, keeping any that were set with an equivalent field name.
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// ```rust
|
2021-01-15 02:11:10 +00:00
|
|
|
/// # use actix_http::Response;
|
|
|
|
/// use actix_http::http::header::ContentType;
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2021-01-15 02:11:10 +00:00
|
|
|
/// Response::Ok()
|
|
|
|
/// .append_header(ContentType(mime::APPLICATION_JSON))
|
|
|
|
/// .append_header(("X-TEST", "value1"))
|
|
|
|
/// .append_header(("X-TEST", "value2"))
|
|
|
|
/// .finish();
|
2017-12-20 23:19:21 -08:00
|
|
|
/// ```
|
2021-01-15 02:11:10 +00:00
|
|
|
pub fn append_header<H>(&mut self, header: H) -> &mut Self
|
2018-04-13 16:02:01 -07:00
|
|
|
where
|
2021-01-15 02:11:10 +00:00
|
|
|
H: IntoHeaderPair,
|
2017-10-10 16:03:32 -07:00
|
|
|
{
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
2021-01-15 02:11:10 +00:00
|
|
|
match header.try_into_header_pair() {
|
|
|
|
Ok((key, value)) => parts.headers.append(key, value),
|
2017-10-10 16:03:32 -07:00
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
};
|
|
|
|
}
|
2021-01-15 02:11:10 +00:00
|
|
|
|
2017-10-10 16:03:32 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:11:10 +00:00
|
|
|
/// Replaced with [`Self::insert_header()`].
|
|
|
|
#[deprecated = "Replaced with `insert_header((key, value))`."]
|
2018-11-19 14:57:12 -08:00
|
|
|
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
|
|
where
|
2021-01-15 02:11:10 +00:00
|
|
|
K: TryInto<HeaderName>,
|
|
|
|
K::Error: Into<HttpError>,
|
2018-11-19 14:57:12 -08:00
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
2021-01-15 02:11:10 +00:00
|
|
|
if self.err.is_some() {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (key.try_into(), value.try_into_value()) {
|
|
|
|
(Ok(name), Ok(value)) => return self.insert_header((name, value)),
|
|
|
|
(Err(err), _) => self.err = Some(err.into()),
|
|
|
|
(_, Err(err)) => self.err = Some(err.into()),
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replaced with [`Self::append_header()`].
|
|
|
|
#[deprecated = "Replaced with `append_header((key, value))`."]
|
|
|
|
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
|
|
where
|
|
|
|
K: TryInto<HeaderName>,
|
|
|
|
K::Error: Into<HttpError>,
|
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
if self.err.is_some() {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
match (key.try_into(), value.try_into_value()) {
|
|
|
|
(Ok(name), Ok(value)) => return self.append_header((name, value)),
|
|
|
|
(Err(err), _) => self.err = Some(err.into()),
|
|
|
|
(_, Err(err)) => self.err = Some(err.into()),
|
2018-11-19 14:57:12 -08:00
|
|
|
}
|
2021-01-15 02:11:10 +00:00
|
|
|
|
2018-11-19 14:57:12 -08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:03:32 -07:00
|
|
|
/// Set the custom reason for the response.
|
|
|
|
#[inline]
|
|
|
|
pub fn reason(&mut self, reason: &'static str) -> &mut Self {
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.reason = Some(reason);
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-18 17:52:56 -08:00
|
|
|
/// Set connection type to KeepAlive
|
2017-12-03 20:47:15 -08:00
|
|
|
#[inline]
|
2018-11-18 17:52:56 -08:00
|
|
|
pub fn keep_alive(&mut self) -> &mut Self {
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.set_connection_type(ConnectionType::KeepAlive);
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-14 10:01:53 -07:00
|
|
|
/// Set connection type to Upgrade
|
2017-12-03 20:47:15 -08:00
|
|
|
#[inline]
|
2018-11-19 14:57:12 -08:00
|
|
|
pub fn upgrade<V>(&mut self, value: V) -> &mut Self
|
|
|
|
where
|
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.set_connection_type(ConnectionType::Upgrade);
|
2018-11-18 17:52:56 -08:00
|
|
|
}
|
2021-01-15 02:11:10 +00:00
|
|
|
|
|
|
|
if let Ok(value) = value.try_into_value() {
|
|
|
|
self.insert_header((header::UPGRADE, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
2017-10-14 10:01:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Force close connection, even if it is marked as keep-alive
|
2017-12-03 20:47:15 -08:00
|
|
|
#[inline]
|
2017-10-14 10:01:53 -07:00
|
|
|
pub fn force_close(&mut self) -> &mut Self {
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.set_connection_type(ConnectionType::Close);
|
2018-11-18 17:52:56 -08:00
|
|
|
}
|
|
|
|
self
|
2017-10-14 10:01:53 -07:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:24:50 -08:00
|
|
|
/// Disable chunked transfer encoding for HTTP/1.1 streaming responses.
|
|
|
|
#[inline]
|
2020-05-19 00:46:31 +02:00
|
|
|
pub fn no_chunking(&mut self, len: u64) -> &mut Self {
|
2021-01-15 02:11:10 +00:00
|
|
|
self.insert_header((header::CONTENT_LENGTH, len));
|
2020-05-19 00:46:31 +02:00
|
|
|
|
2019-02-18 20:24:50 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
2019-03-27 10:38:01 -07:00
|
|
|
parts.no_chunking(true);
|
2019-02-18 20:24:50 -08:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-09 13:17:19 +00:00
|
|
|
/// Set response content type.
|
2017-12-03 20:47:15 -08:00
|
|
|
#[inline]
|
2017-10-14 10:01:53 -07:00
|
|
|
pub fn content_type<V>(&mut self, value: V) -> &mut Self
|
2018-04-13 16:02:01 -07:00
|
|
|
where
|
2021-01-09 13:17:19 +00:00
|
|
|
V: IntoHeaderValue,
|
2017-10-14 10:01:53 -07:00
|
|
|
{
|
2019-02-07 21:16:46 -08:00
|
|
|
if let Some(parts) = parts(&mut self.head, &self.err) {
|
2021-01-15 02:11:10 +00:00
|
|
|
match value.try_into_value() {
|
2018-04-13 16:02:01 -07:00
|
|
|
Ok(value) => {
|
2019-02-07 21:16:46 -08:00
|
|
|
parts.headers.insert(header::CONTENT_TYPE, value);
|
2018-04-13 16:02:01 -07:00
|
|
|
}
|
2017-10-14 10:01:53 -07:00
|
|
|
Err(e) => self.err = Some(e.into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-14 10:40:58 -07:00
|
|
|
/// Set a cookie
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_http::{http, Request, Response};
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// fn index(req: Request) -> Response {
|
2018-10-05 11:04:59 -07:00
|
|
|
/// Response::Ok()
|
2017-12-20 23:19:21 -08:00
|
|
|
/// .cookie(
|
2018-03-30 17:31:18 -07:00
|
|
|
/// http::Cookie::build("name", "value")
|
2017-12-20 23:19:21 -08:00
|
|
|
/// .domain("www.rust-lang.org")
|
|
|
|
/// .path("/")
|
|
|
|
/// .secure(true)
|
|
|
|
/// .http_only(true)
|
2018-06-01 09:37:14 -07:00
|
|
|
/// .finish(),
|
|
|
|
/// )
|
2018-03-30 23:07:33 -07:00
|
|
|
/// .finish()
|
2017-12-20 23:19:21 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
2017-10-14 10:40:58 -07:00
|
|
|
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
|
2017-12-13 16:44:35 -08:00
|
|
|
if self.cookies.is_none() {
|
|
|
|
let mut jar = CookieJar::new();
|
|
|
|
jar.add(cookie.into_owned());
|
|
|
|
self.cookies = Some(jar)
|
|
|
|
} else {
|
2018-05-15 16:41:46 -07:00
|
|
|
self.cookies.as_mut().unwrap().add(cookie.into_owned());
|
2017-10-14 10:40:58 -07:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-05-24 21:36:17 +03:00
|
|
|
/// Remove cookie
|
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_http::{http, Request, Response, HttpMessage};
|
2018-05-24 21:36:17 +03:00
|
|
|
///
|
2019-03-29 22:06:14 -07:00
|
|
|
/// fn index(req: Request) -> Response {
|
2018-10-05 11:04:59 -07:00
|
|
|
/// let mut builder = Response::Ok();
|
2018-05-24 21:36:17 +03:00
|
|
|
///
|
2018-06-25 10:58:04 +06:00
|
|
|
/// if let Some(ref cookie) = req.cookie("name") {
|
2018-05-24 21:36:17 +03:00
|
|
|
/// builder.del_cookie(cookie);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// builder.finish()
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-10-14 10:40:58 -07:00
|
|
|
pub fn del_cookie<'a>(&mut self, cookie: &Cookie<'a>) -> &mut Self {
|
2019-04-07 23:06:21 -07:00
|
|
|
if self.cookies.is_none() {
|
|
|
|
self.cookies = Some(CookieJar::new())
|
2017-10-14 10:40:58 -07:00
|
|
|
}
|
2019-04-07 23:06:21 -07:00
|
|
|
let jar = self.cookies.as_mut().unwrap();
|
|
|
|
let cookie = cookie.clone().into_owned();
|
|
|
|
jar.add_original(cookie.clone());
|
|
|
|
jar.remove(cookie);
|
2017-10-14 10:40:58 -07:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:10:05 +00:00
|
|
|
/// This method calls provided closure with builder reference if value is `true`.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated = "Use an if statement."]
|
2017-10-29 14:51:02 -07:00
|
|
|
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
|
2018-04-13 16:02:01 -07:00
|
|
|
where
|
2018-10-05 11:04:59 -07:00
|
|
|
F: FnOnce(&mut ResponseBuilder),
|
2017-10-29 14:51:02 -07:00
|
|
|
{
|
|
|
|
if value {
|
|
|
|
f(self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-30 02:10:05 +00:00
|
|
|
/// This method calls provided closure with builder reference if value is `Some`.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated = "Use an if-let construction."]
|
2018-01-20 16:36:57 -08:00
|
|
|
pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Self
|
2018-04-13 16:02:01 -07:00
|
|
|
where
|
2018-10-05 11:04:59 -07:00
|
|
|
F: FnOnce(T, &mut ResponseBuilder),
|
2018-01-09 23:55:42 -08:00
|
|
|
{
|
|
|
|
if let Some(val) = value {
|
|
|
|
f(val, self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-27 11:29:31 -07:00
|
|
|
/// Responses extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-27 11:29:31 -07:00
|
|
|
let head = self.head.as_ref().expect("cannot reuse response builder");
|
|
|
|
head.extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the response's extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
2019-03-27 11:29:31 -07:00
|
|
|
let head = self.head.as_ref().expect("cannot reuse response builder");
|
|
|
|
head.extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:06:21 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
/// Set a body and generate `Response`.
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2018-10-05 11:04:59 -07:00
|
|
|
/// `ResponseBuilder` can not be used after this call.
|
2018-11-18 13:48:42 -08:00
|
|
|
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response {
|
|
|
|
self.message_body(body.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a body and generate `Response`.
|
|
|
|
///
|
|
|
|
/// `ResponseBuilder` can not be used after this call.
|
2019-03-05 18:14:30 -08:00
|
|
|
pub fn message_body<B>(&mut self, body: B) -> Response<B> {
|
2018-11-21 07:49:24 -08:00
|
|
|
if let Some(e) = self.err.take() {
|
|
|
|
return Response::from(Error::from(e)).into_body();
|
|
|
|
}
|
2018-11-17 20:21:28 -08:00
|
|
|
|
2019-02-07 21:16:46 -08:00
|
|
|
let mut response = self.head.take().expect("cannot reuse response builder");
|
2019-03-23 09:40:20 -07:00
|
|
|
|
2019-03-29 21:13:39 -07:00
|
|
|
if let Some(ref jar) = self.cookies {
|
|
|
|
for cookie in jar.delta() {
|
|
|
|
match HeaderValue::from_str(&cookie.to_string()) {
|
2019-04-07 23:06:21 -07:00
|
|
|
Ok(val) => response.headers.append(header::SET_COOKIE, val),
|
2019-03-29 21:13:39 -07:00
|
|
|
Err(e) => return Response::from(Error::from(e)).into_body(),
|
|
|
|
};
|
2017-12-12 21:32:58 -08:00
|
|
|
}
|
2017-10-14 10:40:58 -07:00
|
|
|
}
|
2018-11-17 20:21:28 -08:00
|
|
|
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: response,
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: None,
|
|
|
|
}
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
2017-10-23 23:52:20 -07:00
|
|
|
|
2018-03-30 23:07:33 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
/// Set a streaming body and generate `Response`.
|
2018-03-07 17:40:13 -08:00
|
|
|
///
|
2018-10-05 11:04:59 -07:00
|
|
|
/// `ResponseBuilder` can not be used after this call.
|
2018-11-18 13:48:42 -08:00
|
|
|
pub fn streaming<S, E>(&mut self, stream: S) -> Response
|
2018-04-13 16:02:01 -07:00
|
|
|
where
|
2020-01-29 11:15:13 +03:00
|
|
|
S: Stream<Item = Result<Bytes, E>> + Unpin + 'static,
|
2019-11-19 18:54:19 +06:00
|
|
|
E: Into<Error> + 'static,
|
2018-03-07 17:40:13 -08:00
|
|
|
{
|
2018-11-18 13:48:42 -08:00
|
|
|
self.body(Body::from_message(BodyStream::new(stream)))
|
2018-03-07 17:40:13 -08:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
/// Set a json body and generate `Response`
|
2018-08-08 11:11:15 -06:00
|
|
|
///
|
2018-10-05 11:04:59 -07:00
|
|
|
/// `ResponseBuilder` can not be used after this call.
|
2021-01-18 03:14:29 -09:00
|
|
|
pub fn json<T>(&mut self, value: T) -> Response
|
|
|
|
where
|
|
|
|
T: ops::Deref,
|
|
|
|
T::Target: Serialize,
|
|
|
|
{
|
|
|
|
match serde_json::to_string(&*value) {
|
2018-03-30 23:07:33 -07:00
|
|
|
Ok(body) => {
|
2019-02-07 21:16:46 -08:00
|
|
|
let contains = if let Some(parts) = parts(&mut self.head, &self.err) {
|
|
|
|
parts.headers.contains_key(header::CONTENT_TYPE)
|
2018-04-13 16:02:01 -07:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2021-01-15 02:11:10 +00:00
|
|
|
|
2018-03-30 23:07:33 -07:00
|
|
|
if !contains {
|
2021-01-15 02:11:10 +00:00
|
|
|
self.insert_header(header::ContentType(mime::APPLICATION_JSON));
|
2018-03-30 23:07:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
self.body(Body::from(body))
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
2018-11-18 13:48:42 -08:00
|
|
|
Err(e) => Error::from(e).into(),
|
2017-11-27 10:39:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 23:07:33 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
/// Set an empty body and generate `Response`
|
2017-12-20 23:19:21 -08:00
|
|
|
///
|
2018-10-05 11:04:59 -07:00
|
|
|
/// `ResponseBuilder` can not be used after this call.
|
2018-11-18 13:48:42 -08:00
|
|
|
pub fn finish(&mut self) -> Response {
|
|
|
|
self.body(Body::Empty)
|
2017-10-23 23:52:20 -07:00
|
|
|
}
|
2017-12-31 17:26:32 -08:00
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
/// This method construct new `ResponseBuilder`
|
|
|
|
pub fn take(&mut self) -> ResponseBuilder {
|
|
|
|
ResponseBuilder {
|
2019-02-07 21:16:46 -08:00
|
|
|
head: self.head.take(),
|
2017-12-31 17:26:32 -08:00
|
|
|
err: self.err.take(),
|
|
|
|
cookies: self.cookies.take(),
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
2017-12-15 20:00:12 -08:00
|
|
|
#[inline]
|
2018-04-13 16:02:01 -07:00
|
|
|
fn parts<'a>(
|
2019-04-06 15:02:02 -07:00
|
|
|
parts: &'a mut Option<BoxedResponseHead>,
|
2018-10-29 16:39:46 -07:00
|
|
|
err: &Option<HttpError>,
|
2019-04-06 15:02:02 -07:00
|
|
|
) -> Option<&'a mut ResponseHead> {
|
2017-10-10 16:03:32 -07:00
|
|
|
if err.is_some() {
|
2018-04-13 16:02:01 -07:00
|
|
|
return None;
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
2019-04-06 15:02:02 -07:00
|
|
|
parts.as_mut().map(|r| &mut **r)
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
2017-10-22 22:54:11 -07:00
|
|
|
|
2019-03-10 15:30:31 -07:00
|
|
|
/// Convert `Response` to a `ResponseBuilder`. Body get dropped.
|
|
|
|
impl<B> From<Response<B>> for ResponseBuilder {
|
|
|
|
fn from(res: Response<B>) -> ResponseBuilder {
|
|
|
|
// If this response has cookies, load them into a jar
|
|
|
|
let mut jar: Option<CookieJar> = None;
|
|
|
|
for c in res.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResponseBuilder {
|
|
|
|
head: Some(res.head),
|
|
|
|
err: None,
|
|
|
|
cookies: jar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert `ResponseHead` to a `ResponseBuilder`
|
|
|
|
impl<'a> From<&'a ResponseHead> for ResponseBuilder {
|
|
|
|
fn from(head: &'a ResponseHead) -> ResponseBuilder {
|
|
|
|
// If this response has cookies, load them into a jar
|
|
|
|
let mut jar: Option<CookieJar> = None;
|
2019-03-23 09:40:20 -07:00
|
|
|
|
2019-03-29 21:13:39 -07:00
|
|
|
let cookies = CookieIter {
|
2019-04-06 15:02:02 -07:00
|
|
|
iter: head.headers.get_all(header::SET_COOKIE),
|
2019-03-29 21:13:39 -07:00
|
|
|
};
|
|
|
|
for c in 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);
|
2019-03-10 15:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:06:21 -07:00
|
|
|
let mut msg = BoxedResponseHead::new(head.status);
|
2019-03-10 15:30:31 -07:00
|
|
|
msg.version = head.version;
|
|
|
|
msg.reason = head.reason;
|
2021-02-09 22:59:17 +00:00
|
|
|
|
|
|
|
for (k, v) in head.headers.iter() {
|
2019-04-07 23:06:21 -07:00
|
|
|
msg.headers.append(k.clone(), v.clone());
|
|
|
|
}
|
2021-02-09 22:59:17 +00:00
|
|
|
|
2019-03-27 10:38:01 -07:00
|
|
|
msg.no_chunking(!head.chunked());
|
2019-03-10 15:30:31 -07:00
|
|
|
|
|
|
|
ResponseBuilder {
|
|
|
|
head: Some(msg),
|
|
|
|
err: None,
|
|
|
|
cookies: jar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 21:34:04 +06:00
|
|
|
impl Future for ResponseBuilder {
|
|
|
|
type Output = Result<Response, Error>;
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2019-12-08 00:46:51 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-21 21:34:04 +06:00
|
|
|
Poll::Ready(Ok(self.finish()))
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2019-06-01 17:57:25 +06:00
|
|
|
impl fmt::Debug for ResponseBuilder {
|
2019-12-08 00:46:51 +06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-06-01 17:57:25 +06:00
|
|
|
let head = self.head.as_ref().unwrap();
|
|
|
|
|
|
|
|
let res = writeln!(
|
|
|
|
f,
|
|
|
|
"\nResponseBuilder {:?} {}{}",
|
|
|
|
head.version,
|
|
|
|
head.status,
|
|
|
|
head.reason.unwrap_or(""),
|
|
|
|
);
|
|
|
|
let _ = writeln!(f, " headers:");
|
|
|
|
for (key, val) in head.headers.iter() {
|
|
|
|
let _ = writeln!(f, " {:?}: {:?}", key, val);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
/// Helper converters
|
2018-10-05 11:04:59 -07:00
|
|
|
impl<I: Into<Response>, E: Into<Error>> From<Result<I, E>> for Response {
|
2017-11-28 13:52:53 -08:00
|
|
|
fn from(res: Result<I, E>) -> Self {
|
|
|
|
match res {
|
|
|
|
Ok(val) => val.into(),
|
|
|
|
Err(err) => err.into().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
impl From<ResponseBuilder> for Response {
|
|
|
|
fn from(mut builder: ResponseBuilder) -> Self {
|
2018-03-30 23:07:33 -07:00
|
|
|
builder.finish()
|
2017-11-29 09:17:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl From<&'static str> for Response {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: &'static str) -> Self {
|
2018-10-05 11:04:59 -07:00
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl From<&'static [u8]> for Response {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: &'static [u8]) -> Self {
|
2018-10-05 11:04:59 -07:00
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl From<String> for Response {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: String) -> Self {
|
2018-10-05 11:04:59 -07:00
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl<'a> From<&'a String> for Response {
|
|
|
|
fn from(val: &'a String) -> Self {
|
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-11-18 13:48:42 -08:00
|
|
|
.body(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Bytes> for Response {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: Bytes) -> Self {
|
2018-10-05 11:04:59 -07:00
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl From<BytesMut> for Response {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: BytesMut) -> Self {
|
2018-10-05 11:04:59 -07:00
|
|
|
Response::Ok()
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 22:54:11 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-01-15 02:11:10 +00:00
|
|
|
use serde_json::json;
|
|
|
|
|
2017-10-22 22:54:11 -07:00
|
|
|
use super::*;
|
2018-12-06 15:03:01 -08:00
|
|
|
use crate::body::Body;
|
2019-04-06 15:02:02 -07:00
|
|
|
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE, SET_COOKIE};
|
2021-02-11 22:58:35 +00:00
|
|
|
use crate::HttpMessage;
|
2018-06-25 10:58:04 +06:00
|
|
|
|
2017-12-13 11:16:26 -08:00
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp = Response::Ok()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((COOKIE, HeaderValue::from_static("cookie1=value1; ")))
|
|
|
|
.append_header((COOKIE, HeaderValue::from_static("cookie2=value2; ")))
|
2018-03-30 23:07:33 -07:00
|
|
|
.finish();
|
2017-12-13 11:16:26 -08:00
|
|
|
let dbg = format!("{:?}", resp);
|
2018-10-05 11:04:59 -07:00
|
|
|
assert!(dbg.contains("Response"));
|
2017-12-13 11:16:26 -08:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:16:26 -08:00
|
|
|
#[test]
|
|
|
|
fn test_response_cookies() {
|
2019-03-23 09:40:20 -07:00
|
|
|
let req = crate::test::TestRequest::default()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((COOKIE, "cookie1=value1"))
|
|
|
|
.append_header((COOKIE, "cookie2=value2"))
|
2019-03-05 13:16:26 -08:00
|
|
|
.finish();
|
|
|
|
let cookies = req.cookies().unwrap();
|
|
|
|
|
|
|
|
let resp = Response::Ok()
|
|
|
|
.cookie(
|
2019-03-23 09:40:20 -07:00
|
|
|
crate::http::Cookie::build("name", "value")
|
2019-03-05 13:16:26 -08:00
|
|
|
.domain("www.rust-lang.org")
|
|
|
|
.path("/test")
|
|
|
|
.http_only(true)
|
2020-06-19 14:34:14 +01:00
|
|
|
.max_age(time::Duration::days(1))
|
2019-03-05 13:16:26 -08:00
|
|
|
.finish(),
|
|
|
|
)
|
2021-02-09 22:59:17 +00:00
|
|
|
.del_cookie(&cookies[0])
|
2019-03-05 13:16:26 -08:00
|
|
|
.finish();
|
|
|
|
|
2021-02-09 22:59:17 +00:00
|
|
|
let mut val = resp
|
2019-03-05 13:16:26 -08:00
|
|
|
.headers()
|
2019-04-06 15:02:02 -07:00
|
|
|
.get_all(SET_COOKIE)
|
2019-03-05 13:16:26 -08:00
|
|
|
.map(|v| v.to_str().unwrap().to_owned())
|
2021-02-09 22:59:17 +00:00
|
|
|
.collect::<Vec<_>>();
|
2019-03-05 13:16:26 -08:00
|
|
|
val.sort();
|
2021-02-09 22:59:17 +00:00
|
|
|
|
|
|
|
// the .del_cookie call
|
2019-03-05 13:16:26 -08:00
|
|
|
assert!(val[0].starts_with("cookie1=; Max-Age=0;"));
|
2021-02-09 22:59:17 +00:00
|
|
|
|
|
|
|
// the .cookie call
|
2019-03-05 13:16:26 -08:00
|
|
|
assert_eq!(
|
|
|
|
val[1],
|
|
|
|
"name=value; HttpOnly; Path=/test; Domain=www.rust-lang.org; Max-Age=86400"
|
|
|
|
);
|
|
|
|
}
|
2017-10-22 22:54:11 -07:00
|
|
|
|
2018-06-12 13:49:07 +00:00
|
|
|
#[test]
|
|
|
|
fn test_update_response_cookies() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let mut r = Response::Ok()
|
2019-03-23 09:40:20 -07:00
|
|
|
.cookie(crate::http::Cookie::new("original", "val100"))
|
2018-06-12 13:49:07 +00:00
|
|
|
.finish();
|
|
|
|
|
2019-03-23 09:40:20 -07:00
|
|
|
r.add_cookie(&crate::http::Cookie::new("cookie2", "val200"))
|
2018-06-13 23:37:19 -07:00
|
|
|
.unwrap();
|
2019-03-23 09:40:20 -07:00
|
|
|
r.add_cookie(&crate::http::Cookie::new("cookie2", "val250"))
|
2018-06-13 23:37:19 -07:00
|
|
|
.unwrap();
|
2019-03-23 09:40:20 -07:00
|
|
|
r.add_cookie(&crate::http::Cookie::new("cookie3", "val300"))
|
2018-06-13 23:37:19 -07:00
|
|
|
.unwrap();
|
2018-06-12 13:49:07 +00:00
|
|
|
|
|
|
|
assert_eq!(r.cookies().count(), 4);
|
|
|
|
r.del_cookie("cookie2");
|
|
|
|
|
|
|
|
let mut iter = r.cookies();
|
|
|
|
let v = iter.next().unwrap();
|
2019-04-06 15:02:02 -07:00
|
|
|
assert_eq!((v.name(), v.value()), ("original", "val100"));
|
2021-02-09 22:59:17 +00:00
|
|
|
let v = iter.next().unwrap();
|
|
|
|
assert_eq!((v.name(), v.value()), ("cookie3", "val300"));
|
2018-06-12 13:49:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 14:29:22 -08:00
|
|
|
#[test]
|
|
|
|
fn test_basic_builder() {
|
2021-01-15 02:11:10 +00:00
|
|
|
let resp = Response::Ok().insert_header(("X-TEST", "value")).finish();
|
2017-12-20 23:36:52 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2017-11-28 14:29:22 -08:00
|
|
|
}
|
|
|
|
|
2017-10-22 22:54:11 -07:00
|
|
|
#[test]
|
|
|
|
fn test_upgrade() {
|
2018-11-19 14:57:12 -08:00
|
|
|
let resp = Response::build(StatusCode::OK)
|
|
|
|
.upgrade("websocket")
|
|
|
|
.finish();
|
|
|
|
assert!(resp.upgrade());
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::UPGRADE).unwrap(),
|
|
|
|
HeaderValue::from_static("websocket")
|
|
|
|
);
|
2017-10-22 22:54:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_force_close() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp = Response::build(StatusCode::OK).force_close().finish();
|
2018-11-18 17:52:56 -08:00
|
|
|
assert!(!resp.keep_alive())
|
2017-10-22 22:54:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_content_type() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp = Response::build(StatusCode::OK)
|
2018-04-13 16:02:01 -07:00
|
|
|
.content_type("text/plain")
|
|
|
|
.body(Body::Empty);
|
2018-05-15 16:41:46 -07:00
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "text/plain")
|
2017-10-22 22:54:11 -07:00
|
|
|
}
|
2017-11-06 14:56:38 -08:00
|
|
|
|
2017-11-27 10:39:47 -08:00
|
|
|
#[test]
|
|
|
|
fn test_json() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp = Response::build(StatusCode::OK).json(vec!["v1", "v2", "v3"]);
|
2018-03-05 19:28:42 -08:00
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
2018-03-06 00:43:25 -08:00
|
|
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
2018-11-21 07:49:24 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
2017-11-27 10:39:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_json_ct() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp = Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((CONTENT_TYPE, "text/json"))
|
2021-01-18 03:14:29 -09:00
|
|
|
.json(&vec!["v1", "v2", "v3"]);
|
2018-08-08 11:58:56 -06:00
|
|
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
|
|
|
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
2018-11-21 07:49:24 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"[\"v1\",\"v2\",\"v3\"]");
|
2018-08-08 11:58:56 -06:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:33:52 +08:00
|
|
|
#[test]
|
|
|
|
fn test_serde_json_in_body() {
|
|
|
|
use serde_json::json;
|
|
|
|
let resp =
|
|
|
|
Response::build(StatusCode::OK).body(json!({"test-key":"test-value"}));
|
|
|
|
assert_eq!(resp.body().get_ref(), br#"{"test-key":"test-value"}"#);
|
|
|
|
}
|
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = "test".into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = b"test".as_ref().into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = "test".to_owned().into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2018-04-13 16:02:01 -07:00
|
|
|
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = (&"test".to_owned()).into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2018-04-13 16:02:01 -07:00
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
let b = Bytes::from_static(b"test");
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = b.into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2017-12-03 14:22:04 -08:00
|
|
|
let b = Bytes::from_static(b"test");
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = b.into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-12-03 14:22:04 -08:00
|
|
|
|
|
|
|
let b = BytesMut::from("test");
|
2018-10-05 11:04:59 -07:00
|
|
|
let resp: Response = b.into();
|
2017-12-03 14:22:04 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2019-04-06 15:02:02 -07:00
|
|
|
|
2017-12-03 14:22:04 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
2018-03-30 14:30:24 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_into_builder() {
|
2018-10-05 11:04:59 -07:00
|
|
|
let mut resp: Response = "test".into();
|
2018-03-30 14:30:24 -07:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
|
2019-03-23 09:40:20 -07:00
|
|
|
resp.add_cookie(&crate::http::Cookie::new("cookie1", "val100"))
|
2018-06-13 23:37:19 -07:00
|
|
|
.unwrap();
|
2018-06-12 13:49:07 +00:00
|
|
|
|
2019-03-10 15:30:31 -07:00
|
|
|
let mut builder: ResponseBuilder = resp.into();
|
2018-03-30 23:07:33 -07:00
|
|
|
let resp = builder.status(StatusCode::BAD_REQUEST).finish();
|
2018-03-30 14:30:24 -07:00
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
2018-06-12 13:49:07 +00:00
|
|
|
|
|
|
|
let cookie = resp.cookies().next().unwrap();
|
|
|
|
assert_eq!((cookie.name(), cookie.value()), ("cookie1", "val100"));
|
2018-03-30 14:30:24 -07:00
|
|
|
}
|
2021-01-15 02:11:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn response_builder_header_insert_kv() {
|
|
|
|
let mut res = Response::Ok();
|
|
|
|
res.insert_header(("Content-Type", "application/octet-stream"));
|
|
|
|
let res = res.finish();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get("Content-Type"),
|
|
|
|
Some(&HeaderValue::from_static("application/octet-stream"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn response_builder_header_insert_typed() {
|
|
|
|
let mut res = Response::Ok();
|
|
|
|
res.insert_header(header::ContentType(mime::APPLICATION_OCTET_STREAM));
|
|
|
|
let res = res.finish();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
res.headers().get("Content-Type"),
|
|
|
|
Some(&HeaderValue::from_static("application/octet-stream"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn response_builder_header_append_kv() {
|
|
|
|
let mut res = Response::Ok();
|
|
|
|
res.append_header(("Content-Type", "application/octet-stream"));
|
|
|
|
res.append_header(("Content-Type", "application/json"));
|
|
|
|
let res = res.finish();
|
|
|
|
|
|
|
|
let headers: Vec<_> = res.headers().get_all("Content-Type").cloned().collect();
|
|
|
|
assert_eq!(headers.len(), 2);
|
|
|
|
assert!(headers.contains(&HeaderValue::from_static("application/octet-stream")));
|
|
|
|
assert!(headers.contains(&HeaderValue::from_static("application/json")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn response_builder_header_append_typed() {
|
|
|
|
let mut res = Response::Ok();
|
|
|
|
res.append_header(header::ContentType(mime::APPLICATION_OCTET_STREAM));
|
|
|
|
res.append_header(header::ContentType(mime::APPLICATION_JSON));
|
|
|
|
let res = res.finish();
|
|
|
|
|
|
|
|
let headers: Vec<_> = res.headers().get_all("Content-Type").cloned().collect();
|
|
|
|
assert_eq!(headers.len(), 2);
|
|
|
|
assert!(headers.contains(&HeaderValue::from_static("application/octet-stream")));
|
|
|
|
assert!(headers.contains(&HeaderValue::from_static("application/json")));
|
|
|
|
}
|
2017-10-22 22:54:11 -07:00
|
|
|
}
|