为什么我的 powershell localhost 服务器这么慢?

Why is my powershell localhost server so slow?

我拼凑了一个简单的本地服务器来托管 HTML-only 网络应用程序。我需要它,因为某些功能,如 AJAX 请求,无法从 HTML 直接从文件系统打开。

它看起来像这样并且实际有效。

# source: https://gist.github.com/19WAS85/5424431

$http = [System.Net.HttpListener]::new()
# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")
# Start the Http Server
$http.Start()

Add-Type -AssemblyName System.Web
# Log ready message to terminal
if ($http.IsListening) {
    write-host "HTTP Server Ready!  " -f 'black' -b 'gre'
    write-host "$($http.Prefixes)" -f 'y'
    #write-host "then try going to $($http.Prefixes)other/path" -f 'y'
}

#New-PSDrive -Name MyPowerShellSite -PSProvider FileSystem -Root $PWD.Path
# INFINTE LOOP
# Used to listen for requests
while ($http.IsListening) {
    # Get Request Url
    # When a request is made in a web browser the GetContext() method will return a request object
    # Our route examples below will use the request object properties to decide how to respond
    $context = $http.GetContext()

    if ($context.Request.HttpMethod -eq 'GET') {

        # We can log the request to the terminal
        write-host "$($context.Request.UserHostAddress)  =>  $($context.Request.Url)" -f 'mag'


        $URL = $context.Request.Url.LocalPath

        # Redirect root to index.html
        if($URL -eq "/") {
          $URL = "/index.html"
        }
        $Content = Get-Content -Encoding Byte -Path "web/$URL"
        $Context.Response.ContentType = [System.Web.MimeMapping]::GetMimeMapping("web/$URL")
        $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
        $Context.Response.Close()

    }
    # powershell will continue looping and listen for new requests...

}

然而,这些请求需要花费大量时间。文件读取和写入输出流的方式有问题。

有解决办法吗?太慢了,基本没用。要尝试它,请确保你 运行 它位于 web/ 子文件夹所在的位置。低速可以通过尝试打开照片等轻松测试。

关键是使用 [System.IO.File]::OpenRead 创建一个流,然后将其读取的所有内容直接复制到套接字中,套接字将其发送到浏览器。

这是整个 Powershell 本地目录 HTTP 服务器。如果您想直接提供它运行的目录,请将 "web/$URL" 更改为 $URL:

$http = [System.Net.HttpListener]::new()
# Hostname and port to listen on
$http.Prefixes.Add("http://localhost:8080/")
# Start the Http Server
$http.Start()

Add-Type -AssemblyName System.Web
# Log ready message to terminal
if ($http.IsListening) {
    write-host "HTTP Server Ready!  " -f 'black' -b 'gre'
    write-host "$($http.Prefixes)" -f 'y'
}

# INFINTE LOOP
# Used to listen for requests
while ($http.IsListening) {
    # Get Request Url
    # When a request is made in a web browser the GetContext() method will return a request object
    # Our route examples below will use the request object properties to decide how to respond
    $context = $http.GetContext()

    if ($context.Request.HttpMethod -eq 'GET') {

        # We can log the request to the terminal
        write-host "$($context.Request.UserHostAddress)  =>  $($context.Request.Url)" -f 'mag'


        $URL = $context.Request.Url.LocalPath

        # Redirect root to index.html
        if($URL -eq "/") {
          $URL = "/index.html"
        }

        $ContentStream = [System.IO.File]::OpenRead( "web/$URL" );
        $Context.Response.ContentType = [System.Web.MimeMapping]::GetMimeMapping("web/$URL")
        $ContentStream.CopyTo( $Context.Response.OutputStream );
        $Context.Response.Close()
    }
    # powershell will continue looping and listen for new requests...
}