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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

use std::net::SocketAddr;

#[cfg(feature = "encdec")]
use std::borrow::Cow;

use http as raw;

mod statuscode;
pub use statuscode::StatusCode;

mod method;
pub use method::Method;

mod version;
pub use version::Version;

mod uri;
pub use uri::Uri;

mod contenttype;
pub use contenttype::{ContentType, Mime, AnyMime, Charset};

mod into_header_value;
pub use into_header_value::IntoHeaderValue;

// TODO: add url encoding to the http header values
// allowing to insert any utf8 string

/// Contains all http header values.
/// 
/// This is really similar to `http::header::HeaderMap` except
/// that is uses IntoHeaderValue for inserting. And it does not allow
/// multiples values for a given key.
#[derive(Debug, Clone)]
pub struct HeaderValues(raw::HeaderMap<raw::HeaderValue>);

impl HeaderValues {

	/// Creates a new empty `HeaderValues`.
	pub fn new() -> Self {
		Self(raw::HeaderMap::new())
	}

	//  This is not documented but won't break between minor changes.
	/// Creates a new `HeaderValues` from it's inner type.
	#[doc(hidden)]
	pub fn from_inner(raw: raw::HeaderMap<raw::HeaderValue>) -> Self {
		Self(raw)
	}

	/// Insert a new key and value into the header.
	/// 
	/// If a value to this key is already present
	/// that value is dropped.
	/// 
	/// ## Panics
	/// If the value is not a valid HeaderValue.
	pub fn insert<K, V>(&mut self, key: K, val: V)
	where
		K: raw::header::IntoHeaderName,
		V: IntoHeaderValue
	{
		let val = val.try_into_header_value()
			.expect("invalid HeaderValue");
		let _ = self.0.insert(key, val);
	}

	/// Insert a new key and value into the header. Returning
	/// None if the value is not valid.
	/// 
	/// If a value to this key is already present
	/// that value is dropped.
	pub fn try_insert<K, V>(&mut self, key: K, val: V) -> Option<()>
	where
		K: raw::header::IntoHeaderName,
		V: IntoHeaderValue {
		let _ = self.0.insert(key, val.try_into_header_value()?);
		Some(())
	}

	/// Insert a new key and value into the header. Percent encoding
	/// the value if necessary.
	#[cfg(feature = "encdec")]
	pub fn encode<K, V>(&mut self, key: K, val: V)
	where
		K: raw::header::IntoHeaderName,
		V: IntoHeaderValue
	{
		let val = val.into_enc_header_value();
		let _ = self.0.insert(key, val);
	}

	/// Returns the value if it exists.
	pub fn get<K>(&self, key: K) -> Option<&raw::HeaderValue>
	where K: raw::header::AsHeaderName {
		self.0.get(key)
	}

	/// Returns the value mutably if it exists.
	pub fn get_mut<K>(&mut self, key: K) -> Option<&mut raw::HeaderValue>
	where K: raw::header::AsHeaderName {
		self.0.get_mut(key)
	}

	/// Returns the value as a string if it exists and is valid.
	pub fn get_str<K>(&self, key: K) -> Option<&str>
	where K: raw::header::AsHeaderName {
		self.get(key)
			.and_then(|v| {
				v.to_str().ok()
			})
	}

	/// Returns the value percent decoded as a string if it exists and is valid.
	#[cfg(feature = "encdec")]
	pub fn decode<K>(&self, key: K) -> Option<Cow<'_, str>>
	where K: raw::header::AsHeaderName {
		self.get(key)
			.and_then(|v| {
				percent_encoding::percent_decode(v.as_bytes())
					.decode_utf8()
					.ok()
			})
	}

	/// Insert a new key and a serializeable value. The value will be serialized
	/// as json and percent encoded.
	/// 
	/// Returns `None` if the value could not be serialized or inserted.
	#[cfg(all(feature = "json", feature = "encdec"))]
	pub fn serialize<K, V: ?Sized>(&mut self, key: K, val: &V) -> Option<()>
	where
		K: raw::header::IntoHeaderName,
		V: serde::Serialize
	{
		let v = serde_json::to_string(val).ok()?;
		Some(self.encode(key, v))
	}

	/// Deserializes a given value. Returning `None` if the value
	/// does not exist or is not valid json.
	#[cfg(all(feature = "json", feature = "encdec"))]
	pub fn deserialize<K, D>(&self, key: K) -> Option<D>
	where
		K: raw::header::AsHeaderName,
		D: serde::de::DeserializeOwned {
		let v = self.decode(key)?;
		serde_json::from_str(v.as_ref()).ok()
	}

	//  This is not documented but won't break between minor changes.
	/// Returns the inner `HeaderMap`.
	#[doc(hidden)]
	pub fn into_inner(self) -> raw::HeaderMap<raw::HeaderValue> {
		self.0
	}

}

/// RequestHeader received from a client.
#[derive(Debug, Clone)]
pub struct RequestHeader {
	pub address: SocketAddr,
	pub method: Method,
	pub uri: Uri,
	pub version: Version,
	pub values: HeaderValues
}

impl RequestHeader {

	/// Returns the ip address of the requesting client.
	pub fn address(&self) -> &SocketAddr {
		&self.address
	}

	/// Returns the requesting method.
	pub fn method(&self) -> &Method {
		&self.method
	}

	/// Returns the requesting uri.
	pub fn uri(&self) -> &Uri {
		&self.uri
	}

	/// Returns the http version used for the request.
	pub fn version(&self) -> &Version {
		&self.version
	}

	/// Returns all header values.
	pub fn values(&self) -> &HeaderValues {
		&self.values
	}

	/// Returns a header value from it's key
	/// if it exists and is valid ascii.
	/// 
	/// ## Note
	/// If you wan't a decoded value use `self.values().decode(key)`.
	pub fn value<K>(&self, key: K) -> Option<&str>
	where K: raw::header::AsHeaderName {
		self.values.get_str(key)
	}

}

/// ResponseHeader created from a server.
/// 
/// To create a ResponseHeader you should probably
/// use ResponseHeaderBuilder.
#[derive(Debug, Clone)]
pub struct ResponseHeader {
	pub version: Version,
	pub status_code: StatusCode,
	pub content_type: ContentType,
	pub values: HeaderValues
}

impl ResponseHeader {

	/// Returns the http version.
	pub fn version(&self) -> &Version {
		&self.version
	}

	/// Returns the used status code.
	pub fn status_code(&self) -> &StatusCode {
		&self.status_code
	}

	/// Returns the used content type.
	pub fn content_type(&self) -> &ContentType {
		&self.content_type
	}

	/// Returns all header values.
	pub fn values(&self) -> &HeaderValues {
		&self.values
	}

	/// Returns a header value from it's key
	/// if it exists and is valid ascii.
	/// 
	/// ## Note
	/// If you wan't a decoded value use `self.values().decode(key)`.
	pub fn value<K>(&self, key: K) -> Option<&str>
	where K: raw::header::AsHeaderName {
		self.values.get_str(key)
	}

}

impl Default for ResponseHeader {
	fn default() -> Self {
		Self {
			version: Version::default(),
			status_code: StatusCode::Ok,
			content_type: Mime::Text.into(),
			values: HeaderValues::new()
		}
	}
}

// TODO: can we remove the version???

/// A build to create a `ResponseHeader`.
#[derive(Debug, Clone)]
pub struct ResponseHeaderBuilder {
	pub version: Option<Version>,
	pub status_code: Option<StatusCode>,
	pub content_type: Option<ContentType>,
	pub values: HeaderValues
}

impl ResponseHeaderBuilder {

	/// Creates a new builder.
	pub fn new() -> Self {
		Self {
			version: None,
			status_code: None,
			content_type: None,
			values: HeaderValues::new()
		}
	}

	/// Sets the http version.
	pub fn version(&mut self, version: Version) {
		self.version = Some(version);
	}

	/// Sets the status code.
	pub fn status_code(&mut self, status_code: StatusCode) {
		self.status_code = Some(status_code);
	}

	/// Sets the content type.
	pub fn content_type(&mut self, content_type: impl Into<ContentType>) {
		self.content_type = Some(content_type.into());
	}

	// /// Sets a header value.
	// /// 
	// /// ## Panics
	// /// If the value is not a valid `HeaderValue`.
	// pub fn header<K, V>(&mut self, key: K, val: V)
	// where
	// 	K: raw::header::IntoHeaderName,
	// 	V: IntoHeaderValue {
	// 	self.values.insert(key, val);
	// }

	/// Returns `HeaderValues` mutably.
	pub fn values_mut(&mut self) -> &mut HeaderValues {
		&mut self.values
	}

	/// Builds a ResponseHeader. Using default values for all
	/// not configured fields.
	pub fn build(self) -> ResponseHeader {
		ResponseHeader {
			version: self.version.unwrap_or(Version::default()),
			status_code: self.status_code.unwrap_or(StatusCode::Ok),
			content_type: self.content_type.unwrap_or(ContentType::empty()),
			values: self.values
		}
	}

}


#[cfg(test)]
mod tests {
	#![allow(unused_imports)]

	use super::*;
	use serde::{Serialize, Deserialize};

	#[cfg(feature = "encdec")]
	#[test]
	fn test_encdec() {

		let mut values = HeaderValues::new();
		values.encode("Rocket", "🚀 Rocket");
		let s = values.get_str("Rocket").unwrap();
		assert_eq!(s, "%F0%9F%9A%80 Rocket");

		let s = values.decode("Rocket").unwrap();
		assert_eq!(s, "🚀 Rocket");

	}

	#[cfg(all(feature="encdec", feature="json"))]
	#[test]
	fn test_serde() {

		#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
		struct Value {
			text: String,
			number: usize
		}

		let mut values = HeaderValues::new();
		let val = Value {
			text: "🚀 Rocket".into(),
			number: 42
		};
		values.serialize("Value", &val).unwrap();

		let s = values.get_str("Value").unwrap();
		assert_eq!(s, "{\"text\":\"%F0%9F%9A%80 Rocket\",\"number\":42}");

		let n_val: Value = values.deserialize("Value").unwrap();
		assert_eq!(n_val, val);

	}

}