async function protocolNegotiatingHandler()

in packages/dubbo/src/protocol/universal-handler.ts [260:293]


  async function protocolNegotiatingHandler(request: UniversalServerRequest) {
    if (
      method.kind == MethodKind.BiDiStreaming &&
      request.httpVersion.startsWith("1.")
    ) {
      return {
        ...uResponseVersionNotSupported,
        // Clients coded to expect full-duplex connections may hang if they've
        // mistakenly negotiated HTTP/1.1. To unblock them, we must close the
        // underlying TCP connection.
        header: new Headers({ Connection: "close" }),
      };
    }
    const contentType = request.header.get("Content-Type") ?? "";
    const matchingMethod = protocolHandlers.filter((h) =>
      h.allowedMethods.includes(request.method)
    );
    if (matchingMethod.length == 0) {
      return uResponseMethodNotAllowed;
    }
    // If Content-Type is unset but only one handler matches, use it.
    if (matchingMethod.length == 1 && contentType === "") {
      const onlyMatch = matchingMethod[0];
      return onlyMatch(request);
    }
    const matchingContentTypes = matchingMethod.filter((h) =>
      h.supportedContentType(contentType)
    );
    if (matchingContentTypes.length == 0) {
      return uResponseUnsupportedMediaType;
    }
    const firstMatch = matchingContentTypes[0];
    return firstMatch(request);
  }