@@ -122,7 +122,7 @@ function get_fs_path(
122
122
silent:: Bool = false ,
123
123
onlyfs:: Bool = false
124
124
)
125
-
125
+
126
126
uri = HTTP. URI (req_path)
127
127
r_parts = HTTP. URIs. unescapeuri .(split (lstrip (uri. path, ' /' ), ' /' ))
128
128
fs_path = joinpath (CONTENT_DIR[], r_parts... )
@@ -208,70 +208,86 @@ Generate a page which lists content at path `dir`.
208
208
"""
209
209
function get_dir_list (dir:: AbstractString ):: String
210
210
list = readdir (dir; join= true , sort= true )
211
- io = IOBuffer ()
212
211
sdir = dir
213
212
cdir = CONTENT_DIR[]
214
213
if ! isempty (cdir)
215
214
sdir = join ([cdir, lstrip_cdir (dir)], " /" )
216
215
end
216
+ pagehtml (title= " Directory listing" ) do io
217
+ write (io, """
218
+ <h1 style='margin-top: 1em;'>
219
+ Directory listing
220
+ </h1>
221
+ <h3>
222
+ <a href="/" alt="root">🏠</a>
223
+ <a href="/$(dirname (dir)) " alt="parent dir">⬆️</a>
224
+ path: <code style='color:gray;'>$(sdir) </code>
225
+ </h3>
217
226
227
+ <hr>
228
+ <ul>
229
+ """
230
+ )
231
+
232
+ list_files = [f for f in list if isfile (f)]
233
+ list_dirs = [d for d in list if d ∉ list_files]
234
+
235
+ for fname in list_files
236
+ link = lstrip_cdir (fname)
237
+ name = splitdir (fname)[end ]
238
+ post = ifelse (islink (fname), " @" , " " )
239
+ write (io, """
240
+ <li><a href="/$(link) ">$(name)$(post) </a></li>
241
+ """
242
+ )
243
+ end
244
+ for fdir in list_dirs
245
+ link = lstrip_cdir (fdir)
246
+ # ensure ends with slash, see #135
247
+ link *= ifelse (endswith (link, " /" ), " " , " /" )
248
+ name = splitdir (fdir)[end ]
249
+ pre = " 📂 "
250
+ post = ifelse (islink (fdir), " @" , " " )
251
+ write (io, """
252
+ <li><a href="/$(link) ">$(pre)$(name)$(post) </a></li>
253
+ """
254
+ )
255
+ end
256
+ write (io, """
257
+ </ul>
258
+ <hr>
259
+ <a href="https://github.com/tlienart/LiveServer.jl">
260
+ 💻 LiveServer.jl
261
+ </a>
262
+ </body>
263
+ </html>
264
+ """
265
+ )
266
+ end
267
+ end
268
+
269
+ function pagehtml (f:: Base.Callable ; title:: AbstractString )
270
+ io = IOBuffer ()
271
+ # Construct the shared head part of the HTML
218
272
write (io, """
219
273
<!DOCTYPE HTML>
220
274
<html>
221
275
<head>
222
276
<meta charset="utf-8">
223
277
<meta name="viewport" content="width=device-width, initial-scale=1">
224
278
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/spcss">
225
- <title>Directory listing </title>
279
+ <title>$(title) </title>
226
280
<style>
227
281
a {text-decoration: none;}
228
282
</style>
229
283
</head>
230
- <body>
231
- <h1 style='margin-top: 1em;'>
232
- Directory listing
233
- </h1>
234
- <h3>
235
- <a href="/" alt="root">🏠</a>
236
- <a href="/$(dirname (dir)) " alt="parent dir">⬆️</a>
237
- path: <code style='color:gray;'>$(sdir) </code>
238
- </h3>
239
-
240
- <hr>
241
- <ul>
284
+ <body>
242
285
"""
243
286
)
244
-
245
- list_files = [f for f in list if isfile (f)]
246
- list_dirs = [d for d in list if d ∉ list_files]
247
-
248
- for fname in list_files
249
- link = lstrip_cdir (fname)
250
- name = splitdir (fname)[end ]
251
- post = ifelse (islink (fname), " @" , " " )
252
- write (io, """
253
- <li><a href="/$(link) ">$(name)$(post) </a></li>
254
- """
255
- )
256
- end
257
- for fdir in list_dirs
258
- link = lstrip_cdir (fdir)
259
- # ensure ends with slash, see #135
260
- link *= ifelse (endswith (link, " /" ), " " , " /" )
261
- name = splitdir (fdir)[end ]
262
- pre = " 📂 "
263
- post = ifelse (islink (fdir), " @" , " " )
264
- write (io, """
265
- <li><a href="/$(link) ">$(pre)$(name)$(post) </a></li>
266
- """
267
- )
268
- end
287
+ # Write the page-specific HTML (should only write the _contents_ of <body>...</body> tag)
288
+ f (io)
289
+ # Write the shared footer
269
290
write (io, """
270
- </ul>
271
- <hr>
272
- <a href="https://github.com/tlienart/LiveServer.jl">
273
- 💻 LiveServer.jl
274
- </a>
275
291
</body>
276
292
</html>
277
293
"""
@@ -334,24 +350,25 @@ function serve_file(
334
350
fs_path, case = get_fs_path (req. target)
335
351
336
352
if case == :not_found_without_404
337
- return HTTP. Response (404 ,
338
- """
339
- <div style="width: 100%; max-width: 500px; margin: auto">
340
- <h1 style="margin-top: 2em">404 Not Found</h1>
341
- <p>
342
- The requested URL [<code>$(req. target) </code>] does not correspond to a resource on the server.
343
- </p>
344
- <p>
345
- Perhaps you made a typo in the URL, or the URL corresponds to a file that has been
346
- deleted or renamed.
347
- </p>
348
- <p>
349
- <a href="/">Home</a>
350
- </p>
351
- </div>
352
- """
353
- )
354
- ret_code = 404
353
+ html_404 = pagehtml (title = " 404 Not Found" ) do io
354
+ write (io, """
355
+ <div style="width: 100%; max-width: 500px; margin: auto">
356
+ <h1 style="margin-top: 2em">404 Not Found</h1>
357
+ <p>
358
+ The requested URL [<code>$(req. target) </code>] does not correspond to a resource on the server.
359
+ </p>
360
+ <p>
361
+ Perhaps you made a typo in the URL, or the URL corresponds to a file that has been
362
+ deleted or renamed.
363
+ </p>
364
+ <p>
365
+ <a href="/">Home</a>
366
+ </p>
367
+ </div>
368
+ """
369
+ )
370
+ end
371
+ return HTTP. Response (404 , html_404)
355
372
elseif case == :not_found_with_404
356
373
ret_code = 404
357
374
elseif case == :dir_without_index
@@ -419,7 +436,7 @@ function serve_file(
419
436
content = take! (io)
420
437
end
421
438
end
422
-
439
+
423
440
range_match = match (r" bytes=(\d +)-(\d +)" , HTTP. header (req, " Range" , " " ))
424
441
is_ranged = ! isnothing (range_match)
425
442
0 commit comments