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"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-03-15 18:38:07 +01:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-04-13 15:05:05 +02:00
|
|
|
"time"
|
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"
|
2020-04-01 21:22:13 +02:00
|
|
|
adapter "github.com/mainflux/mainflux/http"
|
2022-01-27 17:03:57 +01:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
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.
|
2020-04-01 21:22:13 +02:00
|
|
|
func MakeHandler(svc adapter.Service, 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
|
|
|
))
|
|
|
|
|
2022-01-24 21:18:53 +01:00
|
|
|
r.GetFunc("/health", mainflux.Health("http"))
|
2019-07-18 15:01:09 +02:00
|
|
|
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
|
|
|
|
}
|
2021-07-22 15:47:55 +02:00
|
|
|
token := r.Header.Get("Authorization")
|
|
|
|
if _, pass, ok := r.BasicAuth(); ok {
|
|
|
|
token = pass
|
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-28 11:02:35 +02:00
|
|
|
msg := messaging.Message{
|
2020-04-21 19:33:11 +02:00
|
|
|
Protocol: protocol,
|
|
|
|
Channel: chanID,
|
|
|
|
Subtopic: subtopic,
|
|
|
|
Payload: payload,
|
2020-05-04 13:14:06 +02:00
|
|
|
Created: time.Now().UnixNano(),
|
2017-09-29 00:42:45 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
req := publishReq{
|
|
|
|
msg: msg,
|
2021-07-22 15:47:55 +02:00
|
|
|
token: token,
|
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)
|
2022-02-01 17:33:23 +01:00
|
|
|
case errors.ErrAuthentication:
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2017-09-23 01:55:29 +02:00
|
|
|
default:
|
2018-05-11 16:37:32 +02:00
|
|
|
if e, ok := status.FromError(err); ok {
|
|
|
|
switch e.Code() {
|
2022-02-01 17:33:23 +01:00
|
|
|
case codes.Unauthenticated:
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2018-05-11 16:37:32 +02:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-09-23 01:55:29 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|