func()

in internal/serving/disk/helpers.go [88:130]


func (reader *Reader) handleContentEncoding(ctx context.Context, w http.ResponseWriter, r *http.Request, root vfs.Root, fullPath string) string {
	// don't accept range requests for compressed content
	if r.Header.Get("Range") != "" {
		return fullPath
	}

	acceptHeader := r.Header.Get("Accept-Encoding")

	// don't send compressed content if there's no accept-encoding header
	if acceptHeader == "" {
		return fullPath
	}

	results, err := supportedEncodings.Negotiate(acceptHeader, contentencoding.AliasIdentity)
	if err != nil {
		return fullPath
	}

	if len(results) == 0 {
		return fullPath
	}

	for _, encoding := range results {
		if encoding == contentencoding.Identity {
			break
		}

		extension := compressedEncodings[encoding]
		path := fullPath + extension

		// Ensure the file is not a symlink
		if fi, err := root.Lstat(ctx, path); err == nil && fi.Mode().IsRegular() {
			w.Header().Set("Content-Encoding", encoding)

			// http.ServeContent doesn't set Content-Length if Content-Encoding is set
			w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10))

			return path
		}
	}

	return fullPath
}