1 /*
   2  * Copyright (c) 2015, 2016, 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  */
  24 
  25 package java.net.http;
  26 
  27 import java.io.IOException;
  28 import java.nio.ByteBuffer;
  29 
  30 class GoAwayFrame extends ErrorFrame {
  31 
  32     GoAwayFrame() {
  33         type = TYPE;
  34     }
  35 
  36     int lastStream;
  37     byte[] debugData = new byte[0];
  38 
  39     public static final int TYPE = 0x7;
  40 
  41     // Flags
  42     public static final int ACK = 0x1;
  43 
  44     public void setDebugData(byte[] debugData) {
  45         this.debugData = debugData;
  46     }
  47 
  48     @Override
  49     public String toString() {
  50         return super.toString() + " Debugdata: " + new String(debugData);
  51     }
  52 
  53     @Override
  54     String flagAsString(int flag) {
  55         switch (flag) {
  56         case ACK:
  57             return "ACK";
  58         }
  59         return super.flagAsString(flag);
  60     }
  61 
  62     public void setLastStream(int lastStream) {
  63         this.lastStream = lastStream;
  64     }
  65 
  66     public int getLastStream() {
  67         return this.lastStream;
  68     }
  69 
  70     public byte[] getDebugData() {
  71         return debugData;
  72     }
  73 
  74     @Override
  75     void readIncomingImpl(ByteBufferConsumer bc) throws IOException {
  76         if (length < 8) {
  77             throw new IOException("Invalid GoAway frame");
  78         }
  79         lastStream = bc.getInt() & 0x7fffffff;
  80         errorCode = bc.getInt();
  81         //debugData = bc.getBytes(8);
  82         int datalen = length - 8;
  83         if (datalen > 0) {
  84             debugData = bc.getBytes(datalen);
  85             Log.logError("GoAway debugData " + new String(debugData));
  86         }
  87     }
  88 
  89     @Override
  90     void writeOutgoing(ByteBufferGenerator bg) {
  91         super.writeOutgoing(bg);
  92         ByteBuffer buf = bg.getBuffer(length);
  93         buf.putInt(lastStream);
  94         buf.putInt(errorCode);
  95         if (length > 8) {
  96             buf.put(debugData);
  97         }
  98     }
  99 
 100     @Override
 101     void computeLength() {
 102         length = 8 + debugData.length;
 103     }
 104 }