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
use super::{file, partial_file, Caching, Range};
use super::static_files::CachingBuilder;
use crate::{Request, Response, IntoRoute, Route, Data, Error};
use crate::routes::check_static;
use crate::header::{RequestHeader, Method};
use crate::util::PinnedFuture;
use crate::into::IntoResponse;

use std::io;
use std::time::Duration;


pub fn serve_memory_file(
	path: &'static str,
	bytes: &'static [u8],
	req: &Request,
	caching: Option<Caching>
) -> io::Result<Response> {
	// check caching and if the etag matches return NOT_MODIFIED
	if matches!(&caching, Some(c) if c.if_none_match(req.header())) {
		return Ok(caching.unwrap().into_response())
	}

	let range = Range::parse(req.header());

	let mut res = match range {
		Some(range) => {
			partial_file::serve_memory_partial_file(path, bytes, range)?
		},
		None => file::serve_memory_file(path, bytes)?
	};

	// set etag
	if let Some(caching) = caching {
		caching.complete_header(&mut res.header);
	}

	Ok(res)
}


/// Static get handler which servers/returns a file.
/// 
/// ## Example
/// ```
/// # use fire_http as fire;
/// use fire::fs::MemoryFile;
/// use fire::memory_file;
///
/// const INDEX: MemoryFile = memory_file!(
/// 	"/",
/// 	"../../examples/www/hello_world.html"
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemoryFile {
	uri: &'static str,
	path: &'static str,
	bytes: &'static [u8],
	caching: CachingBuilder
}

impl MemoryFile {
	/// Creates a `MemoryFile` with Default caching settings
	pub const fn new(
		uri: &'static str,
		path: &'static str,
		bytes: &'static [u8]
	) -> Self {
		Self { uri, path, bytes, caching: CachingBuilder::Default }
	}

	pub const fn no_cache(
		uri: &'static str,
		path: &'static str,
		bytes: &'static [u8]
	) -> Self {
		Self { uri, path, bytes, caching: CachingBuilder::None }
	}

	pub const fn cache_with_age(
		uri: &'static str,
		path: &'static str,
		bytes: &'static [u8],
		max_age: Duration
	) -> Self {
		Self { uri, path, bytes, caching: CachingBuilder::MaxAge(max_age) }
	}
}

impl IntoRoute for MemoryFile {
	type IntoRoute = MemoryFileRoute;

	fn into_route(self) -> MemoryFileRoute {
		MemoryFileRoute {
			uri: self.uri,
			path: self.path,
			bytes: self.bytes,
			caching: self.caching.into()
		}
	}
}

#[doc(hidden)]
pub struct MemoryFileRoute {
	uri: &'static str,
	path: &'static str,
	bytes: &'static [u8],
	caching: Option<Caching>
}

impl Route for MemoryFileRoute {
	fn check(&self, header: &RequestHeader) -> bool {
		header.method() == &Method::GET &&
		check_static(header.uri().path(), self.uri)
	}

	fn validate_data(&self, _data: &Data) {}

	fn call<'a>(
		&'a self,
		req: &'a mut Request,
		_: &'a Data
	) -> PinnedFuture<'a, crate::Result<Response>> {
		let caching = self.caching.clone();

		PinnedFuture::new(async move {
			serve_memory_file(self.path, self.bytes, &req, caching)
				.map_err(Error::from_client_io)
		})
	}

}