weihanglo / sfz

A simple static file serving command-line tool written in Rust.
https://crates.io/crates/sfz
Apache License 2.0
400 stars 30 forks source link

Set Content-Length when possible #103

Closed henry40408 closed 2 years ago

henry40408 commented 2 years ago

Currently master branch de0761c doesn't set Content-Length header when compression is disabled:

$ curl -v http://localhost:5000/Cargo.toml > /dev/null
*   Trying 127.0.0.1:5000...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /Cargo.toml HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< server: sfz/0.7.1
< cache-control: public, max-age=0
< last-modified: Wed, 21 Sep 2022 15:41:00 GMT
< etag: "1663774860-1535"
< accept-ranges: bytes
< content-type: text/x-toml; charset=utf-8
< transfer-encoding: chunked 👈 no Content-Length header so Transfer-Encoding is enabled
< date: Wed, 28 Sep 2022 14:27:20 GMT
<
{ [1547 bytes data]
100  1535    0  1535    0     0   151k      0 --:--:-- --:--:-- --:--:-- 1499k
* Connection #0 to host localhost left intact

This pull request 12cf78d adds Content-Length when possible

$ curl -v http://localhost:5000/Cargo.toml > /dev/null
*   Trying 127.0.0.1:5000...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /Cargo.toml HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< server: sfz/0.7.1
< cache-control: public, max-age=0
< last-modified: Wed, 21 Sep 2022 15:41:00 GMT
< etag: "1663774860-1535"
< accept-ranges: bytes
< content-type: text/x-toml; charset=utf-8
< content-length: 1535 👈 Content-Length is enabled so Transfer-Encoding is disabled and client can know the content length
< date: Wed, 28 Sep 2022 14:28:53 GMT
<
{ [1535 bytes data]
100  1535  100  1535    0     0   114k      0 --:--:-- --:--:-- --:--:--  749k
* Connection #0 to host localhost left intact

I'll rebase #101 after this pull request is merged