Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8e55e89943b0803c160b981c3464547de7843a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket;

import java.io.IOException;
import java.util.Map;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * TODO Implement proposed deflate frame draft
 */
public class DeflateFrameExtension extends AbstractExtension
{
    private static final Logger LOG = Log.getLogger(DeflateFrameExtension.class);

    private int _minLength=8;
    private Deflater _deflater;
    private Inflater _inflater;

    public DeflateFrameExtension()
    {
        super("x-deflate-frame");
    }

    @Override
    public boolean init(Map<String, String> parameters)
    {
        if (!parameters.containsKey("minLength"))
            parameters.put("minLength",Integer.toString(_minLength));
        if(super.init(parameters))
        {
            _minLength=getInitParameter("minLength",_minLength);

            _deflater=new Deflater();
            _inflater=new Inflater();

            return true;
        }
        return false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer)
     */
    @Override
    public void onFrame(byte flags, byte opcode, Buffer buffer)
    {
        if (getConnection().isControl(opcode) || !isFlag(flags,1))
        {
            super.onFrame(flags,opcode,buffer);
            return;
        }

        if (buffer.array()==null)
            buffer=buffer.asMutableBuffer();

        int length=0xff&buffer.get();
        if (length>=0x7e)
        {
            int b=(length==0x7f)?8:2;
            length=0;
            while(b-->0)
                length=0x100*length+(0xff&buffer.get());
        }

        // TODO check a max framesize

        _inflater.setInput(buffer.array(),buffer.getIndex(),buffer.length());
        ByteArrayBuffer buf = new ByteArrayBuffer(length);
        try
        {
            while(_inflater.getRemaining()>0)
            {
                int inflated=_inflater.inflate(buf.array(),buf.putIndex(),buf.space());
                if (inflated==0)
                    throw new DataFormatException("insufficient data");
                buf.setPutIndex(buf.putIndex()+inflated);
            }

            super.onFrame(clearFlag(flags,1),opcode,buf);
        }
        catch(DataFormatException e)
        {
            LOG.warn(e);
            getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD,e.toString());
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.jetty.websocket.AbstractExtension#addFrame(byte, byte, byte[], int, int)
     */
    @Override
    public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException
    {
        if (getConnection().isControl(opcode) || length<_minLength)
        {
            super.addFrame(clearFlag(flags,1),opcode,content,offset,length);
            return;
        }

        // prepare the uncompressed input
        _deflater.reset();
        _deflater.setInput(content,offset,length);
        _deflater.finish();

        // prepare the output buffer
        byte[] out= new byte[length];
        int out_offset=0;

        // write the uncompressed length
        if (length>0xffff)
        {
            out[out_offset++]=0x7f;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)0;
            out[out_offset++]=(byte)((length>>24)&0xff);
            out[out_offset++]=(byte)((length>>16)&0xff);
            out[out_offset++]=(byte)((length>>8)&0xff);
            out[out_offset++]=(byte)(length&0xff);
        }
        else if (length >=0x7e)
        {
            out[out_offset++]=0x7e;
            out[out_offset++]=(byte)(length>>8);
            out[out_offset++]=(byte)(length&0xff);
        }
        else
        {
            out[out_offset++]=(byte)(length&0x7f);
        }

        int l = _deflater.deflate(out,out_offset,length-out_offset);

        if (_deflater.finished())
            super.addFrame(setFlag(flags,1),opcode,out,0,l+out_offset);
        else
            super.addFrame(clearFlag(flags,1),opcode,content,offset,length);
    }
}

Back to the top