-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.ps1
More file actions
82 lines (75 loc) · 3.62 KB
/
Copy pathserve.ps1
File metadata and controls
82 lines (75 loc) · 3.62 KB
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
# Simple local web server using .NET's built-in HttpListener
# No external dependencies needed!
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Starting local web server..." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "The wiki will be available at:" -ForegroundColor Yellow
Write-Host " http://localhost:8080" -ForegroundColor White
Write-Host ""
Write-Host "Press Ctrl+C to stop the server" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Set-Location "$PSScriptRoot\docs"
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://localhost:8080/')
$listener.Start()
Write-Host 'Server started at http://localhost:8080' -ForegroundColor Green
Start-Process 'http://localhost:8080'
try {
while ($listener.IsListening) {
try {
$context = $listener.GetContext()
$request = $context.Request
$response = $context.Response
$path = $request.Url.LocalPath
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] Request: $path" -ForegroundColor Cyan
$filePath = Join-Path (Get-Location) $path.TrimStart('/')
if (Test-Path $filePath -PathType Container) {
$filePath = Join-Path $filePath 'index.html'
Write-Host " -> Directory, trying: index.html" -ForegroundColor Gray
}
if (Test-Path $filePath -PathType Leaf) {
try {
$content = [System.IO.File]::ReadAllBytes($filePath)
$ext = [System.IO.Path]::GetExtension($filePath)
$contentType = switch ($ext) {
'.html' { 'text/html; charset=utf-8' }
'.css' { 'text/css; charset=utf-8' }
'.js' { 'application/javascript; charset=utf-8' }
'.json' { 'application/json; charset=utf-8' }
'.png' { 'image/png' }
'.jpg' { 'image/jpeg' }
'.jpeg' { 'image/jpeg' }
'.ico' { 'image/x-icon' }
default { 'application/octet-stream' }
}
$response.ContentType = $contentType
$response.ContentLength64 = $content.Length
$response.OutputStream.Write($content, 0, $content.Length)
Write-Host " [OK] Served: $filePath ($($content.Length) bytes)" -ForegroundColor Green
} catch {
Write-Host " [ERROR] Failed to read file: $filePath" -ForegroundColor Red
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
$response.StatusCode = 500
}
} else {
Write-Host " [404] Not found: $filePath" -ForegroundColor Yellow
$response.StatusCode = 404
}
$response.Close()
} catch {
Write-Host "[ERROR] Request handling failed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
if ($response) {
try { $response.Close() } catch {}
}
}
}
} catch {
Write-Host "[FATAL] Server crashed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
} finally {
Write-Host 'Stopping server...' -ForegroundColor Yellow
$listener.Stop()
}