Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
0e7b8e73a8 | |||
35146aa717 | |||
3f44a41d30 | |||
090100e522 |
5
http.go
5
http.go
@@ -147,6 +147,11 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client.
|
|||||||
if opts.AuthToken != "" {
|
if opts.AuthToken != "" {
|
||||||
header.Set(metadata.HeaderAuthorization, opts.AuthToken)
|
header.Set(metadata.HeaderAuthorization, opts.AuthToken)
|
||||||
}
|
}
|
||||||
|
if opts.RequestMetadata != nil {
|
||||||
|
for k, v := range opts.RequestMetadata {
|
||||||
|
header.Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
for k, v := range md {
|
for k, v := range md {
|
||||||
|
58
stream.go
58
stream.go
@@ -2,11 +2,9 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -121,59 +119,55 @@ func (h *httpStream) Close() error {
|
|||||||
|
|
||||||
func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
|
||||||
var err error
|
var err error
|
||||||
|
var buf []byte
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
err = ctx.Err()
|
|
||||||
default:
|
|
||||||
// fast path return
|
// fast path return
|
||||||
if hrsp.StatusCode == http.StatusNoContent {
|
if hrsp.StatusCode == http.StatusNoContent {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if hrsp.StatusCode < 400 {
|
select {
|
||||||
if log.V(logger.DebugLevel) {
|
case <-ctx.Done():
|
||||||
buf, rerr := io.ReadAll(hrsp.Body)
|
err = ctx.Err()
|
||||||
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
default:
|
||||||
|
if hrsp.Body != nil {
|
||||||
|
buf, err = io.ReadAll(hrsp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", rerr.Error())
|
if log.V(logger.ErrorLevel) {
|
||||||
|
log.Errorf(ctx, "failed to read body: %v", err)
|
||||||
}
|
}
|
||||||
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
|
return errors.InternalServerError("go.micro.client", string(buf))
|
||||||
}
|
}
|
||||||
if err = cf.ReadBody(hrsp.Body, rsp); err != nil {
|
}
|
||||||
|
|
||||||
|
if log.V(logger.DebugLevel) {
|
||||||
|
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hrsp.StatusCode < 400 {
|
||||||
|
if err = cf.Unmarshal(buf, rsp); err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rerr interface{}
|
||||||
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
errmap, ok := opts.Context.Value(errorMapKey{}).(map[string]interface{})
|
||||||
if ok && errmap != nil {
|
if ok && errmap != nil {
|
||||||
if err, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
if rerr, ok = errmap[fmt.Sprintf("%d", hrsp.StatusCode)].(error); !ok {
|
||||||
err, ok = errmap["default"].(error)
|
rerr, ok = errmap["default"].(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !ok || err == nil {
|
if !ok || rerr == nil {
|
||||||
buf, cerr := io.ReadAll(hrsp.Body)
|
|
||||||
if log.V(logger.DebugLevel) {
|
|
||||||
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
|
||||||
}
|
|
||||||
if cerr != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", cerr.Error())
|
|
||||||
}
|
|
||||||
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
if log.V(logger.DebugLevel) {
|
if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
|
||||||
buf, rerr := io.ReadAll(hrsp.Body)
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
||||||
log.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", rerr.Error())
|
|
||||||
}
|
|
||||||
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cerr := cf.ReadBody(hrsp.Body, err); cerr != nil {
|
if err, ok = rerr.(error); !ok {
|
||||||
err = errors.InternalServerError("go.micro.client", cerr.Error())
|
err = &Error{rerr}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
61
util.go
61
util.go
@@ -1,11 +1,9 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -15,6 +13,7 @@ import (
|
|||||||
"go.unistack.org/micro/v3/client"
|
"go.unistack.org/micro/v3/client"
|
||||||
"go.unistack.org/micro/v3/errors"
|
"go.unistack.org/micro/v3/errors"
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v3/logger"
|
||||||
|
"go.unistack.org/micro/v3/metadata"
|
||||||
rutil "go.unistack.org/micro/v3/util/reflect"
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -247,45 +246,55 @@ func newTemplate(path string) ([]string, error) {
|
|||||||
|
|
||||||
func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error {
|
func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp interface{}, opts client.CallOptions) error {
|
||||||
var err error
|
var err error
|
||||||
|
var buf []byte
|
||||||
|
|
||||||
|
// fast path return
|
||||||
|
if hrsp.StatusCode == http.StatusNoContent {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.ResponseMetadata != nil {
|
||||||
|
*opts.ResponseMetadata = metadata.New(len(hrsp.Header))
|
||||||
|
for k, v := range hrsp.Header {
|
||||||
|
opts.ResponseMetadata.Set(k, strings.Join(v, ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
err = ctx.Err()
|
err = ctx.Err()
|
||||||
default:
|
default:
|
||||||
// fast path return
|
|
||||||
if hrsp.StatusCode == http.StatusNoContent {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ct := DefaultContentType
|
ct := DefaultContentType
|
||||||
|
|
||||||
if htype := hrsp.Header.Get("Content-Type"); htype != "" {
|
if htype := hrsp.Header.Get("Content-Type"); htype != "" {
|
||||||
ct = htype
|
ct = htype
|
||||||
}
|
}
|
||||||
|
|
||||||
cf, cerr := h.newCodec(ct)
|
|
||||||
if hrsp.StatusCode >= 400 && cerr != nil {
|
|
||||||
var buf []byte
|
|
||||||
if hrsp.Body != nil {
|
if hrsp.Body != nil {
|
||||||
buf, err = io.ReadAll(hrsp.Body)
|
buf, err = io.ReadAll(hrsp.Body)
|
||||||
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
if err != nil {
|
||||||
|
if h.opts.Logger.V(logger.ErrorLevel) {
|
||||||
h.opts.Logger.Errorf(ctx, "failed to read body: %v", err)
|
h.opts.Logger.Errorf(ctx, "failed to read body: %v", err)
|
||||||
}
|
}
|
||||||
|
return errors.InternalServerError("go.micro.client", string(buf))
|
||||||
}
|
}
|
||||||
if h.opts.Logger.V(logger.DebugLevel) {
|
|
||||||
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
|
||||||
}
|
}
|
||||||
// response like text/plain or something else, return original error
|
|
||||||
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
cf, cerr := h.newCodec(ct)
|
||||||
} else if cerr != nil {
|
if cerr != nil {
|
||||||
if h.opts.Logger.V(logger.DebugLevel) {
|
if h.opts.Logger.V(logger.DebugLevel) {
|
||||||
h.opts.Logger.Debugf(ctx, "response with %v unknown content-type", hrsp.Header, ct)
|
h.opts.Logger.Debugf(ctx, "response with %v unknown content-type %s %s", hrsp.Header, ct, buf)
|
||||||
}
|
}
|
||||||
return errors.InternalServerError("go.micro.client", cerr.Error())
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if h.opts.Logger.V(logger.DebugLevel) {
|
||||||
|
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
||||||
|
}
|
||||||
|
|
||||||
// succeseful response
|
// succeseful response
|
||||||
if hrsp.StatusCode < 400 {
|
if hrsp.StatusCode < 400 {
|
||||||
if err = cf.ReadBody(hrsp.Body, rsp); err != nil {
|
if err = cf.Unmarshal(buf, rsp); err != nil {
|
||||||
return errors.InternalServerError("go.micro.client", err.Error())
|
return errors.InternalServerError("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -302,26 +311,10 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !ok || rerr == nil {
|
if !ok || rerr == nil {
|
||||||
buf, rerr := io.ReadAll(hrsp.Body)
|
|
||||||
if h.opts.Logger.V(logger.DebugLevel) {
|
|
||||||
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
|
||||||
}
|
|
||||||
if rerr != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", rerr.Error())
|
|
||||||
}
|
|
||||||
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
return errors.New("go.micro.client", string(buf), int32(hrsp.StatusCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.opts.Logger.V(logger.DebugLevel) {
|
if cerr := cf.Unmarshal(buf, rerr); cerr != nil {
|
||||||
buf, rerr := io.ReadAll(hrsp.Body)
|
|
||||||
h.opts.Logger.Debugf(ctx, "response %s with %v", buf, hrsp.Header)
|
|
||||||
if err != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", rerr.Error())
|
|
||||||
}
|
|
||||||
hrsp.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
|
|
||||||
}
|
|
||||||
|
|
||||||
if cerr := cf.ReadBody(hrsp.Body, rerr); cerr != nil {
|
|
||||||
return errors.InternalServerError("go.micro.client", cerr.Error())
|
return errors.InternalServerError("go.micro.client", cerr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user