2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleweb
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2019-09-09 19:33:05 +01:00
|
|
|
"mime"
|
2018-11-14 10:50:15 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-09-20 18:40:26 +01:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console/consoleweb/consoleql"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2019-09-20 18:40:26 +01:00
|
|
|
// ContentLengthLimit describes 4KB limit
|
|
|
|
const ContentLengthLimit = 4 * memory.KB
|
|
|
|
|
2019-09-09 19:33:05 +01:00
|
|
|
func init() {
|
|
|
|
err := mime.AddExtensionType(".ttf", "font/ttf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mime.AddExtensionType(".txt", "text/plain")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// JSON request from graphql clients
|
|
|
|
type graphqlJSON struct {
|
|
|
|
Query string
|
|
|
|
OperationName string
|
|
|
|
Variables map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
// getToken retrieves token from request
|
|
|
|
func getToken(req *http.Request) string {
|
|
|
|
value := req.Header.Get(authorization)
|
|
|
|
if value == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(value, authorizationBearer) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return value[len(authorizationBearer):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQuery retrieves graphql query from request
|
2019-09-20 18:40:26 +01:00
|
|
|
func getQuery(w http.ResponseWriter, req *http.Request) (query graphqlJSON, err error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
2019-01-15 13:03:24 +00:00
|
|
|
query.Query = req.URL.Query().Get(consoleql.Query)
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
case http.MethodPost:
|
2019-09-20 18:40:26 +01:00
|
|
|
return queryPOST(w, req)
|
2018-11-14 10:50:15 +00:00
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("wrong http request type")
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// queryPOST retrieves graphql query from POST request
|
2019-09-20 18:40:26 +01:00
|
|
|
func queryPOST(w http.ResponseWriter, req *http.Request) (query graphqlJSON, err error) {
|
|
|
|
limitedReader := http.MaxBytesReader(w, req.Body, ContentLengthLimit.Int64())
|
2018-11-14 10:50:15 +00:00
|
|
|
switch typ := req.Header.Get(contentType); typ {
|
|
|
|
case applicationGraphql:
|
2019-09-20 18:40:26 +01:00
|
|
|
body, err := ioutil.ReadAll(limitedReader)
|
2018-11-16 14:34:35 +00:00
|
|
|
query.Query = string(body)
|
2019-09-20 18:40:26 +01:00
|
|
|
return query, errs.Combine(err, limitedReader.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
case applicationJSON:
|
2019-09-20 18:40:26 +01:00
|
|
|
err := json.NewDecoder(limitedReader).Decode(&query)
|
|
|
|
return query, errs.Combine(err, limitedReader.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("can't parse request body of type %s", typ)
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|