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
19package org.eclipse.jetty.websocket;
20
21import java.io.IOException;
22import java.util.Map;
23import java.util.zip.DataFormatException;
24import java.util.zip.Deflater;
25import java.util.zip.Inflater;
26
27import org.eclipse.jetty.io.Buffer;
28import org.eclipse.jetty.io.ByteArrayBuffer;
29import org.eclipse.jetty.util.log.Log;
30import org.eclipse.jetty.util.log.Logger;
31
32/**
33 * TODO Implement proposed deflate frame draft
34 */
35public class DeflateFrameExtension extends AbstractExtension
36{
37    private static final Logger LOG = Log.getLogger(DeflateFrameExtension.class);
38
39    private int _minLength=8;
40    private Deflater _deflater;
41    private Inflater _inflater;
42
43    public DeflateFrameExtension()
44    {
45        super("x-deflate-frame");
46    }
47
48    @Override
49    public boolean init(Map<String, String> parameters)
50    {
51        if (!parameters.containsKey("minLength"))
52            parameters.put("minLength",Integer.toString(_minLength));
53        if(super.init(parameters))
54        {
55            _minLength=getInitParameter("minLength",_minLength);
56
57            _deflater=new Deflater();
58            _inflater=new Inflater();
59
60            return true;
61        }
62        return false;
63    }
64
65    /* (non-Javadoc)
66     * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer)
67     */
68    @Override
69    public void onFrame(byte flags, byte opcode, Buffer buffer)
70    {
71        if (getConnection().isControl(opcode) || !isFlag(flags,1))
72        {
73            super.onFrame(flags,opcode,buffer);
74            return;
75        }
76
77        if (buffer.array()==null)
78            buffer=buffer.asMutableBuffer();
79
80        int length=0xff&buffer.get();
81        if (length>=0x7e)
82        {
83            int b=(length==0x7f)?8:2;
84            length=0;
85            while(b-->0)
86                length=0x100*length+(0xff&buffer.get());
87        }
88
89        // TODO check a max framesize
90
91        _inflater.setInput(buffer.array(),buffer.getIndex(),buffer.length());
92        ByteArrayBuffer buf = new ByteArrayBuffer(length);
93        try
94        {
95            while(_inflater.getRemaining()>0)
96            {
97                int inflated=_inflater.inflate(buf.array(),buf.putIndex(),buf.space());
98                if (inflated==0)
99                    throw new DataFormatException("insufficient data");
100                buf.setPutIndex(buf.putIndex()+inflated);
101            }
102
103            super.onFrame(clearFlag(flags,1),opcode,buf);
104        }
105        catch(DataFormatException e)
106        {
107            LOG.warn(e);
108            getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD,e.toString());
109        }
110    }
111
112    /* (non-Javadoc)
113     * @see org.eclipse.jetty.websocket.AbstractExtension#addFrame(byte, byte, byte[], int, int)
114     */
115    @Override
116    public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
117    {
118        if (getConnection().isControl(opcode) || length<_minLength)
119        {
120            super.addFrame(clearFlag(flags,1),opcode,content,offset,length);
121            return;
122        }
123
124        // prepare the uncompressed input
125        _deflater.reset();
126        _deflater.setInput(content,offset,length);
127        _deflater.finish();
128
129        // prepare the output buffer
130        byte[] out= new byte[length];
131        int out_offset=0;
132
133        // write the uncompressed length
134        if (length>0xffff)
135        {
136            out[out_offset++]=0x7f;
137            out[out_offset++]=(byte)0;
138            out[out_offset++]=(byte)0;
139            out[out_offset++]=(byte)0;
140            out[out_offset++]=(byte)0;
141            out[out_offset++]=(byte)((length>>24)&0xff);
142            out[out_offset++]=(byte)((length>>16)&0xff);
143            out[out_offset++]=(byte)((length>>8)&0xff);
144            out[out_offset++]=(byte)(length&0xff);
145        }
146        else if (length >=0x7e)
147        {
148            out[out_offset++]=0x7e;
149            out[out_offset++]=(byte)(length>>8);
150            out[out_offset++]=(byte)(length&0xff);
151        }
152        else
153        {
154            out[out_offset++]=(byte)(length&0x7f);
155        }
156
157        int l = _deflater.deflate(out,out_offset,length-out_offset);
158
159        if (_deflater.finished())
160            super.addFrame(setFlag(flags,1),opcode,out,0,l+out_offset);
161        else
162            super.addFrame(clearFlag(flags,1),opcode,content,offset,length);
163    }
164}
165