erigon-pulse/cl/beacon/rw.go

60 lines
1.0 KiB
Go
Raw Normal View History

package beacon
import (
"net/http"
)
type notFoundNoWriter struct {
rw http.ResponseWriter
code int
headers http.Header
}
2023-12-16 18:12:40 +00:00
func isNotFound(code int) bool {
return code == 404 || code == 405
}
func (f *notFoundNoWriter) Header() http.Header {
2023-12-16 18:12:40 +00:00
if isNotFound(f.code) {
return make(http.Header)
}
2023-12-16 18:12:40 +00:00
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)
}
2023-12-16 18:12:40 +00:00
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
2023-12-16 18:12:40 +00:00
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()
}