Skip to content

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

Closed as duplicate of#60940
@averms

Description

@averms

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions