package jdk.internal.net.http.websocket;
import jdk.internal.net.http.common.Logger;
import jdk.internal.net.http.common.Utils;
import jdk.internal.net.http.websocket.Frame.Opcode;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
public class MessageEncoder {
private static final Logger debug =
Utils.getWebSocketLogger("[Output]"::toString, Utils.DEBUG_WS);
private final SecureRandom maskingKeySource = new SecureRandom();
private final Frame.HeaderWriter = new Frame.HeaderWriter();
private final Frame.Masker payloadMasker = new Frame.Masker();
private final CharsetEncoder charsetEncoder
= StandardCharsets.UTF_8.newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
private final ByteBuffer intermediateBuffer = createIntermediateBuffer(
Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH);
private final ByteBuffer = ByteBuffer.allocate(
Frame.MAX_HEADER_SIZE_BYTES);
private boolean started;
private boolean flushing;
private boolean moreText = true;
private long ;
private boolean previousFin = true;
private boolean previousText;
private boolean closed;
private int actualLen;
private int expectedLen;
protected ByteBuffer createIntermediateBuffer(int minSize) {
int capacity = Utils.getIntegerNetProperty(
"jdk.httpclient.websocket.intermediateBufferSize", 16384);
return ByteBuffer.allocate(Math.max(minSize, capacity));
}
public void reset() {
started = false;
flushing = false;
moreText = true;
headerCount = 0;
actualLen = 0;
}
public boolean encodeText(CharBuffer src, boolean last, ByteBuffer dst)
throws IOException
{
if (debug.on()) {
debug.log("encode text src=[pos=%s lim=%s cap=%s] last=%s dst=%s",
src.position(), src.limit(), src.capacity(), last, dst);
}
if (closed) {
throw new IOException("Output closed");
}
if (!started) {
if (!previousText && !previousFin) {
throw new IllegalStateException("Unexpected text message");
}
started = true;
headerBuffer.position(0).limit(0);
intermediateBuffer.position(0).limit(0);
charsetEncoder.reset();
}
while (true) {
if (debug.on()) {
debug.log("put");
}
if (!putAvailable(headerBuffer, dst)) {
return false;
}
if (debug.on()) {
debug.log("mask");
}
if (maskAvailable(intermediateBuffer, dst) < 0) {
return false;
}
if (debug.on()) {
debug.log("moreText");
}
if (!moreText) {
previousFin = last;
previousText = true;
return true;
}
intermediateBuffer.clear();
CoderResult r = null;
if (!flushing) {
r = charsetEncoder.encode(src, intermediateBuffer, true);
if (r.isUnderflow()) {
flushing = true;
}
}
if (flushing) {
r = charsetEncoder.flush(intermediateBuffer);
if (r.isUnderflow()) {
moreText = false;
}
}
if (r.isError()) {
try {
r.throwException();
} catch (CharacterCodingException e) {
throw new IOException("Malformed text message", e);
}
}
if (debug.on()) {
debug.log("frame #%s", headerCount);
}
intermediateBuffer.flip();
Opcode opcode = previousFin && headerCount == 0
? Opcode.TEXT : Opcode.CONTINUATION;
boolean fin = last && !moreText;
setupHeader(opcode, fin, intermediateBuffer.remaining());
headerCount++;
}
}
private boolean putAvailable(ByteBuffer src, ByteBuffer dst) {
int available = dst.remaining();
if (available >= src.remaining()) {
dst.put(src);
return true;
} else {
int lim = src.limit();
src.limit(src.position() + available);
dst.put(src);
src.limit(lim);
return false;
}
}
public boolean encodeBinary(ByteBuffer src, boolean last, ByteBuffer dst)
throws IOException
{
if (debug.on()) {
debug.log("encode binary src=%s last=%s dst=%s",
src, last, dst);
}
if (closed) {
throw new IOException("Output closed");
}
if (!started) {
if (previousText && !previousFin) {
throw new IllegalStateException("Unexpected binary message");
}
expectedLen = src.remaining();
Opcode opcode = previousFin ? Opcode.BINARY : Opcode.CONTINUATION;
setupHeader(opcode, last, expectedLen);
previousFin = last;
previousText = false;
started = true;
}
if (!putAvailable(headerBuffer, dst)) {
return false;
}
int count = maskAvailable(src, dst);
actualLen += Math.abs(count);
if (count >= 0 && actualLen != expectedLen) {
throw new IOException("Concurrent message modification");
}
return count >= 0;
}
private int maskAvailable(ByteBuffer src, ByteBuffer dst) {
int r0 = dst.remaining();
payloadMasker.transferMasking(src, dst);
int masked = r0 - dst.remaining();
return src.hasRemaining() ? -masked : masked;
}
public boolean encodePing(ByteBuffer src, ByteBuffer dst)
throws IOException
{
if (debug.on()) {
debug.log("encode ping src=%s dst=%s", src, dst);
}
if (closed) {
throw new IOException("Output closed");
}
if (!started) {
expectedLen = src.remaining();
if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
throw new IllegalArgumentException("Long message: " + expectedLen);
}
setupHeader(Opcode.PING, true, expectedLen);
started = true;
}
if (!putAvailable(headerBuffer, dst)) {
return false;
}
int count = maskAvailable(src, dst);
actualLen += Math.abs(count);
if (count >= 0 && actualLen != expectedLen) {
throw new IOException("Concurrent message modification");
}
return count >= 0;
}
public boolean encodePong(ByteBuffer src, ByteBuffer dst)
throws IOException
{
if (debug.on()) {
debug.log("encode pong src=%s dst=%s",
src, dst);
}
if (closed) {
throw new IOException("Output closed");
}
if (!started) {
expectedLen = src.remaining();
if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
throw new IllegalArgumentException("Long message: " + expectedLen);
}
setupHeader(Opcode.PONG, true, expectedLen);
started = true;
}
if (!putAvailable(headerBuffer, dst)) {
return false;
}
int count = maskAvailable(src, dst);
actualLen += Math.abs(count);
if (count >= 0 && actualLen != expectedLen) {
throw new IOException("Concurrent message modification");
}
return count >= 0;
}
public boolean encodeClose(int statusCode, CharBuffer reason, ByteBuffer dst)
throws IOException
{
if (debug.on()) {
debug.log("encode close statusCode=%s reason=[pos=%s lim=%s cap=%s] dst=%s",
statusCode, reason.position(), reason.limit(), reason.capacity(), dst);
}
if (closed) {
throw new IOException("Output closed");
}
if (!started) {
if (debug.on()) {
debug.log("reason [pos=%s lim=%s cap=%s]",
reason.position(), reason.limit(), reason.capacity());
}
intermediateBuffer.position(0).limit(Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH);
intermediateBuffer.putChar((char) statusCode);
CoderResult r = charsetEncoder.reset().encode(reason, intermediateBuffer, true);
if (r.isUnderflow()) {
if (debug.on()) {
debug.log("flushing");
}
r = charsetEncoder.flush(intermediateBuffer);
}
if (debug.on()) {
debug.log("encoding result: %s", r);
}
if (r.isError()) {
try {
r.throwException();
} catch (CharacterCodingException e) {
throw new IOException("Malformed reason", e);
}
} else if (r.isOverflow()) {
throw new IOException("Long reason");
} else if (!r.isUnderflow()) {
throw new InternalError();
}
intermediateBuffer.flip();
setupHeader(Opcode.CLOSE, true, intermediateBuffer.remaining());
started = true;
closed = true;
if (debug.on()) {
debug.log("intermediateBuffer=%s", intermediateBuffer);
}
}
if (!putAvailable(headerBuffer, dst)) {
return false;
}
return maskAvailable(intermediateBuffer, dst) >= 0;
}
private void (Opcode opcode, boolean fin, long payloadLen) {
if (debug.on()) {
debug.log("frame opcode=%s fin=%s len=%s",
opcode, fin, payloadLen);
}
headerBuffer.clear();
int mask = maskingKeySource.nextInt();
headerWriter.fin(fin)
.opcode(opcode)
.payloadLen(payloadLen)
.mask(mask)
.write(headerBuffer);
headerBuffer.flip();
payloadMasker.mask(mask);
}
}