mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
admin: Make Etag
a header, not a trailer (#6208)
* Making eTags a header not a trailer * Checked the write * Fixed typo * Corrected comment * Added sync Pool * Changed control flow of buffer reset / putting and changed error code * Switched from interface{} to any in bufferPool
This commit is contained in:
parent
654a3bb090
commit
4a0492f3e1
1 changed files with 21 additions and 6 deletions
27
admin.go
27
admin.go
|
@ -954,17 +954,28 @@ func makeEtag(path string, hash hash.Hash) string {
|
||||||
return fmt.Sprintf(`"%s %x"`, path, hash.Sum(nil))
|
return fmt.Sprintf(`"%s %x"`, path, hash.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This buffer pool is used to keep buffers for
|
||||||
|
// reading the config file during eTag header generation
|
||||||
|
var bufferPool = sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
return new(bytes.Buffer)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func handleConfig(w http.ResponseWriter, r *http.Request) error {
|
func handleConfig(w http.ResponseWriter, r *http.Request) error {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
// Set the ETag as a trailer header.
|
|
||||||
// The alternative is to write the config to a buffer, and
|
|
||||||
// then hash that.
|
|
||||||
w.Header().Set("Trailer", "ETag")
|
|
||||||
|
|
||||||
hash := etagHasher()
|
hash := etagHasher()
|
||||||
configWriter := io.MultiWriter(w, hash)
|
|
||||||
|
// Read the config into a buffer instead of writing directly to
|
||||||
|
// the response writer, as we want to set the ETag as the header,
|
||||||
|
// not the trailer.
|
||||||
|
buf := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
buf.Reset()
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
|
||||||
|
configWriter := io.MultiWriter(buf, hash)
|
||||||
err := readConfig(r.URL.Path, configWriter)
|
err := readConfig(r.URL.Path, configWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIError{HTTPStatus: http.StatusBadRequest, Err: err}
|
return APIError{HTTPStatus: http.StatusBadRequest, Err: err}
|
||||||
|
@ -973,6 +984,10 @@ func handleConfig(w http.ResponseWriter, r *http.Request) error {
|
||||||
// we could consider setting up a sync.Pool for the summed
|
// we could consider setting up a sync.Pool for the summed
|
||||||
// hashes to reduce GC pressure.
|
// hashes to reduce GC pressure.
|
||||||
w.Header().Set("Etag", makeEtag(r.URL.Path, hash))
|
w.Header().Set("Etag", makeEtag(r.URL.Path, hash))
|
||||||
|
_, err = w.Write(buf.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return APIError{HTTPStatus: http.StatusInternalServerError, Err: err}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue