Skip to content

Instantly share code, notes, and snippets.

@UnleashTheCode
Last active February 28, 2023 14:59
Show Gist options
  • Save UnleashTheCode/79f6d65cc028a87372406f7f26a78e71 to your computer and use it in GitHub Desktop.
Save UnleashTheCode/79f6d65cc028a87372406f7f26a78e71 to your computer and use it in GitHub Desktop.
Simple HTTP Server Powershell for file transfer
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"
}
}
@UnleashTheCode
Copy link
Author

UnleashTheCode commented Sep 28, 2022

Help:

# Start
./shs.ps1

### The above and below commands are equivalents

# You can pass DPath and DPort
./shs.ps1 -DPath '.' -DPort '4444' #This values are the defaults

To exit the loop make a get request to quit:

curl http://IP:PORT/quit

Possible problems:

  • Not tested in Win environment(Path maybe won't work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment