Enforce application/json content type for request body
This commit is contained in:
parent
a2935b2060
commit
496cc86933
|
@ -76,7 +76,18 @@ func info(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureJSONRequest(w http.ResponseWriter, req *http.Request) bool {
|
||||||
|
if strings.ToLower(req.Header["Content-Type"][0]) != "application/json" {
|
||||||
|
sendJSONError(w, syncstore.RequiredDataNotFoundError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func createSync(w http.ResponseWriter, req *http.Request) {
|
func createSync(w http.ResponseWriter, req *http.Request) {
|
||||||
|
if !ensureJSONRequest(w, req) {
|
||||||
|
return
|
||||||
|
}
|
||||||
body := new(syncstore.CreateReq)
|
body := new(syncstore.CreateReq)
|
||||||
req.Body = http.MaxBytesReader(w, req.Body, 10000)
|
req.Body = http.MaxBytesReader(w, req.Body, 10000)
|
||||||
err := json.NewDecoder(req.Body).Decode(&body)
|
err := json.NewDecoder(req.Body).Decode(&body)
|
||||||
|
@ -124,6 +135,9 @@ func getVersion(syncId string, w http.ResponseWriter, _ *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateSync(syncId string, w http.ResponseWriter, req *http.Request) {
|
func updateSync(syncId string, w http.ResponseWriter, req *http.Request) {
|
||||||
|
if !ensureJSONRequest(w, req) {
|
||||||
|
return
|
||||||
|
}
|
||||||
body := new(syncstore.UpdateReq)
|
body := new(syncstore.UpdateReq)
|
||||||
req.Body = http.MaxBytesReader(w, req.Body, int64(10000 + maxSyncSize))
|
req.Body = http.MaxBytesReader(w, req.Body, int64(10000 + maxSyncSize))
|
||||||
err := json.NewDecoder(req.Body).Decode(&body)
|
err := json.NewDecoder(req.Body).Decode(&body)
|
||||||
|
|
|
@ -52,4 +52,9 @@ var (
|
||||||
"SyncDataLimitExceededException",
|
"SyncDataLimitExceededException",
|
||||||
"Sync data limit exceeded",
|
"Sync data limit exceeded",
|
||||||
http.StatusRequestEntityTooLarge)
|
http.StatusRequestEntityTooLarge)
|
||||||
|
|
||||||
|
RequiredDataNotFoundError = NewSyncError(
|
||||||
|
"RequiredDataNotFoundException",
|
||||||
|
"Unable to find required data",
|
||||||
|
http.StatusBadRequest)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue