simple-server icon indicating copy to clipboard operation
simple-server copied to clipboard

Content-Length or Transfer-Encoding header (RFC 7230)

Open jbolila opened this issue 8 years ago • 3 comments

The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field.

https://tools.ietf.org/html/rfc7230#section-3.3

Without this some tools don't work well, for example wrk.

let data = String::from_utf8_lossy(request.body()).into_owned();
let body = format!(r#"{{"data": {}, "url": "{}"}}"#, request.uri().path(), data);
Ok(response
    .header("content-length", body.len().to_string().as_str())   //  ~:o
    .body(body.into_bytes())?)

Is valid code, although has some issues, the first one is the header is not set (or any other header).

< HTTP/1.1 200 OK
* no chunk, no close, no size. Assume close to signal end

Compared with the minimal out of the box set by the http module of node:

< HTTP/1.1 200 OK
< Date: Sun, 29 Oct 2017 08:19:45 GMT
< Connection: keep-alive
< Content-Length: 21

Can these values be set automatically?

jbolila avatar Oct 29 '17 08:10 jbolila

I believe https://github.com/steveklabnik/simple-server/pull/90 has fixed this, by setting it automatically. @jbolila , any chance you could give master a try and see if wrk works?

steveklabnik avatar Mar 19 '18 13:03 steveklabnik

On master, examples/server.rs's response now looks like

< HTTP/1.1 200 OK
< content-length: 11
< 

so it does include the content length and curl doesn't produce * no chunk, no close, no size. Assume close to signal end anymore.

But the Date and Connection fields are still missing. Date would be nice and easy to add. I'm not sure what would need to change to make keep-alive work though.


about wrk:

In my tests, since https://github.com/wg/wrk/commit/522ec607f824f1928e3e0d0f02bf126117a93cb2 (~3 yrs ago) wrk will compute Req/Sec even without the Content-Length header.

The server will invariably crash after wrk finishes though. Cause is a broken pipe error here

https://github.com/steveklabnik/simple-server/blob/10103f59775591a470dc4d0275cfe54e635e9a5c/src/lib.rs#L265-L266

scurest avatar Mar 24 '18 20:03 scurest

Oh. but there still isn't Content-Length for these errors:

https://github.com/steveklabnik/simple-server/blob/10103f59775591a470dc4d0275cfe54e635e9a5c/src/lib.rs#L318-L321

https://github.com/steveklabnik/simple-server/blob/10103f59775591a470dc4d0275cfe54e635e9a5c/src/lib.rs#L341-L343

https://github.com/steveklabnik/simple-server/blob/10103f59775591a470dc4d0275cfe54e635e9a5c/src/lib.rs#L378-L380

scurest avatar Mar 24 '18 21:03 scurest