37 lines
771 B
Go
37 lines
771 B
Go
package httpsecurity
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"proxy-pool/internal/platform/httpapi"
|
|
)
|
|
|
|
func WriteProblem(writer http.ResponseWriter, requestID string, err error) bool {
|
|
var securityError *HTTPError
|
|
if !errors.As(err, &securityError) || securityError.StatusCode < 400 || securityError.StatusCode > 599 {
|
|
return false
|
|
}
|
|
for name, values := range securityError.Header {
|
|
for _, value := range values {
|
|
writer.Header().Add(name, value)
|
|
}
|
|
}
|
|
code := securityError.Code
|
|
if code == "" {
|
|
code = "SECURITY_REJECTED"
|
|
}
|
|
title := http.StatusText(securityError.StatusCode)
|
|
if title == "" {
|
|
title = "Request rejected"
|
|
}
|
|
httpapi.WriteProblem(writer, httpapi.NewProblem(
|
|
securityError.StatusCode,
|
|
code,
|
|
title,
|
|
"",
|
|
requestID,
|
|
))
|
|
return true
|
|
}
|