2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2017-09-23 01:55:29 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-03-15 18:38:07 +01:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
kitot "github.com/go-kit/kit/tracing/opentracing"
|
2017-09-23 01:55:29 +02:00
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
|
|
"github.com/go-zoo/bone"
|
2017-09-23 15:52:39 +02:00
|
|
|
"github.com/mainflux/mainflux"
|
2018-05-15 17:13:09 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2019-07-18 15:01:09 +02:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-09-23 01:55:29 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-05-11 16:37:32 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2017-09-23 01:55:29 +02:00
|
|
|
)
|
|
|
|
|
2018-05-21 12:51:46 +02:00
|
|
|
const protocol = "http"
|
2017-09-23 01:55:29 +02:00
|
|
|
|
|
|
|
var (
|
2019-03-15 18:38:07 +01:00
|
|
|
errMalformedData = errors.New("malformed request data")
|
|
|
|
errMalformedSubtopic = errors.New("malformed subtopic")
|
|
|
|
)
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
var channelPartRegExp = regexp.MustCompile(`^/channels/([\w\-]+)/messages(/[^?]*)?(\?.*)?$`)
|
2017-09-23 01:55:29 +02:00
|
|
|
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
2019-07-18 15:01:09 +02:00
|
|
|
func MakeHandler(svc mainflux.MessagePublisher, tracer opentracing.Tracer) http.Handler {
|
2017-09-23 01:55:29 +02:00
|
|
|
opts := []kithttp.ServerOption{
|
|
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
r := bone.New()
|
|
|
|
r.Post("/channels/:id/messages", kithttp.NewServer(
|
|
|
|
kitot.TraceServer(tracer, "publish")(sendMessageEndpoint(svc)),
|
|
|
|
decodeRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
))
|
|
|
|
|
|
|
|
r.Post("/channels/:id/messages/*", kithttp.NewServer(
|
|
|
|
kitot.TraceServer(tracer, "publish")(sendMessageEndpoint(svc)),
|
2017-09-23 01:55:29 +02:00
|
|
|
decodeRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
2019-07-18 15:01:09 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
r.GetFunc("/version", mainflux.Version("http"))
|
|
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
|
|
|
|
|
|
return r
|
2019-03-15 18:38:07 +01:00
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func parseSubtopic(subtopic string) (string, error) {
|
|
|
|
if subtopic == "" {
|
|
|
|
return subtopic, nil
|
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
subtopic, err := url.QueryUnescape(subtopic)
|
2019-03-15 18:38:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", errMalformedSubtopic
|
|
|
|
}
|
|
|
|
subtopic = strings.Replace(subtopic, "/", ".", -1)
|
2019-03-19 16:05:56 +01:00
|
|
|
|
|
|
|
elems := strings.Split(subtopic, ".")
|
|
|
|
filteredElems := []string{}
|
|
|
|
for _, elem := range elems {
|
|
|
|
if elem == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(elem) > 1 && (strings.Contains(elem, "*") || strings.Contains(elem, ">")) {
|
|
|
|
return "", errMalformedSubtopic
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredElems = append(filteredElems, elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
subtopic = strings.Join(filteredElems, ".")
|
2019-03-15 18:38:07 +01:00
|
|
|
return subtopic, nil
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func decodeRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
2019-03-15 18:38:07 +01:00
|
|
|
channelParts := channelPartRegExp.FindStringSubmatch(r.RequestURI)
|
|
|
|
if len(channelParts) < 2 {
|
|
|
|
return nil, errMalformedData
|
|
|
|
}
|
|
|
|
|
|
|
|
chanID := bone.GetValue(r, "id")
|
|
|
|
subtopic, err := parseSubtopic(channelParts[2])
|
2017-09-23 01:55:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
payload, err := decodePayload(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-05-21 12:51:46 +02:00
|
|
|
}
|
|
|
|
|
2019-07-25 02:22:28 +02:00
|
|
|
ct := r.Header.Get("Content-Type")
|
2019-11-05 11:57:16 +01:00
|
|
|
msg := mainflux.Message{
|
2017-09-29 00:42:45 +02:00
|
|
|
Protocol: protocol,
|
2019-07-25 02:22:28 +02:00
|
|
|
ContentType: ct,
|
2019-03-15 18:38:07 +01:00
|
|
|
Channel: chanID,
|
|
|
|
Subtopic: subtopic,
|
2017-09-29 00:42:45 +02:00
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
req := publishReq{
|
|
|
|
msg: msg,
|
|
|
|
token: r.Header.Get("Authorization"),
|
2018-01-07 14:42:38 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
return req, nil
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-29 00:42:45 +02:00
|
|
|
func decodePayload(body io.ReadCloser) ([]byte, error) {
|
|
|
|
payload, err := ioutil.ReadAll(body)
|
2017-09-23 01:55:29 +02:00
|
|
|
if err != nil {
|
2017-09-29 00:42:45 +02:00
|
|
|
return nil, errMalformedData
|
2017-09-23 01:55:29 +02:00
|
|
|
}
|
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|
|
|
switch err {
|
2019-03-19 16:05:56 +01:00
|
|
|
case errMalformedData, errMalformedSubtopic:
|
2017-09-23 01:55:29 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2018-05-15 17:13:09 +02:00
|
|
|
case things.ErrUnauthorizedAccess:
|
2017-09-23 01:55:29 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
default:
|
2018-05-11 16:37:32 +02:00
|
|
|
if e, ok := status.FromError(err); ok {
|
|
|
|
switch e.Code() {
|
|
|
|
case codes.PermissionDenied:
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|