mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
0452fd02e8
* attestations * post * tests * Revert "Auxiliary commit to revert individual files from afede4d949a7519902be2f1e0c485306c4ccdea7" This reverts commit 9de74879e0c41e43183da2fa7e63094cac030abe. * remove test * remove redundant return * Update beacon-chain/rpc/eth/beacon/handlers_pool.go Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> * review * include index in broadcast log --------- Co-authored-by: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package http
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
jsonMediaType = "application/json"
|
|
octetStreamMediaType = "application/octet-stream"
|
|
)
|
|
|
|
type HasStatusCode interface {
|
|
StatusCode() int
|
|
}
|
|
|
|
// DefaultErrorJson is a JSON representation of a simple error value, containing only a message and an error code.
|
|
type DefaultErrorJson struct {
|
|
Message string `json:"message"`
|
|
Code int `json:"code"`
|
|
}
|
|
|
|
func (e *DefaultErrorJson) StatusCode() int {
|
|
return e.Code
|
|
}
|
|
|
|
// WriteJson writes the response message in JSON format.
|
|
func WriteJson(w http.ResponseWriter, v any) {
|
|
w.Header().Set("Content-Type", jsonMediaType)
|
|
w.WriteHeader(http.StatusOK)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
log.WithError(err).Error("Could not write response message")
|
|
}
|
|
}
|
|
|
|
// WriteSsz writes the response message in ssz format
|
|
func WriteSsz(w http.ResponseWriter, respSsz []byte, fileName string) {
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(respSsz)))
|
|
w.Header().Set("Content-Type", octetStreamMediaType)
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+fileName)
|
|
if _, err := io.Copy(w, io.NopCloser(bytes.NewReader(respSsz))); err != nil {
|
|
log.WithError(err).Error("could not write response message")
|
|
}
|
|
}
|
|
|
|
// WriteError writes the error by manipulating headers and the body of the final response.
|
|
func WriteError(w http.ResponseWriter, errJson HasStatusCode) {
|
|
j, err := json.Marshal(errJson)
|
|
if err != nil {
|
|
log.WithError(err).Error("Could not marshal error message")
|
|
return
|
|
}
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
|
|
w.Header().Set("Content-Type", jsonMediaType)
|
|
w.WriteHeader(errJson.StatusCode())
|
|
if _, err := io.Copy(w, io.NopCloser(bytes.NewReader(j))); err != nil {
|
|
log.WithError(err).Error("Could not write error message")
|
|
}
|
|
}
|