diff --git a/fileserver/fileserver.go b/fileserver/fileserver.go index 524504f..a0a4074 100644 --- a/fileserver/fileserver.go +++ b/fileserver/fileserver.go @@ -13,6 +13,7 @@ import ( "os" "path" "path/filepath" + "regexp" "sort" "strconv" "strings" @@ -27,6 +28,8 @@ import ( const sniffLen = 512 +var simpleRangeRegexp = regexp.MustCompile(`^bytes=(\d+)-(\d*)$`) + func rawHeaderGet(h http.Header, key string) string { if v := h[key]; len(v) > 0 { return v[0] @@ -148,6 +151,26 @@ func serveContent(ci inject.CopyInject, w http.ResponseWriter, r *http.Request, } } + rangeopt := r.Header.Get("Range") + if rangeopt != "" && !injector.Found() && r.Method == "GET" { + m := simpleRangeRegexp.FindStringSubmatch(rangeopt) + if m != nil { + start, err1 := strconv.ParseInt(m[1], 10, 64) + end, err2 := strconv.ParseInt(m[2], 10, 64) + if err2 != nil { + end = size - 1 + } + + if err1 == nil && start <= end && end < size { + w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, size)) + w.WriteHeader(206) + content.Seek(start, io.SeekStart) + _, err = io.CopyN(w, content, end-start+1) + return err + } + } + } + w.WriteHeader(code) if r.Method != "HEAD" { _, err := injector.Copy(w)