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

Code before:

	// 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 {
				grpcErrorCode = codes.Unknown
			}
		}
		var errs []string
		if httpStatusErr != "" {
			errs = append(errs, httpStatusErr)
		}

		if contentTypeErr != "" {
			errs = append(errs, contentTypeErr)
		}

		se := status.New(grpcErrorCode, strings.Join(errs, "; "))
		t.closeStream(s, se.Err(), true, http2.ErrCodeProtocol, se, nil, endStream)
		return

Review comment (suggestion):
Lots of options, here, e.g.:

```suggestion
				// In case of informational headers, return.
```

Code after:
		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))
					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 {
				grpcErrorCode = codes.Unknown
			}