2017-07-06 05:38:20 -07:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/textproto"
|
|
|
|
|
2019-01-15 06:32:05 -08:00
|
|
|
"context"
|
2017-07-06 05:38:20 -07:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-01-15 06:32:05 -08:00
|
|
|
"github.com/golang/protobuf/ptypes/any"
|
2019-04-04 02:55:32 -07:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/internal"
|
2017-11-30 02:19:08 -08:00
|
|
|
"google.golang.org/grpc/codes"
|
2017-07-06 05:38:20 -07:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2017-11-30 02:19:08 -08:00
|
|
|
"google.golang.org/grpc/status"
|
2017-07-06 05:38:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ForwardResponseStream forwards the stream from gRPC server to REST client.
|
2017-11-30 02:19:08 -08:00
|
|
|
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) {
|
2017-07-06 05:38:20 -07:00
|
|
|
f, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Flush not supported in %T", w)
|
2017-07-06 05:38:20 -07:00
|
|
|
http.Error(w, "unexpected type of web server", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
md, ok := ServerMetadataFromContext(ctx)
|
|
|
|
if !ok {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to extract ServerMetadata from context")
|
2017-07-06 05:38:20 -07:00
|
|
|
http.Error(w, "unexpected error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-11-30 02:19:08 -08:00
|
|
|
handleForwardResponseServerMetadata(w, mux, md)
|
2017-07-06 05:38:20 -07:00
|
|
|
|
|
|
|
w.Header().Set("Transfer-Encoding", "chunked")
|
|
|
|
w.Header().Set("Content-Type", marshaler.ContentType())
|
|
|
|
if err := handleForwardResponseOptions(ctx, w, nil, opts); err != nil {
|
2017-11-30 02:19:08 -08:00
|
|
|
HTTPError(ctx, mux, marshaler, w, req, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
2017-11-30 02:19:08 -08:00
|
|
|
|
|
|
|
var delimiter []byte
|
|
|
|
if d, ok := marshaler.(Delimited); ok {
|
|
|
|
delimiter = d.Delimiter()
|
2019-01-15 06:32:05 -08:00
|
|
|
} else {
|
|
|
|
delimiter = []byte("\n")
|
2017-11-30 02:19:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var wroteHeader bool
|
2017-07-06 05:38:20 -07:00
|
|
|
for {
|
|
|
|
resp, err := recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-11-30 02:19:08 -08:00
|
|
|
handleForwardResponseStreamError(wroteHeader, marshaler, w, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil {
|
2017-11-30 02:19:08 -08:00
|
|
|
handleForwardResponseStreamError(wroteHeader, marshaler, w, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := marshaler.Marshal(streamChunk(resp, nil))
|
|
|
|
if err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to marshal response chunk: %v", err)
|
2017-11-30 02:19:08 -08:00
|
|
|
handleForwardResponseStreamError(wroteHeader, marshaler, w, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
2017-11-30 02:19:08 -08:00
|
|
|
if _, err = w.Write(buf); err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to send response chunk: %v", err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
2017-11-30 02:19:08 -08:00
|
|
|
wroteHeader = true
|
|
|
|
if _, err = w.Write(delimiter); err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to send delimiter chunk: %v", err)
|
2017-11-30 02:19:08 -08:00
|
|
|
return
|
|
|
|
}
|
2017-07-06 05:38:20 -07:00
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 02:19:08 -08:00
|
|
|
func handleForwardResponseServerMetadata(w http.ResponseWriter, mux *ServeMux, md ServerMetadata) {
|
2017-07-06 05:38:20 -07:00
|
|
|
for k, vs := range md.HeaderMD {
|
2017-11-30 02:19:08 -08:00
|
|
|
if h, ok := mux.outgoingHeaderMatcher(k); ok {
|
|
|
|
for _, v := range vs {
|
|
|
|
w.Header().Add(h, v)
|
|
|
|
}
|
2017-07-06 05:38:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleForwardResponseTrailerHeader(w http.ResponseWriter, md ServerMetadata) {
|
|
|
|
for k := range md.TrailerMD {
|
|
|
|
tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", MetadataTrailerPrefix, k))
|
|
|
|
w.Header().Add("Trailer", tKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) {
|
|
|
|
for k, vs := range md.TrailerMD {
|
|
|
|
tKey := fmt.Sprintf("%s%s", MetadataTrailerPrefix, k)
|
2017-11-30 02:19:08 -08:00
|
|
|
for _, v := range vs {
|
|
|
|
w.Header().Add(tKey, v)
|
2017-07-06 05:38:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 06:32:05 -08:00
|
|
|
// responseBody interface contains method for getting field for marshaling to the response body
|
|
|
|
// this method is generated for response struct from the value of `response_body` in the `google.api.HttpRule`
|
|
|
|
type responseBody interface {
|
|
|
|
XXX_ResponseBody() interface{}
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:38:20 -07:00
|
|
|
// ForwardResponseMessage forwards the message "resp" from gRPC server to REST client.
|
2017-11-30 02:19:08 -08:00
|
|
|
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) {
|
2017-07-06 05:38:20 -07:00
|
|
|
md, ok := ServerMetadataFromContext(ctx)
|
|
|
|
if !ok {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to extract ServerMetadata from context")
|
2017-07-06 05:38:20 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 02:19:08 -08:00
|
|
|
handleForwardResponseServerMetadata(w, mux, md)
|
2017-07-06 05:38:20 -07:00
|
|
|
handleForwardResponseTrailerHeader(w, md)
|
2019-04-04 02:55:32 -07:00
|
|
|
|
|
|
|
contentType := marshaler.ContentType()
|
|
|
|
// Check marshaler on run time in order to keep backwards compatability
|
|
|
|
// An interface param needs to be added to the ContentType() function on
|
|
|
|
// the Marshal interface to be able to remove this check
|
|
|
|
if httpBodyMarshaler, ok := marshaler.(*HTTPBodyMarshaler); ok {
|
|
|
|
contentType = httpBodyMarshaler.ContentTypeFromMessage(resp)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
2017-07-06 05:38:20 -07:00
|
|
|
if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil {
|
2017-11-30 02:19:08 -08:00
|
|
|
HTTPError(ctx, mux, marshaler, w, req, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
2019-01-15 06:32:05 -08:00
|
|
|
var buf []byte
|
|
|
|
var err error
|
|
|
|
if rb, ok := resp.(responseBody); ok {
|
|
|
|
buf, err = marshaler.Marshal(rb.XXX_ResponseBody())
|
|
|
|
} else {
|
|
|
|
buf, err = marshaler.Marshal(resp)
|
|
|
|
}
|
2017-07-06 05:38:20 -07:00
|
|
|
if err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Marshal error: %v", err)
|
2017-11-30 02:19:08 -08:00
|
|
|
HTTPError(ctx, mux, marshaler, w, req, err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = w.Write(buf); err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to write response: %v", err)
|
2017-07-06 05:38:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleForwardResponseTrailer(w, md)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleForwardResponseOptions(ctx context.Context, w http.ResponseWriter, resp proto.Message, opts []func(context.Context, http.ResponseWriter, proto.Message) error) error {
|
|
|
|
if len(opts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(ctx, w, resp); err != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Error handling ForwardResponseOptions: %v", err)
|
2017-07-06 05:38:20 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 02:19:08 -08:00
|
|
|
func handleForwardResponseStreamError(wroteHeader bool, marshaler Marshaler, w http.ResponseWriter, err error) {
|
2017-07-06 05:38:20 -07:00
|
|
|
buf, merr := marshaler.Marshal(streamChunk(nil, err))
|
|
|
|
if merr != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to marshal an error: %v", merr)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
2017-11-30 02:19:08 -08:00
|
|
|
if !wroteHeader {
|
|
|
|
s, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
s = status.New(codes.Unknown, err.Error())
|
|
|
|
}
|
|
|
|
w.WriteHeader(HTTPStatusFromCode(s.Code()))
|
|
|
|
}
|
|
|
|
if _, werr := w.Write(buf); werr != nil {
|
2019-01-15 06:32:05 -08:00
|
|
|
grpclog.Infof("Failed to notify error to client: %v", werr)
|
2017-07-06 05:38:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamChunk(result proto.Message, err error) map[string]proto.Message {
|
|
|
|
if err != nil {
|
2017-11-30 02:19:08 -08:00
|
|
|
grpcCode := codes.Unknown
|
2019-01-15 06:32:05 -08:00
|
|
|
grpcMessage := err.Error()
|
|
|
|
var grpcDetails []*any.Any
|
2017-11-30 02:19:08 -08:00
|
|
|
if s, ok := status.FromError(err); ok {
|
|
|
|
grpcCode = s.Code()
|
2019-01-15 06:32:05 -08:00
|
|
|
grpcMessage = s.Message()
|
|
|
|
grpcDetails = s.Proto().GetDetails()
|
2017-11-30 02:19:08 -08:00
|
|
|
}
|
2017-07-06 05:38:20 -07:00
|
|
|
httpCode := HTTPStatusFromCode(grpcCode)
|
|
|
|
return map[string]proto.Message{
|
|
|
|
"error": &internal.StreamError{
|
|
|
|
GrpcCode: int32(grpcCode),
|
|
|
|
HttpCode: int32(httpCode),
|
2019-01-15 06:32:05 -08:00
|
|
|
Message: grpcMessage,
|
2017-07-06 05:38:20 -07:00
|
|
|
HttpStatus: http.StatusText(httpCode),
|
2019-01-15 06:32:05 -08:00
|
|
|
Details: grpcDetails,
|
2017-07-06 05:38:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return streamChunk(nil, fmt.Errorf("empty response"))
|
|
|
|
}
|
|
|
|
return map[string]proto.Message{"result": result}
|
|
|
|
}
|