1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

mod builder;
pub use builder::ResponseBuilder;

use crate::header::{ResponseHeader, StatusCode};
use crate::body::Body;

#[cfg(feature = "timeout")]
use std::time::Duration;

/// The response created from a server.
#[derive(Debug)]
pub struct Response {
	pub header: ResponseHeader,
	// if you overide the body
	// you should pobably reset the content-length
	pub body: Body
}

impl Response {

	/// Creates a new `Response`.
	pub fn new(header: ResponseHeader, body: Body) -> Self {
		Self { header, body }
	}

	/// Creates a new `Response` with a builder.
	pub fn builder() -> ResponseBuilder {
		ResponseBuilder::new()
	}

	/// Get the response header by reference.
	pub fn header(&self) -> &ResponseHeader {
		&self.header
	}

	/// Takes the body replacing it with an empty one.
	/// 
	/// ## Note
	/// If you use the builder to create a `Response`
	/// you should probably reset the `content-length` header.
	pub fn take_body(&mut self) -> Body {
		self.body.take()
	}

	/// Takes the body adding a timeout to it.
	/// 
	/// ## Note
	/// If you use the builder to create a `Response`
	/// you should probably reset the `content-length` header.
	#[cfg(feature = "timeout")]
	pub fn body_with_timeout(&mut self, timeout: Duration) -> crate::body::BodyWithTimeout {
		self.body.take().add_timeout(timeout)
	}
}

impl From<Body> for Response {
	fn from(body: Body) -> Self {
		Self::builder()
			.body(body)
			.build()
	}
}

impl From<StatusCode> for Response {
	fn from(status_code: StatusCode) -> Self {
		Self::builder()
			.status_code(status_code)
			.build()
	}
}