OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 156 |
157 PassRefPtr<WebSocket> WebSocket::create(ScriptExecutionContext* context, const S
tring& url, ExceptionCode& ec) | 157 PassRefPtr<WebSocket> WebSocket::create(ScriptExecutionContext* context, const S
tring& url, ExceptionCode& ec) |
158 { | 158 { |
159 Vector<String> protocols; | 159 Vector<String> protocols; |
160 return WebSocket::create(context, url, protocols, ec); | 160 return WebSocket::create(context, url, protocols, ec); |
161 } | 161 } |
162 | 162 |
163 PassRefPtr<WebSocket> WebSocket::create(ScriptExecutionContext* context, const S
tring& url, const Vector<String>& protocols, ExceptionCode& ec) | 163 PassRefPtr<WebSocket> WebSocket::create(ScriptExecutionContext* context, const S
tring& url, const Vector<String>& protocols, ExceptionCode& ec) |
164 { | 164 { |
165 if (url.isNull()) { | 165 if (url.isNull()) { |
166 ec = SYNTAX_ERR; | 166 ec = SyntaxError; |
167 return 0; | 167 return 0; |
168 } | 168 } |
169 | 169 |
170 RefPtr<WebSocket> webSocket(adoptRef(new WebSocket(context))); | 170 RefPtr<WebSocket> webSocket(adoptRef(new WebSocket(context))); |
171 webSocket->suspendIfNeeded(); | 171 webSocket->suspendIfNeeded(); |
172 | 172 |
173 webSocket->connect(context->completeURL(url), protocols, ec); | 173 webSocket->connect(context->completeURL(url), protocols, ec); |
174 if (ec) | 174 if (ec) |
175 return 0; | 175 return 0; |
176 | 176 |
(...skipping 21 matching lines...) Expand all Loading... |
198 } | 198 } |
199 | 199 |
200 void WebSocket::connect(const String& url, const Vector<String>& protocols, Exce
ptionCode& ec) | 200 void WebSocket::connect(const String& url, const Vector<String>& protocols, Exce
ptionCode& ec) |
201 { | 201 { |
202 LOG(Network, "WebSocket %p connect() url='%s'", this, url.utf8().data()); | 202 LOG(Network, "WebSocket %p connect() url='%s'", this, url.utf8().data()); |
203 m_url = KURL(KURL(), url); | 203 m_url = KURL(KURL(), url); |
204 | 204 |
205 if (!m_url.isValid()) { | 205 if (!m_url.isValid()) { |
206 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "Invalid url for WebSocket " + m_url.elidedString()); | 206 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "Invalid url for WebSocket " + m_url.elidedString()); |
207 m_state = CLOSED; | 207 m_state = CLOSED; |
208 ec = SYNTAX_ERR; | 208 ec = SyntaxError; |
209 return; | 209 return; |
210 } | 210 } |
211 | 211 |
212 if (!m_url.protocolIs("ws") && !m_url.protocolIs("wss")) { | 212 if (!m_url.protocolIs("ws") && !m_url.protocolIs("wss")) { |
213 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "Wrong url scheme for WebSocket " + m_url.elidedString()); | 213 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "Wrong url scheme for WebSocket " + m_url.elidedString()); |
214 m_state = CLOSED; | 214 m_state = CLOSED; |
215 ec = SYNTAX_ERR; | 215 ec = SyntaxError; |
216 return; | 216 return; |
217 } | 217 } |
218 if (m_url.hasFragmentIdentifier()) { | 218 if (m_url.hasFragmentIdentifier()) { |
219 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "URL has fragment component " + m_url.elidedString()); | 219 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "URL has fragment component " + m_url.elidedString()); |
220 m_state = CLOSED; | 220 m_state = CLOSED; |
221 ec = SYNTAX_ERR; | 221 ec = SyntaxError; |
222 return; | 222 return; |
223 } | 223 } |
224 if (!portAllowed(m_url)) { | 224 if (!portAllowed(m_url)) { |
225 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket port " + String::number(m_url.port()) + " blocked"); | 225 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket port " + String::number(m_url.port()) + " blocked"); |
226 m_state = CLOSED; | 226 m_state = CLOSED; |
227 ec = SECURITY_ERR; | 227 ec = SecurityError; |
228 return; | 228 return; |
229 } | 229 } |
230 | 230 |
231 // FIXME: Convert this to check the isolated world's Content Security Policy
once webkit.org/b/104520 is solved. | 231 // FIXME: Convert this to check the isolated world's Content Security Policy
once webkit.org/b/104520 is solved. |
232 bool shouldBypassMainWorldContentSecurityPolicy = false; | 232 bool shouldBypassMainWorldContentSecurityPolicy = false; |
233 if (scriptExecutionContext()->isDocument()) { | 233 if (scriptExecutionContext()->isDocument()) { |
234 Document* document = toDocument(scriptExecutionContext()); | 234 Document* document = toDocument(scriptExecutionContext()); |
235 shouldBypassMainWorldContentSecurityPolicy = document->frame()->script()
->shouldBypassMainWorldContentSecurityPolicy(); | 235 shouldBypassMainWorldContentSecurityPolicy = document->frame()->script()
->shouldBypassMainWorldContentSecurityPolicy(); |
236 } | 236 } |
237 if (!shouldBypassMainWorldContentSecurityPolicy && !scriptExecutionContext()
->contentSecurityPolicy()->allowConnectToSource(m_url)) { | 237 if (!shouldBypassMainWorldContentSecurityPolicy && !scriptExecutionContext()
->contentSecurityPolicy()->allowConnectToSource(m_url)) { |
238 m_state = CLOSED; | 238 m_state = CLOSED; |
239 | 239 |
240 // FIXME: Should this be throwing an exception? | 240 // FIXME: Should this be throwing an exception? |
241 ec = SECURITY_ERR; | 241 ec = SecurityError; |
242 return; | 242 return; |
243 } | 243 } |
244 | 244 |
245 m_channel = WebSocketChannel::create(scriptExecutionContext(), this); | 245 m_channel = WebSocketChannel::create(scriptExecutionContext(), this); |
246 | 246 |
247 // FIXME: There is a disagreement about restriction of subprotocols between
WebSocket API and hybi-10 protocol | 247 // FIXME: There is a disagreement about restriction of subprotocols between
WebSocket API and hybi-10 protocol |
248 // draft. The former simply says "only characters in the range U+0021 to U+0
07E are allowed," while the latter | 248 // draft. The former simply says "only characters in the range U+0021 to U+0
07E are allowed," while the latter |
249 // imposes a stricter rule: "the elements MUST be non-empty strings with cha
racters as defined in [RFC2616], | 249 // imposes a stricter rule: "the elements MUST be non-empty strings with cha
racters as defined in [RFC2616], |
250 // and MUST all be unique strings." | 250 // and MUST all be unique strings." |
251 // | 251 // |
252 // Here, we throw SYNTAX_ERR if the given protocols do not meet the latter c
riteria. This behavior does not | 252 // Here, we throw SyntaxError if the given protocols do not meet the latter
criteria. This behavior does not |
253 // comply with WebSocket API specification, but it seems to be the only reas
onable way to handle this conflict. | 253 // comply with WebSocket API specification, but it seems to be the only reas
onable way to handle this conflict. |
254 for (size_t i = 0; i < protocols.size(); ++i) { | 254 for (size_t i = 0; i < protocols.size(); ++i) { |
255 if (!isValidProtocolString(protocols[i])) { | 255 if (!isValidProtocolString(protocols[i])) { |
256 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "Wrong protocol for WebSocket '" + encodeProtocolString(protocols[i]
) + "'"); | 256 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "Wrong protocol for WebSocket '" + encodeProtocolString(protocols[i]
) + "'"); |
257 m_state = CLOSED; | 257 m_state = CLOSED; |
258 ec = SYNTAX_ERR; | 258 ec = SyntaxError; |
259 return; | 259 return; |
260 } | 260 } |
261 } | 261 } |
262 HashSet<String> visited; | 262 HashSet<String> visited; |
263 for (size_t i = 0; i < protocols.size(); ++i) { | 263 for (size_t i = 0; i < protocols.size(); ++i) { |
264 if (!visited.add(protocols[i]).isNewEntry) { | 264 if (!visited.add(protocols[i]).isNewEntry) { |
265 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "WebSocket protocols contain duplicates: '" + encodeProtocolString(p
rotocols[i]) + "'"); | 265 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "WebSocket protocols contain duplicates: '" + encodeProtocolString(p
rotocols[i]) + "'"); |
266 m_state = CLOSED; | 266 m_state = CLOSED; |
267 ec = SYNTAX_ERR; | 267 ec = SyntaxError; |
268 return; | 268 return; |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 String protocolString; | 272 String protocolString; |
273 if (!protocols.isEmpty()) | 273 if (!protocols.isEmpty()) |
274 protocolString = joinStrings(protocols, subProtocolSeperator()); | 274 protocolString = joinStrings(protocols, subProtocolSeperator()); |
275 | 275 |
276 m_channel->connect(m_url, protocolString); | 276 m_channel->connect(m_url, protocolString); |
277 ActiveDOMObject::setPendingActivity(this); | 277 ActiveDOMObject::setPendingActivity(this); |
278 } | 278 } |
279 | 279 |
280 void WebSocket::handleSendResult(WebSocketChannel::SendResult result, ExceptionC
ode& ec) | 280 void WebSocket::handleSendResult(WebSocketChannel::SendResult result, ExceptionC
ode& ec) |
281 { | 281 { |
282 switch (result) { | 282 switch (result) { |
283 case WebSocketChannel::InvalidMessage: | 283 case WebSocketChannel::InvalidMessage: |
284 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket message contains invalid character(s)."); | 284 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket message contains invalid character(s)."); |
285 ec = SYNTAX_ERR; | 285 ec = SyntaxError; |
286 return; | 286 return; |
287 case WebSocketChannel::SendFail: | 287 case WebSocketChannel::SendFail: |
288 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket send() failed."); | 288 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessag
eLevel, "WebSocket send() failed."); |
289 return; | 289 return; |
290 case WebSocketChannel::SendSuccess: | 290 case WebSocketChannel::SendSuccess: |
291 return; | 291 return; |
292 } | 292 } |
293 ASSERT_NOT_REACHED(); | 293 ASSERT_NOT_REACHED(); |
294 } | 294 } |
295 | 295 |
296 void WebSocket::updateBufferedAmountAfterClose(unsigned long payloadSize) | 296 void WebSocket::updateBufferedAmountAfterClose(unsigned long payloadSize) |
297 { | 297 { |
298 m_bufferedAmountAfterClose = saturateAdd(m_bufferedAmountAfterClose, payload
Size); | 298 m_bufferedAmountAfterClose = saturateAdd(m_bufferedAmountAfterClose, payload
Size); |
299 m_bufferedAmountAfterClose = saturateAdd(m_bufferedAmountAfterClose, getFram
ingOverhead(payloadSize)); | 299 m_bufferedAmountAfterClose = saturateAdd(m_bufferedAmountAfterClose, getFram
ingOverhead(payloadSize)); |
300 | 300 |
301 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessageLev
el, "WebSocket is already in CLOSING or CLOSED state."); | 301 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMessageLev
el, "WebSocket is already in CLOSING or CLOSED state."); |
302 } | 302 } |
303 | 303 |
304 void WebSocket::send(const String& message, ExceptionCode& ec) | 304 void WebSocket::send(const String& message, ExceptionCode& ec) |
305 { | 305 { |
306 LOG(Network, "WebSocket %p send() Sending String '%s'", this, message.utf8()
.data()); | 306 LOG(Network, "WebSocket %p send() Sending String '%s'", this, message.utf8()
.data()); |
307 if (m_state == CONNECTING) { | 307 if (m_state == CONNECTING) { |
308 ec = INVALID_STATE_ERR; | 308 ec = InvalidStateError; |
309 return; | 309 return; |
310 } | 310 } |
311 // No exception is raised if the connection was once established but has sub
sequently been closed. | 311 // No exception is raised if the connection was once established but has sub
sequently been closed. |
312 if (m_state == CLOSING || m_state == CLOSED) { | 312 if (m_state == CLOSING || m_state == CLOSED) { |
313 updateBufferedAmountAfterClose(message.utf8().length()); | 313 updateBufferedAmountAfterClose(message.utf8().length()); |
314 return; | 314 return; |
315 } | 315 } |
316 ASSERT(m_channel); | 316 ASSERT(m_channel); |
317 handleSendResult(m_channel->send(message), ec); | 317 handleSendResult(m_channel->send(message), ec); |
318 } | 318 } |
319 | 319 |
320 void WebSocket::send(ArrayBuffer* binaryData, ExceptionCode& ec) | 320 void WebSocket::send(ArrayBuffer* binaryData, ExceptionCode& ec) |
321 { | 321 { |
322 LOG(Network, "WebSocket %p send() Sending ArrayBuffer %p", this, binaryData)
; | 322 LOG(Network, "WebSocket %p send() Sending ArrayBuffer %p", this, binaryData)
; |
323 ASSERT(binaryData); | 323 ASSERT(binaryData); |
324 if (m_state == CONNECTING) { | 324 if (m_state == CONNECTING) { |
325 ec = INVALID_STATE_ERR; | 325 ec = InvalidStateError; |
326 return; | 326 return; |
327 } | 327 } |
328 if (m_state == CLOSING || m_state == CLOSED) { | 328 if (m_state == CLOSING || m_state == CLOSED) { |
329 updateBufferedAmountAfterClose(binaryData->byteLength()); | 329 updateBufferedAmountAfterClose(binaryData->byteLength()); |
330 return; | 330 return; |
331 } | 331 } |
332 ASSERT(m_channel); | 332 ASSERT(m_channel); |
333 handleSendResult(m_channel->send(*binaryData, 0, binaryData->byteLength()),
ec); | 333 handleSendResult(m_channel->send(*binaryData, 0, binaryData->byteLength()),
ec); |
334 } | 334 } |
335 | 335 |
336 void WebSocket::send(ArrayBufferView* arrayBufferView, ExceptionCode& ec) | 336 void WebSocket::send(ArrayBufferView* arrayBufferView, ExceptionCode& ec) |
337 { | 337 { |
338 LOG(Network, "WebSocket %p send() Sending ArrayBufferView %p", this, arrayBu
fferView); | 338 LOG(Network, "WebSocket %p send() Sending ArrayBufferView %p", this, arrayBu
fferView); |
339 ASSERT(arrayBufferView); | 339 ASSERT(arrayBufferView); |
340 if (m_state == CONNECTING) { | 340 if (m_state == CONNECTING) { |
341 ec = INVALID_STATE_ERR; | 341 ec = InvalidStateError; |
342 return; | 342 return; |
343 } | 343 } |
344 if (m_state == CLOSING || m_state == CLOSED) { | 344 if (m_state == CLOSING || m_state == CLOSED) { |
345 updateBufferedAmountAfterClose(arrayBufferView->byteLength()); | 345 updateBufferedAmountAfterClose(arrayBufferView->byteLength()); |
346 return; | 346 return; |
347 } | 347 } |
348 ASSERT(m_channel); | 348 ASSERT(m_channel); |
349 RefPtr<ArrayBuffer> arrayBuffer(arrayBufferView->buffer()); | 349 RefPtr<ArrayBuffer> arrayBuffer(arrayBufferView->buffer()); |
350 handleSendResult(m_channel->send(*arrayBuffer, arrayBufferView->byteOffset()
, arrayBufferView->byteLength()), ec); | 350 handleSendResult(m_channel->send(*arrayBuffer, arrayBufferView->byteOffset()
, arrayBufferView->byteLength()), ec); |
351 } | 351 } |
352 | 352 |
353 void WebSocket::send(Blob* binaryData, ExceptionCode& ec) | 353 void WebSocket::send(Blob* binaryData, ExceptionCode& ec) |
354 { | 354 { |
355 LOG(Network, "WebSocket %p send() Sending Blob '%s'", this, binaryData->url(
).elidedString().utf8().data()); | 355 LOG(Network, "WebSocket %p send() Sending Blob '%s'", this, binaryData->url(
).elidedString().utf8().data()); |
356 ASSERT(binaryData); | 356 ASSERT(binaryData); |
357 if (m_state == CONNECTING) { | 357 if (m_state == CONNECTING) { |
358 ec = INVALID_STATE_ERR; | 358 ec = InvalidStateError; |
359 return; | 359 return; |
360 } | 360 } |
361 if (m_state == CLOSING || m_state == CLOSED) { | 361 if (m_state == CLOSING || m_state == CLOSED) { |
362 updateBufferedAmountAfterClose(static_cast<unsigned long>(binaryData->si
ze())); | 362 updateBufferedAmountAfterClose(static_cast<unsigned long>(binaryData->si
ze())); |
363 return; | 363 return; |
364 } | 364 } |
365 ASSERT(m_channel); | 365 ASSERT(m_channel); |
366 handleSendResult(m_channel->send(*binaryData), ec); | 366 handleSendResult(m_channel->send(*binaryData), ec); |
367 } | 367 } |
368 | 368 |
(...skipping 12 matching lines...) Expand all Loading... |
381 closeInternal(code, String(), ec); | 381 closeInternal(code, String(), ec); |
382 } | 382 } |
383 | 383 |
384 void WebSocket::closeInternal(int code, const String& reason, ExceptionCode& ec) | 384 void WebSocket::closeInternal(int code, const String& reason, ExceptionCode& ec) |
385 { | 385 { |
386 if (code == WebSocketChannel::CloseEventCodeNotSpecified) | 386 if (code == WebSocketChannel::CloseEventCodeNotSpecified) |
387 LOG(Network, "WebSocket %p close() without code and reason", this); | 387 LOG(Network, "WebSocket %p close() without code and reason", this); |
388 else { | 388 else { |
389 LOG(Network, "WebSocket %p close() code=%d reason='%s'", this, code, rea
son.utf8().data()); | 389 LOG(Network, "WebSocket %p close() code=%d reason='%s'", this, code, rea
son.utf8().data()); |
390 if (!(code == WebSocketChannel::CloseEventCodeNormalClosure || (WebSocke
tChannel::CloseEventCodeMinimumUserDefined <= code && code <= WebSocketChannel::
CloseEventCodeMaximumUserDefined))) { | 390 if (!(code == WebSocketChannel::CloseEventCodeNormalClosure || (WebSocke
tChannel::CloseEventCodeMinimumUserDefined <= code && code <= WebSocketChannel::
CloseEventCodeMaximumUserDefined))) { |
391 ec = INVALID_ACCESS_ERR; | 391 ec = InvalidAccessError; |
392 return; | 392 return; |
393 } | 393 } |
394 CString utf8 = reason.utf8(String::StrictConversionReplacingUnpairedSurr
ogatesWithFFFD); | 394 CString utf8 = reason.utf8(String::StrictConversionReplacingUnpairedSurr
ogatesWithFFFD); |
395 if (utf8.length() > maxReasonSizeInBytes) { | 395 if (utf8.length() > maxReasonSizeInBytes) { |
396 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "WebSocket close message is too long."); | 396 scriptExecutionContext()->addConsoleMessage(JSMessageSource, ErrorMe
ssageLevel, "WebSocket close message is too long."); |
397 ec = SYNTAX_ERR; | 397 ec = SyntaxError; |
398 return; | 398 return; |
399 } | 399 } |
400 } | 400 } |
401 | 401 |
402 if (m_state == CLOSING || m_state == CLOSED) | 402 if (m_state == CLOSING || m_state == CLOSED) |
403 return; | 403 return; |
404 if (m_state == CONNECTING) { | 404 if (m_state == CONNECTING) { |
405 m_state = CLOSING; | 405 m_state = CLOSING; |
406 m_channel->fail("WebSocket is closed before the connection is establishe
d.", WarningMessageLevel); | 406 m_channel->fail("WebSocket is closed before the connection is establishe
d.", WarningMessageLevel); |
407 return; | 407 return; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 static const size_t minimumPayloadSizeWithEightByteExtendedPayloadLength = 0
x10000; | 612 static const size_t minimumPayloadSizeWithEightByteExtendedPayloadLength = 0
x10000; |
613 size_t overhead = hybiBaseFramingOverhead + hybiMaskingKeyLength; | 613 size_t overhead = hybiBaseFramingOverhead + hybiMaskingKeyLength; |
614 if (payloadSize >= minimumPayloadSizeWithEightByteExtendedPayloadLength) | 614 if (payloadSize >= minimumPayloadSizeWithEightByteExtendedPayloadLength) |
615 overhead += 8; | 615 overhead += 8; |
616 else if (payloadSize >= minimumPayloadSizeWithTwoByteExtendedPayloadLength) | 616 else if (payloadSize >= minimumPayloadSizeWithTwoByteExtendedPayloadLength) |
617 overhead += 2; | 617 overhead += 2; |
618 return overhead; | 618 return overhead; |
619 } | 619 } |
620 | 620 |
621 } // namespace WebCore | 621 } // namespace WebCore |
OLD | NEW |