View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http2.hpack;
29  
30  import org.apache.hc.core5.http.Header;
31  import org.apache.hc.core5.util.Args;
32  import org.apache.hc.core5.util.Asserts;
33  
34  final class InboundDynamicTable {
35  
36      private final StaticTable staticTable;
37      private final FifoBuffer headers;
38  
39      private int maxSize;
40      private int currentSize;
41  
42      InboundDynamicTable(final StaticTable staticTable) {
43          this.staticTable = staticTable;
44          this.headers = new FifoBuffer(256);
45          this.maxSize = Integer.MAX_VALUE;
46          this.currentSize = 0;
47      }
48  
49      InboundDynamicTable() {
50          this(StaticTable.INSTANCE);
51      }
52  
53      public int getMaxSize() {
54          return maxSize;
55      }
56  
57      public void setMaxSize(final int maxSize) {
58          this.maxSize = maxSize;
59          evict();
60      }
61  
62      public int getCurrentSize() {
63          return currentSize;
64      }
65  
66      int staticLength() {
67          return staticTable.length();
68      }
69  
70      int dynamicLength() {
71          return headers.size();
72      }
73  
74      Header getDynamicEntry(final int index) {
75          return headers.get(index);
76      }
77  
78      public int length() {
79          return staticTable.length() + headers.size();
80      }
81  
82      public HPackHeader getHeader(final int index) {
83          final int length = length();
84          Args.check(index >= 1, "index %s cannot be less than 1", index);
85          Args.check(index <= length, "length %s cannot be greater than index %s", length, index);
86          return index <= staticTable.length()
87                          ? staticTable.get(index)
88                          : headers.get(index - staticTable.length() - 1);
89      }
90  
91      public void add(final HPackHeader header) {
92          final int entrySize = header.getTotalSize();
93          if (entrySize > this.maxSize) {
94              clear();
95              return;
96          }
97          headers.addFirst(header);
98          currentSize += entrySize;
99          evict();
100     }
101 
102     private void clear() {
103         currentSize = 0;
104         headers.clear();
105     }
106 
107     private void evict() {
108         while (currentSize > maxSize) {
109             final HPackHeader header = headers.removeLast();
110             if (header != null) {
111                 currentSize -= header.getTotalSize();
112             } else {
113                 Asserts.check(currentSize == 0, "Current table size must be zero");
114                 break;
115             }
116         }
117     }
118 
119 }