open method

  1. @override
Future<HttpClientRequest> open(
  1. String method,
  2. String host,
  3. int port,
  4. String path,
)
override

Opens a HTTP connection.

The HTTP method to use is specified in method, the server is specified using host and port, and the path (including a possible query) is specified using path. The path may also contain a URI fragment, which will be ignored.

The Host header for the request will be set to the value host:port (if host is an IP address, it will still be used in the Host header). This can be overridden through the HttpClientRequest interface before the request is sent.

For additional information on the sequence of events during an HTTP transaction, and the objects returned by the futures, see the overall documentation for the class HttpClient.

Implementation

@override
Future<HttpClientRequest> open(
    String method, String host, int port, String path) {
  // This implementation is copied from http_impl.dart in dart:io, essentially
  // stripping out the query from the path. All roads eventually lead to
  // _openUrl
  const int hashMark = 0x23;
  const int questionMark = 0x3f;
  int fragmentStart = path.length;
  int queryStart = path.length;
  for (int i = path.length - 1; i >= 0; i--) {
    var char = path.codeUnitAt(i);
    if (char == hashMark) {
      fragmentStart = i;
      queryStart = i;
    } else if (char == questionMark) {
      queryStart = i;
    }
  }
  String? query;
  if (queryStart < fragmentStart) {
    query = path.substring(queryStart + 1, fragmentStart);
    path = path.substring(0, queryStart);
  }
  Uri uri =
      Uri(scheme: 'http', host: host, port: port, path: path, query: query);
  return _openUrl(method, uri);
}