autumn-web 0.2.0

An opinionated, convention-over-configuration web framework for Rust
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use axum::body::Body;
use axum::http::header::{
    CACHE_CONTROL, CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TYPE, EXPIRES, PRAGMA,
};
use axum::http::{HeaderValue, Request, Response, StatusCode};
use axum::middleware::Next;
use axum::response::IntoResponse;

pub const LIVE_RELOAD_PATH: &str = "/__autumn/live-reload";
const DEV_RELOAD_ENV: &str = "AUTUMN_DEV_RELOAD";
const DEV_RELOAD_STATE_ENV: &str = "AUTUMN_DEV_RELOAD_STATE";
const DEV_RELOAD_CACHE_CONTROL: &str = "no-store, no-cache, must-revalidate";

#[allow(dead_code)]
pub fn is_enabled() -> bool {
    is_enabled_with_env(&crate::config::OsEnv)
}

pub fn is_enabled_with_env(env: &dyn crate::config::Env) -> bool {
    env.var(DEV_RELOAD_ENV).is_ok() && env.var(DEV_RELOAD_STATE_ENV).is_ok()
}

pub async fn live_reload_state_handler() -> impl IntoResponse {
    let body = read_reload_state_body_with_env(&crate::config::OsEnv)
        .unwrap_or_else(|| r#"{"version":0,"kind":"full"}"#.to_owned());
    let mut response = Response::new(Body::from(body));
    *response.status_mut() = StatusCode::OK;
    let headers = response.headers_mut();
    headers.insert(
        CONTENT_TYPE,
        HeaderValue::from_static("application/json; charset=utf-8"),
    );
    apply_no_store(headers);
    response
}

pub async fn disable_static_cache(request: Request<Body>, next: Next) -> Response<Body> {
    let is_static = is_static_path(request.uri().path());
    let mut response = next.run(request).await;
    if is_static {
        apply_no_store(response.headers_mut());
    }
    response
}

pub async fn inject_live_reload(request: Request<Body>, next: Next) -> Response<Body> {
    let response = next.run(request).await;
    inject_live_reload_into_response(response).await
}

async fn inject_live_reload_into_response(response: Response<Body>) -> Response<Body> {
    if !is_html_response(&response) {
        return response;
    }

    let (mut parts, body) = response.into_parts();
    // Dev-only middleware wraps normal HTML page responses.
    let body = axum::body::to_bytes(body, usize::MAX)
        .await
        .expect("live reload middleware should only wrap infallible HTML bodies");
    let updated = inject_snippet(&body);

    if updated == body {
        return Response::from_parts(parts, Body::from(body));
    }

    if let Ok(value) = HeaderValue::from_str(&updated.len().to_string()) {
        parts.headers.insert(CONTENT_LENGTH, value);
    } else {
        parts.headers.remove(CONTENT_LENGTH);
    }

    Response::from_parts(parts, Body::from(updated))
}

fn read_reload_state_body_with_env(env: &dyn crate::config::Env) -> Option<String> {
    let path = env.var(DEV_RELOAD_STATE_ENV).ok()?;
    std::fs::read_to_string(path).ok()
}

fn is_html_response(response: &Response<Body>) -> bool {
    if response.headers().contains_key(CONTENT_ENCODING) {
        return false;
    }

    response
        .headers()
        .get(CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .is_some_and(|content_type| content_type.contains("text/html"))
}

fn is_static_path(path: &str) -> bool {
    path == "/static" || path.starts_with("/static/")
}

fn apply_no_store(headers: &mut axum::http::HeaderMap) {
    headers.insert(
        CACHE_CONTROL,
        HeaderValue::from_static(DEV_RELOAD_CACHE_CONTROL),
    );
    headers.insert(PRAGMA, HeaderValue::from_static("no-cache"));
    headers.insert(EXPIRES, HeaderValue::from_static("0"));
}

fn inject_snippet(body: &[u8]) -> Vec<u8> {
    let html = String::from_utf8_lossy(body);
    let snippet = live_reload_script();

    if let Some(index) = html.rfind("</body>") {
        let mut html = html.into_owned();
        html.insert_str(index, &snippet);
        return html.into_bytes();
    }

    if html.contains("<html") || html.contains("</html>") {
        let mut html = html.into_owned();
        html.push_str(&snippet);
        return html.into_bytes();
    }

    body.to_vec()
}

fn live_reload_script() -> String {
    format!(
        r#"<script>
(() => {{
  const endpoint = "{LIVE_RELOAD_PATH}";
  let version = null;
  let polling = false;

  function refreshStylesheets(nextVersion) {{
    let refreshed = 0;
    document.querySelectorAll('link[rel="stylesheet"]').forEach((link) => {{
      try {{
        const url = new URL(link.href, window.location.href);
        if (url.origin !== window.location.origin || !url.pathname.startsWith('/static/')) {{
          return;
        }}
        url.searchParams.set('__autumn_reload', String(nextVersion));
        link.href = url.toString();
        refreshed += 1;
      }} catch (_error) {{
      }}
    }});
    return refreshed > 0;
  }}

  async function poll() {{
    if (polling || document.visibilityState === 'hidden') {{
      return;
    }}
    polling = true;
    try {{
      const response = await fetch(endpoint + '?t=' + Date.now(), {{
        cache: 'no-store',
        headers: {{ Accept: 'application/json' }},
      }});
      if (!response.ok) {{
        return;
      }}

      const state = await response.json();
      if (version === null) {{
        version = state.version;
        return;
      }}
      if (state.version === version) {{
        return;
      }}

      version = state.version;
      if (state.kind === 'css' && refreshStylesheets(state.version)) {{
        return;
      }}

      window.location.reload();
    }} catch (_error) {{
    }} finally {{
      polling = false;
    }}
  }}

  window.addEventListener('pageshow', () => {{
    void poll();
  }});
  document.addEventListener('visibilitychange', () => {{
    if (document.visibilityState === 'visible') {{
      void poll();
    }}
  }});
  setInterval(() => {{
    void poll();
  }}, 700);
  void poll();
}})();
</script>"#
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::MockEnv;
    use axum::body::to_bytes;
    use axum::http::header::ACCEPT;
    use tower::ServiceExt;

    #[test]
    fn is_enabled_requires_both_env_vars() {
        {
            let env = MockEnv::new();
            assert!(!is_enabled_with_env(&env));
        }

        {
            let env = MockEnv::new().with(DEV_RELOAD_ENV, "1");
            assert!(!is_enabled_with_env(&env));
        }

        {
            let env = MockEnv::new()
                .with(DEV_RELOAD_ENV, "1")
                .with(DEV_RELOAD_STATE_ENV, "state.json");
            assert!(is_enabled_with_env(&env));
        }
    }

    #[tokio::test]
    async fn read_reload_state_body_defaults_when_state_missing() {
        let env = MockEnv::new().with(DEV_RELOAD_ENV, "1");

        let body = read_reload_state_body_with_env(&env)
            .unwrap_or_else(|| r#"{"version":0,"kind":"full"}"#.to_owned());
        let mut response = Response::new(Body::from(body));
        *response.status_mut() = StatusCode::OK;
        let headers = response.headers_mut();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("application/json; charset=utf-8"),
        );
        apply_no_store(headers);
        let response = response.into_response();
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(CACHE_CONTROL).unwrap(),
            DEV_RELOAD_CACHE_CONTROL
        );
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body bytes");
        assert_eq!(&body[..], br#"{"version":0,"kind":"full"}"#);
    }

    #[tokio::test]
    async fn read_reload_state_body_reads_file_when_present() {
        let tmp_file = tempfile::NamedTempFile::new().expect("failed to create temp file");
        let content = r#"{"version":42,"kind":"css"}"#;
        std::fs::write(tmp_file.path(), content).expect("failed to write to temp file");
        let env = MockEnv::new()
            .with(DEV_RELOAD_ENV, "1")
            .with(DEV_RELOAD_STATE_ENV, tmp_file.path().to_str().unwrap());

        let body = read_reload_state_body_with_env(&env).unwrap();
        let mut response = Response::new(Body::from(body));
        *response.status_mut() = StatusCode::OK;
        let headers = response.headers_mut();
        apply_no_store(headers);
        let response = response.into_response();
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(CACHE_CONTROL).unwrap(),
            DEV_RELOAD_CACHE_CONTROL
        );
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body bytes");
        assert_eq!(&body[..], content.as_bytes());
    }

    #[tokio::test]
    async fn disable_static_cache_only_marks_static_paths() {
        let app = axum::Router::new()
            .route("/static/demo.txt", axum::routing::get(|| async { "demo" }))
            .route("/page", axum::routing::get(|| async { "page" }))
            .layer(axum::middleware::from_fn(disable_static_cache));

        let static_response = app
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/static/demo.txt")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .expect("static response");
        assert_eq!(
            static_response.headers().get(CACHE_CONTROL).unwrap(),
            DEV_RELOAD_CACHE_CONTROL
        );

        let page_response = app
            .oneshot(Request::builder().uri("/page").body(Body::empty()).unwrap())
            .await
            .expect("page response");
        assert!(page_response.headers().get(CACHE_CONTROL).is_none());
    }

    #[tokio::test]
    async fn inject_live_reload_into_response_updates_html_and_length() {
        let mut response = Response::new(Body::from("<html><body><main>ok</main></body></html>"));
        response.headers_mut().insert(
            CONTENT_TYPE,
            HeaderValue::from_static("text/html; charset=utf-8"),
        );
        response
            .headers_mut()
            .insert(CONTENT_LENGTH, HeaderValue::from_static("1"));

        let response = inject_live_reload_into_response(response).await;
        let content_length = response
            .headers()
            .get(CONTENT_LENGTH)
            .and_then(|value| value.to_str().ok())
            .expect("content-length header")
            .to_owned();
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body bytes");
        let html = std::str::from_utf8(&body).expect("utf-8 html");

        assert!(html.contains(LIVE_RELOAD_PATH));
        assert_eq!(content_length, body.len().to_string());
    }

    #[tokio::test]
    async fn inject_live_reload_into_response_skips_encoded_html() {
        let mut response = Response::new(Body::from("<html><body>ok</body></html>"));
        response.headers_mut().insert(
            CONTENT_TYPE,
            HeaderValue::from_static("text/html; charset=utf-8"),
        );
        response
            .headers_mut()
            .insert(CONTENT_ENCODING, HeaderValue::from_static("gzip"));

        let response = inject_live_reload_into_response(response).await;
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body bytes");
        assert_eq!(&body[..], b"<html><body>ok</body></html>");
    }

    #[tokio::test]
    async fn inject_live_reload_middleware_leaves_non_html_responses_alone() {
        let app = axum::Router::new()
            .route(
                "/data",
                axum::routing::get(|| async {
                    ([(CONTENT_TYPE, "application/json")], r#"{"status":"ok"}"#)
                }),
            )
            .layer(axum::middleware::from_fn(inject_live_reload));

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/data")
                    .header(ACCEPT, "application/json")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .expect("json response");
        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("body bytes");
        assert_eq!(&body[..], br#"{"status":"ok"}"#);
    }

    #[test]
    fn inject_snippet_inserts_before_body_or_appends_to_html_shell() {
        let with_body = inject_snippet(b"<html><body><main>ok</main></body></html>");
        let with_body = std::str::from_utf8(&with_body).expect("utf-8 html");
        assert!(with_body.contains(LIVE_RELOAD_PATH));
        assert!(with_body.contains("</script></body>"));

        let html_shell = inject_snippet(b"<html><main>ok</main></html>");
        let html_shell = std::str::from_utf8(&html_shell).expect("utf-8 html");
        assert!(html_shell.ends_with("</script>"));

        let plain = inject_snippet(b"not html");
        assert_eq!(&plain[..], b"not html");
    }

    #[test]
    fn inject_snippet_edge_cases() {
        // Multiple </body> tags: Should insert before the *last* </body> tag.
        let multi_body = inject_snippet(b"<html><body>text</body> <!-- </body> --> </html>");
        let multi_body_str = std::str::from_utf8(&multi_body).expect("utf-8");
        assert!(multi_body_str.ends_with("</script></body> --> </html>"));

        // <html tag with attributes.
        let html_attr = inject_snippet(b"<html lang=\"en\"><main>ok</main>");
        let html_attr_str = std::str::from_utf8(&html_attr).expect("utf-8");
        assert!(html_attr_str.ends_with("</script>"));

        // Only </html> tag.
        let html_close = inject_snippet(b"<div>content</div></html>");
        let html_close_str = std::str::from_utf8(&html_close).expect("utf-8");
        assert!(html_close_str.ends_with("</script>"));

        // Invalid UTF-8 sequence, but containing </body>.
        let invalid_utf8 = b"<html><body>\xFF\xFE</body></html>".to_vec();
        let invalid_result = inject_snippet(&invalid_utf8);
        // String::from_utf8_lossy Replaces invalid bytes with U+FFFD.
        assert!(String::from_utf8_lossy(&invalid_result).contains("</script></body></html>"));
    }

    #[test]
    fn is_static_path_matches_root_and_nested_assets() {
        assert!(is_static_path("/static"));
        assert!(is_static_path("/static/css/autumn.css"));
        assert!(!is_static_path("/assets/autumn.css"));
    }

    #[test]
    fn inject_snippet_edge_cases_empty_body() {
        let empty = inject_snippet(b"");
        assert_eq!(empty, b"");
    }

    #[test]
    fn inject_snippet_edge_cases_case_insensitivity() {
        // Our current implementation is case sensitive.
        // If a user has upper case tags, we shouldn't fail or panic, we just won't inject.
        let upper_body = inject_snippet(b"<HTML><BODY>ok</BODY></HTML>");
        let upper_result = std::str::from_utf8(&upper_body).expect("utf-8");
        assert_eq!(upper_result, "<HTML><BODY>ok</BODY></HTML>");
    }

    #[test]
    fn inject_snippet_edge_cases_malformed_but_matching() {
        let malformed = inject_snippet(b"<html<body>");
        let result = std::str::from_utf8(&malformed).expect("utf-8");
        assert!(result.ends_with("</script>"));
    }
}