Pode icon indicating copy to clipboard operation
Pode copied to clipboard

HEAD requests are reading the contents of static files

Open benwa opened this issue 4 months ago • 2 comments

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

benwa avatar Aug 25 '25 18:08 benwa

@Badgerati, do you think this is something that is temporarily solvable with Middleware, or is it a bit deeper than that?

benwa avatar Aug 27 '25 23:08 benwa

Hi @benwa,

Glancing at the code it appears that the inbuilt /public middleware isn't respecting the HEAD method 🤔

There is an approach to override inbuilt middleware - in this case for __pode_mw_static_content__ - here: https://badgerati.github.io/Pode/Tutorials/Middleware/Overview/#overriding-inbuilt. This would involve building your own /public middleware handler - the original code can be found in the Private Get-PodePublicMiddleware function, and some of the functions it calls itself are private as well so might be tricky.

This is definitely a bug though, the files shouldn't be being read on HEAD requests.

Badgerati avatar Sep 01 '25 22:09 Badgerati