|
22 | 22 | import io.netty.channel.SimpleChannelInboundHandler;
|
23 | 23 | import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
|
24 | 24 | import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
| 25 | +import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame; |
25 | 26 | import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
26 | 27 | import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
27 | 28 | import org.openqa.selenium.remote.http.BinaryMessage;
|
28 | 29 | import org.openqa.selenium.remote.http.CloseMessage;
|
29 | 30 | import org.openqa.selenium.remote.http.Message;
|
30 | 31 | import org.openqa.selenium.remote.http.TextMessage;
|
31 | 32 |
|
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.UncheckedIOException; |
32 | 36 | import java.nio.ByteBuffer;
|
33 | 37 | import java.util.logging.Logger;
|
34 | 38 |
|
35 | 39 | class MessageInboundConverter extends SimpleChannelInboundHandler<WebSocketFrame> {
|
36 | 40 |
|
| 41 | + private enum Continuation { |
| 42 | + Text, Binary, None |
| 43 | + } |
| 44 | + |
37 | 45 | private static final Logger LOG = Logger.getLogger(MessageInboundConverter.class.getName());
|
38 | 46 |
|
| 47 | + private Continuation next; |
| 48 | + private StringBuilder builder; |
| 49 | + private ByteArrayOutputStream buffer; |
| 50 | + |
| 51 | + public MessageInboundConverter() { |
| 52 | + next = Continuation.None; |
| 53 | + buffer = new ByteArrayOutputStream(); |
| 54 | + builder = new StringBuilder(); |
| 55 | + } |
| 56 | + |
39 | 57 | @Override
|
40 | 58 | protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
41 |
| - if (!frame.isFinalFragment()) { |
42 |
| - LOG.warning("Frame is not final. Chaos may ensue"); |
43 |
| - } |
| 59 | + boolean finalFragment = frame.isFinalFragment(); |
| 60 | + Message message; |
| 61 | + |
| 62 | + if (frame instanceof ContinuationWebSocketFrame) { |
| 63 | + switch (next) { |
| 64 | + case Binary: |
| 65 | + try { |
| 66 | + ByteBuf content = frame.content(); |
| 67 | + content.readBytes(buffer, content.readableBytes()); |
| 68 | + } catch (IOException e) { |
| 69 | + throw new UncheckedIOException("failed to transfer buffer", e); |
| 70 | + } |
44 | 71 |
|
45 |
| - Message message = null; |
| 72 | + if (finalFragment) { |
| 73 | + message = new BinaryMessage(buffer.toByteArray()); |
| 74 | + buffer.reset(); |
| 75 | + next = Continuation.None; |
| 76 | + } else { |
| 77 | + message = null; |
| 78 | + } |
| 79 | + break; |
| 80 | + case Text: |
| 81 | + builder.append(((ContinuationWebSocketFrame) frame).text()); |
46 | 82 |
|
47 |
| - if (frame instanceof TextWebSocketFrame) { |
48 |
| - message = new TextMessage(((TextWebSocketFrame) frame).text()); |
| 83 | + if (finalFragment) { |
| 84 | + message = new TextMessage(builder.toString()); |
| 85 | + builder.setLength(0); |
| 86 | + next = Continuation.None; |
| 87 | + } else { |
| 88 | + message = null; |
| 89 | + } |
| 90 | + break; |
| 91 | + case None: |
| 92 | + ctx.write(frame); |
| 93 | + return; |
| 94 | + default: |
| 95 | + throw new IllegalStateException("unexpected enum: " + next); |
| 96 | + } |
| 97 | + } else if (next != Continuation.None) { |
| 98 | + throw new IllegalStateException("expected a continuation frame"); |
| 99 | + } else if (frame instanceof TextWebSocketFrame) { |
| 100 | + if (finalFragment) { |
| 101 | + message = new TextMessage(((TextWebSocketFrame) frame).text()); |
| 102 | + } else { |
| 103 | + next = Continuation.Text; |
| 104 | + message = null; |
| 105 | + builder.append(((TextWebSocketFrame) frame).text()); |
| 106 | + } |
49 | 107 | } else if (frame instanceof BinaryWebSocketFrame) {
|
50 |
| - ByteBuf buf = frame.content(); |
51 |
| - if (buf.nioBufferCount() != -1) { |
52 |
| - message = new BinaryMessage(buf.nioBuffer()); |
53 |
| - } else if (buf.hasArray()) { |
54 |
| - message = new BinaryMessage(ByteBuffer.wrap(buf.array())); |
| 108 | + ByteBuf content = frame.content(); |
| 109 | + if (finalFragment) { |
| 110 | + if (content.nioBufferCount() != -1) { |
| 111 | + message = new BinaryMessage(content.nioBuffer()); |
| 112 | + } else if (content.hasArray()) { |
| 113 | + message = new BinaryMessage(ByteBuffer.wrap(content.array())); |
| 114 | + } else { |
| 115 | + throw new IllegalStateException("Unable to handle bytebuf: " + content); |
| 116 | + } |
55 | 117 | } else {
|
56 |
| - throw new IllegalStateException("Unable to handle bytebuf: " + buf); |
| 118 | + next = Continuation.Binary; |
| 119 | + message = null; |
| 120 | + try { |
| 121 | + content.readBytes(buffer, content.readableBytes()); |
| 122 | + } catch (IOException e) { |
| 123 | + throw new UncheckedIOException("failed to transfer buffer", e); |
| 124 | + } |
57 | 125 | }
|
58 | 126 | } else if (frame instanceof CloseWebSocketFrame) {
|
59 | 127 | CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
|
60 | 128 | message = new CloseMessage(closeFrame.statusCode(), closeFrame.reasonText());
|
| 129 | + } else { |
| 130 | + ctx.write(frame); |
| 131 | + return; |
61 | 132 | }
|
62 | 133 |
|
63 | 134 | if (message != null) {
|
64 | 135 | ctx.fireChannelRead(message);
|
65 |
| - } else { |
66 |
| - ctx.write(frame); |
67 | 136 | }
|
68 | 137 | }
|
69 | 138 | }
|
0 commit comments