serialize method
Serialize response to wire format
Implementation
Uint8List serialize() {
final buffer = StringBuffer();
// Status line
buffer.write('$version ${status.code} ${status.message}${HttpProtocolConstants.crlf}');
// Headers
for (final entry in headers.entries) {
buffer.write('${entry.key}: ${entry.value}${HttpProtocolConstants.crlf}');
}
// Content-Length header if body exists
if (body != null && !headers.containsKey('content-length')) {
buffer.write('content-length: ${body!.length}${HttpProtocolConstants.crlf}');
}
// End of headers
buffer.write(HttpProtocolConstants.crlf);
final headerBytes = utf8.encode(buffer.toString());
if (body != null) {
final result = Uint8List(headerBytes.length + body!.length);
result.setRange(0, headerBytes.length, headerBytes);
result.setRange(headerBytes.length, result.length, body!);
return result;
}
return headerBytes;
}