mirror of
https://github.com/lus/pasty.git
synced 2023-08-10 21:13:09 +03:00
validate request content type
This commit is contained in:
parent
695b900f28
commit
5b0ba721b8
14
internal/web/request_accept.go
Normal file
14
internal/web/request_accept.go
Normal 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
|
||||||
|
}
|
|
@ -16,6 +16,9 @@ type v2EndpointCreatePastePayload struct {
|
||||||
|
|
||||||
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
|
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
|
if !accept(writer, request, "application/json") {
|
||||||
|
return
|
||||||
|
}
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
|
|
@ -20,6 +20,9 @@ func (server *Server) v2EndpointModifyPaste(writer http.ResponseWriter, request
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
|
if !accept(writer, request, "application/json") {
|
||||||
|
return
|
||||||
|
}
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
|
|
@ -20,6 +20,9 @@ func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read, parse and validate the request payload
|
// Read, parse and validate the request payload
|
||||||
|
if !accept(writer, request, "application/json") {
|
||||||
|
return
|
||||||
|
}
|
||||||
body, err := io.ReadAll(request.Body)
|
body, err := io.ReadAll(request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr(request, writer, err)
|
writeErr(request, writer, err)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user