Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status 204 No Content: Not spec conform #60

Closed
dhenneke opened this issue Oct 23, 2016 · 1 comment
Closed

Status 204 No Content: Not spec conform #60

dhenneke opened this issue Oct 23, 2016 · 1 comment
Assignees

Comments

@dhenneke
Copy link

dhenneke commented Oct 23, 2016

When responding to a HttpRequest with a 204 No Content status and no body, a body containing the number "0" is returned. This confuses some proxy servers (in case of keep alive). According to the spec:

The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
https://tools.ietf.org/html/rfc2616#section-10.2.5

We are using the following example code:

import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;

void main() {
  var handler = const shelf.Pipeline().addMiddleware(shelf.logRequests())
      .addHandler(_echoRequest);

  io.serve(handler, '0.0.0.0', 8080).then((server) {
    print('Serving at http://${server.address.host}:${server.port}');
  });
}

shelf.Response _echoRequest(shelf.Request request) {
  return new shelf.Response(204);
}

Wireshark shows the malformed result:
image

We debugged the cause:

  1. We don't supply a body to new shelf.Response(204) so it internally creates an empty array as body.
  2. On Response generation (in dart:io) the "Transfer-Encoding" header defaults to "chunked" and thus the framework adds the 0 as initial chunk length of 0.

We managed to circumvent the problem by manually forcing the "Transfer-Encoding" header to "identity" for the 204 status results in a shelf middleware. However this issue should be fixed in order to be conformant to RFC 2616. But we are not sure if the issue is located in shelf or in dart:io.

Update: This is issue is also present for new Response.notModified(); (i.e. status code 304).

@nex3
Copy link
Member

nex3 commented Oct 24, 2016

This is really an issue in dart:io. It shouldn't force empty-bodied responses to be chunked, especially when they have a status code that disallows that. I've filed dart-lang/sdk#27660 to track that.

We can still work around this in shelf, though, so I'm keeping this issue open.

@nex3 nex3 self-assigned this Oct 24, 2016
nex3 added a commit that referenced this issue Oct 24, 2016
This also automatically sets the content-length header when possible,
and works around dart-lang/sdk#27660.

Closes #60
@nex3 nex3 closed this as completed in 57b5e48 Oct 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants