http-server-rs 0.0.19

Simple, zero-configuration command-line static HTTP server.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#![deny(unused_crate_dependencies)]
#![allow(clippy::module_inception)]

mod auth;
mod b64;
mod cli;
mod compress;
mod config;
mod explorer;
mod http1;
mod logger;
mod proxy;
mod transpile;
mod utils;
mod watcher;

use std::net::UdpSocket;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::sync::Arc;

use colored::Colorize;
use explorer::reload_script;
use explorer::render_directory_explorer;
use http1::http1_server;
use http1::ResponseBuilderExt;
use logger::Logger;
use mime_guess;
use normalize_path::NormalizePath;
use proxy::build_client;
use proxy::match_proxy_route;
use proxy::proxy_request;
use tokio::fs::File;
use tokio::io;
use tokio::io::AsyncWriteExt;
use watcher::Watcher;
use watcher::WatcherOptions;

use crate::config::Config;

const DEFAULT_CHARSET_SUFFIX: &str = "charset=UTF-8";

// copy from https://github.com/egmkang/local_ipaddress/blob/master/src/lib.rs
// Todo: need all ips use https://crates.io/crates/local-ip-address
fn get_intranet_ip() -> Option<String> {
  let socket = match UdpSocket::bind("0.0.0.0:0") {
    Ok(s) => s,
    Err(_) => return None,
  };

  match socket.connect("8.8.8.8:80") {
    Ok(()) => (),
    Err(_) => return None,
  };

  match socket.local_addr() {
    Ok(addr) => Some(addr.ip().to_string()),
    Err(_) => None,
  }
}

async fn main_async() -> anyhow::Result<()> {
  let config = Arc::new(Config::from_cli()?);
  let logger: Arc<Logger> = match config.quiet {
    true => Arc::new(Logger::Quiet),
    false => Arc::new(Logger::Default),
  };

  logger.println("🚀 HTTP Server 🌏".green().bold().to_string());
  logger.br();

  logger.print_folder(&config.serve_dir_fmt);
  logger.print_config("Directory Listings", &true);
  logger.print_config("Compress (JIT)", &config.compress);
  logger.print_config("CORS", &config.cors);
  logger.print_config("SharedArrayBuffer", &config.sab);
  logger.print_config("SPA", &config.spa);
  logger.print_config("Watch", &config.watch);
  logger.print_config("Transpile", &config.transpile);
  logger.br();

  if !config.proxy.is_empty() {
    for (path, route) in config.proxy.iter() {
      logger.println(format!(
        "🔀 {:<19} {} -> {}",
        format!("{}:", path).bold(),
        path,
        route.target
      ));
    }
    logger.br();
  }

  logger.print_headers(&config.headers);
  logger.br();

  logger.println(format!("🔗 http://{}", config.domain));
  if config.domain != config.domain_pretty {
    logger.println(format!("🔗 http://{}", config.domain_pretty));
  }

  // print intranet ip domain
  // Todo address bind to local ip 127.0.0.1 skip print?
  let intranet_domain = get_intranet_ip();
  if intranet_domain.is_some() {
    let Some(intranet_domain_str) = intranet_domain.as_ref() else {
      return Err(anyhow::anyhow!("Unable to get intranet domain str"));
    };
    if intranet_domain_str != &config.domain_pretty && intranet_domain_str != &config.domain {
      logger.println(format!("🔗 http://{}:{}", intranet_domain_str, config.port));
    }
  }

  logger.br();

  logger.println("📜 LOGS 📜".bold().blue().to_string());

  let watcher = match config.watch {
    true => Some(Watcher::new(WatcherOptions {
      target_dir: config.watch_dir.clone(),
      logger: logger.clone(),
    })?),
    false => None,
  };

  let proxy_client = match config.proxy.is_empty() {
    true => None,
    false => Some(Arc::new(build_client())),
  };

  let tsconfig_cache = Arc::new(transpile::tsconfig::TsConfigCache::new());

  http1_server(&config.domain, {
    let config = config.clone();
    let logger = logger.clone();
    let watcher = watcher.clone();
    let proxy_client = proxy_client.clone();
    let tsconfig_cache = tsconfig_cache.clone();

    move |req, mut res| {
      let config = config.clone();
      let logger = logger.clone();
      let watcher = watcher.clone();
      let proxy_client = proxy_client.clone();
      let tsconfig_cache = tsconfig_cache.clone();

      async move {
        // Basic Auth
        if !config.basic_auth.is_empty() {
          let Some(header) = req.headers().get("authorization") else {
            return Ok(
              res
                .header(
                  "WWW-Authenticate",
                  "Basic realm=\"http-server-rs\"".to_string(),
                )
                .status(401)
                .body_from("")?,
            );
          };

          let Some((_, token)) = header.to_str()?.split_once(" ") else {
            return Ok(res.status(500).body_from("Invalid basic auth header")?);
          };

          let decoded = b64::decode_string(token)?;

          let Some((username, password)) = decoded.split_once(":") else {
            return Ok(res.status(500).body_from("Invalid basic auth header")?);
          };

          let Some(creds) = config.basic_auth.get(username) else {
            return Ok(res.status(403).body_from("")?);
          };

          if creds != password {
            return Ok(res.status(403).body_from("")?);
          }
        }

        // Reverse proxy routes
        if let Some((_proxy_path, route)) = match_proxy_route(&config.proxy, req.uri().path()) {
          let Some(proxy_client) = proxy_client else {
            return Ok(res.status(500).body_from("Proxy client not available")?);
          };

          let target = route.target.clone();
          let request_uri = req.uri().clone();

          logger.println(format!(
            "{} {} -> {}",
            "[proxy]".blue().bold(),
            request_uri,
            target
          ));

          let response = proxy_request(&proxy_client, route, req).await?;

          logger.println(format!(
            "{} {} -> {} {}",
            "[proxy]".blue().bold(),
            request_uri,
            target,
            response.status()
          ));

          return Ok(response);
        }

        // Remove the leading slash
        let req_path = req.uri().path().to_string().replacen("/", "", 1);
        let req_path = urlencoding::decode(&req_path)?.to_string();

        // Guess the file path of the file to serve
        let mut file_path = config.serve_dir_abs.join(req_path.clone());

        // If the watcher is enabled, return an event stream to the client to notify changes
        if req_path == ".http-server-rs/reload.js" {
          if !config.watch {
            return Ok(res.status(404).body_from("Watcher not running")?);
          };

          return Ok(
            res
              .header(
                "Content-Type",
                format!("application/javascript; {}", DEFAULT_CHARSET_SUFFIX),
              )
              .status(200)
              .body_from(reload_script())?,
          );
        }

        // Endpoint for filesystem change event stream
        if req_path == ".http-server-rs/reload" {
          let Some(watcher) = watcher else {
            return Ok(res.status(404).body_from("Watcher not running")?);
          };

          let (res, mut writer) = res
            .header("X-Accel-Buffering", "no")
            .header(
              "Content-Type",
              format!("text/event-stream; {}", DEFAULT_CHARSET_SUFFIX),
            )
            .header("Cache-Control", "no-cache")
            .header("Connection", "keep-alive")
            .status(hyper::StatusCode::OK)
            .body_stream(config.stream_buffer_size)?;

          let mut rx = watcher.subscribe();

          tokio::task::spawn(async move {
            while let Some(changes) = rx.recv().await {
              let msg = format!(
                "data:{}\n\n",
                changes
                  .into_iter()
                  .map(|v| v.to_str().unwrap().to_string())
                  .collect::<Vec<String>>()
                  .join(",")
              );
              if writer.write_all(msg.as_bytes()).await.is_err() {
                break;
              }
            }
          });

          return Ok(res);
        }

        // hyper handles preventing access to parent directories via "../../"
        // but this is an extra layer of protection
        if !file_path.normalize().starts_with(&config.serve_dir_abs) {
          logger.println(format!("{} {}", "[403]".red().bold(), req.uri()));
          return Ok(res.status(403).body_from("Not allowed")?);
        }

        // Redirect directory requests to a trailing slash so that relative
        // URLs within (e.g. "./main.tsx") resolve against the directory
        // rather than the parent.
        if file_path.is_dir() && !req.uri().path().ends_with('/') {
          let mut location = format!("{}/", req.uri().path());

          if let Some(query) = req.uri().query() {
            location.push('?');
            location.push_str(query);
          }

          logger.println(format!("{} {}", "[301]".yellow().bold(), req.uri()));

          return Ok(res.header("Location", location).status(301).body_from("")?);
        }

        // Try to serve index.html
        if file_path.is_dir() && file_path.join("index.html").exists() {
          file_path = file_path.join("index.html");
        }

        // Apply custom headers
        for (key, values) in config.headers.iter() {
          for value in values.iter() {
            res = res.header(key, value);
          }
        }

        // Serve folder explorer
        if file_path.is_dir() {
          let mut output = render_directory_explorer(&config, &req_path, &file_path)?;

          if config.watch {
            output = format!("{}\n<script>{}</script>", output, reload_script());
          }

          // Todo check file charset
          return Ok(
            res
              .header(
                "Content-Type",
                format!("text/html;{}", DEFAULT_CHARSET_SUFFIX),
              )
              .status(200)
              .body_from(output)?,
          );
        }

        // If SPA and file doesn't exist, route to root index
        if config.spa && !file_path.exists() {
          file_path = config.serve_dir_abs.join("index.html");
        }

        // If not SPA an file doesn't exist, route to 404.html
        if !config.spa && !file_path.exists() {
          file_path = config.serve_dir_abs.join("404.html");
        }

        // 404 if no file exists
        if !file_path.exists() {
          logger.println(format!("{} {}", "[404]".red().bold(), req.uri()));
          return Ok(res.status(404).body_from("File not found")?);
        }

        let ext = file_path
          .extension()
          .and_then(|v| v.to_str())
          .unwrap_or_default()
          .to_string();

        if config.transpile && (ext == "ts" || ext == "tsx") {
          let contents = tokio::fs::read(&file_path).await?;

          let tsconfig = tsconfig_cache.resolve(&file_path, &config.serve_dir_abs);

          let result = transpile::typescript::transpile(transpile::TransformerContext {
            content: contents,
            path: file_path.clone(),
            kind: ext,
            tsconfig,
          })?;

          logger.println(format!("{} {}", "[200]".green().bold(), req.uri()));

          let content_type = format!("application/javascript; {}", DEFAULT_CHARSET_SUFFIX);

          if config.compress {
            res = res.header("Content-Encoding", "br");
            return Ok(
              res
                .header("Content-Type", content_type)
                .status(200)
                .body_from(compress::brotli(result.code.as_bytes()))?,
            );
          }

          return Ok(
            res
              .header("Content-Type", content_type)
              .status(200)
              .body_from(result.code)?,
          );
        }

        // Apply mime type
        let mime = self::mime_guess::from_path(&file_path)
          .first()
          .map(|v| v.to_string())
          .unwrap_or_default();

        if !mime.is_empty() {
          let mut content_type = mime.clone();
          // mime starts with "text/" or "application/"
          if content_type.starts_with("text/")
            || content_type.starts_with("application/javascript")
            || content_type.starts_with("application/json")
          {
            // Todo check file charset
            content_type = format!("{}; {}", content_type, DEFAULT_CHARSET_SUFFIX);
          }
          res = res.header("Content-Type", &content_type);
        }

        // If a .br or .gz file is found next to the target, serve that file
        if !config.compress {
          let brotli_path = PathBuf::from(format!("{}.br", file_path.to_str().unwrap()));
          let gzip_path = PathBuf::from(format!("{}.gz", file_path.to_str().unwrap()));

          if brotli_path.exists() {
            file_path = brotli_path;
            res = res.header("Content-Encoding", "br");
          } else if gzip_path.exists() {
            file_path = gzip_path;
            res = res.header("Content-Encoding", "gzip");
          }
        }

        let mut file = File::open(&file_path).await?;

        #[cfg(unix)]
        let content_length = file.metadata().await?.size();
        #[cfg(windows)]
        let content_length = file.metadata().await?.file_size();

        logger.println(format!("{} {}", "[200]".green().bold(), req.uri()));

        // Read file
        // Stream file if it's larger than 5mb
        if !mime.starts_with("text/html") && content_length > 500_000 {
          let (res, mut writer) = res
            .header("Connection", "keep-alive")
            .header("Content-Length", content_length)
            .status(hyper::StatusCode::OK)
            .body_stream(config.stream_buffer_size)?;

          tokio::task::spawn(async move {
            io::copy(&mut file, &mut writer).await.ok();
          });

          return Ok(res);
        }

        let Ok(mut contents) = tokio::fs::read(&file_path).await else {
          return Ok(res.status(500).body_from("Unable to open file")?);
        };

        // If using watch mode and automatically injecting the reload script
        // and file is html, mutate response to inject script
        if config.watch
          && !config.no_watch_inject
          && res
            .headers_ref()
            .unwrap()
            .get("Content-Type")
            .is_some_and(|h| h.to_str().unwrap_or("").starts_with("text/html"))
        {
          let html = String::from_utf8(contents.clone())?;
          if html.contains("<head>") {
            contents = html
              .replacen(
                "<head>",
                &format!("<head>\n<script>{}</script>\n", reload_script(),),
                1,
              )
              .as_bytes()
              .to_vec();
          } else if html.contains("<body>") {
            contents = html
              .replacen(
                "<body>",
                &format!("<body>\n<script>{}</script>\n", reload_script()),
                1,
              )
              .as_bytes()
              .to_vec();
          } else {
            contents.extend(format!("<script>{}</script>", reload_script()).as_bytes());
          }
        }

        if config.compress {
          res = res.header("Content-Encoding", "br");
          Ok(res.status(200).body_from(compress::brotli(&contents))?)
        } else {
          Ok(res.status(200).body_from(contents)?)
        }
      }
    }
  })
  .await
}

fn main() -> anyhow::Result<()> {
  let (tx, rx) = channel::<anyhow::Result<()>>();

  std::thread::spawn(move || {
    tx.send(
      tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .worker_threads(num_cpus::get_physical())
        .build()
        .unwrap()
        .block_on(main_async()),
    )
    .unwrap();
  });

  rx.recv()?
}