Remove old websocket package
This commit is contained in:
parent
679f33a986
commit
54b8b501ee
@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.http.websocket;
|
||||
|
||||
import io.netty.buffer.ChannelBuffer;
|
||||
import io.netty.buffer.ChannelBuffers;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
/**
|
||||
* The default {@link WebSocketFrame} implementation.
|
||||
*/
|
||||
public class DefaultWebSocketFrame implements WebSocketFrame {
|
||||
|
||||
private int type;
|
||||
private ChannelBuffer binaryData;
|
||||
|
||||
/**
|
||||
* Creates a new empty text frame.
|
||||
*/
|
||||
public DefaultWebSocketFrame() {
|
||||
this(0, ChannelBuffers.EMPTY_BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new text frame from with the specified string.
|
||||
*/
|
||||
public DefaultWebSocketFrame(String textData) {
|
||||
this(0, ChannelBuffers.copiedBuffer(textData, CharsetUtil.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new frame with the specified frame type and the specified data.
|
||||
*
|
||||
* @param type
|
||||
* the type of the frame. {@code 0} is the only allowed type currently.
|
||||
* @param binaryData
|
||||
* the content of the frame. If <tt>(type & 0x80 == 0)</tt>,
|
||||
* it must be encoded in UTF-8.
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* if If <tt>(type & 0x80 == 0)</tt> and the data is not encoded
|
||||
* in UTF-8
|
||||
*/
|
||||
public DefaultWebSocketFrame(int type, ChannelBuffer binaryData) {
|
||||
setData(type, binaryData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isText() {
|
||||
return (getType() & 0x80) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBinary() {
|
||||
return !isText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelBuffer getBinaryData() {
|
||||
return binaryData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTextData() {
|
||||
return getBinaryData().toString(CharsetUtil.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(int type, ChannelBuffer binaryData) {
|
||||
if (binaryData == null) {
|
||||
throw new NullPointerException("binaryData");
|
||||
}
|
||||
|
||||
if ((type & 0x80) == 0) {
|
||||
// If text, data should not contain 0xFF.
|
||||
int delimPos = binaryData.indexOf(
|
||||
binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
|
||||
if (delimPos >= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"a text frame should not contain 0xFF.");
|
||||
}
|
||||
}
|
||||
|
||||
this.type = type & 0xFF;
|
||||
this.binaryData = binaryData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() +
|
||||
"(type: " + getType() + ", " + "data: " + getBinaryData() + ')';
|
||||
}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.http.websocket;
|
||||
|
||||
import io.netty.buffer.ChannelBuffer;
|
||||
import io.netty.buffer.ChannelBuffers;
|
||||
|
||||
/**
|
||||
* A Web Socket frame that represents either text or binary data.
|
||||
*/
|
||||
public interface WebSocketFrame {
|
||||
|
||||
/**
|
||||
* Closing handshake message (<tt>0xFF, 0x00</tt>)
|
||||
*/
|
||||
WebSocketFrame CLOSING_HANDSHAKE = new DefaultWebSocketFrame(0xFF, ChannelBuffers.EMPTY_BUFFER);
|
||||
|
||||
/**
|
||||
* Returns the type of this frame.
|
||||
* <tt>0x00-0x7F</tt> means a text frame encoded in UTF-8, and
|
||||
* <tt>0x80-0xFF</tt> means a binary frame. Currently, {@code 0} is the
|
||||
* only allowed type according to the specification.
|
||||
*/
|
||||
int getType();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the content of this frame is a string
|
||||
* encoded in UTF-8.
|
||||
*/
|
||||
boolean isText();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the content of this frame is an
|
||||
* arbitrary binary data.
|
||||
*/
|
||||
boolean isBinary();
|
||||
|
||||
/**
|
||||
* Returns the content of this frame as-is, with no UTF-8 decoding.
|
||||
*/
|
||||
ChannelBuffer getBinaryData();
|
||||
|
||||
/**
|
||||
* Converts the content of this frame into a UTF-8 string and returns the
|
||||
* converted string.
|
||||
*/
|
||||
String getTextData();
|
||||
|
||||
/**
|
||||
* Sets the type and the content of this frame.
|
||||
*
|
||||
* @param type
|
||||
* the type of the frame. {@code 0} is the only allowed type currently.
|
||||
* @param binaryData
|
||||
* the content of the frame. If <tt>(type & 0x80 == 0)</tt>,
|
||||
* it must be encoded in UTF-8.
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* if If <tt>(type & 0x80 == 0)</tt> and the data is not encoded
|
||||
* in UTF-8
|
||||
*/
|
||||
void setData(int type, ChannelBuffer binaryData);
|
||||
|
||||
/**
|
||||
* Returns the string representation of this frame. Please note that this
|
||||
* method is not identical to {@link #getTextData()}.
|
||||
*/
|
||||
@Override
|
||||
String toString();
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.http.websocket;
|
||||
|
||||
import io.netty.buffer.ChannelBuffer;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.frame.TooLongFrameException;
|
||||
import io.netty.handler.codec.replay.ReplayingDecoder;
|
||||
import io.netty.handler.codec.replay.VoidEnum;
|
||||
|
||||
/**
|
||||
* Decodes {@link ChannelBuffer}s into {@link WebSocketFrame}s.
|
||||
* <p>
|
||||
* For the detailed instruction on adding add Web Socket support to your HTTP
|
||||
* server, take a look into the <tt>WebSocketServer</tt> example located in the
|
||||
* {@code io.netty.example.http.websocket} package.
|
||||
* @apiviz.landmark
|
||||
* @apiviz.uses io.netty.handler.codec.http.websocket.WebSocketFrame
|
||||
*/
|
||||
public class WebSocketFrameDecoder extends ReplayingDecoder<VoidEnum> {
|
||||
|
||||
public static final int DEFAULT_MAX_FRAME_SIZE = 16384;
|
||||
|
||||
private final int maxFrameSize;
|
||||
private boolean receivedClosingHandshake;
|
||||
|
||||
public WebSocketFrameDecoder() {
|
||||
this(DEFAULT_MAX_FRAME_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@code WebSocketFrameDecoder} with the specified {@code maxFrameSize}. If the client
|
||||
* sends a frame size larger than {@code maxFrameSize}, the channel will be closed.
|
||||
*
|
||||
* @param maxFrameSize the maximum frame size to decode
|
||||
*/
|
||||
public WebSocketFrameDecoder(int maxFrameSize) {
|
||||
this.maxFrameSize = maxFrameSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object decode(ChannelHandlerContext ctx, Channel channel,
|
||||
ChannelBuffer buffer, VoidEnum state) throws Exception {
|
||||
|
||||
// Discard all data received if closing handshake was received before.
|
||||
if (receivedClosingHandshake) {
|
||||
buffer.skipBytes(actualReadableBytes());
|
||||
return null;
|
||||
}
|
||||
|
||||
// Decode a frame otherwise.
|
||||
byte type = buffer.readByte();
|
||||
if ((type & 0x80) == 0x80) {
|
||||
// If the MSB on type is set, decode the frame length
|
||||
return decodeBinaryFrame(type, buffer);
|
||||
} else {
|
||||
// Decode a 0xff terminated UTF-8 string
|
||||
return decodeTextFrame(type, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
private WebSocketFrame decodeBinaryFrame(int type, ChannelBuffer buffer) throws TooLongFrameException {
|
||||
long frameSize = 0;
|
||||
int lengthFieldSize = 0;
|
||||
byte b;
|
||||
do {
|
||||
b = buffer.readByte();
|
||||
frameSize <<= 7;
|
||||
frameSize |= b & 0x7f;
|
||||
if (frameSize > maxFrameSize) {
|
||||
throw new TooLongFrameException();
|
||||
}
|
||||
lengthFieldSize ++;
|
||||
if (lengthFieldSize > 8) {
|
||||
// Perhaps a malicious peer?
|
||||
throw new TooLongFrameException();
|
||||
}
|
||||
} while ((b & 0x80) == 0x80);
|
||||
|
||||
if (type == 0xFF && frameSize == 0) {
|
||||
receivedClosingHandshake = true;
|
||||
}
|
||||
|
||||
return new DefaultWebSocketFrame(
|
||||
type, buffer.readBytes((int) frameSize));
|
||||
}
|
||||
|
||||
private WebSocketFrame decodeTextFrame(int type, ChannelBuffer buffer) throws TooLongFrameException {
|
||||
int ridx = buffer.readerIndex();
|
||||
int rbytes = actualReadableBytes();
|
||||
int delimPos = buffer.indexOf(ridx, ridx + rbytes, (byte) 0xFF);
|
||||
if (delimPos == -1) {
|
||||
// Frame delimiter (0xFF) not found
|
||||
if (rbytes > maxFrameSize) {
|
||||
// Frame length exceeded the maximum
|
||||
throw new TooLongFrameException();
|
||||
} else {
|
||||
// Wait until more data is received
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
int frameSize = delimPos - ridx;
|
||||
if (frameSize > maxFrameSize) {
|
||||
throw new TooLongFrameException();
|
||||
}
|
||||
|
||||
ChannelBuffer binaryData = buffer.readBytes(frameSize);
|
||||
buffer.skipBytes(1);
|
||||
return new DefaultWebSocketFrame(type, binaryData);
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.http.websocket;
|
||||
|
||||
import io.netty.buffer.ChannelBuffer;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.handler.codec.oneone.OneToOneEncoder;
|
||||
|
||||
/**
|
||||
* Encodes a {@link WebSocketFrame} into a {@link ChannelBuffer}.
|
||||
* <p>
|
||||
* For the detailed instruction on adding add Web Socket support to your HTTP
|
||||
* server, take a look into the <tt>WebSocketServer</tt> example located in the
|
||||
* {@code io.netty.example.http.websocket} package.
|
||||
* @apiviz.landmark
|
||||
* @apiviz.uses io.netty.handler.codec.http.websocket.WebSocketFrame
|
||||
*/
|
||||
@Sharable
|
||||
public class WebSocketFrameEncoder extends OneToOneEncoder {
|
||||
|
||||
@Override
|
||||
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
|
||||
if (msg instanceof WebSocketFrame) {
|
||||
WebSocketFrame frame = (WebSocketFrame) msg;
|
||||
int type = frame.getType();
|
||||
if (frame.isText()) {
|
||||
// Text frame
|
||||
ChannelBuffer data = frame.getBinaryData();
|
||||
ChannelBuffer encoded =
|
||||
channel.getConfig().getBufferFactory().getBuffer(
|
||||
data.order(), data.readableBytes() + 2);
|
||||
encoded.writeByte((byte) type);
|
||||
encoded.writeBytes(data, data.readerIndex(), data.readableBytes());
|
||||
encoded.writeByte((byte) 0xFF);
|
||||
return encoded;
|
||||
} else {
|
||||
// Binary frame
|
||||
ChannelBuffer data = frame.getBinaryData();
|
||||
int dataLen = data.readableBytes();
|
||||
ChannelBuffer encoded =
|
||||
channel.getConfig().getBufferFactory().getBuffer(
|
||||
data.order(), dataLen + 5);
|
||||
|
||||
// Encode type.
|
||||
encoded.writeByte((byte) type);
|
||||
|
||||
// Encode length.
|
||||
int b1 = dataLen >>> 28 & 0x7F;
|
||||
int b2 = dataLen >>> 14 & 0x7F;
|
||||
int b3 = dataLen >>> 7 & 0x7F;
|
||||
int b4 = dataLen & 0x7F;
|
||||
if (b1 == 0) {
|
||||
if (b2 == 0) {
|
||||
if (b3 == 0) {
|
||||
encoded.writeByte(b4);
|
||||
} else {
|
||||
encoded.writeByte(b3 | 0x80);
|
||||
encoded.writeByte(b4);
|
||||
}
|
||||
} else {
|
||||
encoded.writeByte(b2 | 0x80);
|
||||
encoded.writeByte(b3 | 0x80);
|
||||
encoded.writeByte(b4);
|
||||
}
|
||||
} else {
|
||||
encoded.writeByte(b1 | 0x80);
|
||||
encoded.writeByte(b2 | 0x80);
|
||||
encoded.writeByte(b3 | 0x80);
|
||||
encoded.writeByte(b4);
|
||||
}
|
||||
|
||||
// Encode binary data.
|
||||
encoded.writeBytes(data, data.readerIndex(), dataLen);
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Encoder, decoder and their related message types for
|
||||
* <a href="http://en.wikipedia.org/wiki/Web_Sockets">Web Socket</a> data frames.
|
||||
* <p>
|
||||
* For the detailed instruction on adding add Web Socket support to your HTTP
|
||||
* server, take a look into the <tt>WebSocketServer</tt> example located in the
|
||||
* {@code io.netty.example.http.websocket} package.
|
||||
* *
|
||||
* @apiviz.exclude OneToOne(Encoder|Decoder)$
|
||||
* @apiviz.exclude \.codec\.replay\.
|
||||
* @apiviz.exclude \.Default
|
||||
*/
|
||||
package io.netty.handler.codec.http.websocket;
|
@ -23,7 +23,8 @@
|
||||
* <ul>
|
||||
* <li><a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">draft-ietf-hybi-thewebsocketprotocol-00</a></li>
|
||||
* <li><a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10">draft-ietf-hybi-thewebsocketprotocol-10</a></li>
|
||||
* </ul>
|
||||
* <li><a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">draft-ietf-hybi-thewebsocketprotocol-17</a></li>
|
||||
* </ul>
|
||||
* </p>
|
||||
* <p>
|
||||
* In the future, as the specification develops, more versions will be supported.
|
||||
|
Loading…
x
Reference in New Issue
Block a user