Compare commits

...

3 Commits

6 changed files with 73 additions and 19 deletions

View File

@ -27,7 +27,7 @@ const (
<meta name="theme-color" content="#ffffff">` <meta name="theme-color" content="#ffffff">`
) )
func serveFavicons() httprouter.Handle { func serveFavicons(errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fname := strings.TrimPrefix(r.URL.Path, "/") fname := strings.TrimPrefix(r.URL.Path, "/")
@ -36,8 +36,18 @@ func serveFavicons() httprouter.Handle {
return return
} }
w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) err = w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
if err != nil {
errorChannel <- err
w.Write(data) return
}
_, err = w.Write(data)
if err != nil {
errorChannel <- err
return
}
} }
} }

View File

@ -110,9 +110,7 @@ func getReader(format string, file io.Reader) (io.Reader, error) {
case "zlib": case "zlib":
return zlib.NewReader(file) return zlib.NewReader(file)
case "zstd": case "zstd":
decoder, err := zstd.NewReader(file) return zstd.NewReader(file)
return decoder.IOReadCloser(), err
} }
return io.NopCloser(file), ErrInvalidCompression return io.NopCloser(file), ErrInvalidCompression
@ -264,13 +262,28 @@ func (index *fileIndex) Import(path string) error {
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
index.clear() index.clear()
fileList(args, &filters{}, "", index, formats, errorChannel) fileList(args, &filters{}, "", index, formats, errorChannel)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Ok\n")) _, err := w.Write([]byte("Ok\n"))
if err != nil {
errorChannel <- err
return
}
if Verbose {
fmt.Printf("%s | SERVE: Index rebuild requested by %s took %s\n",
startTime.Format(logDate),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
)
}
} }
} }

View File

@ -145,7 +145,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
htmlBody.WriteString(`</table></body></html>`) htmlBody.WriteString(`</table></body></html>`)
length, err := io.WriteString(w, gohtml.Format(htmlBody.String())) written, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil { if err != nil {
return return
} }
@ -153,7 +153,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n", fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(length), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"strings"
"time" "time"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
@ -16,6 +17,10 @@ import (
func registerProfileHandler(mux *httprouter.Router, verb, path string, handler http.HandlerFunc) { func registerProfileHandler(mux *httprouter.Router, verb, path string, handler http.HandlerFunc) {
mux.HandlerFunc(verb, path, handler) mux.HandlerFunc(verb, path, handler)
if Redact && AdminPrefix != "" {
path = strings.ReplaceAll(path, AdminPrefix, "/<admin_prefix>")
}
if Handlers { if Handlers {
fmt.Printf("%s | SERVE: Registered handler for %s\n", fmt.Printf("%s | SERVE: Registered handler for %s\n",
time.Now().Format(logDate), time.Now().Format(logDate),

View File

@ -17,7 +17,7 @@ import (
const ( const (
AllowedCharacters string = `^[A-z0-9.\-_]+$` AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "4.0.1" ReleaseVersion string = "4.0.4"
) )
var ( var (

View File

@ -253,6 +253,8 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
filters := &filters{ filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes), included: splitQueryParams(r.URL.Query().Get("include"), regexes),
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
@ -384,8 +386,6 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
htmlBody.WriteString(`</body></html>`) htmlBody.WriteString(`</body></html>`)
startTime := time.Now()
formattedPage := gohtml.Format(htmlBody.String()) formattedPage := gohtml.Format(htmlBody.String())
written, err := io.WriteString(w, formattedPage) written, err := io.WriteString(w, formattedPage)
@ -409,19 +409,45 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
} }
if Russian { if Russian {
kill(path, index) err := kill(path, index)
if err != nil {
errorChannel <- err
return
}
} }
} }
} }
} }
func serveVersion() httprouter.Handle { func serveVersion(errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion)) data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion))
w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) err := w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
if err != nil {
errorChannel <- err
w.Write(data) return
}
written, err := w.Write(data)
if err != nil {
errorChannel <- err
return
}
if Verbose {
fmt.Printf("%s | SERVE: Version page (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(written),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
)
}
} }
} }
@ -578,15 +604,15 @@ func ServePage(args []string) error {
registerHandler(mux, "/", redirectRoot()) registerHandler(mux, "/", redirectRoot())
} }
registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons()) registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons(errorChannel))
registerHandler(mux, Prefix+"/favicon.ico", serveFavicons()) registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel))
registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel)) registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel))
registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel)) registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel))
registerHandler(mux, Prefix+"/version", serveVersion()) registerHandler(mux, Prefix+"/version", serveVersion(errorChannel))
if Index { if Index {
registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, errorChannel)) registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, errorChannel))