close method

Send the HTTP request and get the response.

Implementation

Future<SSHHttpClientResponse> close() async {
  var queryString = '';
  if (uri.hasQuery) {
    final query = StringBuffer();
    query.write('?');
    uri.queryParameters.forEach((k, v) {
      query.write(Uri.encodeComponent(k));
      query.write('=');
      query.write(Uri.encodeComponent(v));
      query.write('&');
    });
    queryString = query.toString().substring(0, query.length - 1);
  }
  final buffer = StringBuffer();

  final path = uri.path.isEmpty ? '/' : uri.path;

  buffer.write('$method $path$queryString HTTP/${protocolVersion.name}\r\n');

  headers.forEach((name, values) {
    for (var value in values) {
      buffer.write('$name: $value\r\n');
    }
  });

  buffer.write('\r\n');

  if (hasBody) {
    buffer.write(String.fromCharCodes(_body!.takeBytes()));
  }

  final socket = await client.forwardLocal(uri.host, uri.port);
  socket.sink.add(buffer.toString().codeUnits);
  return SSHHttpClientResponse.from(socket);
}