add method
Adds a header value.
The header named name
will have a string value derived from value
added to its list of values.
Some headers are single valued, and for these, adding a value will
replace a previous value. If the value
is a DateTime, an
HTTP date format will be applied. If the value is an Iterable,
each element will be added separately. For all other
types the default Object.toString method will be used.
Header names are converted to lower-case unless
preserveHeaderCase
is set to true. If two header names are
the same when converted to lower-case, they are considered to be
the same header, with one set of values.
The current case of the a header name is that of the name used by the last set or add call for that header.
Implementation
@override
void add(String name, Object value, {bool preserveHeaderCase = false}) {
// Dart 2.8 adds the preserveHeaderCase option
final lcName = name.toLowerCase();
var values = _data[lcName];
if (values == null) {
final createdList = <String>[];
_data[lcName] = createdList;
values = createdList;
}
// A HttpHeader from a real HTTP request discards any whitespace from
// the left (i.e. after the colon), but preserves any whitespaces at the
// right end of the line. Empty string values are preserved.
// Values which are empty strings, and values which are all whitespace,
// are kept as an empty string.
//
// Note: when testing, be aware clients may modify/discard headers
// before sending them.
values.add(value.toString().trimLeft());
_originalHeaderNames[lcName] = preserveHeaderCase ? name : lcName;
}