simple-server
simple-server copied to clipboard
Content-Length or Transfer-Encoding header (RFC 7230)
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?
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?
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
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