Skip to content

HEAD requests are reading the contents of static files #1600

@benwa

Description

@benwa

Describe the Bug

When performing a HEAD request against a static file (likely a dynamically generated one too), Pode will read the entire contents of the file first, and then reply with the status code. The larger the file, the longer the response will take.
In my use case, the client will perform a HEAD request for every file first, and then a GET request for those files, which can take much longer than necessary as the file list is about a hundred files going up to a couple of hundred of megabytes.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a new Pode project with the following server.ps1
      Start-PodeServer -ScriptBlock {
          New-PodeLoggingMethod -Terminal | Enable-PodeRequestLogging
      
          Add-PodeEndpoint -Address 127.0.0.1 -Port 8080 -Protocol Http
      
          'Creating files...' | Out-PodeHost
          1..100 | ForEach-Object {
              New-Item -Path ".\public\$_.txt" -Force
              $fileSize = 1MB
              $randomBytes = New-Object byte[] $fileSize
              (New-Object System.Random).NextBytes($randomBytes)
              [System.IO.File]::WriteAllBytes(".\public\$_.txt", $randomBytes)
          }
      }
    
  2. Start the Pode server, and in another PowerShell instance, run
      Measure-Command { 1..100 | %{ Invoke-WebRequest -Method Head -Uri "http://127.0.0.1:8080/$_.txt" }}
  3. It will be nearly instantaneous.
  4. Modify the $fileSize variable to 100MB and rerun the server and test
  5. It will take about 15 seconds (depending on the processor)

Platform

  • OS: Windows
  • Browser: Chrome
  • Versions:
    • Pode: 2.12.1
    • PowerShell: 7.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions