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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

use crate::util::convert_hyper_req_to_fire_req;
use crate::fire::RequestConfigs;
use crate::util::HeaderError;

use std::net::SocketAddr;
use std::mem;
use std::time::Duration;

use http::header::RequestHeader;
use http::body::{Body, BodyWithTimeout};

pub type HyperRequest = hyper::Request<hyper::Body>;

/// The Request that get's returned to every route.
#[derive(Debug)]
pub struct Request {
	header: RequestHeader,
	body: BodyWithTimeout
}

impl Request {

	pub(crate) fn new(header: RequestHeader, body: BodyWithTimeout) -> Self {
		Self {header, body}
	}

	/// Takes the current body and leaves the request with an empty body.
	pub fn take_body(&mut self) -> BodyWithTimeout {
		self.body.take()
	}

	/// Takes the current body and leaves the request an empty body.
	/// 
	/// This discard the timout associated with the body.
	pub fn take_raw_body(&mut self) -> Body {
		self.body.body_mut().take()
	}

	/// Checks if the request body is empty.
	pub fn is_empty(&self) -> bool {
		self.body.is_empty()
	}

	/// Returns a reference to the body contained in the request.
	pub fn body(&self) -> &BodyWithTimeout {
		&self.body
	}

	/// Returns a mutable reference to the body contained in the request.
	pub fn body_mut(&mut self) -> &mut BodyWithTimeout {
		&mut self.body
	}

	/// Returns a reference to the body contained in the request without a
	/// timeout being associated with it.
	pub fn raw_body(&self) -> &Body {
		self.body.body()
	}

	/// Returns a reference to the body contained in the request without a
	/// timeout being associated with it.
	pub fn raw_body_mut(&mut self) -> &mut Body {
		self.body.body_mut()
	}

	/// Changes the size limit of the current request.
	pub fn set_size_limit(&mut self, size_limit: usize) {
		self.body.set_size_limit(size_limit);
	}

	/// Changes the timeout of the current request.
	pub fn set_timeout(&mut self, duration: Duration) {
		self.body.set_timeout(duration);
	}

	/// Returns a reference to the request header.
	pub fn header(&self) -> &RequestHeader {
		&self.header
	}

	/// Returns a mutable reference to the request header.
	pub fn header_mut(&mut self) -> &mut RequestHeader {
		&mut self.header
	}

	/// Tries to deserialize the request body.
	/// 
	/// ## Errors
	/// - If the header `content-type` does not contain `application/json`.
	/// - If the body does not contain a valid json or some data is missing.
	///
	/// ## Note
	/// The request will now contain an empty body.
	//
	// this is a client error
	// because well, its in request
	#[cfg(feature = "json")]
	pub async fn deserialize<D>(&mut self) -> crate::Result<D>
	where D: serde::de::DeserializeOwned {
		use crate::error::{Error, ClientErrorKind};
		use http::header::Mime;
		use http::body::JsonError;
		// try to read mime
		// this will not work if content-type has charset
		// TODO allow charset (probably implement Parse for ContentType)
		let raw_content_type = self.header()
			.value("content-type")
			.ok_or(DeserializeError::NoContentType)?;
		let mime = Mime::try_from_mime(raw_content_type)
			.ok_or_else(|| DeserializeError::UnknownContentType(
				raw_content_type.to_string()
			))?;

		if !matches!(mime, Mime::Json) {
			return Err(DeserializeError::WrongMimeType(mime).into())
		}

		// now parse body
		self.body.take().deserialize().await
			.map_err(|e| match e {
				JsonError::IoError(e) => {
					Error::new(ClientErrorKind::BadRequest, e)
				},
				JsonError::SerdeJson(e) => DeserializeError::JsonError(e).into()
			})
	}

}

#[cfg(feature = "json")]
mod json_error {

	use crate::error::{Error, ClientErrorKind};
	use std::fmt;
	use http::header::Mime;

	#[derive(Debug)]
	pub enum DeserializeError {
		NoContentType,
		UnknownContentType(String),
		WrongMimeType(Mime),
		JsonError(serde_json::Error)
	}

	impl From<DeserializeError> for Error {
		fn from(e: DeserializeError) -> Self {
			Self::new(ClientErrorKind::BadRequest, e)
		}
	}

	impl fmt::Display for DeserializeError {
		fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
			fmt::Debug::fmt(self, f)
		}
	}

	impl std::error::Error for DeserializeError {
		fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
			match self {
				Self::JsonError(e) => Some(e),
				_ => None
			}
		}
	}
}

#[cfg(feature = "json")]
use json_error::DeserializeError;


/// a container that contains a HyperRequest
/// or a Fire Request
/// if you receive this in a RawRoute
/// you always receiver a hyper request
#[derive(Debug)]
pub enum RequestBuilder<'a> {
	Hyper {
		hyper_req: HyperRequest,
		address: SocketAddr,
		configs: &'a RequestConfigs
	},
	Fire(Request),
	ConvertError(HeaderError)
}

impl<'a> RequestBuilder<'a> {

	pub(crate) fn new(
		hyper_req: HyperRequest,
		address: SocketAddr,
		configs: &'a RequestConfigs
	) -> Self {
		Self::Hyper {
			hyper_req,
			address,
			configs
		}
	}

	/// if you receive this in a raw request
	/// you can unwrap
	#[inline]
	pub fn hyper_mut(&mut self) -> Option<&mut HyperRequest> {
		match self {
			Self::Hyper { hyper_req, .. } => Some(hyper_req),
			_ => None
		}
	}

	/// if you receive this in a raw request
	/// you can unwrap
	#[inline]
	pub fn hyper_ref(&self) -> Option<&HyperRequest> {
		match self {
			Self::Hyper { hyper_req, .. } => Some(hyper_req),
			_ => None
		}
	}

	/// if this is called once
	/// the hyper request is gone
	/// if this returns Error
	/// You are not allowed to call it again
	/// it will panic
	pub fn fire_mut(&mut self) -> Result<&mut Request, HeaderError> {
		if let Self::Fire(f) = self {
			return Ok(f)
		}

		let this = mem::replace(self, Self::ConvertError(HeaderError::Unknown));
		match this.into_fire() {
			Ok(r) => {
				let _ = mem::replace(self, Self::Fire(r));
				Ok(match self {
					Self::Fire(r) => r,
					_ => unreachable!()
				})
			},
			Err(e) => {
				let _ = mem::replace(self, Self::ConvertError(e.clone()));
				Err(e)
			}
		}
	}

	pub fn into_fire(self) -> Result<Request, HeaderError> {
		match self {
			Self::Fire(r) => Ok(r),
			Self::Hyper { hyper_req, address, configs } => {
				convert_hyper_req_to_fire_req(hyper_req, address, configs)
			},
			Self::ConvertError(e) => Err(e)
		}
	}

}