http-rs / http-rs/tide

Possible encoding regression

Open
#514 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
5.1k
Forks
329
PR merge metrics
No merged PRs in 30d

Description

Thanks again for all your great work! ❤️

It seems like between `tide` 0.6.0 and 0.8.1 there might have been a regression in encoding behavior for responses. Previously, `tide` set the content type headers on simple `.get()` endpoints to `content-type: text/plain; charset=utf-8`, in a newer version the headers say `content-type: text/plain`. This can cause non-ascii characters to display incorrectly when writing code based on the hello world example in the readme and examples directory.

Users might want to have the ability to choose their own charsets, but perhaps a default of utf-8 could make sense - it is fairly common, and it is the default encoding Rust expects strings to be in. The two examples below use an emoji to highlight the issue, but this seems to apply to other non-ascii characters too, including some fairly common diacritics in other languages.

---

Old code:

```rust
// tide = "0.6.0"
use std::env;

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
let port = env::var("PORT").unwrap();

app.at("/").get(|_| async move { "Hello from Rust! 🦀\n" });

app.listen(format!("0.0.0.0:{}", port)).await?;
Ok(())
}
```

Result:

![Screenshot 2020-05-17 at 22 02 18](https://user-images.githubusercontent.com/2943750/82157445-21e84380-988a-11ea-8aec-037b268439b2.png)

`curl`

```
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: text/plain; charset=utf-8
< transfer-encoding: chunked
< date: Sun, 17 May 2020 19:00:20 GMT
<
Hello from Rust! 🦀
* Connection #0 to host localhost left intact
* Closing connection 0
```

---

New code

```rust
// tide = "0.8.1"
use std::env;

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
let port = env::var("PORT").unwrap();

app.at("/").get(|_| async { Ok("Hello from Rust! 🦀\n") });

app.listen(format!("0.0.0.0:{}", port)).await?;
Ok(())
}
```

Result

![Screenshot 2020-05-17 at 22 03 59](https://user-images.githubusercontent.com/2943750/82157493-6c69c000-988a-11ea-9c5b-a799e501df7f.png)

`curl`

```
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-length: 22
< date: Sun, 17 May 2020 19:03:50 GMT
< content-type: text/plain
<
Hello from Rust! 🦀
* Connection #0 to host localhost left intact
* Closing connection 0
```

(Note that in the latter example, my terminal shows the emoji correctly. Perhaps it assumes utf-8, whereas browser defaults to something else?)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.