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 (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/sys/unix"
"src.lwithers.me.uk/go/htpack/packed"
)
@ -21,31 +19,14 @@ const (
// New returns a new handler. Standard security headers are set.
func New(packfile string) (*Handler, error) {
f, err := os.Open(packfile)
mapped, err := packed.MapAndLoad(packfile)
if err != nil {
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{
mapped: mapped,
dir: dir.Files,
mapped: mapped.Data,
dir: mapped.Directory.Files,
headers: make(map[string]string),
startTime: time.Now(),
}