Handler: use packed.MapAndLoad

This commit is contained in:
Laurence Withers 2026-06-06 10:19:50 +01:00
commit 203138bc05

View file

@ -3,14 +3,12 @@ package htpack
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"golang.org/x/sys/unix"
"src.lwithers.me.uk/go/htpack/packed" "src.lwithers.me.uk/go/htpack/packed"
) )
@ -21,31 +19,14 @@ const (
// New returns a new handler. Standard security headers are set. // New returns a new handler. Standard security headers are set.
func New(packfile string) (*Handler, error) { func New(packfile string) (*Handler, error) {
f, err := os.Open(packfile) mapped, err := packed.MapAndLoad(packfile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
mapped, err := unix.Mmap(int(f.Fd()), 0, int(fi.Size()),
unix.PROT_READ, unix.MAP_SHARED)
if err != nil {
return nil, err
}
_, dir, err := packed.Load(f)
if err != nil {
unix.Munmap(mapped)
return nil, err
}
h := &Handler{ h := &Handler{
mapped: mapped, mapped: mapped.Data,
dir: dir.Files, dir: mapped.Directory.Files,
headers: make(map[string]string), headers: make(map[string]string),
startTime: time.Now(), startTime: time.Now(),
} }