0
|
1 |
/*
|
|
2 |
* Copyright (C) 2009 Ericsson AB
|
|
3 |
* All rights reserved.
|
|
4 |
*
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
6 |
* modification, are permitted provided that the following conditions
|
|
7 |
* are met:
|
|
8 |
*
|
|
9 |
* 1. Redistributions of source code must retain the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer.
|
|
11 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer
|
|
13 |
* in the documentation and/or other materials provided with the
|
|
14 |
* distribution.
|
|
15 |
* 3. Neither the name of Ericsson nor the names of its contributors
|
|
16 |
* may be used to endorse or promote products derived from this
|
|
17 |
* software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
#include "config.h"
|
|
33 |
|
|
34 |
#if ENABLE(EVENTSOURCE)
|
|
35 |
|
|
36 |
#include "EventSource.h"
|
|
37 |
|
|
38 |
#include "Cache.h"
|
|
39 |
#include "DOMWindow.h"
|
|
40 |
#include "Event.h"
|
|
41 |
#include "EventException.h"
|
|
42 |
#include "PlatformString.h"
|
|
43 |
#include "MessageEvent.h"
|
|
44 |
#include "ResourceError.h"
|
|
45 |
#include "ResourceRequest.h"
|
|
46 |
#include "ResourceResponse.h"
|
|
47 |
#include "ScriptExecutionContext.h"
|
|
48 |
#include "SerializedScriptValue.h"
|
|
49 |
#include "TextResourceDecoder.h"
|
|
50 |
#include "ThreadableLoader.h"
|
|
51 |
|
|
52 |
namespace WebCore {
|
|
53 |
|
|
54 |
const unsigned long long EventSource::defaultReconnectDelay = 3000;
|
|
55 |
|
|
56 |
EventSource::EventSource(const String& url, ScriptExecutionContext* context, ExceptionCode& ec)
|
|
57 |
: ActiveDOMObject(context, this)
|
|
58 |
, m_state(CONNECTING)
|
|
59 |
, m_reconnectTimer(this, &EventSource::reconnectTimerFired)
|
|
60 |
, m_failSilently(false)
|
|
61 |
, m_requestInFlight(false)
|
|
62 |
, m_reconnectDelay(defaultReconnectDelay)
|
|
63 |
{
|
|
64 |
if (url.isEmpty() || !(m_url = context->completeURL(url)).isValid()) {
|
|
65 |
ec = SYNTAX_ERR;
|
|
66 |
return;
|
|
67 |
}
|
|
68 |
// FIXME: should support cross-origin requests
|
|
69 |
if (!scriptExecutionContext()->securityOrigin()->canRequest(m_url)) {
|
|
70 |
ec = SECURITY_ERR;
|
|
71 |
return;
|
|
72 |
}
|
|
73 |
|
|
74 |
m_origin = scriptExecutionContext()->securityOrigin()->toString();
|
|
75 |
m_decoder = TextResourceDecoder::create("text/plain", "UTF-8");
|
|
76 |
|
|
77 |
setPendingActivity(this);
|
|
78 |
connect();
|
|
79 |
}
|
|
80 |
|
|
81 |
EventSource::~EventSource()
|
|
82 |
{
|
|
83 |
}
|
|
84 |
|
|
85 |
void EventSource::connect()
|
|
86 |
{
|
|
87 |
ResourceRequest request(m_url);
|
|
88 |
request.setHTTPMethod("GET");
|
|
89 |
request.setHTTPHeaderField("Accept", "text/event-stream");
|
|
90 |
request.setHTTPHeaderField("Cache-Control", "no-cache");
|
|
91 |
if (!m_lastEventId.isEmpty())
|
|
92 |
request.setHTTPHeaderField("Last-Event-ID", m_lastEventId);
|
|
93 |
|
|
94 |
ThreadableLoaderOptions options;
|
|
95 |
options.sendLoadCallbacks = true;
|
|
96 |
options.sniffContent = false;
|
|
97 |
options.allowCredentials = true;
|
|
98 |
|
|
99 |
m_loader = ThreadableLoader::create(scriptExecutionContext(), this, request, options);
|
|
100 |
|
|
101 |
m_requestInFlight = true;
|
|
102 |
|
|
103 |
if (!scriptExecutionContext()->isWorkerContext())
|
|
104 |
cache()->loader()->nonCacheRequestInFlight(m_url);
|
|
105 |
}
|
|
106 |
|
|
107 |
void EventSource::endRequest()
|
|
108 |
{
|
|
109 |
m_requestInFlight = false;
|
|
110 |
|
|
111 |
if (!m_failSilently)
|
|
112 |
dispatchEvent(Event::create(eventNames().errorEvent, false, false));
|
|
113 |
|
|
114 |
if (!scriptExecutionContext()->isWorkerContext())
|
|
115 |
cache()->loader()->nonCacheRequestComplete(m_url);
|
|
116 |
|
|
117 |
if (m_state != CLOSED)
|
|
118 |
scheduleReconnect();
|
|
119 |
else
|
|
120 |
unsetPendingActivity(this);
|
|
121 |
}
|
|
122 |
|
|
123 |
void EventSource::scheduleReconnect()
|
|
124 |
{
|
|
125 |
m_state = CONNECTING;
|
|
126 |
m_reconnectTimer.startOneShot(m_reconnectDelay / 1000);
|
|
127 |
}
|
|
128 |
|
|
129 |
void EventSource::reconnectTimerFired(Timer<EventSource>*)
|
|
130 |
{
|
|
131 |
connect();
|
|
132 |
}
|
|
133 |
|
|
134 |
String EventSource::url() const
|
|
135 |
{
|
|
136 |
return m_url.string();
|
|
137 |
}
|
|
138 |
|
|
139 |
EventSource::State EventSource::readyState() const
|
|
140 |
{
|
|
141 |
return m_state;
|
|
142 |
}
|
|
143 |
|
|
144 |
void EventSource::close()
|
|
145 |
{
|
|
146 |
if (m_state == CLOSED)
|
|
147 |
return;
|
|
148 |
|
|
149 |
if (m_reconnectTimer.isActive()) {
|
|
150 |
m_reconnectTimer.stop();
|
|
151 |
unsetPendingActivity(this);
|
|
152 |
}
|
|
153 |
|
|
154 |
m_state = CLOSED;
|
|
155 |
m_failSilently = true;
|
|
156 |
|
|
157 |
if (m_requestInFlight)
|
|
158 |
m_loader->cancel();
|
|
159 |
}
|
|
160 |
|
|
161 |
ScriptExecutionContext* EventSource::scriptExecutionContext() const
|
|
162 |
{
|
|
163 |
return ActiveDOMObject::scriptExecutionContext();
|
|
164 |
}
|
|
165 |
|
|
166 |
void EventSource::didReceiveResponse(const ResourceResponse& response)
|
|
167 |
{
|
|
168 |
int statusCode = response.httpStatusCode();
|
|
169 |
if (statusCode == 200 && response.httpHeaderField("Content-Type") == "text/event-stream") {
|
|
170 |
m_state = OPEN;
|
|
171 |
dispatchEvent(Event::create(eventNames().openEvent, false, false));
|
|
172 |
} else {
|
|
173 |
if (statusCode <= 200 || statusCode > 299)
|
|
174 |
m_state = CLOSED;
|
|
175 |
m_loader->cancel();
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
void EventSource::didReceiveData(const char* data, int length)
|
|
180 |
{
|
|
181 |
append(m_receiveBuf, m_decoder->decode(data, length));
|
|
182 |
parseEventStream();
|
|
183 |
}
|
|
184 |
|
|
185 |
void EventSource::didFinishLoading(unsigned long)
|
|
186 |
{
|
|
187 |
if (m_receiveBuf.size() > 0 || m_data.size() > 0) {
|
|
188 |
append(m_receiveBuf, "\n\n");
|
|
189 |
parseEventStream();
|
|
190 |
}
|
|
191 |
m_state = CONNECTING;
|
|
192 |
endRequest();
|
|
193 |
}
|
|
194 |
|
|
195 |
void EventSource::didFail(const ResourceError& error)
|
|
196 |
{
|
|
197 |
int canceled = error.isCancellation();
|
|
198 |
if (((m_state == CONNECTING) && !canceled) || ((m_state == OPEN) && canceled))
|
|
199 |
m_state = CLOSED;
|
|
200 |
endRequest();
|
|
201 |
}
|
|
202 |
|
|
203 |
void EventSource::didFailRedirectCheck()
|
|
204 |
{
|
|
205 |
m_state = CLOSED;
|
|
206 |
m_loader->cancel();
|
|
207 |
}
|
|
208 |
|
|
209 |
void EventSource::parseEventStream()
|
|
210 |
{
|
|
211 |
unsigned int bufPos = 0;
|
|
212 |
unsigned int bufSize = m_receiveBuf.size();
|
|
213 |
for (;;) {
|
|
214 |
int lineLength = -1;
|
|
215 |
int fieldLength = -1;
|
|
216 |
int carriageReturn = 0;
|
|
217 |
for (unsigned int i = bufPos; lineLength < 0 && i < bufSize; i++) {
|
|
218 |
switch (m_receiveBuf[i]) {
|
|
219 |
case ':':
|
|
220 |
if (fieldLength < 0)
|
|
221 |
fieldLength = i - bufPos;
|
|
222 |
break;
|
|
223 |
case '\n':
|
|
224 |
if (i > bufPos && m_receiveBuf[i - 1] == '\r') {
|
|
225 |
carriageReturn++;
|
|
226 |
i--;
|
|
227 |
}
|
|
228 |
lineLength = i - bufPos;
|
|
229 |
break;
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
if (lineLength < 0)
|
|
234 |
break;
|
|
235 |
|
|
236 |
parseEventStreamLine(bufPos, fieldLength, lineLength);
|
|
237 |
bufPos += lineLength + carriageReturn + 1;
|
|
238 |
}
|
|
239 |
|
|
240 |
if (bufPos == bufSize)
|
|
241 |
m_receiveBuf.clear();
|
|
242 |
else if (bufPos)
|
|
243 |
m_receiveBuf.remove(0, bufPos);
|
|
244 |
}
|
|
245 |
|
|
246 |
void EventSource::parseEventStreamLine(unsigned int bufPos, int fieldLength, int lineLength)
|
|
247 |
{
|
|
248 |
if (!lineLength) {
|
|
249 |
if (!m_data.isEmpty())
|
|
250 |
dispatchEvent(createMessageEvent());
|
|
251 |
if (!m_eventName.isEmpty())
|
|
252 |
m_eventName = "";
|
|
253 |
} else if (fieldLength) {
|
|
254 |
bool noValue = fieldLength < 0;
|
|
255 |
|
|
256 |
String field(&m_receiveBuf[bufPos], noValue ? lineLength : fieldLength);
|
|
257 |
int step;
|
|
258 |
if (noValue)
|
|
259 |
step = lineLength;
|
|
260 |
else if (m_receiveBuf[bufPos + fieldLength + 1] != ' ')
|
|
261 |
step = fieldLength + 1;
|
|
262 |
else
|
|
263 |
step = fieldLength + 2;
|
|
264 |
bufPos += step;
|
|
265 |
int valueLength = lineLength - step;
|
|
266 |
|
|
267 |
if (field == "data") {
|
|
268 |
if (m_data.size() > 0)
|
|
269 |
m_data.append('\n');
|
|
270 |
if (valueLength)
|
|
271 |
m_data.append(&m_receiveBuf[bufPos], valueLength);
|
|
272 |
} else if (field == "event")
|
|
273 |
m_eventName = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : "";
|
|
274 |
else if (field == "id")
|
|
275 |
m_lastEventId = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : "";
|
|
276 |
else if (field == "retry") {
|
|
277 |
if (!valueLength)
|
|
278 |
m_reconnectDelay = defaultReconnectDelay;
|
|
279 |
else {
|
|
280 |
String value(&m_receiveBuf[bufPos], valueLength);
|
|
281 |
bool ok;
|
|
282 |
unsigned long long retry = value.toUInt64(&ok);
|
|
283 |
if (ok)
|
|
284 |
m_reconnectDelay = retry;
|
|
285 |
}
|
|
286 |
}
|
|
287 |
}
|
|
288 |
}
|
|
289 |
|
|
290 |
void EventSource::stop()
|
|
291 |
{
|
|
292 |
close();
|
|
293 |
}
|
|
294 |
|
|
295 |
PassRefPtr<MessageEvent> EventSource::createMessageEvent()
|
|
296 |
{
|
|
297 |
RefPtr<MessageEvent> event = MessageEvent::create();
|
|
298 |
event->initMessageEvent(m_eventName.isEmpty() ? eventNames().messageEvent : AtomicString(m_eventName), false, false, SerializedScriptValue::create(String::adopt(m_data)), m_origin, m_lastEventId, 0, 0);
|
|
299 |
return event.release();
|
|
300 |
}
|
|
301 |
|
|
302 |
EventTargetData* EventSource::eventTargetData()
|
|
303 |
{
|
|
304 |
return &m_eventTargetData;
|
|
305 |
}
|
|
306 |
|
|
307 |
EventTargetData* EventSource::ensureEventTargetData()
|
|
308 |
{
|
|
309 |
return &m_eventTargetData;
|
|
310 |
}
|
|
311 |
|
|
312 |
} // namespace WebCore
|
|
313 |
|
|
314 |
#endif // ENABLE(EVENTSOURCE)
|