2016-06-30 22:21:57 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-02-05 18:31:51 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-02-06 18:53:25 +03:00
|
|
|
"io"
|
2021-02-05 18:31:51 +03:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-04-16 17:14:27 +03:00
|
|
|
"sync"
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2021-10-26 22:36:04 +03:00
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
rhttp "go.unistack.org/micro/v3/util/http"
|
|
|
|
rflutil "go.unistack.org/micro/v3/util/reflect"
|
2021-02-05 18:31:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) {
|
|
|
|
w.WriteHeader(status)
|
2021-04-26 00:43:06 +03:00
|
|
|
if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
|
|
|
|
logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr)
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
DefaultContentType = "application/json"
|
2016-06-30 22:21:57 +03:00
|
|
|
)
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
type patHandler struct {
|
|
|
|
mtype *methodType
|
|
|
|
rcvr reflect.Value
|
2021-10-26 22:36:04 +03:00
|
|
|
name string
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
type httpHandler struct {
|
2021-03-09 14:19:59 +03:00
|
|
|
opts server.HandlerOptions
|
|
|
|
hd interface{}
|
2022-01-22 00:46:07 +03:00
|
|
|
handlers *rhttp.Trie
|
2021-04-25 12:02:18 +03:00
|
|
|
name string
|
|
|
|
eps []*register.Endpoint
|
|
|
|
sopts server.Options
|
2021-04-16 17:14:27 +03:00
|
|
|
sync.RWMutex
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
func (h *httpHandler) Name() string {
|
2021-02-05 18:31:51 +03:00
|
|
|
return h.name
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) Handler() interface{} {
|
|
|
|
return h.hd
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:32:32 +03:00
|
|
|
func (h *httpHandler) Endpoints() []*register.Endpoint {
|
2017-04-03 17:03:46 +03:00
|
|
|
return h.eps
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) Options() server.HandlerOptions {
|
|
|
|
return h.opts
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2021-03-09 14:19:59 +03:00
|
|
|
func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-01-22 00:46:07 +03:00
|
|
|
// check for http.HandlerFunc handlers
|
2021-09-30 03:00:24 +03:00
|
|
|
if ph, _, ok := h.pathHandlers.Search(r.Method, r.URL.Path); ok {
|
|
|
|
ph.(http.HandlerFunc)(w, r)
|
|
|
|
return
|
2021-04-13 11:32:56 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 23:42:33 +03:00
|
|
|
ct := DefaultContentType
|
2021-10-26 22:36:04 +03:00
|
|
|
if htype := r.Header.Get(metadata.HeaderContentType); htype != "" {
|
2021-04-13 23:42:33 +03:00
|
|
|
ct = htype
|
|
|
|
}
|
|
|
|
|
2021-04-30 00:40:08 +03:00
|
|
|
ctx := context.WithValue(r.Context(), rspCodeKey{}, &rspCodeVal{})
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
2022-01-22 00:46:07 +03:00
|
|
|
md = metadata.New(len(r.Header) + 8)
|
2021-04-30 00:40:08 +03:00
|
|
|
}
|
|
|
|
for k, v := range r.Header {
|
|
|
|
md.Set(k, strings.Join(v, ", "))
|
|
|
|
}
|
2021-08-11 12:47:44 +03:00
|
|
|
md.Set("RemoteAddr", r.RemoteAddr)
|
|
|
|
md.Set("Method", r.Method)
|
|
|
|
md.Set("URL", r.URL.String())
|
|
|
|
md.Set("Proto", r.Proto)
|
|
|
|
md.Set("ContentLength", fmt.Sprintf("%d", r.ContentLength))
|
|
|
|
md.Set("TransferEncoding", strings.Join(r.TransferEncoding, ","))
|
|
|
|
md.Set("Host", r.Host)
|
|
|
|
md.Set("RequestURI", r.RequestURI)
|
2021-04-30 00:40:08 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2021-02-06 18:53:25 +03:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
path := r.URL.Path
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
2021-10-26 22:36:04 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, fmt.Errorf("path must starts with /"), http.StatusBadRequest)
|
2021-03-09 23:54:46 +03:00
|
|
|
return
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 13:54:32 +03:00
|
|
|
cf, err := h.newCodec(ct)
|
2021-02-05 18:31:51 +03:00
|
|
|
if err != nil {
|
2021-03-09 14:19:59 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
2021-03-09 23:54:46 +03:00
|
|
|
return
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
matches := make(map[string]interface{})
|
2021-03-31 00:49:55 +03:00
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
var match bool
|
2022-01-22 00:46:07 +03:00
|
|
|
var hldr *patHandler
|
2021-03-09 14:19:59 +03:00
|
|
|
var handler *httpHandler
|
2021-07-14 13:43:45 +03:00
|
|
|
|
2022-01-22 00:46:07 +03:00
|
|
|
for _, shdlr := range h.handlers {
|
|
|
|
hdlr := shdlr.(*httpHandler)
|
|
|
|
fh, mp, ok := hdlr.handlers.Search(r.Method, path)
|
|
|
|
if ok {
|
|
|
|
match = true
|
|
|
|
for k, v := range mp {
|
|
|
|
matches[k] = v
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2022-01-22 00:46:07 +03:00
|
|
|
hldr = fh.(*patHandler)
|
|
|
|
handler = hdlr
|
|
|
|
break
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match {
|
2021-03-09 14:19:59 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound)
|
2021-02-05 18:31:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:24:28 +03:00
|
|
|
// get fields from url values
|
|
|
|
if len(r.URL.RawQuery) > 0 {
|
2021-04-26 00:43:06 +03:00
|
|
|
umd, cerr := rflutil.URLMap(r.URL.RawQuery)
|
|
|
|
if cerr != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, cerr, http.StatusBadRequest)
|
2021-03-31 00:49:55 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 01:24:28 +03:00
|
|
|
for k, v := range umd {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
2021-03-31 00:49:55 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
var argv, replyv reflect.Value
|
|
|
|
|
|
|
|
// Decode the argument value.
|
|
|
|
argIsValue := false // if true, need to indirect before calling.
|
|
|
|
if hldr.mtype.ArgType.Kind() == reflect.Ptr {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType.Elem())
|
|
|
|
} else {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType)
|
|
|
|
argIsValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if argIsValue {
|
|
|
|
argv = argv.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply value
|
|
|
|
replyv = reflect.New(hldr.mtype.ReplyType.Elem())
|
|
|
|
|
|
|
|
function := hldr.mtype.method.Func
|
|
|
|
var returnValues []reflect.Value
|
|
|
|
|
2022-01-22 00:46:07 +03:00
|
|
|
buf, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil && err != io.EOF {
|
2021-04-17 13:54:32 +03:00
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
2021-02-06 18:53:25 +03:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:46:07 +03:00
|
|
|
if err = cf.Unmarshal(buf, argv.Interface()); err != nil {
|
2021-03-09 14:19:59 +03:00
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
2021-02-05 18:31:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-22 00:46:07 +03:00
|
|
|
matches = rflutil.FlattenMap(matches)
|
|
|
|
if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil {
|
2021-04-17 13:54:32 +03:00
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hr := &rpcRequest{
|
|
|
|
codec: cf,
|
2021-03-09 14:19:59 +03:00
|
|
|
service: handler.sopts.Name,
|
2021-02-05 18:31:51 +03:00
|
|
|
contentType: ct,
|
|
|
|
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
2022-01-22 00:46:07 +03:00
|
|
|
body: buf,
|
2021-02-05 18:31:51 +03:00
|
|
|
payload: argv.Interface(),
|
2021-04-30 00:40:08 +03:00
|
|
|
header: md,
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// define the handler func
|
2021-03-01 13:02:35 +03:00
|
|
|
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
|
|
|
|
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
|
2021-02-05 18:31:51 +03:00
|
|
|
|
|
|
|
// The return value for the method is an error.
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
err = rerr.(error)
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(0)
|
2021-03-01 13:02:35 +03:00
|
|
|
}
|
2021-05-01 01:24:09 +03:00
|
|
|
if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
|
|
|
|
for k, v := range nmd {
|
|
|
|
md.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata.SetOutgoingContext(ctx, md)
|
2021-03-01 13:02:35 +03:00
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap the handler func
|
2021-03-09 14:19:59 +03:00
|
|
|
for i := len(handler.sopts.HdlrWrappers); i > 0; i-- {
|
|
|
|
fn = handler.sopts.HdlrWrappers[i-1](fn)
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 13:54:32 +03:00
|
|
|
if ct == "application/x-www-form-urlencoded" {
|
|
|
|
cf, err = h.newCodec(DefaultContentType)
|
|
|
|
if err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-04-18 15:50:41 +03:00
|
|
|
ct = DefaultContentType
|
2021-04-17 13:54:32 +03:00
|
|
|
}
|
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
scode := int(200)
|
2021-05-13 15:46:47 +03:00
|
|
|
appErr := fn(ctx, hr, replyv.Interface())
|
|
|
|
|
2021-10-26 22:36:04 +03:00
|
|
|
w.Header().Set(metadata.HeaderContentType, ct)
|
2021-05-13 15:46:47 +03:00
|
|
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
|
|
for k, v := range md {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:36:04 +03:00
|
|
|
if nct := w.Header().Get(metadata.HeaderContentType); nct != ct {
|
2021-05-14 14:17:33 +03:00
|
|
|
if cf, err = h.newCodec(nct); err != nil {
|
2021-05-13 15:46:47 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if appErr != nil {
|
2021-02-25 14:16:55 +03:00
|
|
|
switch verr := appErr.(type) {
|
|
|
|
case *errors.Error:
|
|
|
|
scode = int(verr.Code)
|
2022-01-22 00:46:07 +03:00
|
|
|
buf, err = cf.Marshal(verr)
|
2021-02-25 14:16:55 +03:00
|
|
|
case *Error:
|
2022-01-22 00:46:07 +03:00
|
|
|
buf, err = cf.Marshal(verr.err)
|
2021-02-25 14:16:55 +03:00
|
|
|
default:
|
2022-01-22 00:46:07 +03:00
|
|
|
buf, err = cf.Marshal(appErr)
|
2021-02-25 14:16:55 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
} else {
|
2022-01-22 00:46:07 +03:00
|
|
|
buf, err = cf.Marshal(replyv.Interface())
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2021-05-13 15:46:47 +03:00
|
|
|
|
2021-03-09 14:19:59 +03:00
|
|
|
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
2021-02-05 18:31:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
if nscode := GetRspCode(ctx); nscode != 0 {
|
|
|
|
scode = nscode
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2021-05-01 01:24:09 +03:00
|
|
|
w.WriteHeader(scode)
|
|
|
|
|
2022-01-22 00:46:07 +03:00
|
|
|
if _, cerr := w.Write(buf); cerr != nil {
|
|
|
|
handler.sopts.Logger.Errorf(ctx, "write failed: %v", cerr)
|
2021-04-26 00:43:06 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|