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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Event Stream protocol support utilities

use std::convert::TryInto;
use std::fmt::{Display, Formatter};
use std::marker::PhantomData;
use std::pin::Pin;

use crc32fast::Hasher;
use futures::task::{Context, Poll};
use futures::Stream;

use crate::error::RusotoError;
use crate::request::HttpResponse;
use crate::stream::ByteStream;

#[doc(hidden)]
pub trait DeserializeEvent: Sized {
    fn deserialize_event(event_type: &str, data: &[u8]) -> Result<Self, RusotoError<()>>;
}

#[derive(Debug, Eq, PartialEq)]
enum EventStreamParseError {
    UnexpectedEof,
    InvalidCrc,
    InvalidData(&'static str),
}

fn check_crc32(data: &[u8], ref_value: u32) -> Result<(), EventStreamParseError> {
    let mut hasher = Hasher::new();
    hasher.update(data);

    if hasher.finalize() != ref_value {
        Err(EventStreamParseError::InvalidCrc)
    } else {
        Ok(())
    }
}

fn read_slice<'a>(reader: &mut &'a [u8], size: usize) -> Result<&'a [u8], EventStreamParseError> {
    if reader.len() < size {
        return Err(EventStreamParseError::UnexpectedEof);
    }

    let slice = &reader[..size];
    *reader = &reader[size..];
    Ok(slice)
}

fn read_u8(reader: &mut &[u8]) -> Result<u8, EventStreamParseError> {
    let buf = read_slice(reader, std::mem::size_of::<u8>())?
        .try_into()
        .unwrap();
    Ok(u8::from_be_bytes(buf))
}

fn read_u16(reader: &mut &[u8]) -> Result<u16, EventStreamParseError> {
    let buf = read_slice(reader, std::mem::size_of::<u16>())?
        .try_into()
        .unwrap();
    Ok(u16::from_be_bytes(buf))
}

fn read_u32(reader: &mut &[u8]) -> Result<u32, EventStreamParseError> {
    let buf = read_slice(reader, std::mem::size_of::<u32>())?
        .try_into()
        .unwrap();
    Ok(u32::from_be_bytes(buf))
}

fn read_u64(reader: &mut &[u8]) -> Result<u64, EventStreamParseError> {
    let buf = read_slice(reader, std::mem::size_of::<u64>())?
        .try_into()
        .unwrap();
    Ok(u64::from_be_bytes(buf))
}

impl EventStreamParseError {
    fn eof_as_invalid(self) -> Self {
        match self {
            EventStreamParseError::UnexpectedEof => {
                EventStreamParseError::InvalidData("Malformed event: ended unexpectedly")
            }
            other => other,
        }
    }
}

impl std::error::Error for EventStreamParseError {}

impl Display for EventStreamParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            EventStreamParseError::UnexpectedEof => write!(f, "Expected additional data"),
            EventStreamParseError::InvalidCrc => write!(f, "CRC check failed"),
            EventStreamParseError::InvalidData(msg) => write!(f, "{}", msg),
        }
    }
}

impl<T> Into<RusotoError<T>> for EventStreamParseError {
    fn into(self) -> RusotoError<T> {
        RusotoError::ParseError(self.to_string())
    }
}

#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum EventStreamHeaderValue<'a> {
    Bool(bool),
    UInt8(u8),
    UInt16(u16),
    UInt32(u32),
    UInt64(u64),
    ByteArray(&'a [u8]),
    String(&'a str),
    Timestamp(u64),
    Uuid(&'a [u8; 16]), // don't want to pull the uuid dependency for this, so just u8
}

impl<'a> EventStreamHeaderValue<'a> {
    pub fn parse(reader: &mut &'a [u8]) -> Result<Self, EventStreamParseError> {
        let value_type = read_u8(reader)?;
        let value = match value_type {
            0 => EventStreamHeaderValue::Bool(true),
            1 => EventStreamHeaderValue::Bool(false),
            2 => EventStreamHeaderValue::UInt8(read_u8(reader)?),
            3 => EventStreamHeaderValue::UInt16(read_u16(reader)?),
            4 => EventStreamHeaderValue::UInt32(read_u32(reader)?),
            5 => EventStreamHeaderValue::UInt64(read_u64(reader)?),
            6 => {
                let size = read_u16(reader)? as usize;
                let byte_array = read_slice(reader, size)?;
                EventStreamHeaderValue::ByteArray(byte_array)
            }
            7 => {
                let size = read_u16(reader)? as usize;
                let string_bytes = read_slice(reader, size)?;
                let string = std::str::from_utf8(string_bytes).or(Err(
                    EventStreamParseError::InvalidData("Header string data is not valid utf-8"),
                ))?;
                EventStreamHeaderValue::String(string)
            }
            8 => EventStreamHeaderValue::Timestamp(read_u64(reader)?),
            9 => EventStreamHeaderValue::Uuid(read_slice(reader, 16)?.try_into().unwrap()),
            _ => Err(EventStreamParseError::InvalidData(
                "Invalid header value type",
            ))?,
        };
        Ok(value)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct EventStreamHeader<'a> {
    name: &'a str,
    value: EventStreamHeaderValue<'a>,
}

impl<'a> EventStreamHeader<'a> {
    pub fn parse(reader: &mut &'a [u8]) -> Result<Self, EventStreamParseError> {
        let name_size = read_u8(reader)? as usize;
        let name_bytes = read_slice(reader, name_size)?;
        let name = std::str::from_utf8(name_bytes).or(Err(EventStreamParseError::InvalidData(
            "Header name is not valid utf-8",
        )))?;

        let value = EventStreamHeaderValue::parse(reader)?;

        Ok(EventStreamHeader { name, value })
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct EventStreamMessage<'a> {
    headers: Vec<EventStreamHeader<'a>>,
    payload: &'a [u8],
}

impl<'a> EventStreamMessage<'a> {
    const MIN_LENGTH: usize = 16;

    pub fn parse(reader: &mut &'a [u8]) -> Result<Self, EventStreamParseError> {
        // Get a copy of the entire slice before it gets advanced
        let mut event_buf: &[u8] = *reader;

        let total_length = read_u32(reader)? as usize;
        // Ensure later subtractions don't underflow
        if total_length < Self::MIN_LENGTH {
            return Err(EventStreamParseError::InvalidData(
                "Invalid event total length value",
            ));
        }
        let remaining_length = total_length - 4;

        // Ensure we have the entire event data for event_buf
        if event_buf.len() < total_length {
            return Err(EventStreamParseError::UnexpectedEof);
        }
        event_buf = &event_buf[..total_length];

        let mut remainder_reader = read_slice(reader, remaining_length)?;
        Self::parse_complete_event(event_buf, &mut remainder_reader)
            // The entire event is available, EOF is no longer possible with well-formed packets
            .map_err(EventStreamParseError::eof_as_invalid)
    }

    fn parse_complete_event(
        event_buf: &'a [u8],
        remainder_reader: &mut &'a [u8],
    ) -> Result<Self, EventStreamParseError> {
        let headers_length = read_u32(remainder_reader)? as usize;
        let prelude_crc = read_u32(remainder_reader)?;
        check_crc32(&event_buf[..8], prelude_crc)?;

        let mut headers_reader = read_slice(remainder_reader, headers_length)?;
        let mut headers = Vec::with_capacity(3);
        while !headers_reader.is_empty() {
            let header = EventStreamHeader::parse(&mut headers_reader)?;
            headers.push(header);
        }

        if remainder_reader.len() < 4 {
            return Err(EventStreamParseError::InvalidData(
                "Malformed event: unexpected EOF",
            ));
        }
        let payload = read_slice(remainder_reader, remainder_reader.len() - 4)?;
        let payload_crc = read_u32(remainder_reader)?;
        check_crc32(&event_buf[..(event_buf.len() - 4)], payload_crc)?;

        Ok(EventStreamMessage { headers, payload })
    }

    pub fn get_header(&self, name: &str) -> Option<&EventStreamHeader<'a>> {
        self.headers.iter().find(|h| h.name == name)
    }
}

/// Event Stream decoder
///
/// This struct implements `futures::Stream` and decodes events of type `T` from a streaming HTTP body.
#[derive(Debug)]
pub struct EventStream<T> {
    response_body: Option<Pin<Box<ByteStream>>>,
    buf: Vec<u8>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: DeserializeEvent + Unpin> EventStream<T> {
    #[doc(hidden)]
    pub fn new(response: HttpResponse) -> EventStream<T> {
        EventStream {
            response_body: Some(Box::pin(response.body)),
            buf: Vec::with_capacity(512),
            _phantom: PhantomData {},
        }
    }

    fn pop_event(buf: &mut Vec<u8>) -> Result<Option<T>, RusotoError<()>> {
        loop {
            let mut reader: &[u8] = &buf;
            let initial_size = reader.len();
            let event_msg = match EventStreamMessage::parse(&mut reader) {
                Ok(msg) => msg,
                Err(EventStreamParseError::UnexpectedEof) => return Ok(None),
                Err(err) => return Err(err.into()),
            };
            log::trace!("Parsed event stream event: {:?}", event_msg);

            let event_type_header = event_msg
                .get_header(":event-type")
                .or_else(|| event_msg.get_header(":exception-type"))
                .ok_or_else(|| {
                    RusotoError::ParseError(
                        "Expected event-type or exception-type header".to_string(),
                    )
                })?;
            let event_type: &str = match event_type_header.value {
                EventStreamHeaderValue::String(s) => s,
                _ => {
                    return Err(EventStreamParseError::InvalidData(
                        "Invalid event-type header type",
                    )
                    .into())
                }
            };

            let event = if event_type == "initial-response" {
                None
            } else {
                Some(T::deserialize_event(event_type, event_msg.payload)?)
            };

            let bytes_consumed = initial_size - reader.len();
            buf.drain(..bytes_consumed);

            if event.is_none() {
                continue;
            }

            break Ok(event);
        }
    }

    fn drop_response_body(&mut self) {
        self.response_body = None;
    }
}

impl<T: DeserializeEvent + Unpin> futures::Stream for EventStream<T> {
    type Item = Result<T, RusotoError<()>>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let projection = self.get_mut();

        // First try to use the buffer
        match Self::pop_event(&mut projection.buf) {
            Ok(Some(event)) => return Poll::Ready(Some(Ok(event))),
            Ok(None) => {}
            Err(err) => {
                projection.drop_response_body();
                return Poll::Ready(Some(Err(err)));
            }
        };

        // Otherwise, see if new data has arrived from the network
        let chunk_option = match &mut projection.response_body {
            // We still maintain the stream, poll it and return if nothing is available
            Some(body) => futures::ready!(Stream::poll_next(body.as_mut(), cx)),

            // We dropped the stream because we encountered an error.
            // This means that the stream is potentially broken and so we end it.
            None => return Poll::Ready(None),
        };
        match chunk_option {
            // We received an http body chunk
            Some(Ok(byte_chunk)) => {
                log::trace!("Got event stream bytes: {:?}", byte_chunk);

                projection.buf.extend(byte_chunk);

                let parsed_event = match Self::pop_event(&mut projection.buf) {
                    Ok(None) => {
                        cx.waker().wake_by_ref();
                        return Poll::Pending;
                    }
                    Ok(Some(item)) => Ok(item),
                    Err(err) => {
                        projection.drop_response_body();
                        Err(err)
                    }
                };
                Poll::Ready(Some(parsed_event))
            }

            // Something went wrong with the network connection
            Some(Err(e)) => {
                projection.drop_response_body();
                Poll::Ready(Some(Err(RusotoError::from(e))))
            }

            // The underlying stream is closed
            None => {
                projection.drop_response_body();

                if !projection.buf.is_empty() {
                    Poll::Ready(Some(Err(RusotoError::ParseError(
                        "Event stream closed with incomplete data remaining".to_string(),
                    ))))
                } else {
                    Poll::Ready(None)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_initial_response() {
        let data = b"\0\0\0r\0\0\0`\xab\x82\r\x9e\x0b:event-type\x07\0\x10initial-response\r\
            :content-type\x07\0\x1aapplication/x-amz-json-1.1\
            \r:message-type\x07\0\x05event{}\xac\xaek}";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(
            event_msg,
            Ok(EventStreamMessage {
                headers: vec![
                    EventStreamHeader {
                        name: ":event-type",
                        value: EventStreamHeaderValue::String("initial-response"),
                    },
                    EventStreamHeader {
                        name: ":content-type",
                        value: EventStreamHeaderValue::String("application/x-amz-json-1.1"),
                    },
                    EventStreamHeader {
                        name: ":message-type",
                        value: EventStreamHeaderValue::String("event"),
                    },
                ],
                payload: b"{}",
            }),
        );
    }

    #[test]
    fn parse_error_event() {
        let data = b"\0\0\x01\x06\0\0\0pq;\x88P\x0f:exception-type\x07\0\x18\
            KMSAccessDeniedException\r:content-type\x07\0\x1aapplication/x-amz-json-1.1\r\
            :message-type\x07\0\texception{\"message\":\"User AIDAAAAAAAAAAAAAAAAAA is not \
            authorized to decrypt records in stream 666666666666:rusoto-test-tud2Vz6q1V\
            :1590674508\"}\xfc\xd1\x99T";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(
            event_msg,
            Ok(EventStreamMessage {
                headers: vec![
                    EventStreamHeader {
                        name: ":exception-type",
                        value: EventStreamHeaderValue::String("KMSAccessDeniedException"),
                    },
                    EventStreamHeader {
                        name: ":content-type",
                        value: EventStreamHeaderValue::String("application/x-amz-json-1.1"),
                    },
                    EventStreamHeader {
                        name: ":message-type",
                        value: EventStreamHeaderValue::String("exception"),
                    },
                ],
                payload: b"{\"message\":\"User AIDAAAAAAAAAAAAAAAAAA is not \
                    authorized to decrypt records in stream 666666666666:rusoto-test-tud2Vz6q1V\
                    :1590674508\"}",
            }),
        );
    }

    #[test]
    fn invalid_prelude_crc() {
        let data = b"\0\0\0r\0\0\0`\xab\x82\r\x9f\x0b:event-type\x07\0\x10initial-response\r\
            :content-type\x07\0\x1aapplication/x-amz-json-1.1\
            \r:message-type\x07\0\x05event{}\xac\xaek}";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(event_msg, Err(EventStreamParseError::InvalidCrc));
    }

    #[test]
    fn invalid_message_crc() {
        let data = b"\0\0\0r\0\0\0`\xab\x82\r\x9e\x0b:event-type\x07\0\x10initial-response\r\
            :content-type\x07\0\x1aapplication/x-amz-json-1.1\
            \r:message-type\x07\0\x05event{}\xad\xaek}";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(event_msg, Err(EventStreamParseError::InvalidCrc));
    }

    #[test]
    fn incomplete_event() {
        let data = b"\0\0\0r\0\0\0`\xab\x82\r\x9e\x0b:event-type\x07\0\x10initial-response\r\
            :content-type\x07\0\x1aapplication/x-amz-json-1.1\
            \r:message-type\x07\0\x05event{}\xac";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(event_msg, Err(EventStreamParseError::UnexpectedEof));
    }

    #[test]
    fn empty_reader() {
        let data = b"";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert_eq!(event_msg, Err(EventStreamParseError::UnexpectedEof));
    }

    #[test]
    fn invalid_header_size() {
        let data = b"\0\0\0r\0\0\0c2\x8b\\$\x0b:event-type\x07\0\x10initial-response\r\
            :content-type\x07\0\x1aapplication/x-amz-json-1.1\
            \r:message-type\x07\0\x05event{}m\x858\x01";

        let event_msg = EventStreamMessage::parse(&mut &data[..]);
        assert!(matches!(
            event_msg,
            Err(EventStreamParseError::InvalidData(_))
        ));
    }
}