author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
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) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
, m_discardTrailingNewline(false) |
0 | 61 |
, m_failSilently(false) |
62 |
, m_requestInFlight(false) |
|
63 |
, m_reconnectDelay(defaultReconnectDelay) |
|
64 |
{ |
|
65 |
if (url.isEmpty() || !(m_url = context->completeURL(url)).isValid()) { |
|
66 |
ec = SYNTAX_ERR; |
|
67 |
return; |
|
68 |
} |
|
69 |
// FIXME: should support cross-origin requests |
|
70 |
if (!scriptExecutionContext()->securityOrigin()->canRequest(m_url)) { |
|
71 |
ec = SECURITY_ERR; |
|
72 |
return; |
|
73 |
} |
|
74 |
||
75 |
m_origin = scriptExecutionContext()->securityOrigin()->toString(); |
|
76 |
m_decoder = TextResourceDecoder::create("text/plain", "UTF-8"); |
|
77 |
||
78 |
setPendingActivity(this); |
|
79 |
connect(); |
|
80 |
} |
|
81 |
||
82 |
EventSource::~EventSource() |
|
83 |
{ |
|
84 |
} |
|
85 |
||
86 |
void EventSource::connect() |
|
87 |
{ |
|
88 |
ResourceRequest request(m_url); |
|
89 |
request.setHTTPMethod("GET"); |
|
90 |
request.setHTTPHeaderField("Accept", "text/event-stream"); |
|
91 |
request.setHTTPHeaderField("Cache-Control", "no-cache"); |
|
92 |
if (!m_lastEventId.isEmpty()) |
|
93 |
request.setHTTPHeaderField("Last-Event-ID", m_lastEventId); |
|
94 |
||
95 |
ThreadableLoaderOptions options; |
|
96 |
options.sendLoadCallbacks = true; |
|
97 |
options.sniffContent = false; |
|
98 |
options.allowCredentials = true; |
|
99 |
||
100 |
m_loader = ThreadableLoader::create(scriptExecutionContext(), this, request, options); |
|
101 |
||
102 |
m_requestInFlight = true; |
|
103 |
||
104 |
if (!scriptExecutionContext()->isWorkerContext()) |
|
105 |
cache()->loader()->nonCacheRequestInFlight(m_url); |
|
106 |
} |
|
107 |
||
108 |
void EventSource::endRequest() |
|
109 |
{ |
|
110 |
m_requestInFlight = false; |
|
111 |
||
112 |
if (!m_failSilently) |
|
113 |
dispatchEvent(Event::create(eventNames().errorEvent, false, false)); |
|
114 |
||
115 |
if (!scriptExecutionContext()->isWorkerContext()) |
|
116 |
cache()->loader()->nonCacheRequestComplete(m_url); |
|
117 |
||
118 |
if (m_state != CLOSED) |
|
119 |
scheduleReconnect(); |
|
120 |
else |
|
121 |
unsetPendingActivity(this); |
|
122 |
} |
|
123 |
||
124 |
void EventSource::scheduleReconnect() |
|
125 |
{ |
|
126 |
m_state = CONNECTING; |
|
127 |
m_reconnectTimer.startOneShot(m_reconnectDelay / 1000); |
|
128 |
} |
|
129 |
||
130 |
void EventSource::reconnectTimerFired(Timer<EventSource>*) |
|
131 |
{ |
|
132 |
connect(); |
|
133 |
} |
|
134 |
||
135 |
String EventSource::url() const |
|
136 |
{ |
|
137 |
return m_url.string(); |
|
138 |
} |
|
139 |
||
140 |
EventSource::State EventSource::readyState() const |
|
141 |
{ |
|
142 |
return m_state; |
|
143 |
} |
|
144 |
||
145 |
void EventSource::close() |
|
146 |
{ |
|
147 |
if (m_state == CLOSED) |
|
148 |
return; |
|
149 |
||
150 |
if (m_reconnectTimer.isActive()) { |
|
151 |
m_reconnectTimer.stop(); |
|
152 |
unsetPendingActivity(this); |
|
153 |
} |
|
154 |
||
155 |
m_state = CLOSED; |
|
156 |
m_failSilently = true; |
|
157 |
||
158 |
if (m_requestInFlight) |
|
159 |
m_loader->cancel(); |
|
160 |
} |
|
161 |
||
162 |
ScriptExecutionContext* EventSource::scriptExecutionContext() const |
|
163 |
{ |
|
164 |
return ActiveDOMObject::scriptExecutionContext(); |
|
165 |
} |
|
166 |
||
167 |
void EventSource::didReceiveResponse(const ResourceResponse& response) |
|
168 |
{ |
|
169 |
int statusCode = response.httpStatusCode(); |
|
170 |
if (statusCode == 200 && response.httpHeaderField("Content-Type") == "text/event-stream") { |
|
171 |
m_state = OPEN; |
|
172 |
dispatchEvent(Event::create(eventNames().openEvent, false, false)); |
|
173 |
} else { |
|
174 |
if (statusCode <= 200 || statusCode > 299) |
|
175 |
m_state = CLOSED; |
|
176 |
m_loader->cancel(); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
void EventSource::didReceiveData(const char* data, int length) |
|
181 |
{ |
|
182 |
append(m_receiveBuf, m_decoder->decode(data, length)); |
|
183 |
parseEventStream(); |
|
184 |
} |
|
185 |
||
186 |
void EventSource::didFinishLoading(unsigned long) |
|
187 |
{ |
|
188 |
if (m_receiveBuf.size() > 0 || m_data.size() > 0) { |
|
189 |
append(m_receiveBuf, "\n\n"); |
|
190 |
parseEventStream(); |
|
191 |
} |
|
192 |
m_state = CONNECTING; |
|
193 |
endRequest(); |
|
194 |
} |
|
195 |
||
196 |
void EventSource::didFail(const ResourceError& error) |
|
197 |
{ |
|
198 |
int canceled = error.isCancellation(); |
|
199 |
if (((m_state == CONNECTING) && !canceled) || ((m_state == OPEN) && canceled)) |
|
200 |
m_state = CLOSED; |
|
201 |
endRequest(); |
|
202 |
} |
|
203 |
||
204 |
void EventSource::didFailRedirectCheck() |
|
205 |
{ |
|
206 |
m_state = CLOSED; |
|
207 |
m_loader->cancel(); |
|
208 |
} |
|
209 |
||
210 |
void EventSource::parseEventStream() |
|
211 |
{ |
|
212 |
unsigned int bufPos = 0; |
|
213 |
unsigned int bufSize = m_receiveBuf.size(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
while (bufPos < bufSize) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
if (m_discardTrailingNewline) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
if (m_receiveBuf[bufPos] == '\n') |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
bufPos++; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
m_discardTrailingNewline = false; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
|
0 | 221 |
int lineLength = -1; |
222 |
int fieldLength = -1; |
|
223 |
for (unsigned int i = bufPos; lineLength < 0 && i < bufSize; i++) { |
|
224 |
switch (m_receiveBuf[i]) { |
|
225 |
case ':': |
|
226 |
if (fieldLength < 0) |
|
227 |
fieldLength = i - bufPos; |
|
228 |
break; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
case '\r': |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
m_discardTrailingNewline = true; |
0 | 231 |
case '\n': |
232 |
lineLength = i - bufPos; |
|
233 |
break; |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
if (lineLength < 0) |
|
238 |
break; |
|
239 |
||
240 |
parseEventStreamLine(bufPos, fieldLength, lineLength); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
241 |
bufPos += lineLength + 1; |
0 | 242 |
} |
243 |
||
244 |
if (bufPos == bufSize) |
|
245 |
m_receiveBuf.clear(); |
|
246 |
else if (bufPos) |
|
247 |
m_receiveBuf.remove(0, bufPos); |
|
248 |
} |
|
249 |
||
250 |
void EventSource::parseEventStreamLine(unsigned int bufPos, int fieldLength, int lineLength) |
|
251 |
{ |
|
252 |
if (!lineLength) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
if (!m_data.isEmpty()) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
m_data.removeLast(); |
0 | 255 |
dispatchEvent(createMessageEvent()); |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
} |
0 | 257 |
if (!m_eventName.isEmpty()) |
258 |
m_eventName = ""; |
|
259 |
} else if (fieldLength) { |
|
260 |
bool noValue = fieldLength < 0; |
|
261 |
||
262 |
String field(&m_receiveBuf[bufPos], noValue ? lineLength : fieldLength); |
|
263 |
int step; |
|
264 |
if (noValue) |
|
265 |
step = lineLength; |
|
266 |
else if (m_receiveBuf[bufPos + fieldLength + 1] != ' ') |
|
267 |
step = fieldLength + 1; |
|
268 |
else |
|
269 |
step = fieldLength + 2; |
|
270 |
bufPos += step; |
|
271 |
int valueLength = lineLength - step; |
|
272 |
||
273 |
if (field == "data") { |
|
274 |
if (valueLength) |
|
275 |
m_data.append(&m_receiveBuf[bufPos], valueLength); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
m_data.append('\n'); |
0 | 277 |
} else if (field == "event") |
278 |
m_eventName = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : ""; |
|
279 |
else if (field == "id") |
|
280 |
m_lastEventId = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : ""; |
|
281 |
else if (field == "retry") { |
|
282 |
if (!valueLength) |
|
283 |
m_reconnectDelay = defaultReconnectDelay; |
|
284 |
else { |
|
285 |
String value(&m_receiveBuf[bufPos], valueLength); |
|
286 |
bool ok; |
|
287 |
unsigned long long retry = value.toUInt64(&ok); |
|
288 |
if (ok) |
|
289 |
m_reconnectDelay = retry; |
|
290 |
} |
|
291 |
} |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
void EventSource::stop() |
|
296 |
{ |
|
297 |
close(); |
|
298 |
} |
|
299 |
||
300 |
PassRefPtr<MessageEvent> EventSource::createMessageEvent() |
|
301 |
{ |
|
302 |
RefPtr<MessageEvent> event = MessageEvent::create(); |
|
303 |
event->initMessageEvent(m_eventName.isEmpty() ? eventNames().messageEvent : AtomicString(m_eventName), false, false, SerializedScriptValue::create(String::adopt(m_data)), m_origin, m_lastEventId, 0, 0); |
|
304 |
return event.release(); |
|
305 |
} |
|
306 |
||
307 |
EventTargetData* EventSource::eventTargetData() |
|
308 |
{ |
|
309 |
return &m_eventTargetData; |
|
310 |
} |
|
311 |
||
312 |
EventTargetData* EventSource::ensureEventTargetData() |
|
313 |
{ |
|
314 |
return &m_eventTargetData; |
|
315 |
} |
|
316 |
||
317 |
} // namespace WebCore |
|
318 |
||
319 |
#endif // ENABLE(EVENTSOURCE) |