deno.land / std@0.158.0 / http / file_server.ts

file_server.ts
نووسراو ببینە
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#!/usr/bin/env -S deno run --allow-net --allow-read// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This program serves files in the current directory over HTTP.// TODO(bartlomieju): Add tests like these:// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js
import { extname, posix } from "../path/mod.ts";import { encode } from "../encoding/hex.ts";import { contentType } from "../media_types/mod.ts";import { serve, serveTls } from "./server.ts";import { Status, STATUS_TEXT } from "./http_status.ts";import { parse } from "../flags/mod.ts";import { assert } from "../_util/assert.ts";import { red } from "../fmt/colors.ts";import { compareEtag } from "./util.ts";
const DEFAULT_CHUNK_SIZE = 16_640;
interface EntryInfo { mode: string; size: string; url: string; name: string;}
const encoder = new TextEncoder();const decoder = new TextDecoder();
// The fnv-1a hash function.function fnv1a(buf: string): string { let hash = 2166136261; // 32-bit FNV offset basis for (let i = 0; i < buf.length; i++) { hash ^= buf.charCodeAt(i); // Equivalent to `hash *= 16777619` without using BigInt // 32-bit FNV prime hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); } // 32-bit hex string return (hash >>> 0).toString(16);}
/** Algorithm used to determine etag */export type EtagAlgorithm = | "fnv1a" | "sha-1" | "sha-256" | "sha-384" | "sha-512";
// Generates a hash for the provided stringasync function createEtagHash( message: string, algorithm: EtagAlgorithm = "fnv1a",): Promise<string> { if (algorithm === "fnv1a") { return fnv1a(message); } const msgUint8 = encoder.encode(message); const hashBuffer = await crypto.subtle.digest(algorithm, msgUint8); return decoder.decode(encode(new Uint8Array(hashBuffer)));}
function modeToString(isDir: boolean, maybeMode: number | null): string { const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
if (maybeMode === null) { return "(unknown mode)"; } const mode = maybeMode.toString(8); if (mode.length < 3) { return "(unknown mode)"; } let output = ""; mode .split("") .reverse() .slice(0, 3) .forEach((v) => { output = `${modeMap[+v]} ${output}`; }); output = `${isDir ? "d" : "-"} ${output}`; return output;}
function fileLenToString(len: number): string { const multiplier = 1024; let base = 1; const suffix = ["B", "K", "M", "G", "T"]; let suffixIndex = 0;
while (base * multiplier < len) { if (suffixIndex >= suffix.length - 1) { break; } base *= multiplier; suffixIndex++; }
return `${(len / base).toFixed(2)}${suffix[suffixIndex]}`;}
/** Interface for serveFile options. */export interface ServeFileOptions { /** The algorithm to use for generating the ETag. Defaults to "fnv1a". */ etagAlgorithm?: EtagAlgorithm; /** An optional FileInfo object returned by Deno.stat. It is used for optimization purposes. */ fileInfo?: Deno.FileInfo;}
/** * Returns an HTTP Response with the requested file as the body. * @param req The server request context used to cleanup the file handle. * @param filePath Path of the file to serve. * @param options * @param options.etagAlgorithm The algorithm to use for generating the ETag. Defaults to "fnv1a". * @param options.fileInfo An optional FileInfo object returned by Deno.stat. It is used * for optimization purposes. */export async function serveFile( req: Request, filePath: string, { etagAlgorithm, fileInfo }: ServeFileOptions = {},): Promise<Response> { try { fileInfo ??= await Deno.stat(filePath); } catch (error) { if (error instanceof Deno.errors.NotFound) { await req.body?.cancel(); const status = Status.NotFound; const statusText = STATUS_TEXT[status]; return new Response(statusText, { status, statusText, }); } else { throw error; } }
if (fileInfo.isDirectory) { await req.body?.cancel(); const status = Status.NotFound; const statusText = STATUS_TEXT[status]; return new Response(statusText, { status, statusText }); }
const file = await Deno.open(filePath);
const headers = setBaseHeaders();
// Set mime-type using the file extension in filePath const contentTypeValue = contentType(extname(filePath)); if (contentTypeValue) { headers.set("content-type", contentTypeValue); }
// Set date header if access timestamp is available if (fileInfo.atime instanceof Date) { const date = new Date(fileInfo.atime); headers.set("date", date.toUTCString()); }
// Set last modified header if last modification timestamp is available if (fileInfo.mtime instanceof Date) { const lastModified = new Date(fileInfo.mtime); headers.set("last-modified", lastModified.toUTCString());
// Create a simple etag that is an md5 of the last modified date and filesize concatenated const simpleEtag = await createEtagHash( `${lastModified.toJSON()}${fileInfo.size}`, etagAlgorithm || "fnv1a", ); headers.set("etag", simpleEtag);
// If a `if-none-match` header is present and the value matches the tag or // if a `if-modified-since` header is present and the value is bigger than // the access timestamp value, then return 304 const ifNoneMatch = req.headers.get("if-none-match"); const ifModifiedSince = req.headers.get("if-modified-since"); if ( (ifNoneMatch && compareEtag(ifNoneMatch, simpleEtag)) || (ifNoneMatch === null && ifModifiedSince && fileInfo.mtime.getTime() < new Date(ifModifiedSince).getTime() + 1000) ) { const status = Status.NotModified; const statusText = STATUS_TEXT[status];
file.close();
return new Response(null, { status, statusText, headers, }); } }
// Get and parse the "range" header const range = req.headers.get("range") as string; const rangeRe = /bytes=(\d+)-(\d+)?/; const parsed = rangeRe.exec(range);
// Use the parsed value if available, fallback to the start and end of the entire file const start = parsed && parsed[1] ? +parsed[1] : 0; const end = parsed && parsed[2] ? +parsed[2] : fileInfo.size - 1;
// If there is a range, set the status to 206, and set the "Content-range" header. if (range && parsed) { headers.set("content-range", `bytes ${start}-${end}/${fileInfo.size}`); }
// Return 416 if `start` isn't less than or equal to `end`, or `start` or `end` are greater than the file's size const maxRange = fileInfo.size - 1;
if ( range && (!parsed || typeof start !== "number" || start > end || start > maxRange || end > maxRange) ) { const status = Status.RequestedRangeNotSatisfiable; const statusText = STATUS_TEXT[status];
file.close();
return new Response(statusText, { status, statusText, headers, }); }
// Set content length const contentLength = end - start + 1; headers.set("content-length", `${contentLength}`); if (range && parsed) { // Create a stream of the file instead of loading it into memory let bytesSent = 0; const body = new ReadableStream({ async start() { if (start > 0) { await file.seek(start, Deno.SeekMode.Start); } }, async pull(controller) { const bytes = new Uint8Array(DEFAULT_CHUNK_SIZE); const bytesRead = await file.read(bytes); if (bytesRead === null) { file.close(); controller.close(); return; } controller.enqueue( bytes.slice(0, Math.min(bytesRead, contentLength - bytesSent)), ); bytesSent += bytesRead; if (bytesSent > contentLength) { file.close(); controller.close(); } }, });
return new Response(body, { status: Status.PartialContent, statusText: STATUS_TEXT[Status.PartialContent], headers, }); }
return new Response(file.readable, { status: Status.OK, statusText: STATUS_TEXT[Status.OK], headers, });}
// TODO(bartlomieju): simplify this after deno.stat and deno.readDir are fixedasync function serveDirIndex( req: Request, dirPath: string, options: { dotfiles: boolean; target: string; etagAlgorithm?: EtagAlgorithm; },): Promise<Response> { const showDotfiles = options.dotfiles; const dirUrl = `/${posix.relative(options.target, dirPath)}`; const listEntry: EntryInfo[] = [];
// if ".." makes sense if (dirUrl !== "/") { const prevPath = posix.join(dirPath, ".."); const fileInfo = await Deno.stat(prevPath); listEntry.push({ mode: modeToString(true, fileInfo.mode), size: "", name: "../", url: posix.join(dirUrl, ".."), }); }
for await (const entry of Deno.readDir(dirPath)) { if (!showDotfiles && entry.name[0] === ".") { continue; } const filePath = posix.join(dirPath, entry.name); const fileUrl = encodeURI(posix.join(dirUrl, entry.name)); const fileInfo = await Deno.stat(filePath); if (entry.name === "index.html" && entry.isFile) { // in case index.html as dir... return serveFile(req, filePath, { etagAlgorithm: options.etagAlgorithm, fileInfo, }); } listEntry.push({ mode: modeToString(entry.isDirectory, fileInfo.mode), size: entry.isFile ? fileLenToString(fileInfo.size ?? 0) : "", name: `${entry.name}${entry.isDirectory ? "/" : ""}`, url: `${fileUrl}${entry.isDirectory ? "/" : ""}`, }); } listEntry.sort((a, b) => a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1 ); const formattedDirUrl = `${dirUrl.replace(/\/$/, "")}/`; const page = encoder.encode(dirViewerTemplate(formattedDirUrl, listEntry));
const headers = setBaseHeaders(); headers.set("content-type", "text/html");
return new Response(page, { status: Status.OK, headers });}
function serveFallback(_req: Request, e: Error): Promise<Response> { if (e instanceof URIError) { return Promise.resolve( new Response(STATUS_TEXT[Status.BadRequest], { status: Status.BadRequest, statusText: STATUS_TEXT[Status.BadRequest], }), ); } else if (e instanceof Deno.errors.NotFound) { return Promise.resolve( new Response(STATUS_TEXT[Status.NotFound], { status: Status.NotFound, statusText: STATUS_TEXT[Status.NotFound], }), ); }
return Promise.resolve( new Response(STATUS_TEXT[Status.InternalServerError], { status: Status.InternalServerError, statusText: STATUS_TEXT[Status.InternalServerError], }), );}
function serverLog(req: Request, status: number) { const d = new Date().toISOString(); const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`; const normalizedUrl = normalizeURL(req.url); const s = `${dateFmt} [${req.method}] ${normalizedUrl} ${status}`; // using console.debug instead of console.log so chrome inspect users can hide request logs console.debug(s);}
function setBaseHeaders(): Headers { const headers = new Headers(); headers.set("server", "deno");
// Set "accept-ranges" so that the client knows it can make range requests on future requests headers.set("accept-ranges", "bytes"); headers.set("date", new Date().toUTCString());
return headers;}
function dirViewerTemplate(dirname: string, entries: EntryInfo[]): string { const paths = dirname.split("/");
return ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Deno File Server</title> <style> :root { --background-color: #fafafa; --color: rgba(0, 0, 0, 0.87); } @media (prefers-color-scheme: dark) { :root { --background-color: #292929; --color: #fff; } thead { color: #7f7f7f; } } @media (min-width: 960px) { main { max-width: 960px; } body { padding-left: 32px; padding-right: 32px; } } @media (min-width: 600px) { main { padding-left: 24px; padding-right: 24px; } } body { background: var(--background-color); color: var(--color); font-family: "Roboto", "Helvetica", "Arial", sans-serif; font-weight: 400; line-height: 1.43; font-size: 0.875rem; } a { color: #2196f3; text-decoration: none; } a:hover { text-decoration: underline; } thead { text-align: left; } thead th { padding-bottom: 12px; } table td { padding: 6px 36px 6px 0px; } .size { text-align: right; padding: 6px 12px 6px 24px; } .mode { font-family: monospace, monospace; } </style> </head> <body> <main> <h1>Index of <a href="/">home</a>${ paths .map((path, index, array) => { if (path === "") return ""; const link = array.slice(0, index + 1).join("/"); return `<a href="${link}">${path}</a>`; }) .join("/") } </h1> <table> <thead> <tr> <th>Mode</th> <th>Size</th> <th>Name</th> </tr> </thead> ${ entries .map( (entry) => ` <tr> <td class="mode"> ${entry.mode} </td> <td class="size"> ${entry.size} </td> <td> <a href="${entry.url}">${entry.name}</a> </td> </tr> `, ) .join("") } </table> </main> </body> </html> `;}
/** Interface for serveDir options. */export interface ServeDirOptions { /** Serves the files under the given directory root. Defaults to your current directory. */ fsRoot?: string; /** Specified that part is stripped from the beginning of the requested pathname. */ urlRoot?: string; /** Enable directory listing. Defaults to false. */ showDirListing?: boolean; /** Serves dotfiles. Defaults to false. */ showDotfiles?: boolean; /** Enable CORS via the "Access-Control-Allow-Origin" header. Defaults to false. */ enableCors?: boolean; /** Do not print request level logs. Defaults to false. Defaults to false. */ quiet?: boolean; /** The algorithm to use for generating the ETag. Defaults to "fnv1a". */ etagAlgorithm?: EtagAlgorithm;}
/** * Serves the files under the given directory root (opts.fsRoot). * * ```ts * import { serve } from "https://deno.land/std@$STD_VERSION/http/server.ts"; * import { serveDir } from "https://deno.land/std@$STD_VERSION/http/file_server.ts"; * * serve((req) => { * const pathname = new URL(req.url).pathname; * if (pathname.startsWith("/static")) { * return serveDir(req, { * fsRoot: "path/to/static/files/dir", * }); * } * // Do dynamic responses * return new Response(); * }); * ``` * * Optionally you can pass `urlRoot` option. If it's specified that part is stripped from the beginning of the requested pathname. * * ```ts * import { serveDir } from "https://deno.land/std@$STD_VERSION/http/file_server.ts"; * * // ... * serveDir(new Request("http://localhost/static/path/to/file"), { * fsRoot: "public", * urlRoot: "static", * }); * ``` * * The above example serves `./public/path/to/file` for the request to `/static/path/to/file`. * * @param req The request to handle * @param opts * @param opts.fsRoot Serves the files under the given directory root. Defaults to your current directory. * @param opts.urlRoot Specified that part is stripped from the beginning of the requested pathname. * @param opts.showDirListing Enable directory listing. Defaults to false. * @param opts.showDotfiles Serves dotfiles. Defaults to false. * @param opts.enableCors Enable CORS via the "Access-Control-Allow-Origin" header. Defaults to false. * @param opts.quiet Do not print request level logs. Defaults to false. * @param opts.etagAlgorithm Etag The algorithm to use for generating the ETag. Defaults to "fnv1a". */export async function serveDir(req: Request, opts: ServeDirOptions = {}) { let response: Response; const target = opts.fsRoot || "."; const urlRoot = opts.urlRoot;
try { let normalizedPath = normalizeURL(req.url); if (urlRoot) { if (normalizedPath.startsWith("/" + urlRoot)) { normalizedPath = normalizedPath.replace(urlRoot, ""); } else { throw new Deno.errors.NotFound(); } }
const fsPath = posix.join(target, normalizedPath); const fileInfo = await Deno.stat(fsPath);
if (fileInfo.isDirectory) { if (opts.showDirListing) { response = await serveDirIndex(req, fsPath, { dotfiles: opts.showDotfiles || false, target, }); } else { throw new Deno.errors.NotFound(); } } else { response = await serveFile(req, fsPath, { etagAlgorithm: opts.etagAlgorithm, fileInfo, }); } } catch (e) { const err = e instanceof Error ? e : new Error("[non-error thrown]"); if (!opts.quiet) console.error(red(err.message)); response = await serveFallback(req, err); }
if (opts.enableCors) { assert(response); response.headers.append("access-control-allow-origin", "*"); response.headers.append( "access-control-allow-headers", "Origin, X-Requested-With, Content-Type, Accept, Range", ); }
if (!opts.quiet) serverLog(req, response!.status);
return response!;}
function normalizeURL(url: string): string { let normalizedUrl = url;
try { //allowed per https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html const absoluteURI = new URL(normalizedUrl); normalizedUrl = absoluteURI.pathname; } catch (e) { //wasn't an absoluteURI if (!(e instanceof TypeError)) { throw e; } }
try { normalizedUrl = decodeURI(normalizedUrl); } catch (e) { if (!(e instanceof URIError)) { throw e; } }
if (normalizedUrl[0] !== "/") { throw new URIError("The request URI is malformed."); }
normalizedUrl = posix.normalize(normalizedUrl); const startOfParams = normalizedUrl.indexOf("?");
return startOfParams > -1 ? normalizedUrl.slice(0, startOfParams) : normalizedUrl;}
function main() { const serverArgs = parse(Deno.args, { string: ["port", "host", "cert", "key"], boolean: ["help", "dir-listing", "dotfiles", "cors", "verbose"], negatable: ["dir-listing", "dotfiles", "cors"], default: { "dir-listing": true, dotfiles: true, cors: true, verbose: false, host: "0.0.0.0", port: "4507", cert: "", key: "", }, alias: { p: "port", c: "cert", k: "key", h: "help", v: "verbose", }, }); const port = Number(serverArgs.port); const host = serverArgs.host; const certFile = serverArgs.cert; const keyFile = serverArgs.key;
if (serverArgs.help) { printUsage(); Deno.exit(); }
if (keyFile || certFile) { if (keyFile === "" || certFile === "") { console.log("--key and --cert are required for TLS"); printUsage(); Deno.exit(1); } }
const wild = serverArgs._ as string[]; const target = posix.resolve(wild[0] ?? "");
const handler = (req: Request): Promise<Response> => { return serveDir(req, { fsRoot: target, showDirListing: serverArgs["dir-listing"], showDotfiles: serverArgs.dotfiles, enableCors: serverArgs.cors, quiet: !serverArgs.verbose, }); };
const useTls = !!(keyFile && certFile);
if (useTls) { serveTls(handler, { port, hostname: host, certFile, keyFile, }); } else { serve(handler, { port, hostname: host }); }}
function printUsage() { console.log(`Deno File Server Serves a local directory in HTTP.
INSTALL: deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
USAGE: file_server [path] [options]
OPTIONS: -h, --help Prints help information -p, --port <PORT> Set port --cors Enable CORS via the "Access-Control-Allow-Origin" header --host <HOST> Hostname (default is 0.0.0.0) -c, --cert <FILE> TLS certificate file (enables TLS) -k, --key <FILE> TLS key file (enables TLS) --no-dir-listing Disable directory listing --no-dotfiles Do not show dotfiles --no-cors Disable cross-origin resource sharing -v, --verbose Print request level logs
All TLS options are required when one is provided.`);}
if (import.meta.main) { main();}
std

Version Info

Tagged at
a year ago