validate request content type

This commit is contained in:
Lukas Schulte Pelkum 2023-06-17 18:38:58 +02:00
parent 695b900f28
commit 5b0ba721b8
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
4 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package web
import "net/http"
func accept(writer http.ResponseWriter, request *http.Request, contentTypes ...string) bool {
contentType := request.Header.Get("Content-Type")
for _, accepted := range contentTypes {
if contentType == accepted {
return true
}
}
writeString(writer, http.StatusUnsupportedMediaType, "unsupported media type")
return false
}

View File

@ -16,6 +16,9 @@ type v2EndpointCreatePastePayload struct {
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)

View File

@ -20,6 +20,9 @@ func (server *Server) v2EndpointModifyPaste(writer http.ResponseWriter, request
}
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)

View File

@ -20,6 +20,9 @@ func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request
}
// Read, parse and validate the request payload
if !accept(writer, request, "application/json") {
return
}
body, err := io.ReadAll(request.Body)
if err != nil {
writeErr(request, writer, err)