sslobodr | d046be8 | 2019-01-16 10:02:22 -0500 | [diff] [blame] | 1 | package runtime |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "net/textproto" |
| 8 | |
| 9 | "context" |
| 10 | "github.com/golang/protobuf/proto" |
| 11 | "github.com/golang/protobuf/ptypes/any" |
| 12 | "github.com/grpc-ecosystem/grpc-gateway/runtime/internal" |
| 13 | "google.golang.org/grpc/codes" |
| 14 | "google.golang.org/grpc/grpclog" |
| 15 | "google.golang.org/grpc/status" |
| 16 | ) |
| 17 | |
| 18 | // ForwardResponseStream forwards the stream from gRPC server to REST client. |
| 19 | 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) { |
| 20 | f, ok := w.(http.Flusher) |
| 21 | if !ok { |
| 22 | grpclog.Infof("Flush not supported in %T", w) |
| 23 | http.Error(w, "unexpected type of web server", http.StatusInternalServerError) |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | md, ok := ServerMetadataFromContext(ctx) |
| 28 | if !ok { |
| 29 | grpclog.Infof("Failed to extract ServerMetadata from context") |
| 30 | http.Error(w, "unexpected error", http.StatusInternalServerError) |
| 31 | return |
| 32 | } |
| 33 | handleForwardResponseServerMetadata(w, mux, md) |
| 34 | |
| 35 | w.Header().Set("Transfer-Encoding", "chunked") |
| 36 | w.Header().Set("Content-Type", marshaler.ContentType()) |
| 37 | if err := handleForwardResponseOptions(ctx, w, nil, opts); err != nil { |
| 38 | HTTPError(ctx, mux, marshaler, w, req, err) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | var delimiter []byte |
| 43 | if d, ok := marshaler.(Delimited); ok { |
| 44 | delimiter = d.Delimiter() |
| 45 | } else { |
| 46 | delimiter = []byte("\n") |
| 47 | } |
| 48 | |
| 49 | var wroteHeader bool |
| 50 | for { |
| 51 | resp, err := recv() |
| 52 | if err == io.EOF { |
| 53 | return |
| 54 | } |
| 55 | if err != nil { |
| 56 | handleForwardResponseStreamError(wroteHeader, marshaler, w, err) |
| 57 | return |
| 58 | } |
| 59 | if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { |
| 60 | handleForwardResponseStreamError(wroteHeader, marshaler, w, err) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | buf, err := marshaler.Marshal(streamChunk(resp, nil)) |
| 65 | if err != nil { |
| 66 | grpclog.Infof("Failed to marshal response chunk: %v", err) |
| 67 | handleForwardResponseStreamError(wroteHeader, marshaler, w, err) |
| 68 | return |
| 69 | } |
| 70 | if _, err = w.Write(buf); err != nil { |
| 71 | grpclog.Infof("Failed to send response chunk: %v", err) |
| 72 | return |
| 73 | } |
| 74 | wroteHeader = true |
| 75 | if _, err = w.Write(delimiter); err != nil { |
| 76 | grpclog.Infof("Failed to send delimiter chunk: %v", err) |
| 77 | return |
| 78 | } |
| 79 | f.Flush() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func handleForwardResponseServerMetadata(w http.ResponseWriter, mux *ServeMux, md ServerMetadata) { |
| 84 | for k, vs := range md.HeaderMD { |
| 85 | if h, ok := mux.outgoingHeaderMatcher(k); ok { |
| 86 | for _, v := range vs { |
| 87 | w.Header().Add(h, v) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func handleForwardResponseTrailerHeader(w http.ResponseWriter, md ServerMetadata) { |
| 94 | for k := range md.TrailerMD { |
| 95 | tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", MetadataTrailerPrefix, k)) |
| 96 | w.Header().Add("Trailer", tKey) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) { |
| 101 | for k, vs := range md.TrailerMD { |
| 102 | tKey := fmt.Sprintf("%s%s", MetadataTrailerPrefix, k) |
| 103 | for _, v := range vs { |
| 104 | w.Header().Add(tKey, v) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // responseBody interface contains method for getting field for marshaling to the response body |
| 110 | // this method is generated for response struct from the value of `response_body` in the `google.api.HttpRule` |
| 111 | type responseBody interface { |
| 112 | XXX_ResponseBody() interface{} |
| 113 | } |
| 114 | |
| 115 | // ForwardResponseMessage forwards the message "resp" from gRPC server to REST client. |
| 116 | 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) { |
| 117 | md, ok := ServerMetadataFromContext(ctx) |
| 118 | if !ok { |
| 119 | grpclog.Infof("Failed to extract ServerMetadata from context") |
| 120 | } |
| 121 | |
| 122 | handleForwardResponseServerMetadata(w, mux, md) |
| 123 | handleForwardResponseTrailerHeader(w, md) |
| 124 | w.Header().Set("Content-Type", marshaler.ContentType()) |
| 125 | if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { |
| 126 | HTTPError(ctx, mux, marshaler, w, req, err) |
| 127 | return |
| 128 | } |
| 129 | var buf []byte |
| 130 | var err error |
| 131 | if rb, ok := resp.(responseBody); ok { |
| 132 | buf, err = marshaler.Marshal(rb.XXX_ResponseBody()) |
| 133 | } else { |
| 134 | buf, err = marshaler.Marshal(resp) |
| 135 | } |
| 136 | if err != nil { |
| 137 | grpclog.Infof("Marshal error: %v", err) |
| 138 | HTTPError(ctx, mux, marshaler, w, req, err) |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | if _, err = w.Write(buf); err != nil { |
| 143 | grpclog.Infof("Failed to write response: %v", err) |
| 144 | } |
| 145 | |
| 146 | handleForwardResponseTrailer(w, md) |
| 147 | } |
| 148 | |
| 149 | func handleForwardResponseOptions(ctx context.Context, w http.ResponseWriter, resp proto.Message, opts []func(context.Context, http.ResponseWriter, proto.Message) error) error { |
| 150 | if len(opts) == 0 { |
| 151 | return nil |
| 152 | } |
| 153 | for _, opt := range opts { |
| 154 | if err := opt(ctx, w, resp); err != nil { |
| 155 | grpclog.Infof("Error handling ForwardResponseOptions: %v", err) |
| 156 | return err |
| 157 | } |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func handleForwardResponseStreamError(wroteHeader bool, marshaler Marshaler, w http.ResponseWriter, err error) { |
| 163 | buf, merr := marshaler.Marshal(streamChunk(nil, err)) |
| 164 | if merr != nil { |
| 165 | grpclog.Infof("Failed to marshal an error: %v", merr) |
| 166 | return |
| 167 | } |
| 168 | if !wroteHeader { |
| 169 | s, ok := status.FromError(err) |
| 170 | if !ok { |
| 171 | s = status.New(codes.Unknown, err.Error()) |
| 172 | } |
| 173 | w.WriteHeader(HTTPStatusFromCode(s.Code())) |
| 174 | } |
| 175 | if _, werr := w.Write(buf); werr != nil { |
| 176 | grpclog.Infof("Failed to notify error to client: %v", werr) |
| 177 | return |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func streamChunk(result proto.Message, err error) map[string]proto.Message { |
| 182 | if err != nil { |
| 183 | grpcCode := codes.Unknown |
| 184 | grpcMessage := err.Error() |
| 185 | var grpcDetails []*any.Any |
| 186 | if s, ok := status.FromError(err); ok { |
| 187 | grpcCode = s.Code() |
| 188 | grpcMessage = s.Message() |
| 189 | grpcDetails = s.Proto().GetDetails() |
| 190 | } |
| 191 | httpCode := HTTPStatusFromCode(grpcCode) |
| 192 | return map[string]proto.Message{ |
| 193 | "error": &internal.StreamError{ |
| 194 | GrpcCode: int32(grpcCode), |
| 195 | HttpCode: int32(httpCode), |
| 196 | Message: grpcMessage, |
| 197 | HttpStatus: http.StatusText(httpCode), |
| 198 | Details: grpcDetails, |
| 199 | }, |
| 200 | } |
| 201 | } |
| 202 | if result == nil { |
| 203 | return streamChunk(nil, fmt.Errorf("empty response")) |
| 204 | } |
| 205 | return map[string]proto.Message{"result": result} |
| 206 | } |