Description

Status code 404 is always returned when grpc-go is used as gin middleware, and it is normal when external grpc is used

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
        "google.golang.org/grpc"
        "strings"
)

func main() {
        rpc := grpc.NewServer()
        /*
        Register your grpc service
        */

    g := gin.Default()
        g.UseH2C = true
    g.Use(func(c *gin.Context) {
              if c.Request.ProtoMajor == 2 && strings.HasPrefix(c.ContentType(), "application/grpc"){
              rpc.ServeHTTP(c.Writer, c.Request)
              c.Abort()
              return
              }
    })
    g.Run(":9000")
}

Expectations

Use the grpc client to dial to the server and execute RPC Status OK

Actual result

rpc error: code = Unimplemented desc = unexpected HTTP status code received from server: 404 Not Found

Environment

  • go version: v1.18.2
  • gin version: v1.8.1
  • operating system: linux

Comment From: ghost

Preliminary judgment of the problem in the gin. ResponseWriter re-packaged the http.ResponseWriter. If we could make a middleware type for grpc alone, or add a new method to get the original http. ResponseWriter, the problem would be solved