1 // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2 // Use of this source code is governed by a MIT style
3 // license that can be found in the LICENSE file.
17 func Decode(r io.Reader) ([]Event, error) {
22 func (d *decoder) dispatchEvent(event Event, data string) {
23 dataLength := len(data)
25 //If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
26 data = data[:dataLength-1]
29 if dataLength == 0 && event.Event == "" {
32 if event.Event == "" {
33 event.Event = "message"
36 d.events = append(d.events, event)
39 func (d *decoder) decode(r io.Reader) ([]Event, error) {
40 buf, err := ioutil.ReadAll(r)
45 var currentEvent Event
46 var dataBuffer *bytes.Buffer = new(bytes.Buffer)
47 // TODO (and unit tests)
48 // Lines must be separated by either a U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair,
49 // a single U+000A LINE FEED (LF) character,
50 // or a single U+000D CARRIAGE RETURN (CR) character.
51 lines := bytes.Split(buf, []byte{'\n'})
52 for _, line := range lines {
54 // If the line is empty (a blank line). Dispatch the event.
55 d.dispatchEvent(currentEvent, dataBuffer.String())
57 // reset current event and data buffer
58 currentEvent = Event{}
62 if line[0] == byte(':') {
63 // If the line starts with a U+003A COLON character (:), ignore the line.
67 var field, value []byte
68 colonIndex := bytes.IndexRune(line, ':')
70 // If the line contains a U+003A COLON character character (:)
71 // Collect the characters on the line before the first U+003A COLON character (:),
72 // and let field be that string.
73 field = line[:colonIndex]
74 // Collect the characters on the line after the first U+003A COLON character (:),
75 // and let value be that string.
76 value = line[colonIndex+1:]
77 // If value starts with a single U+0020 SPACE character, remove it from value.
78 if len(value) > 0 && value[0] == ' ' {
82 // Otherwise, the string is not empty but does not contain a U+003A COLON character character (:)
83 // Use the whole line as the field name, and the empty string as the field value.
87 // The steps to process the field given a field name and a field value depend on the field name,
88 // as given in the following list. Field names must be compared literally,
89 // with no case folding performed.
90 switch string(field) {
92 // Set the event name buffer to field value.
93 currentEvent.Event = string(value)
95 // Set the event stream's last event ID to the field value.
96 currentEvent.Id = string(value)
98 // If the field value consists of only characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
99 // then interpret the field value as an integer in base ten, and set the event stream's reconnection time to that integer.
100 // Otherwise, ignore the field.
101 currentEvent.Id = string(value)
103 // Append the field value to the data buffer,
104 dataBuffer.Write(value)
105 // then append a single U+000A LINE FEED (LF) character to the data buffer.
106 dataBuffer.WriteString("\n")
108 //Otherwise. The field is ignored.
112 // Once the end of the file is reached, the user agent must dispatch the event one final time.
113 d.dispatchEvent(currentEvent, dataBuffer.String())