getHeader function

int getHeader(
  1. String data,
  2. int headerStart
)

Extract and parse every header of a SIP message.

Implementation

int getHeader(String data, int headerStart) {
  // 'start' position of the header.
  int start = headerStart;
  // 'end' position of the header.
  int end = 0;
  // 'partial end' position of the header.
  int partialEnd = 0;

  // End of message.
  if (data.substring(start, start + 2).contains(RegExp(r'(^\r\n)'))) {
    return -2;
  }

  while (end == 0) {
    // Partial End of Header.
    partialEnd = data.indexOf('\r\n', start);

    // 'indexOf' returns -1 if the value to be found never occurs.
    if (partialEnd == -1) {
      return partialEnd;
    }
    //if (!data.substring(partialEnd + 2, partialEnd + 4).match(/(^\r\n)/) && data.charAt(partialEnd + 2).match(/(^\s+)/))

    if (!data
            .substring(partialEnd + 2, partialEnd + 4)
            .contains(RegExp(r'(^\r\n)')) &&
        String.fromCharCode(data.codeUnitAt(partialEnd + 2))
            .contains(RegExp(r'(^\s+)'))) {
      // Not the end of the message. Continue from the next position.
      start = partialEnd + 2;
    } else {
      end = partialEnd;
    }
  }

  return end;
}