View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.extensions.compress;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.websocket.api.extensions.Frame;
24  import org.eclipse.jetty.websocket.common.OpCode;
25  
26  /**
27   * Implementation of the
28   * <a href="https://tools.ietf.org/id/draft-tyoshino-hybi-websocket-perframe-deflate.txt">deflate-frame</a>
29   * extension seen out in the wild.
30   */
31  public class DeflateFrameExtension extends CompressExtension
32  {
33      @Override
34      public String getName()
35      {
36          return "deflate-frame";
37      }
38      
39      @Override
40      int getRsvUseMode()
41      {
42          return RSV_USE_ALWAYS;
43      }
44      
45      @Override
46      int getTailDropMode()
47      {
48          return TAIL_DROP_ALWAYS;
49      }
50  
51      @Override
52      public void incomingFrame(Frame frame)
53      {
54          // Incoming frames are always non concurrent because
55          // they are read and parsed with a single thread, and
56          // therefore there is no need for synchronization.
57  
58          if (OpCode.isControlFrame(frame.getOpCode()) || !frame.isRsv1() || !frame.hasPayload())
59          {
60              nextIncomingFrame(frame);
61              return;
62          }
63  
64          ByteBuffer payload = frame.getPayload();
65          int remaining = payload.remaining();
66          byte[] input = new byte[remaining + TAIL_BYTES.length];
67          payload.get(input, 0, remaining);
68          System.arraycopy(TAIL_BYTES, 0, input, remaining, TAIL_BYTES.length);
69  
70          forwardIncoming(frame, decompress(input));
71      }
72  }