Skip to content

dart:io HttpServer allows serving a 304 response with a body #26526

Open
@munificent

Description

The HTTP spec says 304 responses must not include a body, not even an empty one:

For response messages, whether or not a message-body is included with a message is dependent on both the request method and the response status code (section 6.1.1). All responses to the HEAD request method MUST NOT include a message-body, even though the presence of entity-header fields might lead one to believe they do. All 1xx (informational), 204 (no content), and 304 (not modified) responses MUST NOT include a message-body. All other responses do include a message-body, although it MAY be of zero length.

But dart:io will serve an empty body if you give it an empty stream:

If I run this:

import 'dart:async';
import 'dart:io';

main() async {
  var requestServer =
      await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 4040);
  await for (HttpRequest request in requestServer) {
    request.response.headers.chunkedTransferEncoding = true;
    request.response.statusCode = 304;
    await request.response.addStream(new Stream.empty());
    request.response.close();
  }
}

And then this:

$ printf "GET /index.html HTTP/1.1\n\n" | nc localhost 4040

I see:

HTTP/1.1 304 Not Modified
content-type: text/plain; charset=utf-8
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
transfer-encoding: chunked
x-content-type-options: nosniff

0

The "\n0\n" is wrong. It's an empty chunked body. It should just be:

HTTP/1.1 304 Not Modified
content-type: text/plain; charset=utf-8
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
transfer-encoding: chunked
x-content-type-options: nosniff

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-iotype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions