parseHeader function
Implementation
dynamic parseHeader(
IncomingMessage message, String data, int headerStart, int headerEnd) {
dynamic parsed;
int hcolonIndex = data.indexOf(':', headerStart);
String headerName = data.substring(headerStart, hcolonIndex).trim();
String headerValue = data.substring(hcolonIndex + 1, headerEnd).trim();
// If header-field is well-known, parse it.
switch (headerName.toLowerCase()) {
case 'via':
case 'v':
message.addHeader('via', headerValue);
if (message.getHeaders('via').length == 1) {
parsed = message.parseHeader('Via');
if (parsed != null) {
message.via_branch = parsed.branch;
}
} else {
parsed = 0;
}
break;
case 'from':
case 'f':
message.setHeader('from', headerValue);
parsed = message.parseHeader('from');
if (parsed != null) {
message.from = parsed;
message.from_tag = parsed.getParam('tag');
}
break;
case 'to':
case 't':
message.setHeader('to', headerValue);
parsed = message.parseHeader('to');
if (parsed != null) {
message.to = parsed;
message.to_tag = parsed.getParam('tag');
}
break;
case 'record-route':
parsed = Grammar.parse(headerValue, 'Record_Route');
if (parsed == -1) {
parsed = null;
} else {
for (Map<String, dynamic> header in parsed) {
message.addHeader('record-route', header['raw']);
message.headers!['Record-Route']
[message.getHeaders('record-route').length - 1]['parsed'] =
header['parsed'];
}
}
break;
case 'call-id':
case 'i':
message.setHeader('call-id', headerValue);
parsed = message.parseHeader('call-id');
if (parsed != null) {
message.call_id = headerValue;
}
break;
case 'contact':
case 'm':
parsed = Grammar.parse(headerValue, 'Contact');
if (parsed == -1) {
parsed = null;
} else {
for (Map<String, dynamic> header in parsed) {
message.addHeader('contact', header['raw']);
message.headers!['Contact'][message.getHeaders('contact').length - 1]
['parsed'] = header['parsed'];
}
}
break;
case 'content-length':
case 'l':
message.setHeader('content-length', headerValue);
parsed = message.parseHeader('content-length');
break;
case 'content-type':
case 'c':
message.setHeader('content-type', headerValue);
parsed = message.parseHeader('content-type');
break;
case 'cseq':
message.setHeader('cseq', headerValue);
parsed = message.parseHeader('cseq');
if (parsed != null) {
message.cseq = parsed.cseq;
}
if (message is IncomingResponse) {
message.method = parsed.method;
}
break;
case 'max-forwards':
message.setHeader('max-forwards', headerValue);
parsed = message.parseHeader('max-forwards');
break;
case 'www-authenticate':
message.setHeader('www-authenticate', headerValue);
parsed = message.parseHeader('www-authenticate');
break;
case 'proxy-authenticate':
message.setHeader('proxy-authenticate', headerValue);
parsed = message.parseHeader('proxy-authenticate');
break;
case 'session-expires':
case 'x':
message.setHeader('session-expires', headerValue);
parsed = message.parseHeader('session-expires');
if (parsed != null) {
message.session_expires = parsed.expires;
message.session_expires_refresher = parsed.refresher;
}
break;
case 'refer-to':
case 'r':
message.setHeader('refer-to', headerValue);
parsed = message.parseHeader('refer-to');
if (parsed != null) {
message.refer_to = parsed;
}
break;
case 'replaces':
message.setHeader('replaces', headerValue);
parsed = message.parseHeader('replaces');
if (parsed != null) {
message.replaces = parsed;
}
break;
case 'event':
case 'o':
message.setHeader('event', headerValue);
parsed = message.parseHeader('event');
if (parsed != null) {
message.event = parsed;
}
break;
default:
// Do not parse this header.
message.addHeader(headerName, headerValue);
parsed = 0;
}
if (parsed == null) {
return <String, dynamic>{'error': 'error parsing header "$headerName"'};
} else {
return true;
}
}