cloudwego / cloudwego/hertz

[Question] Static method not work welll with relative path.

Open
#1,121 5 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Go
Stars
7.4k
Forks
643
Avg merge
14h 5m
Merged PRs (30d)
2

Description

**Describe the bug**
I'm trying to change relative path of a Static method like `h.Static("/static/", "./static")`. When it comes to visit `http://127.0.0.1:8888/static/hello.txt`. It comes back with message `[Cannot open requested path]`. An error report comes out.
···
2024/05/23 22:47:40.016435 transport.go:65: [Info] HERTZ: HTTP server listening on address=[::]:8888
2024/05/23 22:47:48.879962 fs.go:856: [Error] HERTZ: Cannot open file="./static/static/hello.txt", error=open ./static/static/hello.txt: The system cannot find the path specified.
···
It seems like Static join `relative path` and `root path` together.
**To Reproduce**
1. prepare file `static/hello.txt`
2. change relative path of Static to `/static/`
3. start server && visit except path `http://127.0.0.1:8888/static/hello.txt`
4. error occure

**Expected behavior**
get content of hello.txt
**Screenshots**

![image](https://github.com/cloudwego/hertz/assets/37320653/fefa72a1-3b67-4eef-a800-ff04fe793345)

**Hertz version:**

github.com/cloudwego/hertz v0.9.0

**Environment:**

```
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\TB\AppData\Local\go-build
set GOENV=C:\Users\TB\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\goproject\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\goproject
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\goproject\src\go_code\hz_app\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\TB\AppData\Local\Temp\go-build3109564830=/tmp/go-build -gno-record-gcc-switches
```

**Additional context**

The entire family of Static family has this problem.

Contributor guide

Open the contributing guide

Research direction

Start at the Static method and its related Static-family methods, then trace the request handling that produces the fs.go:856 error. Reproduce the Windows case with a relative root and /static/ route, and verify that visiting /static/hello.txt reads static/hello.txt rather than static/static/hello.txt.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.