1 module hunt.http.codec.websocket.frame;
2 
3 public import hunt.http.codec.websocket.frame.BinaryFrame;
4 public import hunt.http.codec.websocket.frame.CloseFrame;
5 public import hunt.http.codec.websocket.frame.ContinuationFrame;
6 public import hunt.http.codec.websocket.frame.ControlFrame;
7 public import hunt.http.codec.websocket.frame.DataFrame;
8 public import hunt.http.WebSocketFrame;
9 public import hunt.http.codec.websocket.frame.PingFrame;
10 public import hunt.http.codec.websocket.frame.PongFrame;
11 public import hunt.http.codec.websocket.frame.ReadOnlyDelegatedFrame;
12 public import hunt.http.codec.websocket.frame.TextFrame;
13 public import hunt.http.codec.websocket.frame.AbstractWebSocketFrame;
14 
15 
16 import hunt.http.WebSocketCommon;
17 import hunt.collection;
18 import hunt.Exceptions;
19 
20 import std.conv;
21 
22 class WebSocketFrameHelper {
23     static AbstractWebSocketFrame copy(WebSocketFrame original) {
24         AbstractWebSocketFrame copy;
25         switch (original.getOpCode()) {
26             case OpCode.BINARY:
27                 copy = new BinaryFrame();
28                 break;
29             case OpCode.TEXT:
30                 copy = new TextFrame();
31                 break;
32             case OpCode.CLOSE:
33                 copy = new CloseFrame();
34                 break;
35             case OpCode.CONTINUATION:
36                 copy = new ContinuationFrame();
37                 break;
38             case OpCode.PING:
39                 copy = new PingFrame();
40                 break;
41             case OpCode.PONG:
42                 copy = new PongFrame();
43                 break;
44             default:
45                 throw new IllegalArgumentException("Cannot copy frame with opcode " ~ 
46                     to!string(cast(int)original.getOpCode()) ~ " - " ~ (cast(Object)original).toString());
47         }
48 
49         copy.copyHeaders(original);
50         ByteBuffer payload = original.getPayload();
51         if (payload !is null) {
52             ByteBuffer payloadCopy = BufferUtils.allocate(payload.remaining());
53             payloadCopy.put(payload.slice()).flip();
54             copy.setPayload(payloadCopy);
55         }
56         return copy;
57     }
58 }