Explicitly set Content-Type header; Ensure that response body contains at least one byte

This commit is contained in:
DarthSim
2024-02-10 20:35:51 +03:00
parent 8d8dc11d09
commit fb028170b3
2 changed files with 11 additions and 2 deletions

View File

@@ -2,15 +2,16 @@ package router
import (
"encoding/json"
"fmt"
"net"
"net/http"
"regexp"
"strings"
nanoid "github.com/matoous/go-nanoid/v2"
log "github.com/sirupsen/logrus"
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/ierrors"
)
const (
@@ -156,9 +157,11 @@ func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}
log.Warningf("Route for %s is not defined", req.URL.Path)
LogResponse(reqID, req, 404, ierrors.New(404, fmt.Sprintf("Route for %s is not defined", req.URL.Path), "Not found"))
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(404)
rw.Write([]byte{' '})
}
func replaceRemoteAddr(req *http.Request, ip string) {

View File

@@ -151,6 +151,7 @@ func withPanicHandler(h router.RouteHandler) router.RouteHandler {
router.LogResponse(reqID, r, ierr.StatusCode, ierr)
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(ierr.StatusCode)
if config.DevelopmentErrorsMode {
@@ -181,11 +182,16 @@ func handleHealth(reqID string, rw http.ResponseWriter, r *http.Request) {
ierr = ierrors.Wrap(err, 1)
}
if len(msg) == 0 {
msg = []byte{' '}
}
// Log response only if something went wrong
if ierr != nil {
router.LogResponse(reqID, r, status, ierr)
}
rw.Header().Set("Content-Type", "text/plain")
rw.Header().Set("Cache-Control", "no-cache")
rw.WriteHeader(status)
rw.Write(msg)