Skip to content

Commit

Permalink
Fix Content-Type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajeetdsouza committed Sep 3, 2020
1 parent cd1dbeb commit b6283b4
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -177,18 +178,27 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
Query string `json:"query"`
Variables map[string]string `json:"variables"`
}

contentType := r.Header.Get("Content-Type")
switch strings.ToLower(contentType) {
mediaType, contentTypeParams, err := mime.ParseMediaType(contentType)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, "Invalid Content-Type")
}
if charset, ok := contentTypeParams["charset"]; ok && strings.ToLower(charset) != "utf-8" {
x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported charset. "+
"Supported charset is UTF-8")
return
}

switch mediaType {
case "application/json":
if err := json.Unmarshal(body, &params); err != nil {
jsonErr := convertJSONError(string(body), err)
x.SetStatus(w, x.ErrorInvalidRequest, jsonErr.Error())
return
}

case "application/graphql+-":
params.Query = string(body)

default:
x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported Content-Type. "+
"Supported content types are application/json, application/graphql+-")
Expand Down Expand Up @@ -300,7 +310,17 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {

var req *api.Request
contentType := r.Header.Get("Content-Type")
switch strings.ToLower(contentType) {
mediaType, contentTypeParams, err := mime.ParseMediaType(contentType)
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, "Invalid Content-Type")
}
if charset, ok := contentTypeParams["charset"]; ok && strings.ToLower(charset) != "utf-8" {
x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported charset. "+
"Supported charset is UTF-8")
return
}

switch mediaType {
case "application/json":
ms := make(map[string]*skipJSONUnmarshal)
if err := json.Unmarshal(body, &ms); err != nil {
Expand Down

0 comments on commit b6283b4

Please sign in to comment.