diff options
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream')
9 files changed, 1470 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/debug.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/debug.go new file mode 100644 index 0000000..ecc7bf8 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/debug.go | |||
@@ -0,0 +1,144 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/base64" | ||
6 | "encoding/json" | ||
7 | "fmt" | ||
8 | "strconv" | ||
9 | ) | ||
10 | |||
11 | type decodedMessage struct { | ||
12 | rawMessage | ||
13 | Headers decodedHeaders `json:"headers"` | ||
14 | } | ||
15 | type jsonMessage struct { | ||
16 | Length json.Number `json:"total_length"` | ||
17 | HeadersLen json.Number `json:"headers_length"` | ||
18 | PreludeCRC json.Number `json:"prelude_crc"` | ||
19 | Headers decodedHeaders `json:"headers"` | ||
20 | Payload []byte `json:"payload"` | ||
21 | CRC json.Number `json:"message_crc"` | ||
22 | } | ||
23 | |||
24 | func (d *decodedMessage) UnmarshalJSON(b []byte) (err error) { | ||
25 | var jsonMsg jsonMessage | ||
26 | if err = json.Unmarshal(b, &jsonMsg); err != nil { | ||
27 | return err | ||
28 | } | ||
29 | |||
30 | d.Length, err = numAsUint32(jsonMsg.Length) | ||
31 | if err != nil { | ||
32 | return err | ||
33 | } | ||
34 | d.HeadersLen, err = numAsUint32(jsonMsg.HeadersLen) | ||
35 | if err != nil { | ||
36 | return err | ||
37 | } | ||
38 | d.PreludeCRC, err = numAsUint32(jsonMsg.PreludeCRC) | ||
39 | if err != nil { | ||
40 | return err | ||
41 | } | ||
42 | d.Headers = jsonMsg.Headers | ||
43 | d.Payload = jsonMsg.Payload | ||
44 | d.CRC, err = numAsUint32(jsonMsg.CRC) | ||
45 | if err != nil { | ||
46 | return err | ||
47 | } | ||
48 | |||
49 | return nil | ||
50 | } | ||
51 | |||
52 | func (d *decodedMessage) MarshalJSON() ([]byte, error) { | ||
53 | jsonMsg := jsonMessage{ | ||
54 | Length: json.Number(strconv.Itoa(int(d.Length))), | ||
55 | HeadersLen: json.Number(strconv.Itoa(int(d.HeadersLen))), | ||
56 | PreludeCRC: json.Number(strconv.Itoa(int(d.PreludeCRC))), | ||
57 | Headers: d.Headers, | ||
58 | Payload: d.Payload, | ||
59 | CRC: json.Number(strconv.Itoa(int(d.CRC))), | ||
60 | } | ||
61 | |||
62 | return json.Marshal(jsonMsg) | ||
63 | } | ||
64 | |||
65 | func numAsUint32(n json.Number) (uint32, error) { | ||
66 | v, err := n.Int64() | ||
67 | if err != nil { | ||
68 | return 0, fmt.Errorf("failed to get int64 json number, %v", err) | ||
69 | } | ||
70 | |||
71 | return uint32(v), nil | ||
72 | } | ||
73 | |||
74 | func (d decodedMessage) Message() Message { | ||
75 | return Message{ | ||
76 | Headers: Headers(d.Headers), | ||
77 | Payload: d.Payload, | ||
78 | } | ||
79 | } | ||
80 | |||
81 | type decodedHeaders Headers | ||
82 | |||
83 | func (hs *decodedHeaders) UnmarshalJSON(b []byte) error { | ||
84 | var jsonHeaders []struct { | ||
85 | Name string `json:"name"` | ||
86 | Type valueType `json:"type"` | ||
87 | Value interface{} `json:"value"` | ||
88 | } | ||
89 | |||
90 | decoder := json.NewDecoder(bytes.NewReader(b)) | ||
91 | decoder.UseNumber() | ||
92 | if err := decoder.Decode(&jsonHeaders); err != nil { | ||
93 | return err | ||
94 | } | ||
95 | |||
96 | var headers Headers | ||
97 | for _, h := range jsonHeaders { | ||
98 | value, err := valueFromType(h.Type, h.Value) | ||
99 | if err != nil { | ||
100 | return err | ||
101 | } | ||
102 | headers.Set(h.Name, value) | ||
103 | } | ||
104 | (*hs) = decodedHeaders(headers) | ||
105 | |||
106 | return nil | ||
107 | } | ||
108 | |||
109 | func valueFromType(typ valueType, val interface{}) (Value, error) { | ||
110 | switch typ { | ||
111 | case trueValueType: | ||
112 | return BoolValue(true), nil | ||
113 | case falseValueType: | ||
114 | return BoolValue(false), nil | ||
115 | case int8ValueType: | ||
116 | v, err := val.(json.Number).Int64() | ||
117 | return Int8Value(int8(v)), err | ||
118 | case int16ValueType: | ||
119 | v, err := val.(json.Number).Int64() | ||
120 | return Int16Value(int16(v)), err | ||
121 | case int32ValueType: | ||
122 | v, err := val.(json.Number).Int64() | ||
123 | return Int32Value(int32(v)), err | ||
124 | case int64ValueType: | ||
125 | v, err := val.(json.Number).Int64() | ||
126 | return Int64Value(v), err | ||
127 | case bytesValueType: | ||
128 | v, err := base64.StdEncoding.DecodeString(val.(string)) | ||
129 | return BytesValue(v), err | ||
130 | case stringValueType: | ||
131 | v, err := base64.StdEncoding.DecodeString(val.(string)) | ||
132 | return StringValue(string(v)), err | ||
133 | case timestampValueType: | ||
134 | v, err := val.(json.Number).Int64() | ||
135 | return TimestampValue(timeFromEpochMilli(v)), err | ||
136 | case uuidValueType: | ||
137 | v, err := base64.StdEncoding.DecodeString(val.(string)) | ||
138 | var tv UUIDValue | ||
139 | copy(tv[:], v) | ||
140 | return tv, err | ||
141 | default: | ||
142 | panic(fmt.Sprintf("unknown type, %s, %T", typ.String(), val)) | ||
143 | } | ||
144 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/decode.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/decode.go new file mode 100644 index 0000000..4b972b2 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/decode.go | |||
@@ -0,0 +1,199 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/binary" | ||
6 | "encoding/hex" | ||
7 | "encoding/json" | ||
8 | "fmt" | ||
9 | "hash" | ||
10 | "hash/crc32" | ||
11 | "io" | ||
12 | |||
13 | "github.com/aws/aws-sdk-go/aws" | ||
14 | ) | ||
15 | |||
16 | // Decoder provides decoding of an Event Stream messages. | ||
17 | type Decoder struct { | ||
18 | r io.Reader | ||
19 | logger aws.Logger | ||
20 | } | ||
21 | |||
22 | // NewDecoder initializes and returns a Decoder for decoding event | ||
23 | // stream messages from the reader provided. | ||
24 | func NewDecoder(r io.Reader) *Decoder { | ||
25 | return &Decoder{ | ||
26 | r: r, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | // Decode attempts to decode a single message from the event stream reader. | ||
31 | // Will return the event stream message, or error if Decode fails to read | ||
32 | // the message from the stream. | ||
33 | func (d *Decoder) Decode(payloadBuf []byte) (m Message, err error) { | ||
34 | reader := d.r | ||
35 | if d.logger != nil { | ||
36 | debugMsgBuf := bytes.NewBuffer(nil) | ||
37 | reader = io.TeeReader(reader, debugMsgBuf) | ||
38 | defer func() { | ||
39 | logMessageDecode(d.logger, debugMsgBuf, m, err) | ||
40 | }() | ||
41 | } | ||
42 | |||
43 | crc := crc32.New(crc32IEEETable) | ||
44 | hashReader := io.TeeReader(reader, crc) | ||
45 | |||
46 | prelude, err := decodePrelude(hashReader, crc) | ||
47 | if err != nil { | ||
48 | return Message{}, err | ||
49 | } | ||
50 | |||
51 | if prelude.HeadersLen > 0 { | ||
52 | lr := io.LimitReader(hashReader, int64(prelude.HeadersLen)) | ||
53 | m.Headers, err = decodeHeaders(lr) | ||
54 | if err != nil { | ||
55 | return Message{}, err | ||
56 | } | ||
57 | } | ||
58 | |||
59 | if payloadLen := prelude.PayloadLen(); payloadLen > 0 { | ||
60 | buf, err := decodePayload(payloadBuf, io.LimitReader(hashReader, int64(payloadLen))) | ||
61 | if err != nil { | ||
62 | return Message{}, err | ||
63 | } | ||
64 | m.Payload = buf | ||
65 | } | ||
66 | |||
67 | msgCRC := crc.Sum32() | ||
68 | if err := validateCRC(reader, msgCRC); err != nil { | ||
69 | return Message{}, err | ||
70 | } | ||
71 | |||
72 | return m, nil | ||
73 | } | ||
74 | |||
75 | // UseLogger specifies the Logger that that the decoder should use to log the | ||
76 | // message decode to. | ||
77 | func (d *Decoder) UseLogger(logger aws.Logger) { | ||
78 | d.logger = logger | ||
79 | } | ||
80 | |||
81 | func logMessageDecode(logger aws.Logger, msgBuf *bytes.Buffer, msg Message, decodeErr error) { | ||
82 | w := bytes.NewBuffer(nil) | ||
83 | defer func() { logger.Log(w.String()) }() | ||
84 | |||
85 | fmt.Fprintf(w, "Raw message:\n%s\n", | ||
86 | hex.Dump(msgBuf.Bytes())) | ||
87 | |||
88 | if decodeErr != nil { | ||
89 | fmt.Fprintf(w, "Decode error: %v\n", decodeErr) | ||
90 | return | ||
91 | } | ||
92 | |||
93 | rawMsg, err := msg.rawMessage() | ||
94 | if err != nil { | ||
95 | fmt.Fprintf(w, "failed to create raw message, %v\n", err) | ||
96 | return | ||
97 | } | ||
98 | |||
99 | decodedMsg := decodedMessage{ | ||
100 | rawMessage: rawMsg, | ||
101 | Headers: decodedHeaders(msg.Headers), | ||
102 | } | ||
103 | |||
104 | fmt.Fprintf(w, "Decoded message:\n") | ||
105 | encoder := json.NewEncoder(w) | ||
106 | if err := encoder.Encode(decodedMsg); err != nil { | ||
107 | fmt.Fprintf(w, "failed to generate decoded message, %v\n", err) | ||
108 | } | ||
109 | } | ||
110 | |||
111 | func decodePrelude(r io.Reader, crc hash.Hash32) (messagePrelude, error) { | ||
112 | var p messagePrelude | ||
113 | |||
114 | var err error | ||
115 | p.Length, err = decodeUint32(r) | ||
116 | if err != nil { | ||
117 | return messagePrelude{}, err | ||
118 | } | ||
119 | |||
120 | p.HeadersLen, err = decodeUint32(r) | ||
121 | if err != nil { | ||
122 | return messagePrelude{}, err | ||
123 | } | ||
124 | |||
125 | if err := p.ValidateLens(); err != nil { | ||
126 | return messagePrelude{}, err | ||
127 | } | ||
128 | |||
129 | preludeCRC := crc.Sum32() | ||
130 | if err := validateCRC(r, preludeCRC); err != nil { | ||
131 | return messagePrelude{}, err | ||
132 | } | ||
133 | |||
134 | p.PreludeCRC = preludeCRC | ||
135 | |||
136 | return p, nil | ||
137 | } | ||
138 | |||
139 | func decodePayload(buf []byte, r io.Reader) ([]byte, error) { | ||
140 | w := bytes.NewBuffer(buf[0:0]) | ||
141 | |||
142 | _, err := io.Copy(w, r) | ||
143 | return w.Bytes(), err | ||
144 | } | ||
145 | |||
146 | func decodeUint8(r io.Reader) (uint8, error) { | ||
147 | type byteReader interface { | ||
148 | ReadByte() (byte, error) | ||
149 | } | ||
150 | |||
151 | if br, ok := r.(byteReader); ok { | ||
152 | v, err := br.ReadByte() | ||
153 | return uint8(v), err | ||
154 | } | ||
155 | |||
156 | var b [1]byte | ||
157 | _, err := io.ReadFull(r, b[:]) | ||
158 | return uint8(b[0]), err | ||
159 | } | ||
160 | func decodeUint16(r io.Reader) (uint16, error) { | ||
161 | var b [2]byte | ||
162 | bs := b[:] | ||
163 | _, err := io.ReadFull(r, bs) | ||
164 | if err != nil { | ||
165 | return 0, err | ||
166 | } | ||
167 | return binary.BigEndian.Uint16(bs), nil | ||
168 | } | ||
169 | func decodeUint32(r io.Reader) (uint32, error) { | ||
170 | var b [4]byte | ||
171 | bs := b[:] | ||
172 | _, err := io.ReadFull(r, bs) | ||
173 | if err != nil { | ||
174 | return 0, err | ||
175 | } | ||
176 | return binary.BigEndian.Uint32(bs), nil | ||
177 | } | ||
178 | func decodeUint64(r io.Reader) (uint64, error) { | ||
179 | var b [8]byte | ||
180 | bs := b[:] | ||
181 | _, err := io.ReadFull(r, bs) | ||
182 | if err != nil { | ||
183 | return 0, err | ||
184 | } | ||
185 | return binary.BigEndian.Uint64(bs), nil | ||
186 | } | ||
187 | |||
188 | func validateCRC(r io.Reader, expect uint32) error { | ||
189 | msgCRC, err := decodeUint32(r) | ||
190 | if err != nil { | ||
191 | return err | ||
192 | } | ||
193 | |||
194 | if msgCRC != expect { | ||
195 | return ChecksumError{} | ||
196 | } | ||
197 | |||
198 | return nil | ||
199 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/encode.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/encode.go new file mode 100644 index 0000000..150a609 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/encode.go | |||
@@ -0,0 +1,114 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/binary" | ||
6 | "hash" | ||
7 | "hash/crc32" | ||
8 | "io" | ||
9 | ) | ||
10 | |||
11 | // Encoder provides EventStream message encoding. | ||
12 | type Encoder struct { | ||
13 | w io.Writer | ||
14 | |||
15 | headersBuf *bytes.Buffer | ||
16 | } | ||
17 | |||
18 | // NewEncoder initializes and returns an Encoder to encode Event Stream | ||
19 | // messages to an io.Writer. | ||
20 | func NewEncoder(w io.Writer) *Encoder { | ||
21 | return &Encoder{ | ||
22 | w: w, | ||
23 | headersBuf: bytes.NewBuffer(nil), | ||
24 | } | ||
25 | } | ||
26 | |||
27 | // Encode encodes a single EventStream message to the io.Writer the Encoder | ||
28 | // was created with. An error is returned if writing the message fails. | ||
29 | func (e *Encoder) Encode(msg Message) error { | ||
30 | e.headersBuf.Reset() | ||
31 | |||
32 | err := encodeHeaders(e.headersBuf, msg.Headers) | ||
33 | if err != nil { | ||
34 | return err | ||
35 | } | ||
36 | |||
37 | crc := crc32.New(crc32IEEETable) | ||
38 | hashWriter := io.MultiWriter(e.w, crc) | ||
39 | |||
40 | headersLen := uint32(e.headersBuf.Len()) | ||
41 | payloadLen := uint32(len(msg.Payload)) | ||
42 | |||
43 | if err := encodePrelude(hashWriter, crc, headersLen, payloadLen); err != nil { | ||
44 | return err | ||
45 | } | ||
46 | |||
47 | if headersLen > 0 { | ||
48 | if _, err := io.Copy(hashWriter, e.headersBuf); err != nil { | ||
49 | return err | ||
50 | } | ||
51 | } | ||
52 | |||
53 | if payloadLen > 0 { | ||
54 | if _, err := hashWriter.Write(msg.Payload); err != nil { | ||
55 | return err | ||
56 | } | ||
57 | } | ||
58 | |||
59 | msgCRC := crc.Sum32() | ||
60 | return binary.Write(e.w, binary.BigEndian, msgCRC) | ||
61 | } | ||
62 | |||
63 | func encodePrelude(w io.Writer, crc hash.Hash32, headersLen, payloadLen uint32) error { | ||
64 | p := messagePrelude{ | ||
65 | Length: minMsgLen + headersLen + payloadLen, | ||
66 | HeadersLen: headersLen, | ||
67 | } | ||
68 | if err := p.ValidateLens(); err != nil { | ||
69 | return err | ||
70 | } | ||
71 | |||
72 | err := binaryWriteFields(w, binary.BigEndian, | ||
73 | p.Length, | ||
74 | p.HeadersLen, | ||
75 | ) | ||
76 | if err != nil { | ||
77 | return err | ||
78 | } | ||
79 | |||
80 | p.PreludeCRC = crc.Sum32() | ||
81 | err = binary.Write(w, binary.BigEndian, p.PreludeCRC) | ||
82 | if err != nil { | ||
83 | return err | ||
84 | } | ||
85 | |||
86 | return nil | ||
87 | } | ||
88 | |||
89 | func encodeHeaders(w io.Writer, headers Headers) error { | ||
90 | for _, h := range headers { | ||
91 | hn := headerName{ | ||
92 | Len: uint8(len(h.Name)), | ||
93 | } | ||
94 | copy(hn.Name[:hn.Len], h.Name) | ||
95 | if err := hn.encode(w); err != nil { | ||
96 | return err | ||
97 | } | ||
98 | |||
99 | if err := h.Value.encode(w); err != nil { | ||
100 | return err | ||
101 | } | ||
102 | } | ||
103 | |||
104 | return nil | ||
105 | } | ||
106 | |||
107 | func binaryWriteFields(w io.Writer, order binary.ByteOrder, vs ...interface{}) error { | ||
108 | for _, v := range vs { | ||
109 | if err := binary.Write(w, order, v); err != nil { | ||
110 | return err | ||
111 | } | ||
112 | } | ||
113 | return nil | ||
114 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/error.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/error.go new file mode 100644 index 0000000..5481ef3 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/error.go | |||
@@ -0,0 +1,23 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import "fmt" | ||
4 | |||
5 | // LengthError provides the error for items being larger than a maximum length. | ||
6 | type LengthError struct { | ||
7 | Part string | ||
8 | Want int | ||
9 | Have int | ||
10 | Value interface{} | ||
11 | } | ||
12 | |||
13 | func (e LengthError) Error() string { | ||
14 | return fmt.Sprintf("%s length invalid, %d/%d, %v", | ||
15 | e.Part, e.Want, e.Have, e.Value) | ||
16 | } | ||
17 | |||
18 | // ChecksumError provides the error for message checksum invalidation errors. | ||
19 | type ChecksumError struct{} | ||
20 | |||
21 | func (e ChecksumError) Error() string { | ||
22 | return "message checksum mismatch" | ||
23 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/api.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/api.go new file mode 100644 index 0000000..97937c8 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/api.go | |||
@@ -0,0 +1,196 @@ | |||
1 | package eventstreamapi | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "io" | ||
6 | |||
7 | "github.com/aws/aws-sdk-go/aws" | ||
8 | "github.com/aws/aws-sdk-go/private/protocol" | ||
9 | "github.com/aws/aws-sdk-go/private/protocol/eventstream" | ||
10 | ) | ||
11 | |||
12 | // Unmarshaler provides the interface for unmarshaling a EventStream | ||
13 | // message into a SDK type. | ||
14 | type Unmarshaler interface { | ||
15 | UnmarshalEvent(protocol.PayloadUnmarshaler, eventstream.Message) error | ||
16 | } | ||
17 | |||
18 | // EventStream headers with specific meaning to async API functionality. | ||
19 | const ( | ||
20 | MessageTypeHeader = `:message-type` // Identifies type of message. | ||
21 | EventMessageType = `event` | ||
22 | ErrorMessageType = `error` | ||
23 | ExceptionMessageType = `exception` | ||
24 | |||
25 | // Message Events | ||
26 | EventTypeHeader = `:event-type` // Identifies message event type e.g. "Stats". | ||
27 | |||
28 | // Message Error | ||
29 | ErrorCodeHeader = `:error-code` | ||
30 | ErrorMessageHeader = `:error-message` | ||
31 | |||
32 | // Message Exception | ||
33 | ExceptionTypeHeader = `:exception-type` | ||
34 | ) | ||
35 | |||
36 | // EventReader provides reading from the EventStream of an reader. | ||
37 | type EventReader struct { | ||
38 | reader io.ReadCloser | ||
39 | decoder *eventstream.Decoder | ||
40 | |||
41 | unmarshalerForEventType func(string) (Unmarshaler, error) | ||
42 | payloadUnmarshaler protocol.PayloadUnmarshaler | ||
43 | |||
44 | payloadBuf []byte | ||
45 | } | ||
46 | |||
47 | // NewEventReader returns a EventReader built from the reader and unmarshaler | ||
48 | // provided. Use ReadStream method to start reading from the EventStream. | ||
49 | func NewEventReader( | ||
50 | reader io.ReadCloser, | ||
51 | payloadUnmarshaler protocol.PayloadUnmarshaler, | ||
52 | unmarshalerForEventType func(string) (Unmarshaler, error), | ||
53 | ) *EventReader { | ||
54 | return &EventReader{ | ||
55 | reader: reader, | ||
56 | decoder: eventstream.NewDecoder(reader), | ||
57 | payloadUnmarshaler: payloadUnmarshaler, | ||
58 | unmarshalerForEventType: unmarshalerForEventType, | ||
59 | payloadBuf: make([]byte, 10*1024), | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // UseLogger instructs the EventReader to use the logger and log level | ||
64 | // specified. | ||
65 | func (r *EventReader) UseLogger(logger aws.Logger, logLevel aws.LogLevelType) { | ||
66 | if logger != nil && logLevel.Matches(aws.LogDebugWithEventStreamBody) { | ||
67 | r.decoder.UseLogger(logger) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | // ReadEvent attempts to read a message from the EventStream and return the | ||
72 | // unmarshaled event value that the message is for. | ||
73 | // | ||
74 | // For EventStream API errors check if the returned error satisfies the | ||
75 | // awserr.Error interface to get the error's Code and Message components. | ||
76 | // | ||
77 | // EventUnmarshalers called with EventStream messages must take copies of the | ||
78 | // message's Payload. The payload will is reused between events read. | ||
79 | func (r *EventReader) ReadEvent() (event interface{}, err error) { | ||
80 | msg, err := r.decoder.Decode(r.payloadBuf) | ||
81 | if err != nil { | ||
82 | return nil, err | ||
83 | } | ||
84 | defer func() { | ||
85 | // Reclaim payload buffer for next message read. | ||
86 | r.payloadBuf = msg.Payload[0:0] | ||
87 | }() | ||
88 | |||
89 | typ, err := GetHeaderString(msg, MessageTypeHeader) | ||
90 | if err != nil { | ||
91 | return nil, err | ||
92 | } | ||
93 | |||
94 | switch typ { | ||
95 | case EventMessageType: | ||
96 | return r.unmarshalEventMessage(msg) | ||
97 | case ExceptionMessageType: | ||
98 | err = r.unmarshalEventException(msg) | ||
99 | return nil, err | ||
100 | case ErrorMessageType: | ||
101 | return nil, r.unmarshalErrorMessage(msg) | ||
102 | default: | ||
103 | return nil, fmt.Errorf("unknown eventstream message type, %v", typ) | ||
104 | } | ||
105 | } | ||
106 | |||
107 | func (r *EventReader) unmarshalEventMessage( | ||
108 | msg eventstream.Message, | ||
109 | ) (event interface{}, err error) { | ||
110 | eventType, err := GetHeaderString(msg, EventTypeHeader) | ||
111 | if err != nil { | ||
112 | return nil, err | ||
113 | } | ||
114 | |||
115 | ev, err := r.unmarshalerForEventType(eventType) | ||
116 | if err != nil { | ||
117 | return nil, err | ||
118 | } | ||
119 | |||
120 | err = ev.UnmarshalEvent(r.payloadUnmarshaler, msg) | ||
121 | if err != nil { | ||
122 | return nil, err | ||
123 | } | ||
124 | |||
125 | return ev, nil | ||
126 | } | ||
127 | |||
128 | func (r *EventReader) unmarshalEventException( | ||
129 | msg eventstream.Message, | ||
130 | ) (err error) { | ||
131 | eventType, err := GetHeaderString(msg, ExceptionTypeHeader) | ||
132 | if err != nil { | ||
133 | return err | ||
134 | } | ||
135 | |||
136 | ev, err := r.unmarshalerForEventType(eventType) | ||
137 | if err != nil { | ||
138 | return err | ||
139 | } | ||
140 | |||
141 | err = ev.UnmarshalEvent(r.payloadUnmarshaler, msg) | ||
142 | if err != nil { | ||
143 | return err | ||
144 | } | ||
145 | |||
146 | var ok bool | ||
147 | err, ok = ev.(error) | ||
148 | if !ok { | ||
149 | err = messageError{ | ||
150 | code: "SerializationError", | ||
151 | msg: fmt.Sprintf( | ||
152 | "event stream exception %s mapped to non-error %T, %v", | ||
153 | eventType, ev, ev, | ||
154 | ), | ||
155 | } | ||
156 | } | ||
157 | |||
158 | return err | ||
159 | } | ||
160 | |||
161 | func (r *EventReader) unmarshalErrorMessage(msg eventstream.Message) (err error) { | ||
162 | var msgErr messageError | ||
163 | |||
164 | msgErr.code, err = GetHeaderString(msg, ErrorCodeHeader) | ||
165 | if err != nil { | ||
166 | return err | ||
167 | } | ||
168 | |||
169 | msgErr.msg, err = GetHeaderString(msg, ErrorMessageHeader) | ||
170 | if err != nil { | ||
171 | return err | ||
172 | } | ||
173 | |||
174 | return msgErr | ||
175 | } | ||
176 | |||
177 | // Close closes the EventReader's EventStream reader. | ||
178 | func (r *EventReader) Close() error { | ||
179 | return r.reader.Close() | ||
180 | } | ||
181 | |||
182 | // GetHeaderString returns the value of the header as a string. If the header | ||
183 | // is not set or the value is not a string an error will be returned. | ||
184 | func GetHeaderString(msg eventstream.Message, headerName string) (string, error) { | ||
185 | headerVal := msg.Headers.Get(headerName) | ||
186 | if headerVal == nil { | ||
187 | return "", fmt.Errorf("error header %s not present", headerName) | ||
188 | } | ||
189 | |||
190 | v, ok := headerVal.Get().(string) | ||
191 | if !ok { | ||
192 | return "", fmt.Errorf("error header value is not a string, %T", headerVal) | ||
193 | } | ||
194 | |||
195 | return v, nil | ||
196 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/error.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/error.go new file mode 100644 index 0000000..5ea5a98 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/eventstreamapi/error.go | |||
@@ -0,0 +1,24 @@ | |||
1 | package eventstreamapi | ||
2 | |||
3 | import "fmt" | ||
4 | |||
5 | type messageError struct { | ||
6 | code string | ||
7 | msg string | ||
8 | } | ||
9 | |||
10 | func (e messageError) Code() string { | ||
11 | return e.code | ||
12 | } | ||
13 | |||
14 | func (e messageError) Message() string { | ||
15 | return e.msg | ||
16 | } | ||
17 | |||
18 | func (e messageError) Error() string { | ||
19 | return fmt.Sprintf("%s: %s", e.code, e.msg) | ||
20 | } | ||
21 | |||
22 | func (e messageError) OrigErr() error { | ||
23 | return nil | ||
24 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header.go new file mode 100644 index 0000000..3b44dde --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header.go | |||
@@ -0,0 +1,166 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "encoding/binary" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | ) | ||
8 | |||
9 | // Headers are a collection of EventStream header values. | ||
10 | type Headers []Header | ||
11 | |||
12 | // Header is a single EventStream Key Value header pair. | ||
13 | type Header struct { | ||
14 | Name string | ||
15 | Value Value | ||
16 | } | ||
17 | |||
18 | // Set associates the name with a value. If the header name already exists in | ||
19 | // the Headers the value will be replaced with the new one. | ||
20 | func (hs *Headers) Set(name string, value Value) { | ||
21 | var i int | ||
22 | for ; i < len(*hs); i++ { | ||
23 | if (*hs)[i].Name == name { | ||
24 | (*hs)[i].Value = value | ||
25 | return | ||
26 | } | ||
27 | } | ||
28 | |||
29 | *hs = append(*hs, Header{ | ||
30 | Name: name, Value: value, | ||
31 | }) | ||
32 | } | ||
33 | |||
34 | // Get returns the Value associated with the header. Nil is returned if the | ||
35 | // value does not exist. | ||
36 | func (hs Headers) Get(name string) Value { | ||
37 | for i := 0; i < len(hs); i++ { | ||
38 | if h := hs[i]; h.Name == name { | ||
39 | return h.Value | ||
40 | } | ||
41 | } | ||
42 | return nil | ||
43 | } | ||
44 | |||
45 | // Del deletes the value in the Headers if it exists. | ||
46 | func (hs *Headers) Del(name string) { | ||
47 | for i := 0; i < len(*hs); i++ { | ||
48 | if (*hs)[i].Name == name { | ||
49 | copy((*hs)[i:], (*hs)[i+1:]) | ||
50 | (*hs) = (*hs)[:len(*hs)-1] | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | func decodeHeaders(r io.Reader) (Headers, error) { | ||
56 | hs := Headers{} | ||
57 | |||
58 | for { | ||
59 | name, err := decodeHeaderName(r) | ||
60 | if err != nil { | ||
61 | if err == io.EOF { | ||
62 | // EOF while getting header name means no more headers | ||
63 | break | ||
64 | } | ||
65 | return nil, err | ||
66 | } | ||
67 | |||
68 | value, err := decodeHeaderValue(r) | ||
69 | if err != nil { | ||
70 | return nil, err | ||
71 | } | ||
72 | |||
73 | hs.Set(name, value) | ||
74 | } | ||
75 | |||
76 | return hs, nil | ||
77 | } | ||
78 | |||
79 | func decodeHeaderName(r io.Reader) (string, error) { | ||
80 | var n headerName | ||
81 | |||
82 | var err error | ||
83 | n.Len, err = decodeUint8(r) | ||
84 | if err != nil { | ||
85 | return "", err | ||
86 | } | ||
87 | |||
88 | name := n.Name[:n.Len] | ||
89 | if _, err := io.ReadFull(r, name); err != nil { | ||
90 | return "", err | ||
91 | } | ||
92 | |||
93 | return string(name), nil | ||
94 | } | ||
95 | |||
96 | func decodeHeaderValue(r io.Reader) (Value, error) { | ||
97 | var raw rawValue | ||
98 | |||
99 | typ, err := decodeUint8(r) | ||
100 | if err != nil { | ||
101 | return nil, err | ||
102 | } | ||
103 | raw.Type = valueType(typ) | ||
104 | |||
105 | var v Value | ||
106 | |||
107 | switch raw.Type { | ||
108 | case trueValueType: | ||
109 | v = BoolValue(true) | ||
110 | case falseValueType: | ||
111 | v = BoolValue(false) | ||
112 | case int8ValueType: | ||
113 | var tv Int8Value | ||
114 | err = tv.decode(r) | ||
115 | v = tv | ||
116 | case int16ValueType: | ||
117 | var tv Int16Value | ||
118 | err = tv.decode(r) | ||
119 | v = tv | ||
120 | case int32ValueType: | ||
121 | var tv Int32Value | ||
122 | err = tv.decode(r) | ||
123 | v = tv | ||
124 | case int64ValueType: | ||
125 | var tv Int64Value | ||
126 | err = tv.decode(r) | ||
127 | v = tv | ||
128 | case bytesValueType: | ||
129 | var tv BytesValue | ||
130 | err = tv.decode(r) | ||
131 | v = tv | ||
132 | case stringValueType: | ||
133 | var tv StringValue | ||
134 | err = tv.decode(r) | ||
135 | v = tv | ||
136 | case timestampValueType: | ||
137 | var tv TimestampValue | ||
138 | err = tv.decode(r) | ||
139 | v = tv | ||
140 | case uuidValueType: | ||
141 | var tv UUIDValue | ||
142 | err = tv.decode(r) | ||
143 | v = tv | ||
144 | default: | ||
145 | panic(fmt.Sprintf("unknown value type %d", raw.Type)) | ||
146 | } | ||
147 | |||
148 | // Error could be EOF, let caller deal with it | ||
149 | return v, err | ||
150 | } | ||
151 | |||
152 | const maxHeaderNameLen = 255 | ||
153 | |||
154 | type headerName struct { | ||
155 | Len uint8 | ||
156 | Name [maxHeaderNameLen]byte | ||
157 | } | ||
158 | |||
159 | func (v headerName) encode(w io.Writer) error { | ||
160 | if err := binary.Write(w, binary.BigEndian, v.Len); err != nil { | ||
161 | return err | ||
162 | } | ||
163 | |||
164 | _, err := w.Write(v.Name[:v.Len]) | ||
165 | return err | ||
166 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header_value.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header_value.go new file mode 100644 index 0000000..e3fc076 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/header_value.go | |||
@@ -0,0 +1,501 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "encoding/base64" | ||
5 | "encoding/binary" | ||
6 | "fmt" | ||
7 | "io" | ||
8 | "strconv" | ||
9 | "time" | ||
10 | ) | ||
11 | |||
12 | const maxHeaderValueLen = 1<<15 - 1 // 2^15-1 or 32KB - 1 | ||
13 | |||
14 | // valueType is the EventStream header value type. | ||
15 | type valueType uint8 | ||
16 | |||
17 | // Header value types | ||
18 | const ( | ||
19 | trueValueType valueType = iota | ||
20 | falseValueType | ||
21 | int8ValueType // Byte | ||
22 | int16ValueType // Short | ||
23 | int32ValueType // Integer | ||
24 | int64ValueType // Long | ||
25 | bytesValueType | ||
26 | stringValueType | ||
27 | timestampValueType | ||
28 | uuidValueType | ||
29 | ) | ||
30 | |||
31 | func (t valueType) String() string { | ||
32 | switch t { | ||
33 | case trueValueType: | ||
34 | return "bool" | ||
35 | case falseValueType: | ||
36 | return "bool" | ||
37 | case int8ValueType: | ||
38 | return "int8" | ||
39 | case int16ValueType: | ||
40 | return "int16" | ||
41 | case int32ValueType: | ||
42 | return "int32" | ||
43 | case int64ValueType: | ||
44 | return "int64" | ||
45 | case bytesValueType: | ||
46 | return "byte_array" | ||
47 | case stringValueType: | ||
48 | return "string" | ||
49 | case timestampValueType: | ||
50 | return "timestamp" | ||
51 | case uuidValueType: | ||
52 | return "uuid" | ||
53 | default: | ||
54 | return fmt.Sprintf("unknown value type %d", uint8(t)) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | type rawValue struct { | ||
59 | Type valueType | ||
60 | Len uint16 // Only set for variable length slices | ||
61 | Value []byte // byte representation of value, BigEndian encoding. | ||
62 | } | ||
63 | |||
64 | func (r rawValue) encodeScalar(w io.Writer, v interface{}) error { | ||
65 | return binaryWriteFields(w, binary.BigEndian, | ||
66 | r.Type, | ||
67 | v, | ||
68 | ) | ||
69 | } | ||
70 | |||
71 | func (r rawValue) encodeFixedSlice(w io.Writer, v []byte) error { | ||
72 | binary.Write(w, binary.BigEndian, r.Type) | ||
73 | |||
74 | _, err := w.Write(v) | ||
75 | return err | ||
76 | } | ||
77 | |||
78 | func (r rawValue) encodeBytes(w io.Writer, v []byte) error { | ||
79 | if len(v) > maxHeaderValueLen { | ||
80 | return LengthError{ | ||
81 | Part: "header value", | ||
82 | Want: maxHeaderValueLen, Have: len(v), | ||
83 | Value: v, | ||
84 | } | ||
85 | } | ||
86 | r.Len = uint16(len(v)) | ||
87 | |||
88 | err := binaryWriteFields(w, binary.BigEndian, | ||
89 | r.Type, | ||
90 | r.Len, | ||
91 | ) | ||
92 | if err != nil { | ||
93 | return err | ||
94 | } | ||
95 | |||
96 | _, err = w.Write(v) | ||
97 | return err | ||
98 | } | ||
99 | |||
100 | func (r rawValue) encodeString(w io.Writer, v string) error { | ||
101 | if len(v) > maxHeaderValueLen { | ||
102 | return LengthError{ | ||
103 | Part: "header value", | ||
104 | Want: maxHeaderValueLen, Have: len(v), | ||
105 | Value: v, | ||
106 | } | ||
107 | } | ||
108 | r.Len = uint16(len(v)) | ||
109 | |||
110 | type stringWriter interface { | ||
111 | WriteString(string) (int, error) | ||
112 | } | ||
113 | |||
114 | err := binaryWriteFields(w, binary.BigEndian, | ||
115 | r.Type, | ||
116 | r.Len, | ||
117 | ) | ||
118 | if err != nil { | ||
119 | return err | ||
120 | } | ||
121 | |||
122 | if sw, ok := w.(stringWriter); ok { | ||
123 | _, err = sw.WriteString(v) | ||
124 | } else { | ||
125 | _, err = w.Write([]byte(v)) | ||
126 | } | ||
127 | |||
128 | return err | ||
129 | } | ||
130 | |||
131 | func decodeFixedBytesValue(r io.Reader, buf []byte) error { | ||
132 | _, err := io.ReadFull(r, buf) | ||
133 | return err | ||
134 | } | ||
135 | |||
136 | func decodeBytesValue(r io.Reader) ([]byte, error) { | ||
137 | var raw rawValue | ||
138 | var err error | ||
139 | raw.Len, err = decodeUint16(r) | ||
140 | if err != nil { | ||
141 | return nil, err | ||
142 | } | ||
143 | |||
144 | buf := make([]byte, raw.Len) | ||
145 | _, err = io.ReadFull(r, buf) | ||
146 | if err != nil { | ||
147 | return nil, err | ||
148 | } | ||
149 | |||
150 | return buf, nil | ||
151 | } | ||
152 | |||
153 | func decodeStringValue(r io.Reader) (string, error) { | ||
154 | v, err := decodeBytesValue(r) | ||
155 | return string(v), err | ||
156 | } | ||
157 | |||
158 | // Value represents the abstract header value. | ||
159 | type Value interface { | ||
160 | Get() interface{} | ||
161 | String() string | ||
162 | valueType() valueType | ||
163 | encode(io.Writer) error | ||
164 | } | ||
165 | |||
166 | // An BoolValue provides eventstream encoding, and representation | ||
167 | // of a Go bool value. | ||
168 | type BoolValue bool | ||
169 | |||
170 | // Get returns the underlying type | ||
171 | func (v BoolValue) Get() interface{} { | ||
172 | return bool(v) | ||
173 | } | ||
174 | |||
175 | // valueType returns the EventStream header value type value. | ||
176 | func (v BoolValue) valueType() valueType { | ||
177 | if v { | ||
178 | return trueValueType | ||
179 | } | ||
180 | return falseValueType | ||
181 | } | ||
182 | |||
183 | func (v BoolValue) String() string { | ||
184 | return strconv.FormatBool(bool(v)) | ||
185 | } | ||
186 | |||
187 | // encode encodes the BoolValue into an eventstream binary value | ||
188 | // representation. | ||
189 | func (v BoolValue) encode(w io.Writer) error { | ||
190 | return binary.Write(w, binary.BigEndian, v.valueType()) | ||
191 | } | ||
192 | |||
193 | // An Int8Value provides eventstream encoding, and representation of a Go | ||
194 | // int8 value. | ||
195 | type Int8Value int8 | ||
196 | |||
197 | // Get returns the underlying value. | ||
198 | func (v Int8Value) Get() interface{} { | ||
199 | return int8(v) | ||
200 | } | ||
201 | |||
202 | // valueType returns the EventStream header value type value. | ||
203 | func (Int8Value) valueType() valueType { | ||
204 | return int8ValueType | ||
205 | } | ||
206 | |||
207 | func (v Int8Value) String() string { | ||
208 | return fmt.Sprintf("0x%02x", int8(v)) | ||
209 | } | ||
210 | |||
211 | // encode encodes the Int8Value into an eventstream binary value | ||
212 | // representation. | ||
213 | func (v Int8Value) encode(w io.Writer) error { | ||
214 | raw := rawValue{ | ||
215 | Type: v.valueType(), | ||
216 | } | ||
217 | |||
218 | return raw.encodeScalar(w, v) | ||
219 | } | ||
220 | |||
221 | func (v *Int8Value) decode(r io.Reader) error { | ||
222 | n, err := decodeUint8(r) | ||
223 | if err != nil { | ||
224 | return err | ||
225 | } | ||
226 | |||
227 | *v = Int8Value(n) | ||
228 | return nil | ||
229 | } | ||
230 | |||
231 | // An Int16Value provides eventstream encoding, and representation of a Go | ||
232 | // int16 value. | ||
233 | type Int16Value int16 | ||
234 | |||
235 | // Get returns the underlying value. | ||
236 | func (v Int16Value) Get() interface{} { | ||
237 | return int16(v) | ||
238 | } | ||
239 | |||
240 | // valueType returns the EventStream header value type value. | ||
241 | func (Int16Value) valueType() valueType { | ||
242 | return int16ValueType | ||
243 | } | ||
244 | |||
245 | func (v Int16Value) String() string { | ||
246 | return fmt.Sprintf("0x%04x", int16(v)) | ||
247 | } | ||
248 | |||
249 | // encode encodes the Int16Value into an eventstream binary value | ||
250 | // representation. | ||
251 | func (v Int16Value) encode(w io.Writer) error { | ||
252 | raw := rawValue{ | ||
253 | Type: v.valueType(), | ||
254 | } | ||
255 | return raw.encodeScalar(w, v) | ||
256 | } | ||
257 | |||
258 | func (v *Int16Value) decode(r io.Reader) error { | ||
259 | n, err := decodeUint16(r) | ||
260 | if err != nil { | ||
261 | return err | ||
262 | } | ||
263 | |||
264 | *v = Int16Value(n) | ||
265 | return nil | ||
266 | } | ||
267 | |||
268 | // An Int32Value provides eventstream encoding, and representation of a Go | ||
269 | // int32 value. | ||
270 | type Int32Value int32 | ||
271 | |||
272 | // Get returns the underlying value. | ||
273 | func (v Int32Value) Get() interface{} { | ||
274 | return int32(v) | ||
275 | } | ||
276 | |||
277 | // valueType returns the EventStream header value type value. | ||
278 | func (Int32Value) valueType() valueType { | ||
279 | return int32ValueType | ||
280 | } | ||
281 | |||
282 | func (v Int32Value) String() string { | ||
283 | return fmt.Sprintf("0x%08x", int32(v)) | ||
284 | } | ||
285 | |||
286 | // encode encodes the Int32Value into an eventstream binary value | ||
287 | // representation. | ||
288 | func (v Int32Value) encode(w io.Writer) error { | ||
289 | raw := rawValue{ | ||
290 | Type: v.valueType(), | ||
291 | } | ||
292 | return raw.encodeScalar(w, v) | ||
293 | } | ||
294 | |||
295 | func (v *Int32Value) decode(r io.Reader) error { | ||
296 | n, err := decodeUint32(r) | ||
297 | if err != nil { | ||
298 | return err | ||
299 | } | ||
300 | |||
301 | *v = Int32Value(n) | ||
302 | return nil | ||
303 | } | ||
304 | |||
305 | // An Int64Value provides eventstream encoding, and representation of a Go | ||
306 | // int64 value. | ||
307 | type Int64Value int64 | ||
308 | |||
309 | // Get returns the underlying value. | ||
310 | func (v Int64Value) Get() interface{} { | ||
311 | return int64(v) | ||
312 | } | ||
313 | |||
314 | // valueType returns the EventStream header value type value. | ||
315 | func (Int64Value) valueType() valueType { | ||
316 | return int64ValueType | ||
317 | } | ||
318 | |||
319 | func (v Int64Value) String() string { | ||
320 | return fmt.Sprintf("0x%016x", int64(v)) | ||
321 | } | ||
322 | |||
323 | // encode encodes the Int64Value into an eventstream binary value | ||
324 | // representation. | ||
325 | func (v Int64Value) encode(w io.Writer) error { | ||
326 | raw := rawValue{ | ||
327 | Type: v.valueType(), | ||
328 | } | ||
329 | return raw.encodeScalar(w, v) | ||
330 | } | ||
331 | |||
332 | func (v *Int64Value) decode(r io.Reader) error { | ||
333 | n, err := decodeUint64(r) | ||
334 | if err != nil { | ||
335 | return err | ||
336 | } | ||
337 | |||
338 | *v = Int64Value(n) | ||
339 | return nil | ||
340 | } | ||
341 | |||
342 | // An BytesValue provides eventstream encoding, and representation of a Go | ||
343 | // byte slice. | ||
344 | type BytesValue []byte | ||
345 | |||
346 | // Get returns the underlying value. | ||
347 | func (v BytesValue) Get() interface{} { | ||
348 | return []byte(v) | ||
349 | } | ||
350 | |||
351 | // valueType returns the EventStream header value type value. | ||
352 | func (BytesValue) valueType() valueType { | ||
353 | return bytesValueType | ||
354 | } | ||
355 | |||
356 | func (v BytesValue) String() string { | ||
357 | return base64.StdEncoding.EncodeToString([]byte(v)) | ||
358 | } | ||
359 | |||
360 | // encode encodes the BytesValue into an eventstream binary value | ||
361 | // representation. | ||
362 | func (v BytesValue) encode(w io.Writer) error { | ||
363 | raw := rawValue{ | ||
364 | Type: v.valueType(), | ||
365 | } | ||
366 | |||
367 | return raw.encodeBytes(w, []byte(v)) | ||
368 | } | ||
369 | |||
370 | func (v *BytesValue) decode(r io.Reader) error { | ||
371 | buf, err := decodeBytesValue(r) | ||
372 | if err != nil { | ||
373 | return err | ||
374 | } | ||
375 | |||
376 | *v = BytesValue(buf) | ||
377 | return nil | ||
378 | } | ||
379 | |||
380 | // An StringValue provides eventstream encoding, and representation of a Go | ||
381 | // string. | ||
382 | type StringValue string | ||
383 | |||
384 | // Get returns the underlying value. | ||
385 | func (v StringValue) Get() interface{} { | ||
386 | return string(v) | ||
387 | } | ||
388 | |||
389 | // valueType returns the EventStream header value type value. | ||
390 | func (StringValue) valueType() valueType { | ||
391 | return stringValueType | ||
392 | } | ||
393 | |||
394 | func (v StringValue) String() string { | ||
395 | return string(v) | ||
396 | } | ||
397 | |||
398 | // encode encodes the StringValue into an eventstream binary value | ||
399 | // representation. | ||
400 | func (v StringValue) encode(w io.Writer) error { | ||
401 | raw := rawValue{ | ||
402 | Type: v.valueType(), | ||
403 | } | ||
404 | |||
405 | return raw.encodeString(w, string(v)) | ||
406 | } | ||
407 | |||
408 | func (v *StringValue) decode(r io.Reader) error { | ||
409 | s, err := decodeStringValue(r) | ||
410 | if err != nil { | ||
411 | return err | ||
412 | } | ||
413 | |||
414 | *v = StringValue(s) | ||
415 | return nil | ||
416 | } | ||
417 | |||
418 | // An TimestampValue provides eventstream encoding, and representation of a Go | ||
419 | // timestamp. | ||
420 | type TimestampValue time.Time | ||
421 | |||
422 | // Get returns the underlying value. | ||
423 | func (v TimestampValue) Get() interface{} { | ||
424 | return time.Time(v) | ||
425 | } | ||
426 | |||
427 | // valueType returns the EventStream header value type value. | ||
428 | func (TimestampValue) valueType() valueType { | ||
429 | return timestampValueType | ||
430 | } | ||
431 | |||
432 | func (v TimestampValue) epochMilli() int64 { | ||
433 | nano := time.Time(v).UnixNano() | ||
434 | msec := nano / int64(time.Millisecond) | ||
435 | return msec | ||
436 | } | ||
437 | |||
438 | func (v TimestampValue) String() string { | ||
439 | msec := v.epochMilli() | ||
440 | return strconv.FormatInt(msec, 10) | ||
441 | } | ||
442 | |||
443 | // encode encodes the TimestampValue into an eventstream binary value | ||
444 | // representation. | ||
445 | func (v TimestampValue) encode(w io.Writer) error { | ||
446 | raw := rawValue{ | ||
447 | Type: v.valueType(), | ||
448 | } | ||
449 | |||
450 | msec := v.epochMilli() | ||
451 | return raw.encodeScalar(w, msec) | ||
452 | } | ||
453 | |||
454 | func (v *TimestampValue) decode(r io.Reader) error { | ||
455 | n, err := decodeUint64(r) | ||
456 | if err != nil { | ||
457 | return err | ||
458 | } | ||
459 | |||
460 | *v = TimestampValue(timeFromEpochMilli(int64(n))) | ||
461 | return nil | ||
462 | } | ||
463 | |||
464 | func timeFromEpochMilli(t int64) time.Time { | ||
465 | secs := t / 1e3 | ||
466 | msec := t % 1e3 | ||
467 | return time.Unix(secs, msec*int64(time.Millisecond)).UTC() | ||
468 | } | ||
469 | |||
470 | // An UUIDValue provides eventstream encoding, and representation of a UUID | ||
471 | // value. | ||
472 | type UUIDValue [16]byte | ||
473 | |||
474 | // Get returns the underlying value. | ||
475 | func (v UUIDValue) Get() interface{} { | ||
476 | return v[:] | ||
477 | } | ||
478 | |||
479 | // valueType returns the EventStream header value type value. | ||
480 | func (UUIDValue) valueType() valueType { | ||
481 | return uuidValueType | ||
482 | } | ||
483 | |||
484 | func (v UUIDValue) String() string { | ||
485 | return fmt.Sprintf(`%X-%X-%X-%X-%X`, v[0:4], v[4:6], v[6:8], v[8:10], v[10:]) | ||
486 | } | ||
487 | |||
488 | // encode encodes the UUIDValue into an eventstream binary value | ||
489 | // representation. | ||
490 | func (v UUIDValue) encode(w io.Writer) error { | ||
491 | raw := rawValue{ | ||
492 | Type: v.valueType(), | ||
493 | } | ||
494 | |||
495 | return raw.encodeFixedSlice(w, v[:]) | ||
496 | } | ||
497 | |||
498 | func (v *UUIDValue) decode(r io.Reader) error { | ||
499 | tv := (*v)[:] | ||
500 | return decodeFixedBytesValue(r, tv) | ||
501 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/message.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/message.go new file mode 100644 index 0000000..2dc012a --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/eventstream/message.go | |||
@@ -0,0 +1,103 @@ | |||
1 | package eventstream | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/binary" | ||
6 | "hash/crc32" | ||
7 | ) | ||
8 | |||
9 | const preludeLen = 8 | ||
10 | const preludeCRCLen = 4 | ||
11 | const msgCRCLen = 4 | ||
12 | const minMsgLen = preludeLen + preludeCRCLen + msgCRCLen | ||
13 | const maxPayloadLen = 1024 * 1024 * 16 // 16MB | ||
14 | const maxHeadersLen = 1024 * 128 // 128KB | ||
15 | const maxMsgLen = minMsgLen + maxHeadersLen + maxPayloadLen | ||
16 | |||
17 | var crc32IEEETable = crc32.MakeTable(crc32.IEEE) | ||
18 | |||
19 | // A Message provides the eventstream message representation. | ||
20 | type Message struct { | ||
21 | Headers Headers | ||
22 | Payload []byte | ||
23 | } | ||
24 | |||
25 | func (m *Message) rawMessage() (rawMessage, error) { | ||
26 | var raw rawMessage | ||
27 | |||
28 | if len(m.Headers) > 0 { | ||
29 | var headers bytes.Buffer | ||
30 | if err := encodeHeaders(&headers, m.Headers); err != nil { | ||
31 | return rawMessage{}, err | ||
32 | } | ||
33 | raw.Headers = headers.Bytes() | ||
34 | raw.HeadersLen = uint32(len(raw.Headers)) | ||
35 | } | ||
36 | |||
37 | raw.Length = raw.HeadersLen + uint32(len(m.Payload)) + minMsgLen | ||
38 | |||
39 | hash := crc32.New(crc32IEEETable) | ||
40 | binaryWriteFields(hash, binary.BigEndian, raw.Length, raw.HeadersLen) | ||
41 | raw.PreludeCRC = hash.Sum32() | ||
42 | |||
43 | binaryWriteFields(hash, binary.BigEndian, raw.PreludeCRC) | ||
44 | |||
45 | if raw.HeadersLen > 0 { | ||
46 | hash.Write(raw.Headers) | ||
47 | } | ||
48 | |||
49 | // Read payload bytes and update hash for it as well. | ||
50 | if len(m.Payload) > 0 { | ||
51 | raw.Payload = m.Payload | ||
52 | hash.Write(raw.Payload) | ||
53 | } | ||
54 | |||
55 | raw.CRC = hash.Sum32() | ||
56 | |||
57 | return raw, nil | ||
58 | } | ||
59 | |||
60 | type messagePrelude struct { | ||
61 | Length uint32 | ||
62 | HeadersLen uint32 | ||
63 | PreludeCRC uint32 | ||
64 | } | ||
65 | |||
66 | func (p messagePrelude) PayloadLen() uint32 { | ||
67 | return p.Length - p.HeadersLen - minMsgLen | ||
68 | } | ||
69 | |||
70 | func (p messagePrelude) ValidateLens() error { | ||
71 | if p.Length == 0 || p.Length > maxMsgLen { | ||
72 | return LengthError{ | ||
73 | Part: "message prelude", | ||
74 | Want: maxMsgLen, | ||
75 | Have: int(p.Length), | ||
76 | } | ||
77 | } | ||
78 | if p.HeadersLen > maxHeadersLen { | ||
79 | return LengthError{ | ||
80 | Part: "message headers", | ||
81 | Want: maxHeadersLen, | ||
82 | Have: int(p.HeadersLen), | ||
83 | } | ||
84 | } | ||
85 | if payloadLen := p.PayloadLen(); payloadLen > maxPayloadLen { | ||
86 | return LengthError{ | ||
87 | Part: "message payload", | ||
88 | Want: maxPayloadLen, | ||
89 | Have: int(payloadLen), | ||
90 | } | ||
91 | } | ||
92 | |||
93 | return nil | ||
94 | } | ||
95 | |||
96 | type rawMessage struct { | ||
97 | messagePrelude | ||
98 | |||
99 | Headers []byte | ||
100 | Payload []byte | ||
101 | |||
102 | CRC uint32 | ||
103 | } | ||