< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/frame/FramesDecoder.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.http.internal.frame;
  27 
  28 import jdk.incubator.http.internal.common.Log;
  29 import jdk.incubator.http.internal.common.Utils;
  30 
  31 import java.io.IOException;
  32 import java.lang.System.Logger.Level;
  33 import java.nio.ByteBuffer;
  34 import java.util.ArrayDeque;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Queue;


  38 
  39 /**
  40  * Frames Decoder
  41  * <p>
  42  * collect buffers until frame decoding is possible,
  43  * all decoded frames are passed to the FrameProcessor callback in order of decoding.
  44  *
  45  * It's a stateful class due to the fact that FramesDecoder stores buffers inside.
  46  * Should be allocated only the single instance per connection.
  47  */
  48 public class FramesDecoder {
  49 
  50     static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
  51     static final System.Logger DEBUG_LOGGER =
  52             Utils.getDebugLogger("FramesDecoder"::toString, DEBUG);
  53 
  54     @FunctionalInterface
  55     public interface FrameProcessor {
  56         void processFrame(Http2Frame frame) throws IOException;
  57     }


 264         byte[] bytes = new byte[n];
 265         int offset = 0;
 266         while (n > 0) {
 267             int length = Math.min(n, currentBuffer.remaining());
 268             currentBuffer.get(bytes, offset, length);
 269             offset += length;
 270             n -= length;
 271             nextBuffer();
 272         }
 273         return bytes;
 274 
 275     }
 276 
 277     private List<ByteBuffer> getBuffers(boolean isDataFrame, int bytecount) {
 278         List<ByteBuffer> res = new ArrayList<>();
 279         while (bytecount > 0) {
 280             int remaining = currentBuffer.remaining();
 281             int extract = Math.min(remaining, bytecount);
 282             ByteBuffer extractedBuf;
 283             if (isDataFrame) {
 284                 extractedBuf = Utils.slice(currentBuffer, extract);

 285                 slicedToDataFrame = true;
 286             } else {
 287                 // Header frames here
 288                 // HPACK decoding should performed under lock and immediately after frame decoding.
 289                 // in that case it is safe to release original buffer,
 290                 // because of sliced buffer has a very short life
 291                 extractedBuf = Utils.slice(currentBuffer, extract);
 292             }
 293             res.add(extractedBuf);
 294             bytecount -= extract;
 295             nextBuffer();
 296         }
 297         return res;
 298     }
 299 
 300     public void close(String msg) {
 301         closed = true;
 302         tailBuffers.clear();
 303         int bytes = tailSize;
 304         ByteBuffer b = currentBuffer;
 305         if (b != null) {
 306             bytes += b.remaining();
 307             b.position(b.limit());
 308         }
 309         tailSize = 0;
 310         currentBuffer = null;
 311         DEBUG_LOGGER.log(Level.DEBUG, "closed %s, ignoring %d bytes", msg, bytes);


 502             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 503                     "PingFrame length is "+ frameLength+", expected 8");
 504         }
 505         return new PingFrame(flags, getBytes(8));
 506     }
 507 
 508     private Http2Frame parseGoAwayFrame(int frameLength, int streamid, int flags) {
 509         // only zero stream; no flags
 510         if (streamid != 0) {
 511             return new MalformedFrame(ErrorFrame.PROTOCOL_ERROR,
 512                     "non-zero streamId for GoAwayFrame");
 513         }
 514         if (frameLength < 8) {
 515             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 516                     "Invalid GoAway frame size");
 517         }
 518         int lastStream = getInt() & 0x7fffffff;
 519         int errorCode = getInt();
 520         byte[] debugData = getBytes(frameLength - 8);
 521         if (debugData.length > 0) {
 522             Log.logError("GoAway debugData " + new String(debugData));
 523         }
 524         return new GoAwayFrame(lastStream, errorCode, debugData);
 525     }
 526 
 527     private Http2Frame parseWindowUpdateFrame(int frameLength, int streamid, int flags) {
 528         // any stream; no flags
 529         if(frameLength != 4) {
 530             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 531                     "WindowUpdateFrame length is "+ frameLength+", expected 4");
 532         }
 533         return new WindowUpdateFrame(streamid, getInt() & 0x7fffffff);
 534     }
 535 
 536     private Http2Frame parseContinuationFrame(int frameLength, int streamid, int flags) {
 537         // non-zero stream;
 538         if (streamid == 0) {
 539             return new MalformedFrame(ErrorFrame.PROTOCOL_ERROR,
 540                     "zero streamId for ContinuationFrame");
 541         }
 542         return new ContinuationFrame(streamid, flags, getBuffers(false, frameLength));
   1 /*
   2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.net.http.frame;



  27 
  28 import java.io.IOException;
  29 import java.lang.System.Logger.Level;
  30 import java.nio.ByteBuffer;
  31 import java.util.ArrayDeque;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import jdk.internal.net.http.common.Log;
  35 import jdk.internal.net.http.common.Utils;
  36 import static java.nio.charset.StandardCharsets.UTF_8;
  37 
  38 /**
  39  * Frames Decoder
  40  * <p>
  41  * collect buffers until frame decoding is possible,
  42  * all decoded frames are passed to the FrameProcessor callback in order of decoding.
  43  *
  44  * It's a stateful class due to the fact that FramesDecoder stores buffers inside.
  45  * Should be allocated only the single instance per connection.
  46  */
  47 public class FramesDecoder {
  48 
  49     static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
  50     static final System.Logger DEBUG_LOGGER =
  51             Utils.getDebugLogger("FramesDecoder"::toString, DEBUG);
  52 
  53     @FunctionalInterface
  54     public interface FrameProcessor {
  55         void processFrame(Http2Frame frame) throws IOException;
  56     }


 263         byte[] bytes = new byte[n];
 264         int offset = 0;
 265         while (n > 0) {
 266             int length = Math.min(n, currentBuffer.remaining());
 267             currentBuffer.get(bytes, offset, length);
 268             offset += length;
 269             n -= length;
 270             nextBuffer();
 271         }
 272         return bytes;
 273 
 274     }
 275 
 276     private List<ByteBuffer> getBuffers(boolean isDataFrame, int bytecount) {
 277         List<ByteBuffer> res = new ArrayList<>();
 278         while (bytecount > 0) {
 279             int remaining = currentBuffer.remaining();
 280             int extract = Math.min(remaining, bytecount);
 281             ByteBuffer extractedBuf;
 282             if (isDataFrame) {
 283                 extractedBuf = Utils.sliceWithLimitedCapacity(currentBuffer, extract)
 284                                     .asReadOnlyBuffer();
 285                 slicedToDataFrame = true;
 286             } else {
 287                 // Header frames here
 288                 // HPACK decoding should performed under lock and immediately after frame decoding.
 289                 // in that case it is safe to release original buffer,
 290                 // because of sliced buffer has a very short life
 291                 extractedBuf = Utils.sliceWithLimitedCapacity(currentBuffer, extract);
 292             }
 293             res.add(extractedBuf);
 294             bytecount -= extract;
 295             nextBuffer();
 296         }
 297         return res;
 298     }
 299 
 300     public void close(String msg) {
 301         closed = true;
 302         tailBuffers.clear();
 303         int bytes = tailSize;
 304         ByteBuffer b = currentBuffer;
 305         if (b != null) {
 306             bytes += b.remaining();
 307             b.position(b.limit());
 308         }
 309         tailSize = 0;
 310         currentBuffer = null;
 311         DEBUG_LOGGER.log(Level.DEBUG, "closed %s, ignoring %d bytes", msg, bytes);


 502             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 503                     "PingFrame length is "+ frameLength+", expected 8");
 504         }
 505         return new PingFrame(flags, getBytes(8));
 506     }
 507 
 508     private Http2Frame parseGoAwayFrame(int frameLength, int streamid, int flags) {
 509         // only zero stream; no flags
 510         if (streamid != 0) {
 511             return new MalformedFrame(ErrorFrame.PROTOCOL_ERROR,
 512                     "non-zero streamId for GoAwayFrame");
 513         }
 514         if (frameLength < 8) {
 515             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 516                     "Invalid GoAway frame size");
 517         }
 518         int lastStream = getInt() & 0x7fffffff;
 519         int errorCode = getInt();
 520         byte[] debugData = getBytes(frameLength - 8);
 521         if (debugData.length > 0) {
 522             Log.logError("GoAway debugData " + new String(debugData, UTF_8));
 523         }
 524         return new GoAwayFrame(lastStream, errorCode, debugData);
 525     }
 526 
 527     private Http2Frame parseWindowUpdateFrame(int frameLength, int streamid, int flags) {
 528         // any stream; no flags
 529         if(frameLength != 4) {
 530             return new MalformedFrame(ErrorFrame.FRAME_SIZE_ERROR,
 531                     "WindowUpdateFrame length is "+ frameLength+", expected 4");
 532         }
 533         return new WindowUpdateFrame(streamid, getInt() & 0x7fffffff);
 534     }
 535 
 536     private Http2Frame parseContinuationFrame(int frameLength, int streamid, int flags) {
 537         // non-zero stream;
 538         if (streamid == 0) {
 539             return new MalformedFrame(ErrorFrame.PROTOCOL_ERROR,
 540                     "zero streamId for ContinuationFrame");
 541         }
 542         return new ContinuationFrame(streamid, flags, getBuffers(false, frameLength));
< prev index next >