mirror of
https://github.com/servo/servo.git
synced 2025-07-29 02:00:23 +01:00
WebSocket: Implement WebSocket#binaryType
This commit is contained in:
parent
f77973c903
commit
e92f4629db
6 changed files with 34 additions and 33 deletions
|
@ -27,7 +27,7 @@ interface WebSocket : EventTarget {
|
|||
|
||||
//messaging
|
||||
attribute EventHandler onmessage;
|
||||
//attribute BinaryType binaryType;
|
||||
attribute BinaryType binaryType;
|
||||
[Throws] void send(optional USVString data);
|
||||
//void send(Blob data);
|
||||
//void send(ArrayBuffer data);
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::WebSocketBinding;
|
||||
use dom::bindings::codegen::Bindings::WebSocketBinding::WebSocketMethods;
|
||||
use dom::bindings::codegen::Bindings::WebSocketBinding::{BinaryType, WebSocketMethods};
|
||||
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
||||
use dom::bindings::codegen::InheritTypes::EventTargetCast;
|
||||
use dom::bindings::codegen::InheritTypes::EventCast;
|
||||
|
@ -30,8 +30,10 @@ use util::str::DOMString;
|
|||
use util::task::spawn_named;
|
||||
|
||||
use js::jsapi::{RootedValue, JSAutoRequest, JSAutoCompartment};
|
||||
use js::jsapi::{JS_NewArrayBuffer, JS_GetArrayBufferData};
|
||||
use js::jsval::UndefinedValue;
|
||||
use hyper::header::Host;
|
||||
use libc::{uint8_t, uint32_t};
|
||||
use websocket::Message;
|
||||
use websocket::ws::sender::Sender as Sender_Object;
|
||||
use websocket::client::sender::Sender;
|
||||
|
@ -46,6 +48,7 @@ use websocket::ws::util::url::parse_url;
|
|||
|
||||
use std::borrow::ToOwned;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::ptr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(JSTraceable, PartialEq, Copy, Clone, Debug)]
|
||||
|
@ -76,6 +79,7 @@ pub struct WebSocket {
|
|||
code: Cell<u16>, //Closing code
|
||||
reason: DOMRefCell<DOMString>, //Closing reason
|
||||
data: DOMRefCell<DOMString>, //Data from send - TODO: Remove after buffer is added.
|
||||
binary_type: Cell<BinaryType>,
|
||||
}
|
||||
|
||||
/// *Establish a WebSocket Connection* as defined in RFC 6455.
|
||||
|
@ -115,6 +119,7 @@ impl WebSocket {
|
|||
code: Cell::new(0),
|
||||
reason: DOMRefCell::new("".to_owned()),
|
||||
data: DOMRefCell::new("".to_owned()),
|
||||
binary_type: Cell::new(BinaryType::Blob),
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -236,6 +241,16 @@ impl<'a> WebSocketMethods for &'a WebSocket {
|
|||
self.ready_state.get() as u16
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
|
||||
fn BinaryType(self) -> BinaryType {
|
||||
self.binary_type.get()
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
|
||||
fn SetBinaryType(self, btype: BinaryType) {
|
||||
self.binary_type.set(btype)
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-websocket-send
|
||||
fn Send(self, data: Option<USVString>) -> Fallible<()> {
|
||||
match self.ready_state.get() {
|
||||
|
@ -394,6 +409,7 @@ struct MessageReceivedTask {
|
|||
}
|
||||
|
||||
impl Runnable for MessageReceivedTask {
|
||||
#[allow(unsafe_code)]
|
||||
fn handler(self: Box<Self>) {
|
||||
let ws = self.address.root();
|
||||
debug!("MessageReceivedTask::handler({:p}): readyState={:?}", &*ws,
|
||||
|
@ -413,8 +429,22 @@ impl Runnable for MessageReceivedTask {
|
|||
match self.message {
|
||||
MessageData::Text(text) => text.to_jsval(cx, message.handle_mut()),
|
||||
MessageData::Binary(data) => {
|
||||
match ws.binary_type.get() {
|
||||
BinaryType::Blob => {
|
||||
let blob = Blob::new(global.r(), Some(data), "");
|
||||
blob.to_jsval(cx, message.handle_mut());
|
||||
}
|
||||
BinaryType::Arraybuffer => {
|
||||
unsafe {
|
||||
let len = data.len() as uint32_t;
|
||||
let buf = JS_NewArrayBuffer(cx, len);
|
||||
let buf_data: *mut uint8_t = JS_GetArrayBufferData(buf, ptr::null());
|
||||
ptr::copy_nonoverlapping(data.as_ptr(), buf_data, len as usize);
|
||||
buf.to_jsval(cx, message.handle_mut());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -8304,9 +8304,6 @@
|
|||
[WebSocket interface: attribute protocol]
|
||||
expected: FAIL
|
||||
|
||||
[WebSocket interface: attribute binaryType]
|
||||
expected: FAIL
|
||||
|
||||
[CloseEvent interface: existence and properties of interface object]
|
||||
expected: FAIL
|
||||
|
||||
|
@ -8987,4 +8984,3 @@
|
|||
|
||||
[HTMLOptionElement interface: new Option() must inherit property "index" with the proper type (7)]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
[Create-Secure-valid-url-binaryType-blob.htm]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[W3C WebSocket API - Create Secure WebSocket - wsocket.binaryType should be set to 'blob' after connection is established - Connection should be opened]
|
||||
expected: FAIL
|
||||
|
||||
[W3C WebSocket API - Create Secure WebSocket - wsocket.binaryType should be set to 'blob' after connection is established - Connection should be closed]
|
||||
expected: NOTRUN
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
[binaryType-wrong-value.htm]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[W3C WebSocket API - Create WebSocket - set binaryType to something other than blob or arraybuffer - SYNTAX_ERR is returned - Connection should be opened]
|
||||
expected: FAIL
|
||||
|
||||
[W3C WebSocket API - Create WebSocket - set binaryType to something other than blob or arraybuffer - SYNTAX_ERR is returned - Connection should be closed]
|
||||
expected: NOTRUN
|
||||
|
|
@ -15,9 +15,6 @@
|
|||
[WebSocket interface: attribute protocol]
|
||||
expected: FAIL
|
||||
|
||||
[WebSocket interface: attribute binaryType]
|
||||
expected: FAIL
|
||||
|
||||
[WebSocket interface: operation send(DOMString)]
|
||||
expected: FAIL
|
||||
|
||||
|
@ -42,9 +39,6 @@
|
|||
[WebSocket interface: new WebSocket("ws://foo") must inherit property "protocol" with the proper type (11)]
|
||||
expected: FAIL
|
||||
|
||||
[WebSocket interface: new WebSocket("ws://foo") must inherit property "binaryType" with the proper type (14)]
|
||||
expected: FAIL
|
||||
|
||||
[WebSocket interface: calling send(DOMString) on new WebSocket("ws://foo") with too few arguments must throw TypeError]
|
||||
expected: FAIL
|
||||
|
||||
|
@ -62,4 +56,3 @@
|
|||
|
||||
[CloseEvent interface: existence and properties of interface prototype object]
|
||||
expected: FAIL
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue