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}; |
| 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 return; |
| 97 } |
| 98 if (fieldName == "data") { |
| 99 m_data.append(m_line.data() + fieldValueStart, fieldValueSize); |
| 100 m_data.append('\n'); |
| 101 return; |
| 102 } |
| 103 if (fieldName == "id") { |
| 104 m_id = AtomicString(fromUTF8(m_line.data() + fieldValueStart, fieldValue
Size)); |
| 105 return; |
| 106 } |
| 107 if (fieldName == "retry") { |
| 108 bool hasOnlyDigits = true; |
| 109 for (size_t i = fieldValueStart; i < m_line.size() && hasOnlyDigits; ++i
) |
| 110 hasOnlyDigits = isASCIIDigit(m_line[i]); |
| 111 if (fieldValueStart == m_line.size()) { |
| 112 m_client->onReconnectionTimeSet(EventSource::defaultReconnectDelay); |
| 113 } else if (hasOnlyDigits) { |
| 114 bool ok; |
| 115 auto reconnectionTime = fromUTF8(m_line.data() + fieldValueStart, fi
eldValueSize).toUInt64Strict(&ok); |
| 116 if (ok) |
| 117 m_client->onReconnectionTimeSet(reconnectionTime); |
| 118 } |
| 119 return; |
| 120 } |
| 121 // Unrecognized field name. Ignore! |
| 122 } |
| 123 |
| 124 String EventSourceParser::fromUTF8(const char* bytes, size_t size) |
| 125 { |
| 126 return m_codec->decode(bytes, size, WTF::DataEOF); |
| 127 } |
| 128 |
| 129 DEFINE_TRACE(EventSourceParser) |
| 130 { |
| 131 visitor->trace(m_client); |
| 132 } |
| 133 |
| 134 } // namespace blink |
| 135 |
OLD | NEW |