mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +08:00

* Split Content-Type header field on semicolon and evaluate all substrings Signed-off-by: Tony Ward <tony@lucidtron.com> * included strings library Signed-off-by: Tony Ward <tony0ward@hotmail.com> * simplify code, run gofmt Signed-off-by: tony <tony0ward@hotmail.com> Signed-off-by: tony <tony@lucidtron.com> * allow Content-Type to only contain the required type Signed-off-by: Tony Ward <tony@lucidtron.com>
336 lines
6.9 KiB
Go
336 lines
6.9 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
"github.com/go-zoo/bone"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/things"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
const (
|
|
contentType = "application/json"
|
|
offset = "offset"
|
|
limit = "limit"
|
|
|
|
defOffset = 0
|
|
defLimit = 10
|
|
)
|
|
|
|
var (
|
|
errUnsupportedContentType = errors.New("unsupported content type")
|
|
errInvalidQueryParams = errors.New("invalid query params")
|
|
)
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
|
func MakeHandler(svc things.Service) http.Handler {
|
|
opts := []kithttp.ServerOption{
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
}
|
|
|
|
r := bone.New()
|
|
|
|
r.Post("/things", kithttp.NewServer(
|
|
addThingEndpoint(svc),
|
|
decodeThingCreation,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/things/:id", kithttp.NewServer(
|
|
updateThingEndpoint(svc),
|
|
decodeThingUpdate,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/things/:id", kithttp.NewServer(
|
|
removeThingEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/things/:id", kithttp.NewServer(
|
|
viewThingEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/things/:id/channels", kithttp.NewServer(
|
|
listChannelsByThingEndpoint(svc),
|
|
decodeListByConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/things", kithttp.NewServer(
|
|
listThingsEndpoint(svc),
|
|
decodeList,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Post("/channels", kithttp.NewServer(
|
|
createChannelEndpoint(svc),
|
|
decodeChannelCreation,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/channels/:id", kithttp.NewServer(
|
|
updateChannelEndpoint(svc),
|
|
decodeChannelUpdate,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/channels/:id", kithttp.NewServer(
|
|
removeChannelEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/channels/:id", kithttp.NewServer(
|
|
viewChannelEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/channels/:id/things", kithttp.NewServer(
|
|
listThingsByChannelEndpoint(svc),
|
|
decodeListByConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/channels", kithttp.NewServer(
|
|
listChannelsEndpoint(svc),
|
|
decodeList,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/channels/:chanId/things/:thingId", kithttp.NewServer(
|
|
connectEndpoint(svc),
|
|
decodeConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/channels/:chanId/things/:thingId", kithttp.NewServer(
|
|
disconnectEndpoint(svc),
|
|
decodeConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.GetFunc("/version", mainflux.Version("things"))
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
|
|
return r
|
|
}
|
|
|
|
func decodeThingCreation(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := addThingReq{key: r.Header.Get("Authorization")}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeThingUpdate(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := updateThingReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChannelCreation(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := createChannelReq{key: r.Header.Get("Authorization")}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChannelUpdate(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := updateChannelReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeView(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := viewResourceReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeList(_ context.Context, r *http.Request) (interface{}, error) {
|
|
o, err := readUintQuery(r, offset, defOffset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
l, err := readUintQuery(r, limit, defLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req := listResourcesReq{
|
|
key: r.Header.Get("Authorization"),
|
|
offset: o,
|
|
limit: l,
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeListByConnection(_ context.Context, r *http.Request) (interface{}, error) {
|
|
o, err := readUintQuery(r, offset, defOffset)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
l, err := readUintQuery(r, limit, defLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req := listByConnectionReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
offset: o,
|
|
limit: l,
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeConnection(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := connectionReq{
|
|
key: r.Header.Get("Authorization"),
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
thingID: bone.GetValue(r, "thingId"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
if ar, ok := response.(mainflux.Response); ok {
|
|
for k, v := range ar.Headers() {
|
|
w.Header().Set(k, v)
|
|
}
|
|
|
|
w.WriteHeader(ar.Code())
|
|
|
|
if ar.Empty() {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
switch err {
|
|
case things.ErrMalformedEntity:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case things.ErrUnauthorizedAccess:
|
|
w.WriteHeader(http.StatusForbidden)
|
|
case things.ErrNotFound:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
case errUnsupportedContentType:
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
case errInvalidQueryParams:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case io.ErrUnexpectedEOF:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case io.EOF:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
default:
|
|
switch err.(type) {
|
|
case *json.SyntaxError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case *json.UnmarshalTypeError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
default:
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
func readUintQuery(r *http.Request, key string, def uint64) (uint64, error) {
|
|
vals := bone.GetQuery(r, key)
|
|
if len(vals) > 1 {
|
|
return 0, errInvalidQueryParams
|
|
}
|
|
|
|
if len(vals) == 0 {
|
|
return def, nil
|
|
}
|
|
|
|
strval := vals[0]
|
|
val, err := strconv.ParseUint(strval, 10, 64)
|
|
if err != nil {
|
|
return 0, errInvalidQueryParams
|
|
}
|
|
|
|
return val, nil
|
|
}
|