What I'm trying to do is a bit complex and specific maybe but any general info could help.
I want to inject javascript at the end of html files. I'm wrapping the reader used in a http fileserver. Code works but only for some pages seemingly at random. Sometimes it hits EOF and the byte slice is the exact length for appending my javascript, but sometimes len(b)=1 which makes no sense to me.
My reader:
func (i injectedFile) Read(b []byte) (n int, err error) {
fi, _ := i.Stat()
n, err = i.file.Read(b)
if err == io.EOF && path.Ext(fi.Name()) == ".html" {
for i, v := range JS {
b[i] = v
}
n += len(JS)
}
return n, err
}
I've wrapped the file info size to account for increased size:
func (ifi injectedFileInfo) Size() int64 {
if path.Ext(ifi.Name()) == ".html" {
return ifi.fileInfo.Size() + int64(len(JS))
} else {
return ifi.fileInfo.Size()
}
}
