2017-01-01 21:39:05 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-03-03 15:28:44 +03:00
|
|
|
"context"
|
2021-01-10 14:48:10 +03:00
|
|
|
"fmt"
|
2021-04-18 15:45:42 +03:00
|
|
|
"io"
|
2017-01-01 21:39:05 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2023-04-28 22:29:18 +03:00
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/errors"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2017-01-01 21:39:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements the streamer interface
|
|
|
|
type httpStream struct {
|
2021-04-25 16:26:36 +03:00
|
|
|
err error
|
|
|
|
conn net.Conn
|
2021-03-23 00:29:27 +03:00
|
|
|
cf codec.Codec
|
2017-01-01 21:39:05 +03:00
|
|
|
context context.Context
|
2023-03-15 22:26:14 +03:00
|
|
|
logger logger.Logger
|
2021-04-25 16:26:36 +03:00
|
|
|
request client.Request
|
2017-01-01 21:39:05 +03:00
|
|
|
closed chan bool
|
|
|
|
reader *bufio.Reader
|
2021-04-25 16:26:36 +03:00
|
|
|
address string
|
|
|
|
ct string
|
|
|
|
opts client.CallOptions
|
|
|
|
sync.RWMutex
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
var errShutdown = fmt.Errorf("connection is shut down")
|
2017-01-01 21:39:05 +03:00
|
|
|
|
|
|
|
func (h *httpStream) isClosed() bool {
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Context() context.Context {
|
|
|
|
return h.context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Request() client.Request {
|
|
|
|
return h.request
|
|
|
|
}
|
|
|
|
|
2019-01-15 00:32:12 +03:00
|
|
|
func (h *httpStream) Response() client.Response {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:47:58 +03:00
|
|
|
func (h *httpStream) SendMsg(msg interface{}) error {
|
|
|
|
return h.Send(msg)
|
|
|
|
}
|
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
func (h *httpStream) Send(msg interface{}) error {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.isClosed() {
|
|
|
|
h.err = errShutdown
|
|
|
|
return errShutdown
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:26:14 +03:00
|
|
|
hreq, err := newRequest(h.context, h.logger, h.address, h.request, h.ct, h.cf, msg, h.opts)
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:12:31 +03:00
|
|
|
return hreq.Write(h.conn)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 17:47:58 +03:00
|
|
|
func (h *httpStream) RecvMsg(msg interface{}) error {
|
|
|
|
return h.Recv(msg)
|
|
|
|
}
|
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
func (h *httpStream) Recv(msg interface{}) error {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.isClosed() {
|
|
|
|
h.err = errShutdown
|
|
|
|
return errShutdown
|
|
|
|
}
|
|
|
|
|
2021-01-10 14:48:10 +03:00
|
|
|
hrsp, err := http.ReadResponse(h.reader, new(http.Request))
|
2017-01-01 21:39:05 +03:00
|
|
|
if err != nil {
|
2021-01-10 14:48:10 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
2021-01-10 14:48:10 +03:00
|
|
|
defer hrsp.Body.Close()
|
2017-01-01 21:39:05 +03:00
|
|
|
|
2023-03-15 22:26:14 +03:00
|
|
|
return h.parseRsp(h.context, h.logger, hrsp, h.cf, msg, h.opts)
|
2017-01-01 21:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpStream) Error() error {
|
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
|
|
|
return h.err
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:47:58 +03:00
|
|
|
func (h *httpStream) CloseSend() error {
|
|
|
|
return h.Close()
|
|
|
|
}
|
|
|
|
|
2017-01-01 21:39:05 +03:00
|
|
|
func (h *httpStream) Close() error {
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(h.closed)
|
|
|
|
return h.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
|
2023-03-15 22:26:14 +03:00
|
|
|
func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
2021-04-18 15:45:42 +03:00
|
|
|
var err error
|
2023-03-16 19:16:35 +03:00
|
|
|
var buf []byte
|
|
|
|
|
|
|
|
// fast path return
|
|
|
|
if hrsp.StatusCode == http.StatusNoContent {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err = ctx.Err()
|
|
|
|
default:
|
2023-03-16 19:16:35 +03:00
|
|
|
if hrsp.Body != nil {
|
|
|
|
buf, err = io.ReadAll(hrsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if log.V(logger.ErrorLevel) {
|
|
|
|
log.Errorf(ctx, "failed to read body: %v", err)
|
|
|
|
}
|
|
|
|
return errors.InternalServerError("go.micro.client", string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if log.V(logger.DebugLevel) {
|
|
|
|
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
|
2021-04-25 16:26:36 +03:00
|
|
|
if hrsp.StatusCode < 400 {
|
2023-03-16 19:16:35 +03:00
|
|
|
if err = cf.Unmarshal(buf, rsp); err != nil {
|
2021-04-25 16:26:36 +03:00
|
|
|
return errors.InternalServerError("go.micro.client", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
var rerr interface{}
|
2021-04-25 16:26:36 +03:00
|
|
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
|
|
|
if ok && errmap != nil {
|
2023-03-16 19:16:35 +03:00
|
|
|
if rerr, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
|
|
|
rerr, ok = errmap["default"].(error)
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
2023-03-16 19:16:35 +03:00
|
|
|
if !ok || rerr == nil {
|
2021-04-25 16:26:36 +03:00
|
|
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
|
|
|
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
2023-03-15 22:26:14 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:16:35 +03:00
|
|
|
if err, ok = rerr.(error); !ok {
|
|
|
|
err = &Error{rerr}
|
2021-04-25 16:26:36 +03:00
|
|
|
}
|
2021-04-18 15:45:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|