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

HTTP/1.0 request without "Keep-Alive", res.end() not close connection. #596

Closed
AndrewTsao opened this issue Jul 5, 2015 · 3 comments
Closed

Comments

@AndrewTsao
Copy link

extern crate hyper;
extern crate env_logger;

use std::io::Write;

use hyper::Server;
use hyper::server::{Request, Response};
use hyper::net::Fresh;
use hyper::header::{ContentLength};

fn hello(_: Request, mut res: Response<Fresh>) {
    let body = b"Hello World!";
    res.send(body).unwrap();
}

fn main() {
    env_logger::init().unwrap();
    println!("start listening on 3000");
    Server::http("127.0.0.1:3000").unwrap().handle_threads(hello, 3).unwrap();
}

I started server

RUST_LOG=trace ./target/debug/web-server

then I use `telnet command start a http request like this:

andi@andi-box:~/rust/web-server$ telnet localhost 3000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.0 200 OK
Date: Sun, 05 Jul 2015 15:09:37 GMT
Content-Length: 12

Hello World!

but connection not closed.

DEBUG:hyper::server: Incoming stream
TRACE:hyper::buffer: read_into_buf pos=0, cap=4096
TRACE:hyper::buffer: slicing (0, 16, 4096)
TRACE:hyper::buffer: read_into_buf pos=16, cap=4096
TRACE:hyper::buffer: slicing (0, 18, 4096)
DEBUG:hyper::server::request: Request Line: Get AbsolutePath("/") Http10
DEBUG:hyper::server::request: Headers { }
TRACE:hyper::http: should_keep_alive( Http10, None )
TRACE:hyper::header: Headers.set( "Content-Length", ContentLength(12) )
DEBUG:hyper::server::response: writing head: Http10 Ok
TRACE:hyper::header: Headers.set( "Date", Date(HttpDate(Tm { tm_sec: 17, tm_min: 12, tm_hour: 15, tm_mday: 5, tm_mon: 6, tm_year: 115, tm_wday: 0, tm_yday: 185, tm_isdst: 0, tm_utcoff: 0, tm_nsec: 280907766 })) )
DEBUG:hyper::server::response: headers [
Headers { Content-Length: 12, Date: Sun, 05 Jul 2015 15:12:17 GMT, }]
DEBUG:hyper::server::response: write 12 bytes
TRACE:hyper::server::response: ending
TRACE:hyper::http: should_keep_alive( Http10, None )
DEBUG:hyper::server: keep_alive = true for 127.0.0.1:46167
TRACE:hyper::buffer: read_into_buf pos=0, cap=4096

The server trace log show connection is keeping alive. why?

@reem
Copy link
Contributor

reem commented Jul 5, 2015

You must send Connection: Close to signal to the client that they should not use keep alive on this connection. See the relevant section of the spec: https://tools.ietf.org/html/rfc7230#section-6.6

@reem
Copy link
Contributor

reem commented Jul 5, 2015

If we immediately closed the connection then the client may not be able to fully read the final response, as noted in the spec.

@seanmonstar
Copy link
Member

No, as Http 1.0, it should close the connection unless keepalive was passed
by the client. It's a bug in the should_keep_alive function.

On Sun, Jul 5, 2015, 12:34 PM Jonathan Reem notifications@github.com
wrote:

If we immediately closed the connection then the client may not be able to
fully read the final response, as noted in the spec.


Reply to this email directly or view it on GitHub
#596 (comment).

@AndrewTsao AndrewTsao changed the title res.end() not close connection. HTTP/1.0 request without "Keep-Alive", res.end() not close connection. Jul 6, 2015
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

3 participants