rocket_include_static_resources/debug/
static_response.rs1use std::{io::Cursor, sync::Arc};
2
3use mime::Mime;
4
5use crate::{
6 EntityTag,
7 rocket::{
8 http::Status,
9 request::Request,
10 response::{self, Responder, Response},
11 },
12};
13
14#[derive(Debug)]
15struct StaticResponseInner {
16 mime: String,
17 data: Arc<[u8]>,
18 etag: String,
19}
20
21#[derive(Debug)]
22pub struct StaticResponse {
24 inner: Option<StaticResponseInner>,
25}
26
27impl StaticResponse {
28 #[inline]
29 pub(crate) fn build(mime: &Mime, data: Arc<[u8]>, etag: &EntityTag<'static>) -> StaticResponse {
30 StaticResponse {
31 inner: Some(StaticResponseInner {
32 mime: mime.to_string(),
33 data,
34 etag: etag.to_string(),
35 }),
36 }
37 }
38
39 #[inline]
40 pub(crate) const fn not_modified() -> StaticResponse {
41 StaticResponse {
42 inner: None
43 }
44 }
45}
46
47impl<'r, 'o: 'r> Responder<'r, 'o> for StaticResponse {
48 #[inline]
49 fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
50 let mut response = Response::build();
51
52 if let Some(inner) = self.inner {
53 response.raw_header("Etag", inner.etag);
54 response.raw_header("Content-Type", inner.mime);
55
56 response.sized_body(inner.data.len(), Cursor::new(inner.data));
57 } else {
58 response.status(Status::NotModified);
59 }
60
61 response.ok()
62 }
63}