View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.util.zip.DataFormatException;
22  
23  import org.eclipse.jetty.websocket.api.BadPayloadException;
24  import org.eclipse.jetty.websocket.api.extensions.Frame;
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 ( frame.getType().isControl() || !frame.isRsv1() || !frame.hasPayload() )
59          {
60              nextIncomingFrame(frame);
61              return;
62          }
63  
64          try
65          {
66              ByteAccumulator accumulator = newByteAccumulator();
67              decompress(accumulator, frame.getPayload());
68              decompress(accumulator, TAIL_BYTES_BUF.slice());
69              forwardIncoming(frame, accumulator);
70          }
71          catch (DataFormatException e)
72          {
73              throw new BadPayloadException(e);
74          }
75      }
76  }