Skip to content

net/http: FileServer is sending 304 even if it is running in a different directory #73618

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

Closed
averms opened this issue May 7, 2025 · 2 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@averms
Copy link

averms commented May 7, 2025

Go version

go version go1.24.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/var/home/anon/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/var/home/anon/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3141057410=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/var/home/anon/.local/share/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/var/home/anon/.local/share/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/var/home/linuxbrew/.linuxbrew/Cellar/go/1.24.3/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='off'
GOTELEMETRYDIR='/var/home/anon/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/var/home/linuxbrew/.linuxbrew/Cellar/go/1.24.3/libexec/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

This is just #42051. Re-reported because I think it was incorrectly closed.

The program is still:

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"
)

func main() {
	port := flag.Int("p", 8000, "port number")
	dir := flag.String("d", ".", "directory path")
	flag.Parse()

	fi, err := os.Stat(*dir)
	if err != nil {
		log.Fatal(err)
	}
	if !fi.IsDir() {
		log.Fatal(fmt.Errorf("%s isn't a directory\n", *dir))
	}

	addr := ":" + strconv.Itoa(*port)
	handler := http.FileServer(http.Dir(*dir))

	fmt.Printf("serving %s on http://localhost:%d\n", *dir, *port)
	log.Fatal(http.ListenAndServe(addr, handler))
}
  1. Run the program in directory A.
  2. Navigate to localhost:8000 in Chrome or Firefox.
  3. A directory listing for directory A is shown.
  4. Now terminate the program and reopen it in directory B
  5. Refresh the page in Chrome or Firefox.
  6. The directory listing for directory A is still shown.

I believe this has something to do with the interaction between the Last-Modified response and If-Modified-Since request headers.

I have tested this on Firefox and Chrome on both Linux and macOS.

What did you see happen?

The content of the directory where the server was previously running and status code 304

What did you expect to see?

The current directory listing

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label May 7, 2025
@seankhliao
Copy link
Member

FileServer uses the directory's modtime as the Last-Modified date.
Given the current behaviour is mostly reasonable (use unique URIs for unique resources), I don't think much will change unless #60940 is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants