1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* This file is part of Digest Header
 *
 * Digest Header is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Digest Header is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Digest Header  If not, see <http://www.gnu.org/licenses/>.
 */

//! The `use_hyper` module provides useful Types and Traits for interacting with `Hyper` with
//! `Digest`s.

use futures::{Future, Stream};
use hyper::{Body, Chunk, Request};
use hyper::header::{Formatter, Header, Raw};
use hyper::error::{Error as HyperError, Result as HyperResult};

use std::error::Error as StdError;
use std::fmt;
use std::str::from_utf8;

use {Digest, ShaSize};
use prelude::*;

/// The Error type for using Digests with Hyper.
#[derive(Debug)]
pub enum Error {
    /// Hyper had an error
    Hyper(HyperError),
    /// There was no body in the Request.
    NoBody,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Hyper(ref hyper_error) => write!(f, "Hyper: {}", hyper_error),
            Error::NoBody => write!(f, "No body was found in the Request."),
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Hyper(ref hyper_error) => hyper_error.description(),
            Error::NoBody => "No body was found in the Request",
        }
    }
}

impl From<HyperError> for Error {
    fn from(e: HyperError) -> Self {
        Error::Hyper(e)
    }
}

/// The `DigestHeader` is used to create and verify Digests on Hyper requests.
///
/// # Example of Creation
///
/// ```rust
/// # extern crate digest_headers;
/// # extern crate hyper;
/// #
/// # use digest_headers::{Digest, ShaSize};
/// # use digest_headers::prelude::*;
/// # use digest_headers::use_hyper::{Error, DigestHeader};
/// # use hyper::{Method, Request};
/// #
/// # fn main() {
/// #     run().unwrap();
/// # }
/// #
/// # fn run() -> Result<(), Error> {
/// let uri = "http://example.com".parse().unwrap();
/// let json = r#"{"Library":"Hyper"}"#;
/// let digest = Digest::new(json.as_bytes(), ShaSize::TwoFiftySix);
///
/// let mut req: Request = Request::new(Method::Post, uri);
/// req.headers_mut().set(DigestHeader(digest));
/// req.set_body(json);
/// # Ok(())
/// # }
/// ```
///
/// # Example of Verification
/// ```rust
/// # extern crate digest_headers;
/// # extern crate futures;
/// # extern crate hyper;
/// #
/// # use digest_headers::{Digest, ShaSize};
/// # use digest_headers::prelude::*;
/// # use digest_headers::use_hyper::{Error, DigestHeader};
/// # use futures::{Future, Stream};
/// # use hyper::{Method, Request};
/// #
/// fn handle_request(mut req: Request) {
///     let digest_header = req
///         .headers_mut()
///         .remove::<DigestHeader>()
///         .unwrap();
///
///     let digest_header = digest_header.0;
///
///     // Don't actually use `.wait()` in real code.
///     req.body()
///         .concat2()
///         .and_then(|body| {
///             assert!(digest_header.verify(&body).is_ok());
///
///             Ok(())
///         })
///         .wait()
///         .unwrap();
/// }
/// #
/// # fn main() {
/// #     let uri = "http://example.com".parse().unwrap();
/// #     let json = r#"{"Library":"Hyper"}"#;
/// #     let digest = Digest::new(json.as_bytes(), ShaSize::TwoFiftySix);
/// #
/// #     let mut req: Request = Request::new(Method::Post, uri);
/// #     req.headers_mut().set(DigestHeader(digest));
/// #     req.set_body(json);
/// #     handle_request(req);
/// # }
/// ```
#[derive(Clone)]
pub struct DigestHeader(pub Digest);

impl DigestHeader {
    pub fn into_digest(self) -> Digest {
        self.0
    }

    pub fn digest_string(&self) -> String {
        self.0.as_string()
    }
}

impl Header for DigestHeader {
    fn header_name() -> &'static str {
        "Digest"
    }

    fn parse_header(raw: &Raw) -> HyperResult<Self> {
        if let Some(one) = raw.one() {
            if let Ok(s) = from_utf8(one) {
                let digest = s.parse::<Digest>().map_err(|_| HyperError::Header)?;

                return Ok(DigestHeader(digest));
            }
        }

        Err(HyperError::Header)
    }

    fn fmt_header(&self, f: &mut Formatter) -> fmt::Result {
        f.fmt_line(&self.0)
    }
}

impl IntoDigest for Body {
    type Item = Chunk;
    type Error = Error;

    fn into_digest(self, sha_size: ShaSize) -> Result<(Self::Item, Digest), Self::Error> {
        let full_body = self.concat2().wait()?;

        let digest = Digest::new(&full_body, sha_size);

        Ok((full_body, digest))
    }
}

impl AsDigest for Request<Body> {
    type Error = Error;

    /// We need to consume the body Stream in order to access it, but we can set the body again
    /// after we've concatenated the whole body together, since getting a Digest is
    /// non-destructive.
    ///
    /// Do not call this method from the context of an asyncronous executor unless you are certain
    /// that the body in the Request is a single item. It will block waiting for all items.
    fn as_digest(self, sha_size: ShaSize) -> Result<(Self, Digest), Self::Error> {
        let (method, uri, http_version, headers, body) = self.deconstruct();

        let (body, digest) = body.into_digest(sha_size)?;

        let mut req = Request::new(method, uri);

        *req.headers_mut() = headers;

        req.set_version(http_version);
        req.set_body(body);

        Ok((req, digest))
    }
}

impl WithDigest for Request<Body> {
    /// We need to consume the body Stream in order to access it, but we can set the body again
    /// after we've concatenated the whole body together, since getting a Digest is
    /// non-destructive.
    ///
    /// Do not call this method from the context of an asyncronous executor unless you are certain
    /// that the body in the Request is a single item. It will block waiting for all items.
    fn with_digest(self, sha_size: ShaSize) -> Result<Self, Self::Error> {
        let (mut req, digest) = self.as_digest(sha_size)?;

        req.headers_mut().set(DigestHeader(digest));

        Ok(req)
    }
}

#[cfg(test)]
mod tests {
    use futures::{Future, Stream};
    use hyper::{Body, Method, Request};
    use hyper::header::{ContentLength, ContentType};
    use tokio_core::reactor::Core;

    use super::DigestHeader;
    use ShaSize;
    use prelude::*;

    #[test]
    fn add_digest_to_request_256() {
        add_digest_to_request(ShaSize::TwoFiftySix);
    }

    #[test]
    fn add_digest_to_request_384() {
        add_digest_to_request(ShaSize::ThreeEightyFour);
    }

    #[test]
    fn add_digest_to_request_512() {
        add_digest_to_request(ShaSize::FiveTwelve);
    }

    fn add_digest_to_request(sha_size: ShaSize) {
        let mut core = Core::new().unwrap();
        let uri = "http://example.com".parse().unwrap();
        let json = r#"{"Library":"Hyper"}"#;

        let mut req: Request<Body> = Request::new(Method::Post, uri);

        req.set_body(json);
        req.headers_mut().set(ContentType::json());
        req.headers_mut().set(ContentLength(json.len() as u64));

        let req = req.with_digest(sha_size);

        assert!(req.is_ok());

        // Stop Building request, start handling request

        let mut req = req.unwrap();

        let digest_header = req.headers_mut().remove::<DigestHeader>();

        assert!(digest_header.is_some());

        let digest_header = digest_header.unwrap();
        let digest_header = digest_header.into_digest();

        // Create a Future that will verify the digest upon reception
        let fut = req.body().concat2().and_then(|body| {
            assert!(digest_header.verify(&body).is_ok());

            Ok(())
        });

        core.run(fut).unwrap();
    }
}