Jake Slack | 03928ae | 2014-05-13 18:41:56 -0700 | [diff] [blame] | 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; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | public class FragmentExtension extends AbstractExtension |
| 25 | { |
| 26 | private int _maxLength=-1; |
| 27 | private int _minFragments=1; |
| 28 | |
| 29 | public FragmentExtension() |
| 30 | { |
| 31 | super("fragment"); |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public boolean init(Map<String, String> parameters) |
| 36 | { |
| 37 | if(super.init(parameters)) |
| 38 | { |
| 39 | _maxLength=getInitParameter("maxLength",_maxLength); |
| 40 | _minFragments=getInitParameter("minFragments",_minFragments); |
| 41 | return true; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length) throws IOException |
| 48 | { |
| 49 | if (getConnection().isControl(opcode)) |
| 50 | { |
| 51 | super.addFrame(flags,opcode,content,offset,length); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | int fragments=1; |
| 56 | |
| 57 | while (_maxLength>0 && length>_maxLength) |
| 58 | { |
| 59 | fragments++; |
| 60 | super.addFrame((byte)(flags&~getConnection().finMask()),opcode,content,offset,_maxLength); |
| 61 | length-=_maxLength; |
| 62 | offset+=_maxLength; |
| 63 | opcode=getConnection().continuationOpcode(); |
| 64 | } |
| 65 | |
| 66 | while (fragments<_minFragments) |
| 67 | { |
| 68 | int frag=length/2; |
| 69 | fragments++; |
| 70 | super.addFrame((byte)(flags&0x7),opcode,content,offset,frag); |
| 71 | length-=frag; |
| 72 | offset+=frag; |
| 73 | opcode=getConnection().continuationOpcode(); |
| 74 | } |
| 75 | |
| 76 | super.addFrame((byte)(flags|getConnection().finMask()),opcode,content,offset,length); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | } |