Repository: grpc/grpc-go
File: internal/transport/http2_client.go (Go)

Code before:
		case ":status":
			httpStatus = hf.Value
		default:
			if isReservedHeader(hf.Name) && !isWhitelistedHeader(hf.Name) {
				break
			}
			v, err := decodeMetadataHeader(hf.Name, hf.Value)
			if err != nil {
				headerError = fmt.Sprintf("transport: malformed %s: %v", hf.Name, err)
				logger.Warningf("Failed to decode metadata header (%q, %q): %v", hf.Name, hf.Value, err)
				break
			}
			mdata[hf.Name] = append(mdata[hf.Name], v)
		}
	}

	// If a non gRPC response is received, then evaluate entire http status and
	//  process close stream / response.
	// In case http status doesn't provide any error information (status : 200),
	//  evalute response code to be Unknown.
	if !isGRPC {
		var grpcErrorCode = codes.Internal
		switch httpStatus {
		case "":
			httpStatusErr = "malformed header: missing HTTP status"
		default:
			// Any other status code (e.g., "404", "503"). We must parse it.
			c, err := strconv.ParseInt(httpStatus, 10, 32)
			if err != nil {
				se := status.New(grpcErrorCode, fmt.Sprintf("transport: malformed http-status: %v", err))
				t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
				return
			}
			statusCode := int(c)
			if statusCode >= 100 && statusCode < 200 {
				if endStream {
					se := status.New(codes.Internal, fmt.Sprintf(
						"protocol error: informational header with status code %d must not have END_STREAM set", statusCode))
					t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
				}
				// In case of informational headers return
				return
			}
			httpStatusErr = fmt.Sprintf(
				"unexpected HTTP status code received from server: %d (%s)",
				statusCode,
				http.StatusText(statusCode),
			)
			var ok bool
			grpcErrorCode, ok = HTTPStatusConvTab[statusCode]
			if !ok {

Review comment (suggestion):
Nit: I'm not sure I like the use of a `switch` instead of an `if`/`else`.  It seems this would be simpler / easier to read as `if httpStatus == "" { ... } else { ... }`

Code after:
			contentTypeErr = ""
			mdata[hf.Name] = append(mdata[hf.Name], hf.Value)
			isGRPC = true
		case "grpc-encoding":
			recvCompress = hf.Value
		case "grpc-status":
			code, err := strconv.ParseInt(hf.Value, 10, 32)
			if err != nil {
				se := status.New(codes.Internal, fmt.Sprintf("transport: malformed grpc-status: %v", err))
				t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
				return
			}
			grpcStatusCode = codes.Code(uint32(code))
		case "grpc-message":
			grpcMessage = decodeGrpcMessage(hf.Value)
		case ":status":
			httpStatus = hf.Value
		default:
			if isReservedHeader(hf.Name) && !isWhitelistedHeader(hf.Name) {
				break
			}
			v, err := decodeMetadataHeader(hf.Name, hf.Value)
			if err != nil {
				headerError = fmt.Sprintf("transport: malformed %s: %v", hf.Name, err)
				logger.Warningf("Failed to decode metadata header (%q, %q): %v", hf.Name, hf.Value, err)
				break
			}
			mdata[hf.Name] = append(mdata[hf.Name], v)
		}
	}

	// If a non-gRPC response is received, then evaluate the HTTP status to
	// process the response and close the stream.
	// In case http status doesn't provide any error information (status : 200),
	// then evalute response code to be Unknown.
	if !isGRPC {
		var grpcErrorCode = codes.Internal
		if httpStatus == "" {
			httpStatusErr = "malformed header: missing HTTP status"
		} else {
			// Parse the status codes (e.g. "200", 404").
			statusCode, err := strconv.Atoi(httpStatus)
			if err != nil {
				se := status.New(grpcErrorCode, fmt.Sprintf("transport: malformed http-status: %v", err))
				t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
				return
			}
			if statusCode >= 100 && statusCode < 200 {
				if endStream {
					se := status.New(codes.Internal, fmt.Sprintf(
						"protocol error: informational header with status code %d must not have END_STREAM set", statusCode))