Cors constructor

Cors(
  1. {List<String> allowedOrigins,
  2. List<String> allowedHeaders,
  3. List<String> allowedMethods,
  4. int maxAge,
  5. bool allowCredentials: false,
  6. List<String> exposedHeaders}
)

Implementation

Cors(
    {List<String> allowedOrigins,
    List<String> allowedHeaders,
    List<String> allowedMethods,
    this.maxAge,
    this.allowCredentials: false,
    this.exposedHeaders}) {
  if (allowedHeaders != null) {
    if (allowedHeaders.isEmpty) {
      _allowedHeaders = List(0);
    } else {
      _allowedHeaders = List.from(allowedHeaders, growable: false);
      for (int i = 0; i < _allowedHeaders.length; i++) {
        _allowedHeaders[i] = recase.ReCase(_allowedHeaders[i]).headerCase;
        if (_allowedHeaders[i] == '*') {
          _allowedHeaders = null;
          _allowAllHeaders = true;
          break;
        }
      }
      ;
    }
  }

  const origin = 'Origin';
  if (!_allowAllHeaders &&
      _allowedHeaders != null &&
      !_allowedHeaders.contains(origin)) {
    _allowedHeaders.add(origin);
  }

  if (allowedMethods != null) {
    if (allowedMethods.isEmpty) {
      _allowedMethods = List(0);
    } else {
      _allowedMethods = List.from(allowedMethods, growable: false);
      for (int i = 0; i < _allowedMethods.length; i++) {
        _allowedMethods[i].toUpperCase();
        if (_allowedMethods[i] == '*') {
          _allowedMethods = null;
          _allowAllMethods = true;
          break;
        }
      }
    }
  }

  if (allowedOrigins != null) {
    _allowedOrigins = null;
    if (allowedOrigins.isEmpty) {
      _allowedOrigins = List(0);
    } else {
      for (int i = 0; i < allowedOrigins.length; i++) {
        final current = allowedOrigins[i];
        if (current == '*') {
          _allowedOrigins = null;
          _allowAllOrigins = true;
          break;
        } else if (current.contains('*')) {
          if (_allowedWildcardOrigins == null)
            _allowedWildcardOrigins = List<List<String>>();
          var split = current.split('*');
          if (split.length != 2) {
            throw ArgumentError(
                '[cors] Invalid wildcard origin provided: ${current}');
          }
          split.map((o) => o.toLowerCase());
          _allowedWildcardOrigins.add(List.from(split, growable: false));
        } else {
          if (_allowedOrigins == null) _allowedOrigins = List<String>();
          _allowedOrigins.add(current.toLowerCase());
        }
      }
    }
  }
}