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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use super::Background;
use buf::SendBuf;
use flush::Flush;
use {Body, RecvBody};

use futures::future::Executor;
use futures::{Async, Future, Poll};
use h2;
use h2::client::{self, Builder, SendRequest};
use http::{self, Request, Response};
use tokio_io::{AsyncRead, AsyncWrite};
use tower_service::Service;

use std::marker::PhantomData;
use std::{error, fmt};

/// Exposes a request/response API on an h2 client connection..
pub struct Connection<T, E, S>
where
    S: Body,
{
    client: SendRequest<SendBuf<S::Data>>,
    executor: E,
    _p: PhantomData<(T, S)>,
}

/// In progress HTTP/2.0 client handshake.
pub struct Handshake<T, E, S>
where
    S: Body,
{
    inner: h2::client::Handshake<T, SendBuf<S::Data>>,
    executor: E,
}

/// Drives the sending of a request (and its body) until a response is received (i.e. the
/// initial HEADERS or RESET frames sent from the remote).
///
/// This is necessary because, for instance, the remote server may not respond until the
/// request body is fully sent.
pub struct ResponseFuture {
    inner: Inner,
}

/// ResponseFuture inner
enum Inner {
    /// Inner response future
    Inner(client::ResponseFuture),

    /// Failed to send the request
    Error(Option<Error>),
}

/// Errors produced by client `Connection` calls.
#[derive(Debug)]
pub struct Error {
    kind: Kind,
}

/// Error produced when performing an HTTP/2.0 handshake.
#[derive(Debug)]
pub enum HandshakeError {
    /// An error occurred when attempting to perform the HTTP/2.0 handshake.
    Proto(h2::Error),

    /// An error occured when attempting to execute a worker task
    Execute,
}

#[derive(Debug)]
enum Kind {
    Inner(h2::Error),
    Spawn,
}

// ===== impl Connection =====

impl<T, E, S> Connection<T, E, S>
where
    S: Body,
    S::Data: 'static,
    S::Error: Into<Box<dyn std::error::Error>>,
    E: Executor<Background<T, S>>,
    T: AsyncRead + AsyncWrite,
{
    /// Builds Connection on an H2 client connection.
    pub(crate) fn new(client: SendRequest<SendBuf<S::Data>>, executor: E) -> Self {
        let _p = PhantomData;

        Connection {
            client,
            executor,
            _p,
        }
    }

    /// Perform the HTTP/2.0 handshake, yielding a `Connection` on completion.
    pub fn handshake(io: T, executor: E) -> Handshake<T, E, S> {
        Handshake::new(io, executor, &Builder::default())
    }
}

impl<T, E, S> Clone for Connection<T, E, S>
where
    S: Body,
    E: Clone,
{
    fn clone(&self) -> Self {
        Connection {
            client: self.client.clone(),
            executor: self.executor.clone(),
            _p: PhantomData,
        }
    }
}

impl<T, E, S> Service<Request<S>> for Connection<T, E, S>
where
    S: Body + 'static,
    S::Data: 'static,
    S::Error: Into<Box<dyn std::error::Error>>,
    E: Executor<Background<T, S>>,
    T: AsyncRead + AsyncWrite,
{
    type Response = Response<RecvBody>;
    type Error = Error;
    type Future = ResponseFuture;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        self.client.poll_ready().map_err(Into::into)
    }

    fn call(&mut self, request: Request<S>) -> Self::Future {
        trace!("request: {} {}", request.method(), request.uri());

        // Split the request from the body
        let (parts, body) = request.into_parts();
        let request = http::Request::from_parts(parts, ());

        // If there is no body, then there is no point spawning a task to flush
        // it.
        let eos = body.is_end_stream();

        // Initiate the H2 request
        let res = self.client.send_request(request, eos);

        let (response, send_body) = match res {
            Ok(success) => success,
            Err(e) => {
                let e = Error {
                    kind: Kind::Inner(e),
                };
                let inner = Inner::Error(Some(e));
                return ResponseFuture { inner };
            }
        };

        if !eos {
            let flush = Flush::new(body, send_body);
            let res = self.executor.execute(Background::flush(flush));

            if let Err(_) = res {
                let e = Error { kind: Kind::Spawn };
                let inner = Inner::Error(Some(e));
                return ResponseFuture { inner };
            }
        }

        ResponseFuture {
            inner: Inner::Inner(response),
        }
    }
}

// ===== impl ResponseFuture =====

impl Future for ResponseFuture {
    type Item = Response<RecvBody>;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        use self::Inner::*;

        match self.inner {
            Inner(ref mut fut) => {
                let response = try_ready!(fut.poll());

                let (parts, body) = response.into_parts();
                let body = RecvBody::new(body);

                Ok(Response::from_parts(parts, body).into())
            }
            Error(ref mut e) => {
                return Err(e.take().unwrap());
            }
        }
    }
}

impl ResponseFuture {
    /// Returns the stream ID of the response stream, or `None` if this future
    /// does not correspond to a stream.
    pub fn stream_id(&self) -> Option<h2::StreamId> {
        match self.inner {
            Inner::Inner(ref rsp) => Some(rsp.stream_id()),
            _ => None,
        }
    }
}

// ===== impl Handshake =====

impl<T, E, S> Handshake<T, E, S>
where
    T: AsyncRead + AsyncWrite,
    S: Body,
    S::Data: 'static,
{
    /// Start an HTTP/2.0 handshake with the provided builder
    pub(crate) fn new(io: T, executor: E, builder: &Builder) -> Self {
        let inner = builder.handshake(io);

        Handshake { inner, executor }
    }
}

impl<T, E, S> Future for Handshake<T, E, S>
where
    T: AsyncRead + AsyncWrite,
    E: Executor<Background<T, S>> + Clone,
    S: Body,
    S::Data: 'static,
    S::Error: Into<Box<dyn std::error::Error>>,
{
    type Item = Connection<T, E, S>;
    type Error = HandshakeError;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let (client, connection) = try_ready!(self.inner.poll());

        // Spawn the worker task
        let task = Background::connection(connection);
        self.executor.execute(task).map_err(|err| {
            warn!("error handshaking: {:?}", err);
            HandshakeError::Execute
        })?;

        // Create an instance of the service
        let service = Connection::new(client, self.executor.clone());

        Ok(Async::Ready(service))
    }
}

// ===== impl Error =====

impl Error {
    pub fn reason(&self) -> Option<h2::Reason> {
        match self.kind {
            Kind::Inner(ref h2) => h2.reason(),
            _ => None,
        }
    }
}

impl From<h2::Error> for Error {
    fn from(src: h2::Error) -> Self {
        Error {
            kind: Kind::Inner(src),
        }
    }
}

impl From<h2::Reason> for Error {
    fn from(src: h2::Reason) -> Self {
        h2::Error::from(src).into()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            Kind::Inner(ref h2) => write!(f, "Error caused by underlying HTTP/2 error: {}", h2),
            Kind::Spawn => write!(f, "Error spawning background task"),
        }
    }
}

impl error::Error for Error {
    fn cause(&self) -> Option<&error::Error> {
        if let Kind::Inner(ref h2) = self.kind {
            Some(h2)
        } else {
            None
        }
    }

    fn description(&self) -> &str {
        match self.kind {
            Kind::Inner(ref h2) => h2.description(),
            Kind::Spawn => "error spawning worker task",
        }
    }
}

// ===== impl HandshakeError =====

impl From<h2::Error> for HandshakeError {
    fn from(src: h2::Error) -> Self {
        HandshakeError::Proto(src)
    }
}

impl fmt::Display for HandshakeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            HandshakeError::Proto(ref h2) => write!(
                f,
                "An error occurred while attempting to perform the HTTP/2 \
                 handshake: {}",
                h2
            ),
            HandshakeError::Execute => write!(
                f,
                "An error occurred while attempting to execute a worker \
                 task."
            ),
        }
    }
}

impl error::Error for HandshakeError {
    fn cause(&self) -> Option<&error::Error> {
        if let HandshakeError::Proto(ref h2) = *self {
            Some(h2)
        } else {
            None
        }
    }

    fn description(&self) -> &str {
        match *self {
            HandshakeError::Proto(_) => "error attempting to perform HTTP/2 handshake",
            HandshakeError::Execute => "error attempting to execute a worker task",
        }
    }
}