private getResponseHeaders()

in src/blob/middlewares/PreflightMiddlewareFactory.ts [299:405]


  private getResponseHeaders(res: Response, err?: MiddlewareError): string[] {
    const responseHeaderSet = [];

    const context = new BlobStorageContext(res.locals, DEFAULT_CONTEXT_PATH);
    const handlerResponse = context.handlerResponses;

    if (handlerResponse && context.operation) {
      const statusCodeInResponse: number = handlerResponse.statusCode;
      const spec = Specifications[context.operation];
      const responseSpec = spec.responses[statusCodeInResponse];
      if (!responseSpec) {
        throw new TypeError(
          `Request specification doesn't include provided response status code`
        );
      }

      // Serialize headers
      const headerSerializer = new msRest.Serializer(Mappers);
      const headersMapper = responseSpec.headersMapper;

      if (headersMapper && headersMapper.type.name === "Composite") {
        const mappersForAllHeaders = headersMapper.type.modelProperties || {};

        // Handle headerMapper one by one
        for (const key in mappersForAllHeaders) {
          if (mappersForAllHeaders.hasOwnProperty(key)) {
            const headerMapper = mappersForAllHeaders[key];
            const headerName = headerMapper.serializedName;
            const headerValueOriginal = handlerResponse[key];
            const headerValueSerialized = headerSerializer.serialize(
              headerMapper,
              headerValueOriginal
            );

            // Handle collection of headers starting with same prefix, such as x-ms-meta prefix
            const headerCollectionPrefix = (
              headerMapper as msRest.DictionaryMapper
            ).headerCollectionPrefix;
            if (
              headerCollectionPrefix !== undefined &&
              headerValueOriginal !== undefined
            ) {
              for (const collectionHeaderPartialName in headerValueSerialized) {
                if (
                  headerValueSerialized.hasOwnProperty(
                    collectionHeaderPartialName
                  )
                ) {
                  const collectionHeaderValueSerialized =
                    headerValueSerialized[collectionHeaderPartialName];
                  const collectionHeaderName = `${headerCollectionPrefix}${collectionHeaderPartialName}`;
                  if (
                    collectionHeaderName &&
                    collectionHeaderValueSerialized !== undefined
                  ) {
                    responseHeaderSet.push(collectionHeaderName);
                  }
                }
              }
            } else {
              if (headerName && headerValueSerialized !== undefined) {
                responseHeaderSet.push(headerName);
              }
            }
          }
        }
      }

      if (
        spec.isXML &&
        responseSpec.bodyMapper &&
        responseSpec.bodyMapper.type.name !== "Stream"
      ) {
        responseHeaderSet.push("content-type");
        responseHeaderSet.push("content-length");
      } else if (
        handlerResponse.body &&
        responseSpec.bodyMapper &&
        responseSpec.bodyMapper.type.name === "Stream"
      ) {
        responseHeaderSet.push("content-length");
      }
    }

    const headers = res.getHeaders();
    for (const header in headers) {
      if (typeof header === "string") {
        responseHeaderSet.push(header);
      }
    }

    if (err) {
      for (const key in err.headers) {
        if (err.headers.hasOwnProperty(key)) {
          responseHeaderSet.push(key);
        }
      }
    }

    // TODO: Should extract the header by some policy.
    // or apply a referred list indicates the related headers.
    responseHeaderSet.push("Date");
    responseHeaderSet.push("Connection");
    responseHeaderSet.push("Transfer-Encoding");

    return responseHeaderSet;
  }