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 java.util.HashMap;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Map;
34  
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.util.Args;
37  import org.apache.hc.core5.util.Asserts;
38  
39  final class OutboundDynamicTable {
40  
41      private final StaticTable staticTable;
42      private final FifoLinkedList headers;
43      private final Map<String, LinkedList<HPackEntry>> mapByName;
44  
45      private int maxSize;
46      private int currentSize;
47  
48      OutboundDynamicTable(final StaticTable staticTable) {
49          this.staticTable = staticTable;
50          this.headers = new FifoLinkedList();
51          this.mapByName = new HashMap<>();
52          this.maxSize = Integer.MAX_VALUE;
53          this.currentSize = 0;
54      }
55  
56      OutboundDynamicTable() {
57          this(StaticTable.INSTANCE);
58      }
59  
60      public int getMaxSize() {
61          return maxSize;
62      }
63  
64      public void setMaxSize(final int maxSize) {
65          this.maxSize = maxSize;
66          evict();
67      }
68  
69      public int getCurrentSize() {
70          return currentSize;
71      }
72  
73      int staticLength() {
74          return staticTable.length();
75      }
76  
77      int dynamicLength() {
78          return headers.size();
79      }
80  
81      Header getDynamicEntry(final int index) {
82          return headers.get(index);
83      }
84  
85      public int length() {
86          return staticTable.length() + headers.size();
87      }
88  
89      public Header getHeader(final int index) {
90          final int length = length();
91          Args.check(index >= 1, "index %s cannot be less then 1", index);
92          Args.check(index <= length, "index %s cannot be greater then length %s ", index, length);
93          return index <= staticTable.length()
94                          ? staticTable.get(index)
95                          : headers.get(index - staticTable.length() - 1);
96      }
97  
98      public void add(final HPackHeader header) {
99          final int entrySize = header.getTotalSize();
100         if (entrySize > this.maxSize) {
101             clear();
102             this.mapByName.clear();
103             return;
104         }
105         final String key = header.getName();
106         final FifoLinkedList.InternalNode node = headers.addFirst(header);
107         final LinkedList<HPackEntry> nodes = mapByName.computeIfAbsent(key, k -> new LinkedList<>());
108         nodes.addFirst(node);
109         currentSize += entrySize;
110         evict();
111     }
112 
113     private void clear() {
114         currentSize = 0;
115         headers.clear();
116     }
117 
118     public List<HPackEntry> getByName(final String key) {
119         return this.mapByName.get(key);
120     }
121 
122     private void evict() {
123         while (currentSize > maxSize) {
124             final FifoLinkedList.InternalNode node = headers.removeLast();
125             if (node != null) {
126                 final HPackHeader header = node.getHeader();
127                 currentSize -= header.getTotalSize();
128 
129                 final String key = header.getName();
130                 final LinkedList<HPackEntry> nodes = mapByName.get(key);
131                 if (nodes != null) {
132                     nodes.remove(node);
133                 }
134             } else {
135                 Asserts.check(currentSize == 0, "Current table size must be zero");
136                 break;
137             }
138         }
139     }
140 
141 }