aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/transport/chttp2/hpack_table.c
blob: 4fc154380e1a5fa9077307f9b9feef55d64d8fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "src/core/transport/chttp2/hpack_table.h"

#include <assert.h>
#include <string.h>

#include <grpc/support/log.h>
#include "src/core/support/murmur_hash.h"

static struct {
  const char *key;
  const char *value;
} static_table[] = {
    /* 0: */ {NULL, NULL},
    /* 1: */ {":authority", ""},
    /* 2: */ {":method", "GET"},
    /* 3: */ {":method", "POST"},
    /* 4: */ {":path", "/"},
    /* 5: */ {":path", "/index.html"},
    /* 6: */ {":scheme", "http"},
    /* 7: */ {":scheme", "https"},
    /* 8: */ {":status", "200"},
    /* 9: */ {":status", "204"},
    /* 10: */ {":status", "206"},
    /* 11: */ {":status", "304"},
    /* 12: */ {":status", "400"},
    /* 13: */ {":status", "404"},
    /* 14: */ {":status", "500"},
    /* 15: */ {"accept-charset", ""},
    /* 16: */ {"accept-encoding", "gzip, deflate"},
    /* 17: */ {"accept-language", ""},
    /* 18: */ {"accept-ranges", ""},
    /* 19: */ {"accept", ""},
    /* 20: */ {"access-control-allow-origin", ""},
    /* 21: */ {"age", ""},
    /* 22: */ {"allow", ""},
    /* 23: */ {"authorization", ""},
    /* 24: */ {"cache-control", ""},
    /* 25: */ {"content-disposition", ""},
    /* 26: */ {"content-encoding", ""},
    /* 27: */ {"content-language", ""},
    /* 28: */ {"content-length", ""},
    /* 29: */ {"content-location", ""},
    /* 30: */ {"content-range", ""},
    /* 31: */ {"content-type", ""},
    /* 32: */ {"cookie", ""},
    /* 33: */ {"date", ""},
    /* 34: */ {"etag", ""},
    /* 35: */ {"expect", ""},
    /* 36: */ {"expires", ""},
    /* 37: */ {"from", ""},
    /* 38: */ {"host", ""},
    /* 39: */ {"if-match", ""},
    /* 40: */ {"if-modified-since", ""},
    /* 41: */ {"if-none-match", ""},
    /* 42: */ {"if-range", ""},
    /* 43: */ {"if-unmodified-since", ""},
    /* 44: */ {"last-modified", ""},
    /* 45: */ {"link", ""},
    /* 46: */ {"location", ""},
    /* 47: */ {"max-forwards", ""},
    /* 48: */ {"proxy-authenticate", ""},
    /* 49: */ {"proxy-authorization", ""},
    /* 50: */ {"range", ""},
    /* 51: */ {"referer", ""},
    /* 52: */ {"refresh", ""},
    /* 53: */ {"retry-after", ""},
    /* 54: */ {"server", ""},
    /* 55: */ {"set-cookie", ""},
    /* 56: */ {"strict-transport-security", ""},
    /* 57: */ {"transfer-encoding", ""},
    /* 58: */ {"user-agent", ""},
    /* 59: */ {"vary", ""},
    /* 60: */ {"via", ""},
    /* 61: */ {"www-authenticate", ""},
};

void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl *tbl, grpc_mdctx *mdctx) {
  size_t i;

  memset(tbl, 0, sizeof(*tbl));
  tbl->mdctx = mdctx;
  tbl->max_bytes = GRPC_CHTTP2_INITIAL_HPACK_TABLE_SIZE;
  for (i = 1; i <= GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
    tbl->static_ents[i - 1] = grpc_mdelem_from_strings(
        mdctx, static_table[i].key, static_table[i].value);
  }
}

void grpc_chttp2_hptbl_destroy(grpc_chttp2_hptbl *tbl) {
  size_t i;
  for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
    GRPC_MDELEM_UNREF(tbl->static_ents[i]);
  }
  for (i = 0; i < tbl->num_ents; i++) {
    GRPC_MDELEM_UNREF(
        tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT]);
  }
}

grpc_mdelem *grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl *tbl,
                                      gpr_uint32 index) {
  /* Static table comes first, just return an entry from it */
  if (index <= GRPC_CHTTP2_LAST_STATIC_ENTRY) {
    return tbl->static_ents[index - 1];
  }
  /* Otherwise, find the value in the list of valid entries */
  index -= (GRPC_CHTTP2_LAST_STATIC_ENTRY + 1);
  if (index < tbl->num_ents) {
    gpr_uint32 offset = (tbl->num_ents - 1 - index + tbl->first_ent) %
                        GRPC_CHTTP2_MAX_TABLE_COUNT;
    return tbl->ents[offset];
  }
  /* Invalid entry: return error */
  return NULL;
}

/* Evict one element from the table */
static void evict1(grpc_chttp2_hptbl *tbl) {
  grpc_mdelem *first_ent = tbl->ents[tbl->first_ent];
  tbl->mem_used -= GPR_SLICE_LENGTH(first_ent->key->slice) +
                   GPR_SLICE_LENGTH(first_ent->value->slice) +
                   GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
  tbl->first_ent = (tbl->first_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT;
  tbl->num_ents--;
  GRPC_MDELEM_UNREF(first_ent);
}

void grpc_chttp2_hptbl_add(grpc_chttp2_hptbl *tbl, grpc_mdelem *md) {
  /* determine how many bytes of buffer this entry represents */
  gpr_uint16 elem_bytes = GPR_SLICE_LENGTH(md->key->slice) +
                          GPR_SLICE_LENGTH(md->value->slice) +
                          GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;

  /* we can't add elements bigger than the max table size */
  if (elem_bytes > tbl->max_bytes) {
    /* HPACK draft 10 section 4.4 states:
     * If the size of the new entry is less than or equal to the maximum
     * size, that entry is added to the table.  It is not an error to
     * attempt to add an entry that is larger than the maximum size; an
     * attempt to add an entry larger than the entire table causes
     * the table
     * to be emptied of all existing entries, and results in an
     * empty table.
     */
    while (tbl->num_ents) {
      evict1(tbl);
    }
    return;
  }

  /* evict entries to ensure no overflow */
  while (elem_bytes > tbl->max_bytes - tbl->mem_used) {
    evict1(tbl);
  }

  /* copy the finalized entry in */
  tbl->ents[tbl->last_ent] = md;

  /* update accounting values */
  tbl->last_ent = (tbl->last_ent + 1) % GRPC_CHTTP2_MAX_TABLE_COUNT;
  tbl->num_ents++;
  tbl->mem_used += elem_bytes;
}

grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find(
    const grpc_chttp2_hptbl *tbl, grpc_mdelem *md) {
  grpc_chttp2_hptbl_find_result r = {0, 0};
  int i;

  /* See if the string is in the static table */
  for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
    grpc_mdelem *ent = tbl->static_ents[i];
    if (md->key != ent->key) continue;
    r.index = i + 1;
    r.has_value = md->value == ent->value;
    if (r.has_value) return r;
  }

  /* Scan the dynamic table */
  for (i = 0; i < tbl->num_ents; i++) {
    int idx = tbl->num_ents - i + GRPC_CHTTP2_LAST_STATIC_ENTRY;
    grpc_mdelem *ent =
        tbl->ents[(tbl->first_ent + i) % GRPC_CHTTP2_MAX_TABLE_COUNT];
    if (md->key != ent->key) continue;
    r.index = idx;
    r.has_value = md->value == ent->value;
    if (r.has_value) return r;
  }

  return r;
}