Skip to content

fix(http): allow HEAD method in file-server #6752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

krugerLucas
Copy link

Summary

Fixes inconsistency where serveFile() supported HEAD requests but serveDir() returned 405 Method Not Allowed.

Problem

  • serveFile() correctly handles HEAD requests (returns headers without body)
  • serveDir() rejects HEAD requests with 405 Method Not Allowed
  • This creates inconsistent behavior and breaks HTTP compliance

Solution

  • Changed line 658 in serveDir() to allow HEAD requests alongside GET
  • HEAD requests now properly delegate to serveFile() which already has correct implementation
  • Added comprehensive tests for both file and directory HEAD requests

Changes

  • http/file_server.ts: Allow HEAD method in serveDir validation
  • http/file_server_test.ts: Add tests for serveDirHeadRequest scenarios

Test Plan

  • New tests pass: serveDirHeadRequest and serveDirHeadRequestForDirectoryWithIndex
  • Existing tests still pass
  • HEAD returns correct headers without body
  • POST/PUT/DELETE still properly rejected with 405

Fixes #6750

This fixes an inconsistency where serveFile() supported HEAD requests
but serveDir() returned 405 Method Not Allowed. Now both functions
consistently support GET and HEAD methods.

The fix changes line 658 in serveDir() to allow HEAD requests, which
then properly delegate to serveFile() that already has correct HEAD
implementation.

Fixes denoland#6750
@krugerLucas krugerLucas requested a review from kt3k as a code owner July 5, 2025 19:52
@CLAassistant
Copy link

CLAassistant commented Jul 5, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the http label Jul 5, 2025
Copy link

codecov bot commented Jul 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.14%. Comparing base (7a77939) to head (a6cb2fa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6752   +/-   ##
=======================================
  Coverage   94.14%   94.14%           
=======================================
  Files         585      585           
  Lines       42367    42367           
  Branches     6683     6683           
=======================================
  Hits        39888    39888           
  Misses       2430     2430           
  Partials       49       49           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kt3k
Copy link
Member

kt3k commented Jul 8, 2025

This doesn't change createServeDirResponse and request body is still returned when the file server returns directory listing (You can check assert(!res.body) fails with the url http://localhost/nested-subdirs/subdir/). We need to modify createServeDirResponse to return null body response when the request method is HEAD (in a similar way as serveFile)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow HEAD mathod in file-server
3 participants