From 6bbeba9f59207104b127a364bb7e76371a901379 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Tue, 19 Jun 2012 13:07:57 -0700 Subject: [PATCH] Fixing PingPayloadParser with new FrameParser.copyBuffer() method --- .../eclipse/jetty/websocket/frames/PingFrame.java | 15 ++++++++++++++- .../eclipse/jetty/websocket/frames/PongFrame.java | 4 +++- .../jetty/websocket/parser/FrameParser.java | 9 +++++++++ .../jetty/websocket/parser/PingPayloadParser.java | 9 ++------- .../jetty/websocket/parser/FrameParseCapture.java | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java index 6e655116d27..f519fb2b27b 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java @@ -34,9 +34,22 @@ public class PingFrame extends ControlFrame return OpCode.PING; } + public String getPayloadAsString() + { + if (hasPayload()) + { + ByteBuffer payload = getPayload(); + return payload.asCharBuffer().toString(); + } + else + { + return ""; + } + } + @Override public String toString() { - return String.format("%s ping, payload[has=%b, string=%s]",super.toString(),hasPayload(),getPayload().toString()); + return String.format("%s ping, payload[has=%b, string=%s]",super.toString(),hasPayload(),getPayloadAsString()); } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java index e36554c874e..15b8339ee62 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java @@ -30,7 +30,9 @@ public class PongFrame extends ControlFrame /** * Construct appropriate PongFrame from PingFrame - * @param ping the ping frame to base pong from + * + * @param ping + * the ping frame to base pong from */ public PongFrame(PingFrame ping) { diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java index 52b036dd367..f7d52bda3f9 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java @@ -54,6 +54,15 @@ public abstract class FrameParser this.settings = settings; } + protected int copyBuffer(ByteBuffer src, ByteBuffer dest, int length) + { + int amt = Math.min(length,src.remaining()); + byte b[] = new byte[amt]; + src.get(b,0,amt); + dest.put(b,0,amt); + return amt; + } + /** * The frame that is being parsed * diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java index 6c66b528989..a9be62ceaa7 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java @@ -38,15 +38,10 @@ public class PingPayloadParser extends FrameParser payload = ByteBuffer.allocate(payloadLength); } - int size = Math.min(payloadLength,buffer.remaining()); - int limit = buffer.limit(); - buffer.limit(buffer.position() + size); - ByteBuffer bytes = buffer.slice(); // TODO: make sure reference to subsection is acceptable - buffer.limit(limit); - payload.put(bytes); + copyBuffer(buffer,payload,payloadLength); if (payload.position() >= payloadLength) { - frame.setPayload(bytes); + frame.setPayload(payload); return true; } } diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/parser/FrameParseCapture.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/parser/FrameParseCapture.java index 97735685f16..8eebf0427c9 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/parser/FrameParseCapture.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/parser/FrameParseCapture.java @@ -39,7 +39,7 @@ public class FrameParseCapture implements Parser.Listener { int count = 0; for(BaseFrame frame: frames) { - if (frame.getClass().isInstance(frameType)) + if (frameType.isInstance(frame)) { count++; }