parseHeader method

dynamic parseHeader(
  1. String name, {
  2. int idx = 0,
})

Parse the given header on the given index. -param {String} name header name -param {Number} idx=0 header index -returns {Object|null} Parsed header object, null if the header is not present or in case of a parsing error.

Implementation

dynamic parseHeader(String name, {int idx = 0}) {
  name = utils.headerize(name);

  if (headers![name] == null) {
    logger.debug('header "$name" not present');
    return null;
  } else if (idx >= headers![name].length) {
    logger.debug('not so many "$name" headers present');
    return null;
  }

  dynamic header = headers![name][idx];
  dynamic value = header['raw'];

  if (header['parsed'] != null) {
    return header['parsed'];
  }

  // Substitute '-' by '_' for grammar rule matching.
  dynamic parsed = Grammar.parse(value, name.replaceAll('-', '_'));
  if (parsed == -1) {
    headers![name].splice(idx, 1); // delete from headers
    logger.debug('error parsing "$name" header field with value "$value"');
    return null;
  } else {
    header['parsed'] = parsed;

    return parsed;
  }
}