OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/page/EventSourceParser.h" | |
6 | |
7 #include "core/EventTypeNames.h" | |
8 #include "core/page/EventSource.h" | |
9 #include "wtf/ASCIICType.h" | |
10 #include "wtf/Assertions.h" | |
11 #include "wtf/NotFound.h" | |
12 #include "wtf/StdLibExtras.h" | |
13 #include "wtf/text/TextEncoding.h" | |
14 #include "wtf/text/TextEncodingRegistry.h" | |
15 | |
16 namespace blink { | |
17 | |
18 EventSourceParser::EventSourceParser(const AtomicString& lastEventId, Client* cl ient) | |
19 : m_id(lastEventId) | |
20 , m_lastEventId(lastEventId) | |
21 , m_client(client) | |
22 , m_codec(newTextCodec(UTF8Encoding())) | |
23 { | |
24 } | |
25 | |
26 void EventSourceParser::addBytes(const char* bytes, size_t size) | |
27 { | |
28 // A line consists of |m_line| followed by | |
29 // |bytes[start..(next line break)]|. | |
30 size_t start = 0; | |
31 const unsigned char kBOM[] = {0xef, 0xbb, 0xbf}; | |
pfeldman
2016/02/11 18:54:41
Do you want to run this by security team since it
Tom Sepez
2016/02/12 17:14:31
The array indexing in here looks ok, what you do w
| |
32 for (size_t i = 0; i < size && !m_isStopped; ++i) { | |
33 // As kBOM contains neither CR nor LF, we can think BOM and the line | |
34 // break separately. | |
35 if (m_isRecognizingBOM && m_line.size() + (i - start) == WTF_ARRAY_LENGT H(kBOM)) { | |
36 Vector<char> line = m_line; | |
37 line.append(&bytes[start], i - start); | |
38 ASSERT(line.size() == WTF_ARRAY_LENGTH(kBOM)); | |
39 m_isRecognizingBOM = false; | |
40 if (memcmp(line.data(), kBOM, sizeof(kBOM)) == 0) { | |
41 start = i; | |
42 m_line.clear(); | |
43 continue; | |
44 } | |
45 } | |
46 if (m_isRecognizingCRLF && bytes[i] == '\n') { | |
47 // This is the latter part of "\r\n". | |
48 m_isRecognizingCRLF = false; | |
49 ++start; | |
50 continue; | |
51 } | |
52 m_isRecognizingCRLF = false; | |
53 if (bytes[i] == '\r' || bytes[i] == '\n') { | |
54 m_line.append(&bytes[start], i - start); | |
55 parseLine(); | |
56 m_line.clear(); | |
57 start = i + 1; | |
58 m_isRecognizingCRLF = bytes[i] == '\r'; | |
59 m_isRecognizingBOM = false; | |
60 } | |
61 } | |
62 if (m_isStopped) | |
63 return; | |
64 m_line.append(&bytes[start], size - start); | |
65 } | |
66 | |
67 void EventSourceParser::parseLine() | |
68 { | |
69 if (m_line.size() == 0) { | |
70 m_lastEventId = m_id; | |
71 // We dispatch an event when seeing an empty line. | |
72 if (!m_data.isEmpty()) { | |
73 ASSERT(m_data[m_data.size() - 1] == '\n'); | |
74 String data = fromUTF8(m_data.data(), m_data.size() - 1); | |
75 m_client->onMessageEvent(m_eventType.isEmpty() ? EventTypeNames::mes sage : m_eventType, data, m_lastEventId); | |
76 m_data.clear(); | |
77 } | |
78 m_eventType = nullAtom; | |
79 return; | |
80 } | |
81 size_t fieldNameEnd = m_line.find(':'); | |
82 size_t fieldValueStart; | |
83 if (fieldNameEnd == WTF::kNotFound) { | |
84 fieldNameEnd = m_line.size(); | |
85 fieldValueStart = fieldNameEnd; | |
86 } else { | |
87 fieldValueStart = fieldNameEnd + 1; | |
88 if (fieldValueStart < m_line.size() && m_line[fieldValueStart] == ' ') { | |
89 ++fieldValueStart; | |
90 } | |
91 } | |
92 size_t fieldValueSize = m_line.size() - fieldValueStart; | |
93 String fieldName = fromUTF8(m_line.data(), fieldNameEnd); | |
94 if (fieldName == "event") { | |
95 m_eventType = AtomicString(fromUTF8(m_line.data() + fieldValueStart, fie ldValueSize)); | |
96 } else if (fieldName == "data") { | |
kinuko
2016/02/18 09:15:00
nit: maybe return at the end of each if and make "
yhirano
2016/02/18 23:06:43
Done.
| |
97 m_data.append(m_line.data() + fieldValueStart, fieldValueSize); | |
98 m_data.append('\n'); | |
99 } else if (fieldName == "id") { | |
100 m_id = AtomicString(fromUTF8(m_line.data() + fieldValueStart, fieldValue Size)); | |
101 } else if (fieldName == "retry") { | |
102 bool hasOnlyDigits = true; | |
103 for (size_t i = fieldValueStart; i < m_line.size() && hasOnlyDigits; ++i ) | |
104 hasOnlyDigits = isASCIIDigit(m_line[i]); | |
105 if (fieldValueStart == m_line.size()) { | |
106 m_client->onReconnectionTimeSet(EventSource::defaultReconnectDelay); | |
107 } else if (hasOnlyDigits) { | |
108 bool ok; | |
109 auto reconnectionTime = fromUTF8(m_line.data() + fieldValueStart, fi eldValueSize).toUInt64Strict(&ok); | |
110 if (ok) | |
111 m_client->onReconnectionTimeSet(reconnectionTime); | |
112 } | |
113 } else { | |
114 // Unrecognized field name. Ignore! | |
115 } | |
116 } | |
117 | |
118 String EventSourceParser::fromUTF8(const char* bytes, size_t size) | |
119 { | |
120 return m_codec->decode(bytes, size, WTF::DataEOF); | |
121 } | |
122 | |
123 DEFINE_TRACE(EventSourceParser) | |
124 { | |
125 visitor->trace(m_client); | |
126 } | |
127 | |
128 } // namespace blink | |
129 | |
OLD | NEW |