-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ps1
More file actions
41 lines (35 loc) · 1.75 KB
/
Copy pathserver.ps1
File metadata and controls
41 lines (35 loc) · 1.75 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
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:8000/")
$listener.Start()
Write-Host "Servidor web ARGOS activo en http://localhost:8000/"
try {
while ($listener.IsListening) {
$context = $listener.GetContext()
$request = $context.Request
$response = $context.Response
# Resolve requested file path
$urlPath = $request.Url.LocalPath
if ($urlPath -eq "/") { $urlPath = "/index.html" }
# Remove leading slash and build full path
$cleanPath = $urlPath.Substring(1).Replace('/', '\')
$filePath = Join-Path -Path $PSScriptRoot -ChildPath $cleanPath
if (Test-Path $filePath -PathType Leaf) {
$bytes = [System.IO.File]::ReadAllBytes($filePath)
# Content-Type Mapping
if ($filePath.EndsWith(".html")) { $response.ContentType = "text/html; charset=utf-8" }
elseif ($filePath.EndsWith(".css")) { $response.ContentType = "text/css; charset=utf-8" }
elseif ($filePath.EndsWith(".js")) { $response.ContentType = "application/javascript; charset=utf-8" }
elseif ($filePath.EndsWith(".jpg") -or $filePath.EndsWith(".jpeg")) { $response.ContentType = "image/jpeg" }
elseif ($filePath.EndsWith(".png")) { $response.ContentType = "image/png" }
$response.ContentLength64 = $bytes.Length
$response.OutputStream.Write($bytes, 0, $bytes.Length)
} else {
$response.StatusCode = 404
$msg = [System.Text.Encoding]::UTF8.GetBytes("404 Not Found")
$response.OutputStream.Write($msg, 0, $msg.Length)
}
$response.OutputStream.Close()
}
} finally {
$listener.Stop()
}