chore: update test/tools/no-content-length/main.go (#1440)

* chore: update test/tools/no-content-length/main.go

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2022-07-07 22:13:44 +08:00 committed by Gaius
parent 93a8ebb4bc
commit 181a6ba87f
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
1 changed files with 19 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"io"
"log"
"math"
"net/http"
"net/textproto"
@ -35,7 +36,7 @@ var port = flag.Int("port", 80, "")
func main() {
http.Handle("/", &fileHandler{dir: "/static"})
fmt.Printf("listen on %d", *port)
log.Printf("listen on %d", *port)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
panic(err)
@ -48,6 +49,22 @@ type fileHandler struct {
}
func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
enableContentLength := f.enableContentLength
if strings.ToLower(r.Header.Get("X-Dragonfly-Enable-Content-Length")) == "true" {
enableContentLength = true
}
if r.Header.Get("X-Dragonfly-E2E-Status-Code") != "" {
str := r.Header.Get("X-Dragonfly-E2E-Status-Code")
code, err := strconv.Atoi(str)
if err != nil {
log.Printf("wrong X-Dragonfly-E2E-Status-Code format %s, error: %s", str, err)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(fmt.Sprintf("wrong X-Dragonfly-E2E-Status-Code format")))
return
}
w.WriteHeader(code)
return
}
var rg *httpRange
if s := r.Header.Get("Range"); s != "" {
rgs, err := parseRange(s, math.MaxInt)
@ -106,7 +123,7 @@ func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rd = io.LimitReader(file, rg.length)
}
if f.enableContentLength {
if enableContentLength {
w.Header().Set("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
}
_, _ = io.Copy(w, rd)