Skip to content
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

HTTP HEAD can not get the content-length(always 0) #447

Closed
2 tasks done
snowyu opened this issue Apr 3, 2024 · 3 comments · Fixed by #448
Closed
2 tasks done

HTTP HEAD can not get the content-length(always 0) #447

snowyu opened this issue Apr 3, 2024 · 3 comments · Fixed by #448

Comments

@snowyu
Copy link

snowyu commented Apr 3, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.26.2

Plugin version

7.0.2

Node.js version

18.19.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 22.04.4 LTS

Description

Just write a simple static files server.
Then HTTP HEAD method to get an exists file info: curl -I http://localhost:8089/x.txt

The wrong is the headRouteOnSendHandler(fastify/lib/headRoute.js) rewrite the content-length to 0.
remove this should be ok.

Steps to Reproduce

Server:

import path from 'path'
import { fastify } from 'fastify';
import { fastifyStatic } from '@fastify/static';
export async function setup() {
  server = createServer({
    root: path.join(__dirname, 'public'),
  })
  const port = 8080
  const host = '127.0.0.1'
  const url = `http://${host}:${port}/`
  await server.listen({port, host})
  server.url = url
  console.log(server.printRoutes({ includeHooks: true, includeMeta: ['errorHandler'] }))
}

function createServer(options) {
  const server = fastify(options)
  options = { ...options }
  if (options.root) {
    server.register(fastifyStatic, options)
  }
  return server
}

Client: curl -I http://localhost:8089/x.txt

curl -I http://localhost:8089/x.txt

HTTP/1.1 200 OK
accept-ranges: bytes
cache-control: public, max-age=0
last-modified: Fri, 22 Mar 2024 11:47:23 GMT
etag: W/"2134c4-18e65fcb2ad"
content-type: text/plain; charset=UTF-8
content-length: 0
Date: Wed, 03 Apr 2024 05:12:42 GMT
Connection: keep-alive
Keep-Alive: timeout=72

Expected Behavior

Get the file size correctly.

@Eomm
Copy link
Member

Eomm commented Apr 3, 2024

I think this is more like a feature tho
https://www.rfc-editor.org/rfc/rfc9110#HEAD

For HEAD routes, we are not sending the body so the content-length is always 0, but the RFC stands that the server MAY sent the real content-length without the payload

@climba03003
Copy link
Member

It is actually a regression introduced in #436
I over-looked the wildcard case and think that exposeHeadRoute may works, but actually based on the current implementation it is not.

@climba03003
Copy link
Member

Released in 7.0.3

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

Successfully merging a pull request may close this issue.

3 participants