ResponseFraming.of constructor
ResponseFraming.of(
- Response response, {
- required bool head,
- required HttpProtocol protocol,
- required bool keepAlive,
- bool stampDate = false,
The framing of response over protocol, for a HEAD request when
head, on a connection kept alive when keepAlive.
With stampDate the Date is stampedDate, for an adapter that
writes the date itself at send time, at ResponseHead.dateOffset.
Throws FormatException when the body's type does not validate or a response header the framing reads does not parse, so a response that cannot be written fails before anything goes out.
Implementation
factory ResponseFraming.of(
Response response, {
required bool head,
required HttpProtocol protocol,
required bool keepAlive,
bool stampDate = false,
}) {
final headers = response.headers;
final body = response.body;
final hasBody = statusMayHaveBody(response.statusCode);
final bodyType = body.bodyType;
String? contentType;
if (bodyType != null) {
bodyType.validate();
contentType = bodyType.toHeaderValue();
}
String? date;
Uint8List? dateLine;
if (!headers.contains(HeaderName.date)) {
if (stampDate) {
date = stampedDate;
} else {
// The value and its line come from one cache refresh.
date = httpDate();
dateLine = httpDateLineOf(date);
}
}
// Most responses set neither Connection nor Transfer-Encoding, so the
// parse runs only when the header is there.
final setsConnection = headers.contains(HeaderName.connection);
final own = setsConnection
? _own(() => headers.connection, HeaderName.connection)
: null;
// An HTTP/1.0 client keeps the connection only when the response says
// keep-alive, so a Connection value of the handler's without it ends
// the connection here too.
var closeAfter =
!keepAlive ||
(own?.isClose ?? false) ||
(setsConnection &&
protocol == HttpProtocol.http10 &&
!(own?.isKeepAlive ?? false));
int? contentLength;
List<String>? transferEncoding;
if (hasBody) {
// A handler's codings stay, except chunked. The body's length decides
// that here.
List<String>? codings;
if (headers.contains(HeaderName.transferEncoding)) {
codings = [
for (final coding
in _own(
() => headers.transferEncoding,
HeaderName.transferEncoding,
)?.encodings ??
const <TransferEncoding>[])
if (coding.name != TransferEncoding.chunked.name) coding.name,
];
if (codings.isEmpty) codings = null;
}
final length = body.contentLength;
if (length != null && codings == null) {
// A message with Transfer-Encoding has no Content-Length (RFC 9112
// 6.1), so its coding frames a coded body even when the length is
// known.
contentLength = length;
} else if (protocol == HttpProtocol.http10 ||
(codings?.contains(TransferEncoding.identity.name) ?? false) ||
_isMultipartByteranges(bodyType)) {
// Nothing frames the body, so the connection's end does.
closeAfter = true;
} else {
(codings ??= []).add(TransferEncoding.chunked.name);
}
transferEncoding = codings;
}
return ResponseFraming._(
sendBody: hasBody && !head,
contentLength: contentLength,
transferEncoding: transferEncoding,
contentType: contentType,
date: date,
dateLine: dateLine,
closeAfter: closeAfter,
connection: _option(closeAfter, protocol, own),
);
}