erigon-pulse/cl/beacon/rw.go
2023-12-16 19:12:40 +01:00

60 lines
1.0 KiB
Go

package beacon
import (
"net/http"
)
type notFoundNoWriter struct {
rw http.ResponseWriter
code int
headers http.Header
}
func isNotFound(code int) bool {
return code == 404 || code == 405
}
func (f *notFoundNoWriter) Header() http.Header {
if isNotFound(f.code) {
return make(http.Header)
}
if f.headers == nil {
f.headers = make(http.Header)
}
return f.headers
}
func (f *notFoundNoWriter) Write(xs []byte) (int, error) {
// write code 200 if code not written yet
if f.code == 0 {
f.WriteHeader(200)
}
if isNotFound(f.code) {
return 0, nil
}
// pass on the write
return f.rw.Write(xs)
}
func (f *notFoundNoWriter) WriteHeader(statusCode int) {
if f.code != 0 {
return
}
f.code = statusCode
if isNotFound(statusCode) {
f.headers = nil
return
}
f.rw.WriteHeader(statusCode)
// if we get here, it means it is a successful write.
if f.headers != nil {
for k, v := range f.headers {
for _, x := range v {
f.rw.Header().Add(k, x)
}
}
}
f.headers = f.rw.Header()
}