Skip to content

Conversation

@richardwhiuk
Copy link
Contributor

In Hyper 0.11, the following code will panic:

use hyper::header::{Authorization, Basic, Bearer, Headers};
fn main() {
    let mut headers = Headers::default();
    let basic = Basic { username: "richard".to_string(), password: None };
    headers.set::<Authorization<Basic>>(Authorization(basic));
    println!("Auth: {:?}", headers.get::<Authorization<Bearer>>());
}

This is because std::any::TypeId::of<Authorization<Basic>> and
std::any::TypeId::of<Authorization<Bearer>> don't match, but the header
name, Authorization does.

We provide a .safe_get::<Authorization<Bearer>> to workaround this issue.

In Hyper 0.11, the following code will panic:

    use hyper::header::{Authorization, Basic, Bearer, Headers};
    fn main() {
        let mut headers = Headers::default();
        let basic = Basic { username: "richard".to_string(), password: None };
        headers.set::<Authorization<Basic>>(Authorization(basic));
        println!("Auth: {:?}", headers.get::<Authorization<Bearer>>());
    }

This is because `std::any::TypeId::of<Authorization<Basic>>` and
`std::any::TypeId::of<Authorization<Bearer>>` don't match, but the header
name, `Authorization` does.

We provide a `.safe_get::<Authorization<Bearer>>` to workaround this issue.

Signed-off-by: Richard Whitehouse <[email protected]>
Signed-off-by: Richard Whitehouse <[email protected]>
Copy link
Contributor

@mthebridge mthebridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One very minor inline comment to prove Iw as paying attention... Looks good otherwise.

Signed-off-by: Richard Whitehouse <[email protected]>
Signed-off-by: Richard Whitehouse <[email protected]>
@richardwhiuk
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Dec 12, 2019
90: Handle Hyper 0.11's Headers implementation r=richardwhiuk a=richardwhiuk

In Hyper 0.11, the following code will panic:

    use hyper::header::{Authorization, Basic, Bearer, Headers};
    fn main() {
        let mut headers = Headers::default();
        let basic = Basic { username: "richard".to_string(), password: None };
        headers.set::<Authorization<Basic>>(Authorization(basic));
        println!("Auth: {:?}", headers.get::<Authorization<Bearer>>());
    }

This is because `std::any::TypeId::of<Authorization<Basic>>` and
`std::any::TypeId::of<Authorization<Bearer>>` don't match, but the header
name, `Authorization` does.

We provide a `.safe_get::<Authorization<Bearer>>` to workaround this issue.

Co-authored-by: Richard Whitehouse <[email protected]>
@richardwhiuk
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Dec 12, 2019
90: Handle Hyper 0.11's Headers implementation r=richardwhiuk a=richardwhiuk

In Hyper 0.11, the following code will panic:

    use hyper::header::{Authorization, Basic, Bearer, Headers};
    fn main() {
        let mut headers = Headers::default();
        let basic = Basic { username: "richard".to_string(), password: None };
        headers.set::<Authorization<Basic>>(Authorization(basic));
        println!("Auth: {:?}", headers.get::<Authorization<Bearer>>());
    }

This is because `std::any::TypeId::of<Authorization<Basic>>` and
`std::any::TypeId::of<Authorization<Bearer>>` don't match, but the header
name, `Authorization` does.

We provide a `.safe_get::<Authorization<Bearer>>` to workaround this issue.

Co-authored-by: Richard Whitehouse <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 12, 2019

Build succeeded

@bors bors bot merged commit ed5b6a9 into v2 Dec 12, 2019
richardwhiuk added a commit to OpenAPITools/openapi-generator that referenced this pull request Jan 5, 2020
[Rust Server] Fix panic handling headers

If we have an API which has multiple auth types, we may panic. This is because
in Hyper 0.11, the following code will panic:

```
use hyper::header::{Authorization, Basic, Bearer, Headers};
fn main() {
    let mut headers = Headers::default();
    let basic = Basic { username: "richard".to_string(), password: None };
    headers.set::<Authorization<Basic>>(Authorization(basic));
    println!("Auth: {:?}", headers.get::<Authorization<Bearer>>());
}
```

as it mixes up an `Authorization<Basic>` and `Authorization<Bearer>` as both
have `Authorization:` as the header name.

This is fixed by using `swagger::SafeHeaders` added in
Metaswitch/swagger-rs#90
@richardwhiuk richardwhiuk deleted the safe_headers branch March 12, 2020 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants