package io.undertow.websockets.core.protocol.version07;
import io.undertow.websockets.core.StreamSourceFrameChannel;
import io.undertow.websockets.core.WebSocketFrameType;
import io.undertow.websockets.core.WebSocketMessages;
import io.undertow.connector.PooledByteBuffer;
import java.io.IOException;
import java.nio.ByteBuffer;
class WebSocket07CloseFrameSourceChannel extends StreamSourceFrameChannel {
WebSocket07CloseFrameSourceChannel(WebSocket07Channel wsChannel, int rsv, Masker masker, PooledByteBuffer pooled, long frameLength) {
super(wsChannel, WebSocketFrameType.CLOSE, rsv, true, pooled, frameLength, masker, new CloseFrameValidatorChannelFunction(wsChannel));
}
WebSocket07CloseFrameSourceChannel(WebSocket07Channel wsChannel, int rsv, PooledByteBuffer pooled, long frameLength) {
super(wsChannel, WebSocketFrameType.CLOSE, rsv, true, pooled, frameLength, null, new CloseFrameValidatorChannelFunction(wsChannel));
}
public static class CloseFrameValidatorChannelFunction extends UTF8Checker {
private final WebSocket07Channel wsChannel;
private int statusBytesRead;
private int status;
CloseFrameValidatorChannelFunction(WebSocket07Channel wsChannel) {
this.wsChannel = wsChannel;
}
@Override
public void afterRead(ByteBuffer buf, int position, int length) throws IOException {
int i = 0;
if(statusBytesRead < 2) {
while (statusBytesRead < 2 && i < length) {
status <<= 8;
status += buf.get(position + i) & 0xFF;
statusBytesRead ++;
++i;
}
if(statusBytesRead == 2) {
if (status >= 0 && status <= 999 || status >= 1004 && status <= 1006
|| status >= 1012 && status <= 2999 || status >= 5000) {
IOException exception = WebSocketMessages.MESSAGES.invalidCloseFrameStatusCode(status);
wsChannel.markReadsBroken(exception);
throw exception;
}
}
}
super.afterRead(buf, position + i, length - i);
}
}
}