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  * questions.
  24  */
  25 package jdk.incubator.http.internal.hpack;
  26 
  27 import java.nio.ByteBuffer;
  28 
  29 /**
  30  * Delivers results of the {@link Decoder#decode(ByteBuffer, boolean,
  31  * DecodingCallback) decoding operation}.
  32  *
  33  * <p> Methods of the callback are never called by a decoder with any of the
  34  * arguments being {@code null}.
  35  *
  36  * @apiNote
  37  *
  38  * <p> The callback provides methods for all possible <a
  39  * href="https://tools.ietf.org/html/rfc7541#section-6">binary
  40  * representations</a>. This could be useful for implementing an intermediary,
  41  * logging, debugging, etc.
  42  *
  43  * <p> The callback is an interface in order to interoperate with lambdas (in
  44  * the most common use case):
  45  * <pre>{@code
  46  *     DecodingCallback callback = (name, value) -> System.out.println(name + ", " + value);
  47  * }</pre>
  48  *
  49  * <p> Names and values are {@link CharSequence}s rather than {@link String}s in
  50  * order to allow users to decide whether or not they need to create objects. A
  51  * {@code CharSequence} might be used in-place, for example, to be appended to
  52  * an {@link Appendable} (e.g. {@link StringBuilder}) and then discarded.
  53  *
  54  * <p> That said, if a passed {@code CharSequence} needs to outlast the method
  55  * call, it needs to be copied.
  56  *
  57  * @since 9
  58  */
  59 @FunctionalInterface
  60 public interface DecodingCallback {
  61 
  62     /**
  63      * A method the more specific methods of the callback forward their calls
  64      * to.
  65      *
  66      * @param name
  67      *         header name
  68      * @param value
  69      *         header value
  70      */
  71     void onDecoded(CharSequence name, CharSequence value);
  72 
  73     /**
  74      * A more finer-grained version of {@link #onDecoded(CharSequence,
  75      * CharSequence)} that also reports on value sensitivity.
  76      *
  77      * <p> Value sensitivity must be considered, for example, when implementing
  78      * an intermediary. A {@code value} is sensitive if it was represented as <a
  79      * href="https://tools.ietf.org/html/rfc7541#section-6.2.3">Literal Header
  80      * Field Never Indexed</a>.
  81      *
  82      * <p> It is required that intermediaries MUST use the {@linkplain
  83      * Encoder#header(CharSequence, CharSequence, boolean) same representation}
  84      * for encoding this header field in order to protect its value which is not
  85      * to be put at risk by compressing it.
  86      *
  87      * @implSpec
  88      *
  89      * <p> The default implementation invokes {@code onDecoded(name, value)}.
  90      *
  91      * @param name
  92      *         header name
  93      * @param value
  94      *         header value
  95      * @param sensitive
  96      *         whether or not the value is sensitive
  97      *
  98      * @see #onLiteralNeverIndexed(int, CharSequence, CharSequence, boolean)
  99      * @see #onLiteralNeverIndexed(CharSequence, boolean, CharSequence, boolean)
 100      */
 101     default void onDecoded(CharSequence name, CharSequence value,
 102                            boolean sensitive) {
 103         onDecoded(name, value);
 104     }
 105 
 106     /**
 107      * An <a href="https://tools.ietf.org/html/rfc7541#section-6.1">Indexed
 108      * Header Field</a> decoded.
 109      *
 110      * @implSpec
 111      *
 112      * <p> The default implementation invokes
 113      * {@code onDecoded(name, value, false)}.
 114      *
 115      * @param index
 116      *         index of an entry in the table
 117      * @param name
 118      *         header name
 119      * @param value
 120      *         header value
 121      */
 122     default void onIndexed(int index, CharSequence name, CharSequence value) {
 123         onDecoded(name, value, false);
 124     }
 125 
 126     /**
 127      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.2">Literal
 128      * Header Field without Indexing</a> decoded, where a {@code name} was
 129      * referred by an {@code index}.
 130      *
 131      * @implSpec
 132      *
 133      * <p> The default implementation invokes
 134      * {@code onDecoded(name, value, false)}.
 135      *
 136      * @param index
 137      *         index of an entry in the table
 138      * @param name
 139      *         header name
 140      * @param value
 141      *         header value
 142      * @param valueHuffman
 143      *         if the {@code value} was Huffman encoded
 144      */
 145     default void onLiteral(int index, CharSequence name,
 146                            CharSequence value, boolean valueHuffman) {
 147         onDecoded(name, value, false);
 148     }
 149 
 150     /**
 151      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.2">Literal
 152      * Header Field without Indexing</a> decoded, where both a {@code name} and
 153      * a {@code value} were literal.
 154      *
 155      * @implSpec
 156      *
 157      * <p> The default implementation invokes
 158      * {@code onDecoded(name, value, false)}.
 159      *
 160      * @param name
 161      *         header name
 162      * @param nameHuffman
 163      *         if the {@code name} was Huffman encoded
 164      * @param value
 165      *         header value
 166      * @param valueHuffman
 167      *         if the {@code value} was Huffman encoded
 168      */
 169     default void onLiteral(CharSequence name, boolean nameHuffman,
 170                            CharSequence value, boolean valueHuffman) {
 171         onDecoded(name, value, false);
 172     }
 173 
 174     /**
 175      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.3">Literal
 176      * Header Field Never Indexed</a> decoded, where a {@code name}
 177      * was referred by an {@code index}.
 178      *
 179      * @implSpec
 180      *
 181      * <p> The default implementation invokes
 182      * {@code onDecoded(name, value, true)}.
 183      *
 184      * @param index
 185      *         index of an entry in the table
 186      * @param name
 187      *         header name
 188      * @param value
 189      *         header value
 190      * @param valueHuffman
 191      *         if the {@code value} was Huffman encoded
 192      */
 193     default void onLiteralNeverIndexed(int index, CharSequence name,
 194                                        CharSequence value,
 195                                        boolean valueHuffman) {
 196         onDecoded(name, value, true);
 197     }
 198 
 199     /**
 200      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.3">Literal
 201      * Header Field Never Indexed</a> decoded, where both a {@code
 202      * name} and a {@code value} were literal.
 203      *
 204      * @implSpec
 205      *
 206      * <p> The default implementation invokes
 207      * {@code onDecoded(name, value, true)}.
 208      *
 209      * @param name
 210      *         header name
 211      * @param nameHuffman
 212      *         if the {@code name} was Huffman encoded
 213      * @param value
 214      *         header value
 215      * @param valueHuffman
 216      *         if the {@code value} was Huffman encoded
 217      */
 218     default void onLiteralNeverIndexed(CharSequence name, boolean nameHuffman,
 219                                        CharSequence value, boolean valueHuffman) {
 220         onDecoded(name, value, true);
 221     }
 222 
 223     /**
 224      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.1">Literal
 225      * Header Field with Incremental Indexing</a> decoded, where a {@code name}
 226      * was referred by an {@code index}.
 227      *
 228      * @implSpec
 229      *
 230      * <p> The default implementation invokes
 231      * {@code onDecoded(name, value, false)}.
 232      *
 233      * @param index
 234      *         index of an entry in the table
 235      * @param name
 236      *         header name
 237      * @param value
 238      *         header value
 239      * @param valueHuffman
 240      *         if the {@code value} was Huffman encoded
 241      */
 242     default void onLiteralWithIndexing(int index,
 243                                        CharSequence name,
 244                                        CharSequence value, boolean valueHuffman) {
 245         onDecoded(name, value, false);
 246     }
 247 
 248     /**
 249      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.2.1">Literal
 250      * Header Field with Incremental Indexing</a> decoded, where both a {@code
 251      * name} and a {@code value} were literal.
 252      *
 253      * @implSpec
 254      *
 255      * <p> The default implementation invokes
 256      * {@code onDecoded(name, value, false)}.
 257      *
 258      * @param name
 259      *         header name
 260      * @param nameHuffman
 261      *         if the {@code name} was Huffman encoded
 262      * @param value
 263      *         header value
 264      * @param valueHuffman
 265      *         if the {@code value} was Huffman encoded
 266      */
 267     default void onLiteralWithIndexing(CharSequence name, boolean nameHuffman,
 268                                        CharSequence value, boolean valueHuffman) {
 269         onDecoded(name, value, false);
 270     }
 271 
 272     /**
 273      * A <a href="https://tools.ietf.org/html/rfc7541#section-6.3">Dynamic Table
 274      * Size Update</a> decoded.
 275      *
 276      * @implSpec
 277      *
 278      * <p> The default implementation does nothing.
 279      *
 280      * @param capacity
 281      *         new capacity of the header table
 282      */
 283     default void onSizeUpdate(int capacity) { }
 284 }