Compare commits
2 Commits
1b84160dcf
...
6ea49bb3b3
Author | SHA1 | Date |
---|---|---|
Laurence Withers | 6ea49bb3b3 | |
Laurence Withers | 83a5226e1a |
4
go.mod
4
go.mod
|
@ -1,8 +1,8 @@
|
|||
module src.lwithers.me.uk/go/htpack
|
||||
|
||||
require (
|
||||
github.com/gogo/protobuf v1.2.1
|
||||
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
|
||||
github.com/gogo/protobuf v1.3.2
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e
|
||||
)
|
||||
|
||||
go 1.13
|
||||
|
|
38
go.sum
38
go.sum
|
@ -1,7 +1,33 @@
|
|||
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1 h1:gZpLHxUX5BdYLA08Lj4YCJNN/jk7KtquiArPoeX0WvA=
|
||||
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
|
89
handler.go
89
handler.go
|
@ -2,14 +2,12 @@ package htpack
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
@ -21,14 +19,13 @@ const (
|
|||
encodingBrotli = "br"
|
||||
)
|
||||
|
||||
// TODO: logging
|
||||
|
||||
// New returns a new handler. Standard security headers are set.
|
||||
func New(packfile string) (*Handler, error) {
|
||||
f, err := os.Open(packfile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
|
@ -37,19 +34,16 @@ func New(packfile string) (*Handler, error) {
|
|||
mapped, err := unix.Mmap(int(f.Fd()), 0, int(fi.Size()),
|
||||
unix.PROT_READ, unix.MAP_SHARED)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, dir, err := packed.Load(f)
|
||||
if err != nil {
|
||||
unix.Munmap(mapped)
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h := &Handler{
|
||||
f: f,
|
||||
mapped: mapped,
|
||||
dir: dir.Files,
|
||||
headers: make(map[string]string),
|
||||
|
@ -67,7 +61,6 @@ func New(packfile string) (*Handler, error) {
|
|||
|
||||
// Handler implements http.Handler and allows options to be set.
|
||||
type Handler struct {
|
||||
f *os.File
|
||||
mapped []byte
|
||||
dir map[string]*packed.File
|
||||
headers map[string]string
|
||||
|
@ -203,87 +196,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
if req.Method == "HEAD" {
|
||||
return
|
||||
}
|
||||
h.sendfile(w, data, offset, length)
|
||||
}
|
||||
|
||||
func (h *Handler) sendfile(w http.ResponseWriter, data *packed.FileData,
|
||||
offset, length uint64,
|
||||
) {
|
||||
hj, ok := w.(http.Hijacker)
|
||||
if !ok {
|
||||
// fallback
|
||||
h.copyfile(w, data, offset, length)
|
||||
return
|
||||
}
|
||||
|
||||
conn, buf, err := hj.Hijack()
|
||||
if err != nil {
|
||||
// fallback
|
||||
h.copyfile(w, data, offset, length)
|
||||
return
|
||||
}
|
||||
|
||||
tcp, ok := conn.(*net.TCPConn)
|
||||
if !ok {
|
||||
// fallback
|
||||
h.copyfile(w, data, offset, length)
|
||||
return
|
||||
}
|
||||
defer tcp.Close()
|
||||
|
||||
rawsock, err := tcp.SyscallConn()
|
||||
if err == nil {
|
||||
err = buf.Flush()
|
||||
}
|
||||
if err != nil {
|
||||
// error only returned if the underlying connection is broken,
|
||||
// so there's no point calling sendfile
|
||||
return
|
||||
}
|
||||
|
||||
var breakErr error
|
||||
off := int64(data.Offset + offset)
|
||||
remain := length
|
||||
|
||||
for breakErr == nil && remain > 0 {
|
||||
// sendfile(2) can send a maximum of 1GiB
|
||||
var amt int
|
||||
if remain > (1 << 30) {
|
||||
amt = (1 << 30)
|
||||
} else {
|
||||
amt = int(remain)
|
||||
}
|
||||
|
||||
// behaviour of control function:
|
||||
// · some bytes written: sets written > 0, returns true (breaks
|
||||
// out of loop on first write)
|
||||
// · EAGAIN: returns false (causes Write() to loop until
|
||||
// success or permanent failure)
|
||||
// · other error: sets breakErr
|
||||
var written int
|
||||
rawsock.Write(func(outfd uintptr) bool {
|
||||
written, err = unix.Sendfile(int(outfd), int(h.f.Fd()), &off, amt)
|
||||
switch err {
|
||||
case nil:
|
||||
return true
|
||||
case syscall.EAGAIN:
|
||||
return false
|
||||
default:
|
||||
breakErr = err
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
// we may have had a partial write, or file may have been > 1GiB
|
||||
remain -= uint64(written)
|
||||
}
|
||||
}
|
||||
|
||||
// copyfile is a fallback handler that uses write(2) on our memory-mapped data
|
||||
// to push out the response.
|
||||
func (h *Handler) copyfile(w http.ResponseWriter, data *packed.FileData,
|
||||
offset, length uint64,
|
||||
) {
|
||||
offset += data.Offset
|
||||
w.Write(h.mapped[offset : offset+length])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue