Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(677)

Unified Diff: third_party/WebKit/Source/core/page/EventSourceParser.cpp

Issue 1642563002: Introduce EventSourceParser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@event-source-retry-fix
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/page/EventSourceParser.cpp
diff --git a/third_party/WebKit/Source/core/page/EventSourceParser.cpp b/third_party/WebKit/Source/core/page/EventSourceParser.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b4118e8e4928b2f6a58464fcdaa8c7c768d5f0c9
--- /dev/null
+++ b/third_party/WebKit/Source/core/page/EventSourceParser.cpp
@@ -0,0 +1,121 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/page/EventSourceParser.h"
+
+#include "core/EventTypeNames.h"
+#include "core/page/EventSource.h"
+#include "wtf/ASCIICType.h"
+#include "wtf/NotFound.h"
+#include "wtf/StdLibExtras.h"
+#include "wtf/text/TextCodec.h"
+#include "wtf/text/TextEncoding.h"
+#include "wtf/text/TextEncodingRegistry.h"
+
+namespace blink {
+
+namespace {
+
+String fromUTF8(const char* bytes, size_t size)
+{
+ return newTextCodec(UTF8Encoding())->decode(bytes, size, WTF::DataEOF);
+}
+
+} // namespace
+
+void EventSourceParser::addBytes(const char* bytes, size_t size)
+{
+ size_t start = 0;
tyoshino (SeeGerritForStatus) 2016/01/28 07:31:43 explain that start is the start position of non-BO
yhirano 2016/01/28 08:45:36 Done.
+ const char kBOM[] = {0xef, 0xbb, 0xbf};
+ for (size_t i = 0; i < size && !m_isStopped; ++i) {
tyoshino (SeeGerritForStatus) 2016/01/28 07:31:43 explain that kBOM doesn't contain either CL or LF
yhirano 2016/01/28 08:45:36 Done.
+ if (m_isRecognizingBOM && m_line.size() + (i - start) == WTF_ARRAY_LENGTH(kBOM)) {
+ Vector<char> line = m_line;
+ line.append(&bytes[start], i - start);
+ ASSERT(line.size() == WTF_ARRAY_LENGTH(kBOM));
+ m_isRecognizingBOM = false;
+ if (strncmp(line.data(), kBOM, WTF_ARRAY_LENGTH(kBOM)) == 0) {
+ start = i;
+ m_line.clear();
+ continue;
+ }
+ }
+ if (m_isRecognizingCRLF && bytes[i] == '\n') {
+ // This is the latter part of "\r\n".
+ m_isRecognizingCRLF = false;
+ ++start;
+ continue;
+ }
+ m_isRecognizingCRLF = false;
+ if (bytes[i] == '\r' || bytes[i] == '\n') {
+ m_line.append(&bytes[start], i - start);
+ parseLine();
+ m_line.clear();
+ start = i + 1;
+ m_isRecognizingCRLF = bytes[i] == '\r';
+ m_isRecognizingBOM = false;
+ }
+ }
+ if (m_isStopped)
+ return;
+ m_line.append(&bytes[start], size - start);
+}
+
+void EventSourceParser::parseLine()
+{
+ if (m_line.size() == 0) {
+ // We dispatch an event when seeing an empty line.
+ if (!m_data.isEmpty()) {
+ ASSERT(m_data[m_data.size() - 1] == '\n');
+ m_lastEventId = m_id;
+ String data = fromUTF8(m_data.data(), m_data.size() - 1);
+ m_client->onMessageEvent(m_event.isEmpty() ? EventTypeNames::message : m_event, data, m_lastEventId);
+ m_data.clear();
+ }
+ m_event = AtomicString();
tyoshino (SeeGerritForStatus) 2016/01/28 07:31:43 use nullAtom?
yhirano 2016/01/28 08:45:36 Done.
+ return;
+ }
+ size_t fieldNameEnd = m_line.find(':');
+ size_t fieldValueStart;
+ if (fieldNameEnd == WTF::kNotFound) {
+ fieldNameEnd = m_line.size();
+ fieldValueStart = fieldNameEnd;
+ } else {
+ fieldValueStart = fieldNameEnd + 1;
+ if (fieldValueStart < m_line.size() && m_line[fieldValueStart] == ' ') {
+ ++fieldValueStart;
+ }
+ }
+ size_t fieldValueSize = m_line.size() - fieldValueStart;
+ String fieldName = fromUTF8(m_line.data(), fieldNameEnd);
+ if (fieldName == "event") {
+ m_event = AtomicString(fromUTF8(m_line.data() + fieldValueStart, fieldValueSize));
+ } else if (fieldName == "data") {
+ m_data.append(m_line.data() + fieldValueStart, fieldValueSize);
+ m_data.append('\n');
+ } else if (fieldName == "id") {
+ m_id = AtomicString(fromUTF8(m_line.data() + fieldValueStart, fieldValueSize));
+ } else if (fieldName == "retry") {
+ bool hasOnlyDigits = true;
+ for (size_t i = fieldValueStart; i < m_line.size() && hasOnlyDigits; ++i)
+ hasOnlyDigits = isASCIIDigit(m_line[i]);
+ if (fieldValueStart == m_line.size()) {
+ m_client->onReconnectionTimeSet(EventSource::defaultReconnectDelay);
+ } else if (hasOnlyDigits) {
+ bool ok;
+ auto reconnectionTime = fromUTF8(m_line.data() + fieldValueStart, fieldValueSize).toUInt64(&ok);
tyoshino (SeeGerritForStatus) 2016/01/28 07:31:43 can we use charactersToUInt64() directly?
yhirano 2016/01/28 08:45:36 charactersToUint64 expects LChar* or UChar* and we
+ if (ok)
+ m_client->onReconnectionTimeSet(reconnectionTime);
+ }
+ } else {
+ // Unrecognized field name. Ignore!
+ }
+}
+
+DEFINE_TRACE(EventSourceParser)
+{
+ visitor->trace(m_client);
+}
+
+} // namespace blink
+

Powered by Google App Engine
This is Rietveld 408576698