Last active
February 28, 2023 14:59
-
-
Save UnleashTheCode/79f6d65cc028a87372406f7f26a78e71 to your computer and use it in GitHub Desktop.
Simple HTTP Server Powershell for file transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ($DPath, $DPort) | |
if (!$DPath) | |
{ | |
$DPath = '.' | |
} | |
if (!$DPort) | |
{ | |
$DPort = '4444' | |
} | |
$url = 'http://*:' + $DPort + '/' | |
$listener = New-Object System.Net.HttpListener | |
$listener.Prefixes.Add($url) | |
$listener.Start() | |
Write-Host "Listening at $url..." | |
while ($listener.IsListening) | |
{ | |
$context = $listener.GetContext() | |
$requestUrl = $context.Request.Url | |
if ($context.Request.HttpMethod -eq 'GET' -and $context.Request.RawUrl -eq '/quit') | |
{ | |
Write-Host 'Terminating...' | |
$listener.Close() | |
exit | |
} | |
else | |
{ | |
$response = $context.Response | |
Write-Host '' | |
Write-Host "> $requestUrl" | |
$localPath = $DPath + $requestUrl.LocalPath | |
Write-Host "< $LocalPath" | |
$MyRawString = Get-Content -Raw $localPath | |
$buffer = [System.Text.Encoding]::UTF8.GetBytes($MyRawString) | |
$response.ContentLength64 = $buffer.Length | |
$response.OutputStream.Write($buffer, 0, $buffer.Length) | |
$response.Close() | |
$responseStatus = $response.StatusCode | |
Write-Host "< $responseStatus" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Help:
To exit the loop make a get request to quit:
curl http://IP:PORT/quit
Possible problems: