parseHeaderParameter function

String? parseHeaderParameter(
  1. String input,
  2. String keyLowercase
)

Implementation

String? parseHeaderParameter(String input, String keyLowercase) {
  final String lower = input.toLowerCase();
  int idx = lower.indexOf(keyLowercase);
  if (idx == -1) return null;

  // Ensure the match starts at a parameter boundary.
  if (idx > 0) {
    final int prev = lower.codeUnitAt(idx - 1);
    if (prev != 0x3B /* ; */ && !isAsciiWhitespaceCodeUnit(prev)) {
      idx = lower.indexOf(keyLowercase, idx + 1);
      if (idx == -1) return null;
    }
  }

  int start = idx + keyLowercase.length;
  // keyLowercase should include '=' (e.g. 'charset=')
  if (start > input.length) return null;

  int end = start;
  while (end < input.length) {
    final int cu = input.codeUnitAt(end);
    if (cu == 0x3B /* ; */) break;
    end++;
  }
  String value = input.substring(start, end).trim();
  if (value.length >= 2) {
    final int first = value.codeUnitAt(0);
    final int last = value.codeUnitAt(value.length - 1);
    if ((first == 0x22 && last == 0x22) || (first == 0x27 && last == 0x27)) {
      value = value.substring(1, value.length - 1);
    }
  }
  return value.isEmpty ? null : value;
}