netty5/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/CloseWebSocketFrame.java

205 lines
6.5 KiB
Java
Raw Normal View History

2011-09-26 14:51:15 +02:00
/*
* Copyright 2019 The Netty Project
2011-09-26 14:51:15 +02:00
*
* 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:
2011-09-26 14:51:15 +02:00
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-09-26 14:51:15 +02:00
*
* 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
2011-09-26 14:51:15 +02:00
* License for the specific language governing permissions and limitations
* under the License.
*/
2011-12-09 04:38:59 +01:00
package io.netty.handler.codec.http.websocketx;
2011-09-26 14:51:15 +02:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.StringUtil;
2011-09-26 14:51:15 +02:00
/**
* Web Socket Frame for closing the connection.
2011-09-26 14:51:15 +02:00
*/
public class CloseWebSocketFrame extends WebSocketFrame {
/**
* Creates a new empty close frame.
*/
public CloseWebSocketFrame() {
super(Unpooled.buffer(0));
}
/**
* Creates a new empty close frame with closing status code and reason text
*
* @param status
* Status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
* example, <tt>1000</tt> indicates normal closure.
*/
public CloseWebSocketFrame(WebSocketCloseStatus status) {
this(status.code(), status.reasonText());
}
/**
* Creates a new empty close frame with closing status code and reason text
*
* @param status
* Status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
* example, <tt>1000</tt> indicates normal closure.
* @param reasonText
* Reason text. Set to null if no text.
*/
public CloseWebSocketFrame(WebSocketCloseStatus status, String reasonText) {
this(status.code(), reasonText);
}
/**
* Creates a new empty close frame with closing status code and reason text
2012-05-31 21:03:01 +02:00
*
* @param statusCode
* Integer status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
* example, <tt>1000</tt> indicates normal closure.
* @param reasonText
* Reason text. Set to null if no text.
*/
public CloseWebSocketFrame(int statusCode, String reasonText) {
this(true, 0, statusCode, reasonText);
}
/**
* Creates a new close frame with no losing status code and no reason text
2012-05-31 21:03:01 +02:00
*
* @param finalFragment
* flag indicating if this frame is the final fragment
* @param rsv
* reserved bits used for protocol extensions.
*/
public CloseWebSocketFrame(boolean finalFragment, int rsv) {
Fix NPE problems Motivation: Now Netty has a few problems with null values. Modifications: - Check HAProxyProxiedProtocol in HAProxyMessage constructor and throw NPE if it is null. If HAProxyProxiedProtocol is null we will set AddressFamily as null. So we will get NPE inside checkAddress(String, AddressFamily) and it won't be easy to understand why addrFamily is null. - Check File in DiskFileUpload.toString(). If File is null we will get NPE when calling toString() method. - Check Result<String> in MqttDecoder.decodeConnectionPayload(...). If !mqttConnectVariableHeader.isWillFlag() || !mqttConnectVariableHeader.hasUserName() || !mqttConnectVariableHeader.hasPassword() we will get NPE when we will try to create new instance of MqttConnectPayload. - Check Unsafe before calling unsafe.getClass() in PlatformDependent0 static block. - Removed unnecessary null check in WebSocket08FrameEncoder.encode(...). Because msg.content() can not return null. - Removed unnecessary null check in DefaultStompFrame(StompCommand) constructor. Because we have this check in the super class. - Removed unnecessary null checks in ConcurrentHashMapV8.removeTreeNode(TreeNode<K,V>). - Removed unnecessary null check in OioDatagramChannel.doReadMessages(List<Object>). Because tmpPacket.getSocketAddress() always returns new SocketAddress instance. - Removed unnecessary null check in OioServerSocketChannel.doReadMessages(List<Object>). Because socket.accept() always returns new Socket instance. - Pass Unpooled.buffer(0) instead of null inside CloseWebSocketFrame(boolean, int) constructor. If we will pass null we will get NPE in super class constructor. - Added throw new IllegalStateException in GlobalEventExecutor.awaitInactivity(long, TimeUnit) if it will be called before GlobalEventExecutor.execute(Runnable). Because now we will get NPE. IllegalStateException will be better in this case. - Fixed null check in OpenSslServerContext.setTicketKeys(byte[]). Now we throw new NPE if byte[] is not null. Result: Added new null checks when it is necessary, removed unnecessary null checks and fixed some NPE problems.
2014-07-19 19:51:19 +02:00
this(finalFragment, rsv, Unpooled.buffer(0));
}
/**
* Creates a new close frame with closing status code and reason text
2012-05-31 21:03:01 +02:00
*
* @param finalFragment
* flag indicating if this frame is the final fragment
* @param rsv
* reserved bits used for protocol extensions
* @param statusCode
* Integer status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. For
* example, <tt>1000</tt> indicates normal closure.
* @param reasonText
* Reason text. Set to null if no text.
*/
public CloseWebSocketFrame(boolean finalFragment, int rsv, int statusCode, String reasonText) {
super(finalFragment, rsv, newBinaryData(statusCode, reasonText));
}
private static ByteBuf newBinaryData(int statusCode, String reasonText) {
if (reasonText == null) {
reasonText = StringUtil.EMPTY_STRING;
}
ByteBuf binaryData = Unpooled.buffer(2 + reasonText.length());
binaryData.writeShort(statusCode);
if (!reasonText.isEmpty()) {
binaryData.writeCharSequence(reasonText, CharsetUtil.UTF_8);
}
binaryData.readerIndex(0);
return binaryData;
}
/**
* Creates a new close frame
2012-05-31 21:03:01 +02:00
*
* @param finalFragment
* flag indicating if this frame is the final fragment
* @param rsv
* reserved bits used for protocol extensions
* @param binaryData
* the content of the frame. Must be 2 byte integer followed by optional UTF-8 encoded string.
*/
public CloseWebSocketFrame(boolean finalFragment, int rsv, ByteBuf binaryData) {
super(finalFragment, rsv, binaryData);
}
/**
* Returns the closing status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. If
* a status code is set, -1 is returned.
*/
public int statusCode() {
ByteBuf binaryData = content();
if (binaryData == null || binaryData.capacity() == 0) {
return -1;
}
binaryData.readerIndex(0);
return binaryData.getShort(0);
}
/**
* Returns the reason text as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a> If a reason
* text is not supplied, an empty string is returned.
*/
public String reasonText() {
ByteBuf binaryData = content();
if (binaryData == null || binaryData.capacity() <= 2) {
return "";
}
binaryData.readerIndex(2);
String reasonText = binaryData.toString(CharsetUtil.UTF_8);
binaryData.readerIndex(0);
return reasonText;
}
2011-11-04 19:28:15 +01:00
@Override
public CloseWebSocketFrame copy() {
return (CloseWebSocketFrame) super.copy();
}
@Override
public CloseWebSocketFrame duplicate() {
return (CloseWebSocketFrame) super.duplicate();
}
@Override
public CloseWebSocketFrame retainedDuplicate() {
return (CloseWebSocketFrame) super.retainedDuplicate();
}
@Override
public CloseWebSocketFrame replace(ByteBuf content) {
return new CloseWebSocketFrame(isFinalFragment(), rsv(), content);
}
@Override
public CloseWebSocketFrame retain() {
super.retain();
return this;
}
@Override
public CloseWebSocketFrame retain(int increment) {
super.retain(increment);
return this;
}
@Override
public CloseWebSocketFrame touch() {
super.touch();
return this;
}
@Override
public CloseWebSocketFrame touch(Object hint) {
super.touch(hint);
return this;
}
2011-09-26 14:51:15 +02:00
}