parse static method

List<ETag> parse(
  1. String source
)

Parses multiple ETags from an If-Match or If-None-Match HTTP header.

source is the raw header string. Returns a list of parsed ETags.

Supports weak ETags, quoted ETags, and wildcard (*).

Implementation

static List<ETag> parse(String source) {
  final result = <ETag>[];
  var state = _State.BEFORE_QUOTES;
  var startIndex = -1;
  var weak = false;

  for (int i = 0; i < source.length; i++) {
    final c = source[i];

    if (state == _State.IN_QUOTES) {
      if (c == '"') {
        final tag = source.substring(startIndex, i);
        if (tag.trim().isNotEmpty) {
          result.add(ETag(tag, weak));
        }
        state = _State.AFTER_QUOTES;
        startIndex = -1;
        weak = false;
      }
      continue;
    }

    if (_isWhitespace(c)) {
      continue;
    }

    if (c == ',') {
      state = _State.BEFORE_QUOTES;
      continue;
    }

    if (state == _State.BEFORE_QUOTES) {
      if (c == '*') {
        result.add(wildcard);
        state = _State.AFTER_QUOTES;
        continue;
      }
      if (c == '"') {
        state = _State.IN_QUOTES;
        startIndex = i + 1;
        continue;
      }
      if (c == 'W' && source.length > i + 2) {
        if (source[i + 1] == '/' && source[i + 2] == '"') {
          state = _State.IN_QUOTES;
          i = i + 2;
          startIndex = i + 1;
          weak = true;
          continue;
        }
      }
    }

    if (_log.getIsDebugEnabled()) {
      _log.debug("Unexpected char at index $i");
    }
  }

  if (state != _State.IN_QUOTES) {
    if (_log.getIsDebugEnabled()) {
      _log.debug("Expected closing '\"'");
    }
  }

  return result;
}