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

illegal HTTP sent back #2392

Closed
weissi opened this issue Jun 10, 2020 · 0 comments · Fixed by #2404
Closed

illegal HTTP sent back #2392

weissi opened this issue Jun 10, 2020 · 0 comments · Fixed by #2404
Labels
bug Something isn't working
Projects

Comments

@weissi
Copy link
Contributor

weissi commented Jun 10, 2020

With my echo server

import Vapor

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

let app = Application(env)
defer { app.shutdown() }

app.on(.POST, "echo")  { request -> Response in
    return Response(body: .init(stream: { writer in
        request.body.drain { body in
            switch body {
            case .buffer(let buffer):
                return writer.write(.buffer(buffer))
            case .error(let error):
                return writer.write(.error(error))
            case .end:
                return writer.write(.end)
            }
        }
    }, count: 0))
}

try app.run()

Even when driving it correctly, vapor responds with illegal HTTP.

Repro:

  • Start the above program
  • echo -ne 'POST /echo HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n1\r\nx\r\n0\r\n\r\n' | nc localhost 8080

Expected:

HTTP/1.1 200 OK
transfer-encoding: chunked
connection: keep-alive
date: Wed, 10 Jun 2020 22:26:51 GMT

1\r\n
x\r\n
0\r\n
\r\n

Actual:

HTTP/1.1 200 OK
content-length: 0
connection: keep-alive
date: Wed, 10 Jun 2020 22:26:51 GMT

x

Note that this is illegal HTTP. content-length is set to 0 and then we're still emitting characters. That should not be possible. For a user-facing API, there should be two options:

  1. user supplies the content length in which case Vapor should validate and crash with good message if there's more or fewer bytes
  2. user does not supply the content length in which case Vapor should automatically set transfer-encoding: chunked (or not add content-length: 0 which will make NIO add transfer-encoding: chunked).
@tanner0101 tanner0101 added the bug Something isn't working label Jun 23, 2020
@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Jun 23, 2020
Vapor 4 automation moved this from To Do to Done Jun 24, 2020
tanner0101 added a commit that referenced this issue Jun 24, 2020
* echo server

* warning

* fix tests

* drain unhandled request after response end

* rm commented code

* add handler tests

* test EOF framing, fixes #2391

* test incorrect response body stream count, fixes #2392

* http fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Vapor 4
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants