cmd/htpacker: cope with zero-length input files
Sometimes we might be asked to serve up a zero-length input file, typically from some machine-generated CSS etc. We make some very rudimentary guess about the content-type the caller wanted and skip the mmap(2) call.
This commit is contained in:
parent
52213cf67e
commit
1b84160dcf
|
@ -325,11 +325,14 @@ func (p *packer) packFile(path string, fileToPack FileToPack) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := unix.Mmap(int(f.Fd()), 0, int(fi.Size()),
|
var data []byte
|
||||||
unix.PROT_READ, unix.MAP_SHARED)
|
if fi.Size() > 0 {
|
||||||
if err != nil {
|
data, err = unix.Mmap(int(f.Fd()), 0, int(fi.Size()),
|
||||||
p.Abort(fmt.Errorf("mmap %s: %v", fileToPack.Filename, err))
|
unix.PROT_READ, unix.MAP_SHARED)
|
||||||
return
|
if err != nil {
|
||||||
|
p.Abort(fmt.Errorf("mmap %s: %v", fileToPack.Filename, err))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare initial directory entry
|
// prepare initial directory entry
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -125,13 +126,22 @@ func filesFromListR(prefix, arg string, ftp packer.FilesToPack) error {
|
||||||
|
|
||||||
case fi.Mode().IsRegular():
|
case fi.Mode().IsRegular():
|
||||||
// sniff content type
|
// sniff content type
|
||||||
|
var ctype string
|
||||||
buf := make([]byte, 512)
|
buf := make([]byte, 512)
|
||||||
n, err := f.Read(buf)
|
n, err := f.Read(buf)
|
||||||
if err != nil {
|
switch err {
|
||||||
|
case nil:
|
||||||
|
buf = buf[:n]
|
||||||
|
ctype = http.DetectContentType(buf)
|
||||||
|
|
||||||
|
case io.EOF:
|
||||||
|
// Empty file; this is typically due to things like
|
||||||
|
// npm webpack producing empty .css files.
|
||||||
|
ctype = "text/plain; charset=UTF-8"
|
||||||
|
|
||||||
|
default:
|
||||||
return fmt.Errorf("failed to read %s: %v", arg, err)
|
return fmt.Errorf("failed to read %s: %v", arg, err)
|
||||||
}
|
}
|
||||||
buf = buf[:n]
|
|
||||||
ctype := http.DetectContentType(buf)
|
|
||||||
|
|
||||||
// augmented rules for JS / CSS / etc.
|
// augmented rules for JS / CSS / etc.
|
||||||
switch {
|
switch {
|
||||||
|
|
Loading…
Reference in New Issue