|
77 | 77 | //! }
|
78 | 78 | //! ```
|
79 | 79 | use std::borrow::{Cow, ToOwned};
|
| 80 | +#[cfg(feature = "compat")] |
| 81 | +use std::convert::From; |
80 | 82 | use std::iter::{FromIterator, IntoIterator};
|
81 | 83 | use std::{mem, fmt};
|
82 | 84 |
|
| 85 | +#[cfg(feature = "compat")] |
| 86 | +use http_types; |
| 87 | + |
83 | 88 | use unicase::Ascii;
|
84 | 89 |
|
85 | 90 | use self::internals::{Item, VecMap, Entry};
|
@@ -546,6 +551,54 @@ impl fmt::Debug for Headers {
|
546 | 551 | }
|
547 | 552 | }
|
548 | 553 |
|
| 554 | +#[cfg(feature = "compat")] |
| 555 | +impl From<http_types::HeaderMap> for Headers { |
| 556 | + fn from(mut header_map: http_types::HeaderMap) -> Headers { |
| 557 | + let mut headers = Headers::new(); |
| 558 | + for (name, mut value_drain) in header_map.drain() { |
| 559 | + if let Some(first_value) = value_drain.next() { |
| 560 | + let mut raw: Raw = first_value.as_bytes().into(); |
| 561 | + for value in value_drain { |
| 562 | + raw.push(value.as_bytes()); |
| 563 | + } |
| 564 | + headers.append_raw(name.as_str().to_string(), raw); |
| 565 | + } |
| 566 | + } |
| 567 | + headers |
| 568 | + } |
| 569 | +} |
| 570 | + |
| 571 | +#[cfg(feature = "compat")] |
| 572 | +impl From<Headers> for http_types::HeaderMap { |
| 573 | + fn from(headers: Headers) -> http_types::HeaderMap { |
| 574 | + let mut header_map = http_types::HeaderMap::new(); |
| 575 | + for header in headers.iter() { |
| 576 | + let entry = header_map.entry(header.name()) |
| 577 | + .expect("attempted to convert invalid header name"); |
| 578 | + let mut value_iter = header.raw().iter().map(|line| { |
| 579 | + http_types::header::HeaderValue::from_bytes(line) |
| 580 | + .expect("attempted to convert invalid header value") |
| 581 | + }); |
| 582 | + match entry { |
| 583 | + http_types::header::Entry::Occupied(mut occupied) => { |
| 584 | + for value in value_iter { |
| 585 | + occupied.append(value); |
| 586 | + } |
| 587 | + }, |
| 588 | + http_types::header::Entry::Vacant(vacant) => { |
| 589 | + if let Some(first_value) = value_iter.next() { |
| 590 | + let mut occupied = vacant.insert_entry(first_value); |
| 591 | + for value in value_iter { |
| 592 | + occupied.append(value); |
| 593 | + } |
| 594 | + } |
| 595 | + } |
| 596 | + } |
| 597 | + } |
| 598 | + header_map |
| 599 | + } |
| 600 | +} |
| 601 | + |
549 | 602 | /// An `Iterator` over the fields in a `Headers` map.
|
550 | 603 | #[allow(missing_debug_implementations)]
|
551 | 604 | pub struct HeadersItems<'a> {
|
@@ -940,6 +993,29 @@ mod tests {
|
940 | 993 | assert_ne!(headers1, headers2);
|
941 | 994 | }
|
942 | 995 |
|
| 996 | + #[test] |
| 997 | + #[cfg(feature = "compat")] |
| 998 | + fn test_compat() { |
| 999 | + use http_types; |
| 1000 | + |
| 1001 | + let mut orig_hyper_headers = Headers::new(); |
| 1002 | + orig_hyper_headers.set(ContentLength(11)); |
| 1003 | + orig_hyper_headers.set(Host::new("foo.bar", None)); |
| 1004 | + orig_hyper_headers.append_raw("x-foo", b"bar".to_vec()); |
| 1005 | + orig_hyper_headers.append_raw("x-foo", b"quux".to_vec()); |
| 1006 | + |
| 1007 | + let mut orig_http_headers = http_types::HeaderMap::new(); |
| 1008 | + orig_http_headers.insert(http_types::header::CONTENT_LENGTH, "11".parse().unwrap()); |
| 1009 | + orig_http_headers.insert(http_types::header::HOST, "foo.bar".parse().unwrap()); |
| 1010 | + orig_http_headers.append("x-foo", "bar".parse().unwrap()); |
| 1011 | + orig_http_headers.append("x-foo", "quux".parse().unwrap()); |
| 1012 | + |
| 1013 | + let conv_hyper_headers: Headers = orig_http_headers.clone().into(); |
| 1014 | + let conv_http_headers: http_types::HeaderMap = orig_hyper_headers.clone().into(); |
| 1015 | + assert_eq!(orig_hyper_headers, conv_hyper_headers); |
| 1016 | + assert_eq!(orig_http_headers, conv_http_headers); |
| 1017 | + } |
| 1018 | + |
943 | 1019 | #[cfg(feature = "nightly")]
|
944 | 1020 | #[bench]
|
945 | 1021 | fn bench_headers_new(b: &mut Bencher) {
|
|
0 commit comments