Site-www: Use browser access to trigger two requests

Created on 6 Jul 2020  ·  12Comments  ·  Source: dart-lang/site-www

dart --version
Dart VM version: 2.6.1 (Mon Nov 11 13:12:24 2019 +0100) on "macos_x64"
import 'dart:io';

Future main() async {
  var server = await HttpServer.bind(
    InternetAddress('0.0.0.0'),
    4000,
  );
  print('Listening on localhost:${server.port}');

  await for (HttpRequest request in server) {
  // if you use browser will print twice
 //  if you use curl will print once
    print('request ${request.method}'); // why run twice???
    request.response
      ..write('Hello, world!')
      ..close();
  }
}
beginner help wanted needs-info

Most helpful comment

Hi @cuichuanteng! The reason you're seeing two requests with the browser is because the browser actualy performs two separate requests - one for the page itself (/) and one for the page's favicon (/favicon.ico). You can verify this by modifying the code above to also print out the request's path:

import 'dart:io';

Future main() async {
  var server = await HttpServer.bind(
    InternetAddress('0.0.0.0'),
    4000,
  );
  print('Listening on localhost:${server.port}');

  await for (HttpRequest request in server) {
    print('request ${request.method}, ${request.uri.path}');
    request.response
      ..write('Hello, world!')
      ..close();
  }
}

Outputs this when using the browser:

Listening on localhost:4000
request GET, /
request GET, /favicon.ico

If you want to know more about favicons, check out this excellent wikipedia article. If you want to disable the favicon request, see this stackoverflow question.

All 12 comments

Could you please clarify what the problem is and what page is affected? Thanks!

This is a sample code and all the code. After running, visit http://127.0.0.1:4000 through the chrome browser, the console will see 2 printouts, but if you use curl http://127.0.0.1: 4000 will only output once.

@kwalrath

Looks like the affected files are https://dart.dev/tutorials/server/httpserver (src/_tutorials/server/httpserver.md) and possibly the following Dart files:

examples/httpserver/test/httpserver_test.dart
examples/httpserver/bin/hello_world_server_secure.dart
examples/httpserver/bin/hello_world_server.dart

看起来受影响的文件是https://dart.dev/tutorials/server/httpserver(src / _tutorials / server / httpserver.md)以及以下Dart文件:

示例/ httpserver /测试/httpserver_test.dart
示例/httpserver/bin/hello_world_server_secure.dart
示例/httpserver/bin/hello_world_server.dart

Can you reproduce this problem? Is this a bug? Or did I write it wrong?

Could you create a gist with your code and exact instructions on how you ran it and used the browser and curl? That way we can duplicate your work.

@kwalrath demo repository:https://github.com/cuichuanteng/dart-httpserver-demo.git

Thanks the video helps!

Hi @cuichuanteng! The reason you're seeing two requests with the browser is because the browser actualy performs two separate requests - one for the page itself (/) and one for the page's favicon (/favicon.ico). You can verify this by modifying the code above to also print out the request's path:

import 'dart:io';

Future main() async {
  var server = await HttpServer.bind(
    InternetAddress('0.0.0.0'),
    4000,
  );
  print('Listening on localhost:${server.port}');

  await for (HttpRequest request in server) {
    print('request ${request.method}, ${request.uri.path}');
    request.response
      ..write('Hello, world!')
      ..close();
  }
}

Outputs this when using the browser:

Listening on localhost:4000
request GET, /
request GET, /favicon.ico

If you want to know more about favicons, check out this excellent wikipedia article. If you want to disable the favicon request, see this stackoverflow question.

Thank you, @theacodes!

Everyone thanks! @kwalrath @theacodes

Was this page helpful?
0 / 5 - 0 ratings