webadev 0.4.0

A tiny web dev server with live reload
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
use std::{
    convert::Infallible,
    net::IpAddr,
    path::{Path, PathBuf},
    str::FromStr,
    sync::Arc,
    time::Duration,
};

use axum::{
    Router,
    body::{Body, Bytes},
    extract::{Path as UrlPath, State},
    http::{HeaderName, HeaderValue, Request, StatusCode, header},
    middleware::{self, Next},
    response::{
        Response,
        sse::{Event, KeepAlive, Sse},
    },
    routing::get,
};
use futures_util::stream::{Stream, unfold};
use tokio::{
    net::TcpListener,
    sync::broadcast::{Sender, error::RecvError},
};

use crate::ReloadType;

struct AppState {
    dir: PathBuf,
    tx: Sender<(ReloadType, Vec<PathBuf>)>,
    headers: Vec<(HeaderName, HeaderValue)>,
}

/// configuration for the dev server
#[derive(Clone, Debug)]
pub struct Config {
    /// directory to serve and watch
    pub dir: PathBuf,
    /// ip address to bind to
    pub ip: IpAddr,
    /// port to listen on
    pub port: u16,
    /// additional HTTP headers, as `Name: value` strings
    pub headers: Vec<String>,
}

/// bind to `config.ip:config.port` and build the router.
/// returns the url, the bound listener and the router.
/// use `0` as `config.port` to let the OS pick a free port (the real one is in the url).
pub async fn bind(
    tx: Sender<(ReloadType, Vec<PathBuf>)>,
    config: Config,
) -> Result<(String, TcpListener, Router), String> {
    let headers =
        headers_parse(&config.headers).map_err(|err| format!("Invalid --header: {err}"))?;

    let listener = TcpListener::bind((config.ip, config.port))
        .await
        .map_err(|err| format!("Failed to bind address: {err}"))?;
    let addr = listener
        .local_addr()
        .map_err(|err| format!("Failed to get bound address: {err}"))?;

    let url = if config.ip.is_unspecified() {
        format!("http://127.0.0.1:{}", addr.port())
    } else {
        format!("http://{}:{}", config.ip, addr.port())
    };

    let app = app_build(AppState {
        dir: config.dir,
        tx,
        headers,
    });

    Ok((url, listener, app))
}

/// serve files from the already-bound `listener` with the given `router`,
/// with live-reload over SSE.
pub async fn serve(listener: TcpListener, router: Router) -> Result<(), String> {
    axum::serve(listener, router)
        .await
        .map_err(|err| format!("{err}"))?;

    Ok(())
}

fn app_build(state: AppState) -> Router {
    let state = Arc::new(state);
    Router::new()
        .route("/livereload", get(livereload))
        .route("/", get(root_serve))
        .route("/{*path}", get(files_serve))
        .with_state(state.clone())
        .layer(middleware::from_fn_with_state(state, headers_middleware))
}

// parse `Name: value` header strings into axum header pairs
// - `:` separator
// - empty names/values and control chars are rejected (header-injection guard)
fn headers_parse(raw: &[String]) -> Result<Vec<(HeaderName, HeaderValue)>, String> {
    raw.iter()
        .map(|item| {
            let (name, value) = item
                .split_once(':')
                .ok_or_else(|| format!("missing ':' separator in `{item}`"))?;
            let name = name.trim();
            let value = value.trim();
            if name.is_empty() {
                return Err(format!("empty header name in `{item}`"));
            }
            let name = HeaderName::from_str(name)
                .map_err(|_| format!("invalid header name in `{item}`"))?;
            let value = HeaderValue::from_str(value)
                .map_err(|_| format!("invalid header value in `{item}`"))?;
            Ok((name, value))
        })
        .collect()
}

// attach user-supplied `--header`s to every response
async fn headers_middleware(
    State(state): State<Arc<AppState>>,
    request: Request<Body>,
    next: Next,
) -> Response {
    let mut response = next.run(request).await;
    for (name, value) in &state.headers {
        response.headers_mut().insert(name.clone(), value.clone());
    }
    response
}

async fn livereload(
    State(state): State<Arc<AppState>>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let rx = state.tx.subscribe();
    Sse::new(unfold(rx, |mut rx| async move {
        let reload_type = match rx.recv().await {
            Ok((reload_type, _)) => reload_type,
            Err(RecvError::Lagged(_)) => ReloadType::Page,
            Err(RecvError::Closed) => return None,
        };
        let message = reload_type.as_str();
        // a `data:` line is required:
        // - SSE specs (WHATWG HTML §9.2.6): events whith no `data:` line are dropped; the line's value could be empty
        // - axum's `Event::data` silently skips empty input, so the field is never emitted
        Some((Ok(Event::default().event(message).data(message)), rx))
    }))
    .keep_alive(
        KeepAlive::new()
            .interval(Duration::from_secs(15))
            .text("keep-alive"),
    )
}

async fn root_serve(State(state): State<Arc<AppState>>) -> Response {
    file_serve(&state.dir, "").await
}

async fn files_serve(
    State(state): State<Arc<AppState>>,
    UrlPath(path): UrlPath<String>,
) -> Response {
    file_serve(&state.dir, &path).await
}

async fn file_serve(dir: &Path, path: &str) -> Response {
    if path.split('/').any(|segment| segment == "..") {
        return html_not_found_build();
    }

    let mut file_path = dir.join(path);
    let is_dir = tokio::fs::metadata(&file_path)
        .await
        .is_ok_and(|m| m.is_dir());
    if is_dir {
        file_path = file_path.join("index.html");
    }

    let bytes = match tokio::fs::read(&file_path).await {
        Ok(bytes) => bytes,
        Err(_) => return html_not_found_build(),
    };

    let is_html = file_path.extension().and_then(|e| e.to_str()) == Some("html");
    if is_html {
        match String::from_utf8(bytes) {
            Ok(html) => html_response_build(js_script_inject(&html)),
            Err(err) => html_response_build(err.into_bytes()),
        }
    } else {
        let mime = mime_guess::from_path(&file_path).first_or_octet_stream();
        Response::builder()
            .status(StatusCode::OK)
            .header(header::CONTENT_TYPE, mime.as_ref())
            .header(header::CACHE_CONTROL, "no-cache")
            .body(Body::from(bytes))
            .expect("failed to build response")
    }
}

fn html_response_build(body: impl Into<Bytes>) -> Response {
    Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "text/html; charset=utf-8")
        .header(header::CACHE_CONTROL, "no-cache")
        .body(Body::from(body.into()))
        .expect("failed to build response")
}

fn html_not_found_build() -> Response {
    Response::builder()
        .status(StatusCode::NOT_FOUND)
        .body(Body::empty())
        .expect("failed to build response")
}

fn js_script_inject(html: &str) -> String {
    let script = r#"<script>
        const es = new EventSource('/livereload');
        let first = true;
        es.addEventListener('open', () => {
            if (first) {
                first = false;
                console.log('[webadev] live reload connected');
            } else {
                // server restarted: reload to resync
                location.reload();
            }
        });
        es.addEventListener('page', () => location.reload());
        es.addEventListener('css', () => {
            document.querySelectorAll('link[rel="stylesheet"]').forEach(link => {
                const url = new URL(link.href, location.href);
                url.searchParams.set('t', Date.now());
                link.href = url.toString();
            });
            console.log('[webadev] css hot-reloaded');
        });
        es.addEventListener('error', () => console.warn('[webadev] live reload connection lost', es.readyState));
    </script>"#;

    if html.contains("</body>") {
        html.replace("</body>", &format!("{}\n</body>", script))
    } else if html.contains("</html>") {
        html.replace("</html>", &format!("{}\n</html>", script))
    } else {
        format!("{}\n{}", html, script)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{create_dir_all, write};

    use axum::{
        body::Body,
        http::{HeaderMap, Request, StatusCode},
    };
    use http_body_util::BodyExt;
    use tempfile::tempdir;
    use tokio::sync::broadcast;
    use tower::ServiceExt;

    fn test_app(dir: &Path) -> Router {
        test_app_with_headers(dir, &[])
    }

    fn test_app_with_headers(dir: &Path, headers: &[&str]) -> Router {
        let (tx, _rx) = broadcast::channel(8);
        let headers = headers.iter().map(|s| s.to_string()).collect::<Vec<_>>();
        app_build(AppState {
            dir: dir.to_path_buf(),
            tx,
            headers: headers_parse(&headers).unwrap(),
        })
    }

    async fn get(app: &Router, uri: &str) -> (StatusCode, Vec<u8>) {
        let resp = app
            .clone()
            .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
            .await
            .unwrap();
        let status = resp.status();
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        (status, bytes.to_vec())
    }

    fn html_write(dir: &Path, rel: &str, content: &str) {
        let full = dir.join(rel);
        create_dir_all(full.parent().unwrap()).unwrap();
        write(full, content).unwrap();
    }

    #[tokio::test]
    async fn serves_root_index_with_injected_script() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "index.html", "<html><body>hi</body></html>");
        let app = test_app(dir.path());
        let (status, body) = get(&app, "/").await;
        assert_eq!(status, 200);
        let html = String::from_utf8(body).unwrap();
        assert!(html.contains("hi"));
        assert!(html.contains("EventSource"));
        assert!(html.contains("/livereload"));
        assert!(html.contains("reload"));
    }

    #[tokio::test]
    async fn serves_nested_directory_index() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "sub/index.html", "<html>sub</html>");
        let app = test_app(dir.path());
        let (status, body) = get(&app, "/sub").await;
        assert_eq!(status, 200);
        assert!(String::from_utf8(body).unwrap().contains("sub"));
    }

    #[tokio::test]
    async fn missing_file_is_404() {
        let dir = tempdir().unwrap();
        let app = test_app(dir.path());
        let (status, _) = get(&app, "/nope.html").await;
        assert_eq!(status, 404);
    }

    #[tokio::test]
    async fn path_traversal_is_rejected() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "index.html", "<html></html>");
        let app = test_app(dir.path());
        let (status, _) = get(&app, "/../../etc/passwd").await;
        assert_eq!(status, 404);
    }

    #[tokio::test]
    async fn css_is_served_without_script() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "style.css", "body {}");
        let app = test_app(dir.path());
        let (status, body) = get(&app, "/style.css").await;
        assert_eq!(status, 200);
        assert_eq!(String::from_utf8(body).unwrap(), "body {}");
    }

    #[tokio::test]
    async fn non_utf8_html_is_served_raw() {
        let dir = tempdir().unwrap();
        let raw: Vec<u8> = vec![0xff, 0xfe, 0xfd];
        write(dir.path().join("index.html"), &raw).unwrap();
        let app = test_app(dir.path());
        let (status, body) = get(&app, "/").await;
        assert_eq!(status, 200);
        assert_eq!(body, raw);
    }

    async fn headers_get(app: &Router, uri: &str) -> HeaderMap {
        let resp = app
            .clone()
            .oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
            .await
            .unwrap();
        resp.into_parts().0.headers
    }

    #[tokio::test]
    async fn custom_header_is_applied_to_html() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "index.html", "<html>hi</html>");
        let app = test_app_with_headers(dir.path(), &["X-Custom: yes"]);
        let headers = headers_get(&app, "/").await;
        assert_eq!(headers["X-Custom"], "yes");
    }

    #[tokio::test]
    async fn custom_header_is_applied_to_static_file() {
        let dir = tempdir().unwrap();
        write(dir.path().join("app.js"), "let x = 1;").unwrap();
        let app = test_app_with_headers(dir.path(), &["X-Custom: yes"]);
        let headers = headers_get(&app, "/app.js").await;
        assert_eq!(headers["X-Custom"], "yes");
    }

    #[tokio::test]
    async fn multiple_custom_headers_are_all_applied() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "index.html", "<html>hi</html>");
        let app = test_app_with_headers(dir.path(), &["X-A: 1", "X-B: 2"]);
        let headers = headers_get(&app, "/").await;
        assert_eq!(headers["X-A"], "1");
        assert_eq!(headers["X-B"], "2");
    }

    #[tokio::test]
    async fn custom_header_overrides_existing_value() {
        let dir = tempdir().unwrap();
        html_write(dir.path(), "index.html", "<html>hi</html>");
        let app = test_app_with_headers(dir.path(), &["Cache-Control: max-age=60"]);
        let headers = headers_get(&app, "/").await;
        assert_eq!(headers["Cache-Control"], "max-age=60");
    }

    #[test]
    fn invalid_header_is_rejected() {
        assert!(headers_parse(&["Bad\nHeader: x".to_string()]).is_err());
        assert!(headers_parse(&["NoSeparator".to_string()]).is_err());
        assert!(headers_parse(&[": novalue".to_string()]).is_err());
        assert!(headers_parse(&["X-Foo:".to_string()]).is_ok());
    }
}