2023-12-04 23:13:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-04 23:13:52 +00:00
|
|
|
func (f *notFoundNoWriter) Header() http.Header {
|
2023-12-16 18:12:40 +00:00
|
|
|
if isNotFound(f.code) {
|
2023-12-04 23:13:52 +00:00
|
|
|
return make(http.Header)
|
|
|
|
}
|
2023-12-16 18:12:40 +00:00
|
|
|
if f.headers == nil {
|
|
|
|
f.headers = make(http.Header)
|
|
|
|
}
|
|
|
|
return f.headers
|
2023-12-04 23:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-12-04 23:13:52 +00:00
|
|
|
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()
|
2023-12-04 23:13:52 +00:00
|
|
|
}
|