http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.46k stars 120 forks source link

surf::Response does not implement Index yet #184

Closed jbr closed 4 years ago

jsha commented 4 years ago

I think this is describing the use of Index for looking up headers in the response, e.g. response["Content-Length"]. Is that right?

I think it might be preferable not to implement Index, since it encourages users to risk a panic based on untrusted data. For instance, if I write an HTTP client that tries to examine response["Content-Length"], all the server has to do to crash my client is send a response without that header field.

The safe way to use Index here would be for the user to check if the header exists first:

if let Some(values) = response.header('Content-Length') {
  response['Content-Length']
}

But by then it's just as easy to use values.

jbr commented 4 years ago

It is important for surf Response and Request to have a similar interface to tide and the underlying http-types for the principle of least surprise. Currently, Index is an exception to this, which is what this issue represents

Fishrock123 commented 4 years ago

https://github.com/http-rs/surf/commit/c22693e2e0a42e0d975fbc3c508b86c677256779 fixed this.