vendor: upgrade grpc-gateway to v1.3
Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>release-3.3
parent
c0c19465fc
commit
7ba4ae01b8
|
@ -9,10 +9,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/grpclog"
|
"google.golang.org/grpc/grpclog"
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MetadataHeaderPrefix is the http prefix that represents custom metadata
|
// MetadataHeaderPrefix is the http prefix that represents custom metadata
|
||||||
|
@ -44,14 +44,14 @@ At a minimum, the RemoteAddr is included in the fashion of "X-Forwarded-For",
|
||||||
except that the forwarded destination is not another HTTP service but rather
|
except that the forwarded destination is not another HTTP service but rather
|
||||||
a gRPC service.
|
a gRPC service.
|
||||||
*/
|
*/
|
||||||
func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, error) {
|
func AnnotateContext(ctx context.Context, mux *ServeMux, req *http.Request) (context.Context, error) {
|
||||||
var pairs []string
|
var pairs []string
|
||||||
timeout := DefaultContextTimeout
|
timeout := DefaultContextTimeout
|
||||||
if tm := req.Header.Get(metadataGrpcTimeout); tm != "" {
|
if tm := req.Header.Get(metadataGrpcTimeout); tm != "" {
|
||||||
var err error
|
var err error
|
||||||
timeout, err = timeoutDecode(tm)
|
timeout, err = timeoutDecode(tm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, grpc.Errorf(codes.InvalidArgument, "invalid grpc-timeout: %s", tm)
|
return nil, status.Errorf(codes.InvalidArgument, "invalid grpc-timeout: %s", tm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,12 +61,8 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e
|
||||||
if strings.ToLower(key) == "authorization" {
|
if strings.ToLower(key) == "authorization" {
|
||||||
pairs = append(pairs, "authorization", val)
|
pairs = append(pairs, "authorization", val)
|
||||||
}
|
}
|
||||||
if isPermanentHTTPHeader(key) {
|
if h, ok := mux.incomingHeaderMatcher(key); ok {
|
||||||
pairs = append(pairs, strings.ToLower(fmt.Sprintf("%s%s", MetadataPrefix, key)), val)
|
pairs = append(pairs, h, val)
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(key, MetadataHeaderPrefix) {
|
|
||||||
pairs = append(pairs, key[len(MetadataHeaderPrefix):], val)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +90,11 @@ func AnnotateContext(ctx context.Context, req *http.Request) (context.Context, e
|
||||||
if len(pairs) == 0 {
|
if len(pairs) == 0 {
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
return metadata.NewOutgoingContext(ctx, metadata.Pairs(pairs...)), nil
|
md := metadata.Pairs(pairs...)
|
||||||
|
if mux.metadataAnnotator != nil {
|
||||||
|
md = metadata.Join(md, mux.metadataAnnotator(ctx, req))
|
||||||
|
}
|
||||||
|
return metadata.NewOutgoingContext(ctx, md), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerMetadata consists of metadata sent from gRPC server.
|
// ServerMetadata consists of metadata sent from gRPC server.
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/grpclog"
|
"google.golang.org/grpc/grpclog"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
|
// HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
|
||||||
|
@ -78,14 +78,20 @@ func (*errorBody) ProtoMessage() {}
|
||||||
//
|
//
|
||||||
// The response body returned by this function is a JSON object,
|
// The response body returned by this function is a JSON object,
|
||||||
// which contains a member whose key is "error" and whose value is err.Error().
|
// which contains a member whose key is "error" and whose value is err.Error().
|
||||||
func DefaultHTTPError(ctx context.Context, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
|
func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
|
||||||
const fallback = `{"error": "failed to marshal error message"}`
|
const fallback = `{"error": "failed to marshal error message"}`
|
||||||
|
|
||||||
w.Header().Del("Trailer")
|
w.Header().Del("Trailer")
|
||||||
w.Header().Set("Content-Type", marshaler.ContentType())
|
w.Header().Set("Content-Type", marshaler.ContentType())
|
||||||
|
|
||||||
|
s, ok := status.FromError(err)
|
||||||
|
if !ok {
|
||||||
|
s = status.New(codes.Unknown, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
body := &errorBody{
|
body := &errorBody{
|
||||||
Error: grpc.ErrorDesc(err),
|
Error: s.Message(),
|
||||||
Code: int32(grpc.Code(err)),
|
Code: int32(s.Code()),
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, merr := marshaler.Marshal(body)
|
buf, merr := marshaler.Marshal(body)
|
||||||
|
@ -103,9 +109,9 @@ func DefaultHTTPError(ctx context.Context, marshaler Marshaler, w http.ResponseW
|
||||||
grpclog.Printf("Failed to extract ServerMetadata from context")
|
grpclog.Printf("Failed to extract ServerMetadata from context")
|
||||||
}
|
}
|
||||||
|
|
||||||
handleForwardResponseServerMetadata(w, md)
|
handleForwardResponseServerMetadata(w, mux, md)
|
||||||
handleForwardResponseTrailerHeader(w, md)
|
handleForwardResponseTrailerHeader(w, md)
|
||||||
st := HTTPStatusFromCode(grpc.Code(err))
|
st := HTTPStatusFromCode(s.Code())
|
||||||
w.WriteHeader(st)
|
w.WriteHeader(st)
|
||||||
if _, err := w.Write(buf); err != nil {
|
if _, err := w.Write(buf); err != nil {
|
||||||
grpclog.Printf("Failed to write response: %v", err)
|
grpclog.Printf("Failed to write response: %v", err)
|
||||||
|
|
|
@ -9,12 +9,13 @@ import (
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
|
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/grpclog"
|
"google.golang.org/grpc/grpclog"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ForwardResponseStream forwards the stream from gRPC server to REST client.
|
// ForwardResponseStream forwards the stream from gRPC server to REST client.
|
||||||
func ForwardResponseStream(ctx context.Context, marshaler Marshaler, w http.ResponseWriter, req *http.Request, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
|
func ForwardResponseStream(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, req *http.Request, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
|
||||||
f, ok := w.(http.Flusher)
|
f, ok := w.(http.Flusher)
|
||||||
if !ok {
|
if !ok {
|
||||||
grpclog.Printf("Flush not supported in %T", w)
|
grpclog.Printf("Flush not supported in %T", w)
|
||||||
|
@ -28,7 +29,7 @@ func ForwardResponseStream(ctx context.Context, marshaler Marshaler, w http.Resp
|
||||||
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handleForwardResponseServerMetadata(w, md)
|
handleForwardResponseServerMetadata(w, mux, md)
|
||||||
|
|
||||||
w.Header().Set("Transfer-Encoding", "chunked")
|
w.Header().Set("Transfer-Encoding", "chunked")
|
||||||
w.Header().Set("Content-Type", marshaler.ContentType())
|
w.Header().Set("Content-Type", marshaler.ContentType())
|
||||||
|
@ -57,7 +58,7 @@ func ForwardResponseStream(ctx context.Context, marshaler Marshaler, w http.Resp
|
||||||
grpclog.Printf("Failed to marshal response chunk: %v", err)
|
grpclog.Printf("Failed to marshal response chunk: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err = fmt.Fprintf(w, "%s\n", buf); err != nil {
|
if _, err = w.Write(buf); err != nil {
|
||||||
grpclog.Printf("Failed to send response chunk: %v", err)
|
grpclog.Printf("Failed to send response chunk: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -65,11 +66,12 @@ func ForwardResponseStream(ctx context.Context, marshaler Marshaler, w http.Resp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleForwardResponseServerMetadata(w http.ResponseWriter, md ServerMetadata) {
|
func handleForwardResponseServerMetadata(w http.ResponseWriter, mux *ServeMux, md ServerMetadata) {
|
||||||
for k, vs := range md.HeaderMD {
|
for k, vs := range md.HeaderMD {
|
||||||
hKey := fmt.Sprintf("%s%s", MetadataHeaderPrefix, k)
|
if h, ok := mux.outgoingHeaderMatcher(k); ok {
|
||||||
for i := range vs {
|
for _, v := range vs {
|
||||||
w.Header().Add(hKey, vs[i])
|
w.Header().Add(h, v)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,31 +86,31 @@ func handleForwardResponseTrailerHeader(w http.ResponseWriter, md ServerMetadata
|
||||||
func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) {
|
func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) {
|
||||||
for k, vs := range md.TrailerMD {
|
for k, vs := range md.TrailerMD {
|
||||||
tKey := fmt.Sprintf("%s%s", MetadataTrailerPrefix, k)
|
tKey := fmt.Sprintf("%s%s", MetadataTrailerPrefix, k)
|
||||||
for i := range vs {
|
for _, v := range vs {
|
||||||
w.Header().Add(tKey, vs[i])
|
w.Header().Add(tKey, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForwardResponseMessage forwards the message "resp" from gRPC server to REST client.
|
// ForwardResponseMessage forwards the message "resp" from gRPC server to REST client.
|
||||||
func ForwardResponseMessage(ctx context.Context, marshaler Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
|
func ForwardResponseMessage(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
|
||||||
md, ok := ServerMetadataFromContext(ctx)
|
md, ok := ServerMetadataFromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
grpclog.Printf("Failed to extract ServerMetadata from context")
|
grpclog.Printf("Failed to extract ServerMetadata from context")
|
||||||
}
|
}
|
||||||
|
|
||||||
handleForwardResponseServerMetadata(w, md)
|
handleForwardResponseServerMetadata(w, mux, md)
|
||||||
handleForwardResponseTrailerHeader(w, md)
|
handleForwardResponseTrailerHeader(w, md)
|
||||||
w.Header().Set("Content-Type", marshaler.ContentType())
|
w.Header().Set("Content-Type", marshaler.ContentType())
|
||||||
if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil {
|
if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil {
|
||||||
HTTPError(ctx, marshaler, w, req, err)
|
HTTPError(ctx, mux, marshaler, w, req, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := marshaler.Marshal(resp)
|
buf, err := marshaler.Marshal(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
grpclog.Printf("Marshal error: %v", err)
|
grpclog.Printf("Marshal error: %v", err)
|
||||||
HTTPError(ctx, marshaler, w, req, err)
|
HTTPError(ctx, mux, marshaler, w, req, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +148,10 @@ func handleForwardResponseStreamError(marshaler Marshaler, w http.ResponseWriter
|
||||||
|
|
||||||
func streamChunk(result proto.Message, err error) map[string]proto.Message {
|
func streamChunk(result proto.Message, err error) map[string]proto.Message {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
grpcCode := grpc.Code(err)
|
grpcCode := codes.Unknown
|
||||||
|
if s, ok := status.FromError(err); ok {
|
||||||
|
grpcCode = s.Code()
|
||||||
|
}
|
||||||
httpCode := HTTPStatusFromCode(grpcCode)
|
httpCode := HTTPStatusFromCode(grpcCode)
|
||||||
return map[string]proto.Message{
|
return map[string]proto.Message{
|
||||||
"error": &internal.StreamError{
|
"error": &internal.StreamError{
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
// Code generated by protoc-gen-go.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// source: runtime/internal/stream_chunk.proto
|
// source: runtime/internal/stream_chunk.proto
|
||||||
// DO NOT EDIT!
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Package internal is a generated protocol buffer package.
|
Package internal is a generated protocol buffer package.
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A HandlerFunc handles a specific pair of path pattern and HTTP method.
|
// A HandlerFunc handles a specific pair of path pattern and HTTP method.
|
||||||
|
@ -19,6 +23,10 @@ type ServeMux struct {
|
||||||
handlers map[string][]handler
|
handlers map[string][]handler
|
||||||
forwardResponseOptions []func(context.Context, http.ResponseWriter, proto.Message) error
|
forwardResponseOptions []func(context.Context, http.ResponseWriter, proto.Message) error
|
||||||
marshalers marshalerRegistry
|
marshalers marshalerRegistry
|
||||||
|
incomingHeaderMatcher HeaderMatcherFunc
|
||||||
|
outgoingHeaderMatcher HeaderMatcherFunc
|
||||||
|
metadataAnnotator func(context.Context, *http.Request) metadata.MD
|
||||||
|
protoErrorHandler ProtoErrorHandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeMuxOption is an option that can be given to a ServeMux on construction.
|
// ServeMuxOption is an option that can be given to a ServeMux on construction.
|
||||||
|
@ -36,6 +44,64 @@ func WithForwardResponseOption(forwardResponseOption func(context.Context, http.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HeaderMatcherFunc checks whether a header key should be forwarded to/from gRPC context.
|
||||||
|
type HeaderMatcherFunc func(string) (string, bool)
|
||||||
|
|
||||||
|
// DefaultHeaderMatcher is used to pass http request headers to/from gRPC context. This adds permanent HTTP header
|
||||||
|
// keys (as specified by the IANA) to gRPC context with grpcgateway- prefix. HTTP headers that start with
|
||||||
|
// 'Grpc-Metadata-' are mapped to gRPC metadata after removing prefix 'Grpc-Metadata-'.
|
||||||
|
func DefaultHeaderMatcher(key string) (string, bool) {
|
||||||
|
key = textproto.CanonicalMIMEHeaderKey(key)
|
||||||
|
if isPermanentHTTPHeader(key) {
|
||||||
|
return MetadataPrefix + key, true
|
||||||
|
} else if strings.HasPrefix(key, MetadataHeaderPrefix) {
|
||||||
|
return key[len(MetadataHeaderPrefix):], true
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithIncomingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for incoming request to gateway.
|
||||||
|
//
|
||||||
|
// This matcher will be called with each header in http.Request. If matcher returns true, that header will be
|
||||||
|
// passed to gRPC context. To transform the header before passing to gRPC context, matcher should return modified header.
|
||||||
|
func WithIncomingHeaderMatcher(fn HeaderMatcherFunc) ServeMuxOption {
|
||||||
|
return func(mux *ServeMux) {
|
||||||
|
mux.incomingHeaderMatcher = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithOutgoingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for outgoing response from gateway.
|
||||||
|
//
|
||||||
|
// This matcher will be called with each header in response header metadata. If matcher returns true, that header will be
|
||||||
|
// passed to http response returned from gateway. To transform the header before passing to response,
|
||||||
|
// matcher should return modified header.
|
||||||
|
func WithOutgoingHeaderMatcher(fn HeaderMatcherFunc) ServeMuxOption {
|
||||||
|
return func(mux *ServeMux) {
|
||||||
|
mux.outgoingHeaderMatcher = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithMetadata returns a ServeMuxOption for passing metadata to a gRPC context.
|
||||||
|
//
|
||||||
|
// This can be used by services that need to read from http.Request and modify gRPC context. A common use case
|
||||||
|
// is reading token from cookie and adding it in gRPC context.
|
||||||
|
func WithMetadata(annotator func(context.Context, *http.Request) metadata.MD) ServeMuxOption {
|
||||||
|
return func(serveMux *ServeMux) {
|
||||||
|
serveMux.metadataAnnotator = annotator
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProtoErrorHandler returns a ServeMuxOption for passing metadata to a gRPC context.
|
||||||
|
//
|
||||||
|
// This can be used to handle an error as general proto message defined by gRPC.
|
||||||
|
// The response including body and status is not backward compatible with the default error handler.
|
||||||
|
// When this option is used, HTTPError and OtherErrorHandler are overwritten on initialization.
|
||||||
|
func WithProtoErrorHandler(fn ProtoErrorHandlerFunc) ServeMuxOption {
|
||||||
|
return func(serveMux *ServeMux) {
|
||||||
|
serveMux.protoErrorHandler = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewServeMux returns a new ServeMux whose internal mapping is empty.
|
// NewServeMux returns a new ServeMux whose internal mapping is empty.
|
||||||
func NewServeMux(opts ...ServeMuxOption) *ServeMux {
|
func NewServeMux(opts ...ServeMuxOption) *ServeMux {
|
||||||
serveMux := &ServeMux{
|
serveMux := &ServeMux{
|
||||||
|
@ -47,6 +113,29 @@ func NewServeMux(opts ...ServeMuxOption) *ServeMux {
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(serveMux)
|
opt(serveMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if serveMux.protoErrorHandler != nil {
|
||||||
|
HTTPError = serveMux.protoErrorHandler
|
||||||
|
// OtherErrorHandler is no longer used when protoErrorHandler is set.
|
||||||
|
// Overwritten by a special error handler to return Unknown.
|
||||||
|
OtherErrorHandler = func(w http.ResponseWriter, r *http.Request, _ string, _ int) {
|
||||||
|
ctx := context.Background()
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(serveMux, r)
|
||||||
|
sterr := status.Error(codes.Unknown, "unexpected use of OtherErrorHandler")
|
||||||
|
serveMux.protoErrorHandler(ctx, serveMux, outboundMarshaler, w, r, sterr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if serveMux.incomingHeaderMatcher == nil {
|
||||||
|
serveMux.incomingHeaderMatcher = DefaultHeaderMatcher
|
||||||
|
}
|
||||||
|
|
||||||
|
if serveMux.outgoingHeaderMatcher == nil {
|
||||||
|
serveMux.outgoingHeaderMatcher = func(key string) (string, bool) {
|
||||||
|
return fmt.Sprintf("%s%s", MetadataHeaderPrefix, key), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return serveMux
|
return serveMux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,9 +146,17 @@ func (s *ServeMux) Handle(meth string, pat Pattern, h HandlerFunc) {
|
||||||
|
|
||||||
// ServeHTTP dispatches the request to the first handler whose pattern matches to r.Method and r.Path.
|
// ServeHTTP dispatches the request to the first handler whose pattern matches to r.Method and r.Path.
|
||||||
func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
|
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
if !strings.HasPrefix(path, "/") {
|
if !strings.HasPrefix(path, "/") {
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.InvalidArgument, http.StatusText(http.StatusBadRequest))
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
OtherErrorHandler(w, r, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +164,13 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
l := len(components)
|
l := len(components)
|
||||||
var verb string
|
var verb string
|
||||||
if idx := strings.LastIndex(components[l-1], ":"); idx == 0 {
|
if idx := strings.LastIndex(components[l-1], ":"); idx == 0 {
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.Unimplemented, http.StatusText(http.StatusNotImplemented))
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
} else if idx > 0 {
|
} else if idx > 0 {
|
||||||
c := components[l-1]
|
c := components[l-1]
|
||||||
|
@ -77,7 +180,13 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if override := r.Header.Get("X-HTTP-Method-Override"); override != "" && isPathLengthFallback(r) {
|
if override := r.Header.Get("X-HTTP-Method-Override"); override != "" && isPathLengthFallback(r) {
|
||||||
r.Method = strings.ToUpper(override)
|
r.Method = strings.ToUpper(override)
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest)
|
OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,18 +213,37 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// X-HTTP-Method-Override is optional. Always allow fallback to POST.
|
// X-HTTP-Method-Override is optional. Always allow fallback to POST.
|
||||||
if isPathLengthFallback(r) {
|
if isPathLengthFallback(r) {
|
||||||
if err := r.ParseForm(); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.InvalidArgument, err.Error())
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest)
|
OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.h(w, r, pathParams)
|
h.h(w, r, pathParams)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.Unimplemented, http.StatusText(http.StatusMethodNotAllowed))
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
OtherErrorHandler(w, r, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.protoErrorHandler != nil {
|
||||||
|
_, outboundMarshaler := MarshalerForRequest(s, r)
|
||||||
|
sterr := status.Error(codes.Unimplemented, http.StatusText(http.StatusNotImplemented))
|
||||||
|
s.protoErrorHandler(ctx, s, outboundMarshaler, w, r, sterr)
|
||||||
|
} else {
|
||||||
OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetForwardResponseOptions returns the ForwardResponseOptions associated with this ServeMux.
|
// GetForwardResponseOptions returns the ForwardResponseOptions associated with this ServeMux.
|
||||||
func (s *ServeMux) GetForwardResponseOptions() []func(context.Context, http.ResponseWriter, proto.Message) error {
|
func (s *ServeMux) GetForwardResponseOptions() []func(context.Context, http.ResponseWriter, proto.Message) error {
|
||||||
|
|
61
cmd/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go
generated
vendored
Normal file
61
cmd/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package runtime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/grpclog"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProtoErrorHandlerFunc handles the error as a gRPC error generated via status package and replies to the request.
|
||||||
|
type ProtoErrorHandlerFunc func(context.Context, *ServeMux, Marshaler, http.ResponseWriter, *http.Request, error)
|
||||||
|
|
||||||
|
var _ ProtoErrorHandlerFunc = DefaultHTTPProtoErrorHandler
|
||||||
|
|
||||||
|
// DefaultHTTPProtoErrorHandler is an implementation of HTTPError.
|
||||||
|
// If "err" is an error from gRPC system, the function replies with the status code mapped by HTTPStatusFromCode.
|
||||||
|
// If otherwise, it replies with http.StatusInternalServerError.
|
||||||
|
//
|
||||||
|
// The response body returned by this function is a Status message marshaled by a Marshaler.
|
||||||
|
//
|
||||||
|
// Do not set this function to HTTPError variable directly, use WithProtoErrorHandler option instead.
|
||||||
|
func DefaultHTTPProtoErrorHandler(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
|
||||||
|
// return Internal when Marshal failed
|
||||||
|
const fallback = `{"code": 13, "message": "failed to marshal error message"}`
|
||||||
|
|
||||||
|
w.Header().Del("Trailer")
|
||||||
|
w.Header().Set("Content-Type", marshaler.ContentType())
|
||||||
|
|
||||||
|
s, ok := status.FromError(err)
|
||||||
|
if !ok {
|
||||||
|
s = status.New(codes.Unknown, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, merr := marshaler.Marshal(s.Proto())
|
||||||
|
if merr != nil {
|
||||||
|
grpclog.Printf("Failed to marshal error message %q: %v", s.Proto(), merr)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
if _, err := io.WriteString(w, fallback); err != nil {
|
||||||
|
grpclog.Printf("Failed to write response: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
md, ok := ServerMetadataFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
grpclog.Printf("Failed to extract ServerMetadata from context")
|
||||||
|
}
|
||||||
|
|
||||||
|
handleForwardResponseServerMetadata(w, mux, md)
|
||||||
|
handleForwardResponseTrailerHeader(w, md)
|
||||||
|
st := HTTPStatusFromCode(s.Code())
|
||||||
|
w.WriteHeader(st)
|
||||||
|
if _, err := w.Write(buf); err != nil {
|
||||||
|
grpclog.Printf("Failed to write response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
handleForwardResponseTrailer(w, md)
|
||||||
|
}
|
|
@ -113,6 +113,9 @@ func fieldByProtoName(m reflect.Value, name string) (reflect.Value, *proto.Prope
|
||||||
if p.OrigName == name {
|
if p.OrigName == name {
|
||||||
return m.FieldByName(p.Name), p, nil
|
return m.FieldByName(p.Name), p, nil
|
||||||
}
|
}
|
||||||
|
if p.JSONName == name {
|
||||||
|
return m.FieldByName(p.Name), p, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return reflect.Value{}, nil, nil
|
return reflect.Value{}, nil, nil
|
||||||
}
|
}
|
||||||
|
@ -161,6 +164,45 @@ func populateField(f reflect.Value, value string, props *proto.Properties) error
|
||||||
f.Field(0).SetInt(int64(t.Unix()))
|
f.Field(0).SetInt(int64(t.Unix()))
|
||||||
f.Field(1).SetInt(int64(t.Nanosecond()))
|
f.Field(1).SetInt(int64(t.Nanosecond()))
|
||||||
return nil
|
return nil
|
||||||
|
case "DoubleValue":
|
||||||
|
fallthrough
|
||||||
|
case "FloatValue":
|
||||||
|
float64Val, err := strconv.ParseFloat(value, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("bad DoubleValue: %s", value)
|
||||||
|
}
|
||||||
|
f.Field(0).SetFloat(float64Val)
|
||||||
|
return nil
|
||||||
|
case "Int64Value":
|
||||||
|
fallthrough
|
||||||
|
case "Int32Value":
|
||||||
|
int64Val, err := strconv.ParseInt(value, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("bad DoubleValue: %s", value)
|
||||||
|
}
|
||||||
|
f.Field(0).SetInt(int64Val)
|
||||||
|
return nil
|
||||||
|
case "UInt64Value":
|
||||||
|
fallthrough
|
||||||
|
case "UInt32Value":
|
||||||
|
uint64Val, err := strconv.ParseUint(value, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("bad DoubleValue: %s", value)
|
||||||
|
}
|
||||||
|
f.Field(0).SetUint(uint64Val)
|
||||||
|
return nil
|
||||||
|
case "BoolValue":
|
||||||
|
if value == "true" {
|
||||||
|
f.Field(0).SetBool(true)
|
||||||
|
} else if value == "false" {
|
||||||
|
f.Field(0).SetBool(false)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("bad BoolValue: %s", value)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "StringValue":
|
||||||
|
f.Field(0).SetString(value)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
hash: 676b396852837415bb09adf5e734ad2d379ee4b5cded756d38031df2f797157c
|
hash: 7e13ef2acb8e54723a9530aadee7f8ef83d3ac58a2d902fbc1a7f80ee36c61f7
|
||||||
updated: 2017-10-31T19:01:57.813466-07:00
|
updated: 2017-11-08T18:45:57.327801-08:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/beorn7/perks
|
- name: github.com/beorn7/perks
|
||||||
version: 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9
|
version: 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9
|
||||||
|
|
Loading…
Reference in New Issue