2013-07-19 09:50:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-19 09:50:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "nghttp2_hd.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2013-12-16 15:54:20 +01:00
|
|
|
#include <stdio.h>
|
2013-07-19 09:50:31 +02:00
|
|
|
|
|
|
|
#include "nghttp2_helper.h"
|
2014-04-30 15:02:37 +02:00
|
|
|
#include "nghttp2_int.h"
|
2016-10-14 15:31:46 +02:00
|
|
|
#include "nghttp2_debug.h"
|
2013-07-19 09:50:31 +02:00
|
|
|
|
2015-04-15 15:32:11 +02:00
|
|
|
/* Make scalar initialization form of nghttp2_hd_entry */
|
2015-09-14 18:05:41 +02:00
|
|
|
#define MAKE_STATIC_ENT(N, V, T, H) \
|
2014-11-27 15:39:04 +01:00
|
|
|
{ \
|
2016-10-15 11:36:04 +02:00
|
|
|
{NULL, NULL, (uint8_t *)(N), sizeof((N)) - 1, -1}, \
|
|
|
|
{NULL, NULL, (uint8_t *)(V), sizeof((V)) - 1, -1}, \
|
2016-03-12 05:23:12 +01:00
|
|
|
{(uint8_t *)(N), (uint8_t *)(V), sizeof((N)) - 1, sizeof((V)) - 1, 0}, \
|
|
|
|
T, H \
|
2014-11-27 15:39:04 +01:00
|
|
|
}
|
2014-09-29 14:58:37 +02:00
|
|
|
|
2014-10-25 11:14:53 +02:00
|
|
|
/* Generated by mkstatictbl.py */
|
2015-04-15 15:32:11 +02:00
|
|
|
/* 3rd parameter is nghttp2_token value for header field name. We use
|
|
|
|
first enum value if same header names are repeated (e.g.,
|
|
|
|
:status). */
|
2016-03-12 05:23:12 +01:00
|
|
|
static nghttp2_hd_static_entry static_table[] = {
|
2015-09-14 18:05:41 +02:00
|
|
|
MAKE_STATIC_ENT(":authority", "", 0, 3153725150u),
|
|
|
|
MAKE_STATIC_ENT(":method", "GET", 1, 695666056u),
|
|
|
|
MAKE_STATIC_ENT(":method", "POST", 1, 695666056u),
|
|
|
|
MAKE_STATIC_ENT(":path", "/", 3, 3292848686u),
|
|
|
|
MAKE_STATIC_ENT(":path", "/index.html", 3, 3292848686u),
|
|
|
|
MAKE_STATIC_ENT(":scheme", "http", 5, 2510477674u),
|
|
|
|
MAKE_STATIC_ENT(":scheme", "https", 5, 2510477674u),
|
|
|
|
MAKE_STATIC_ENT(":status", "200", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "204", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "206", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "304", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "400", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "404", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT(":status", "500", 7, 4000288983u),
|
|
|
|
MAKE_STATIC_ENT("accept-charset", "", 14, 3664010344u),
|
|
|
|
MAKE_STATIC_ENT("accept-encoding", "gzip, deflate", 15, 3379649177u),
|
|
|
|
MAKE_STATIC_ENT("accept-language", "", 16, 1979086614u),
|
|
|
|
MAKE_STATIC_ENT("accept-ranges", "", 17, 1713753958u),
|
|
|
|
MAKE_STATIC_ENT("accept", "", 18, 136609321u),
|
|
|
|
MAKE_STATIC_ENT("access-control-allow-origin", "", 19, 2710797292u),
|
|
|
|
MAKE_STATIC_ENT("age", "", 20, 742476188u),
|
|
|
|
MAKE_STATIC_ENT("allow", "", 21, 2930878514u),
|
|
|
|
MAKE_STATIC_ENT("authorization", "", 22, 2436257726u),
|
|
|
|
MAKE_STATIC_ENT("cache-control", "", 23, 1355326669u),
|
|
|
|
MAKE_STATIC_ENT("content-disposition", "", 24, 3889184348u),
|
|
|
|
MAKE_STATIC_ENT("content-encoding", "", 25, 65203592u),
|
|
|
|
MAKE_STATIC_ENT("content-language", "", 26, 24973587u),
|
|
|
|
MAKE_STATIC_ENT("content-length", "", 27, 1308181789u),
|
|
|
|
MAKE_STATIC_ENT("content-location", "", 28, 2302364718u),
|
|
|
|
MAKE_STATIC_ENT("content-range", "", 29, 3555523146u),
|
|
|
|
MAKE_STATIC_ENT("content-type", "", 30, 4244048277u),
|
|
|
|
MAKE_STATIC_ENT("cookie", "", 31, 2007449791u),
|
|
|
|
MAKE_STATIC_ENT("date", "", 32, 3564297305u),
|
|
|
|
MAKE_STATIC_ENT("etag", "", 33, 113792960u),
|
|
|
|
MAKE_STATIC_ENT("expect", "", 34, 2530896728u),
|
|
|
|
MAKE_STATIC_ENT("expires", "", 35, 1049544579u),
|
|
|
|
MAKE_STATIC_ENT("from", "", 36, 2513272949u),
|
|
|
|
MAKE_STATIC_ENT("host", "", 37, 2952701295u),
|
|
|
|
MAKE_STATIC_ENT("if-match", "", 38, 3597694698u),
|
|
|
|
MAKE_STATIC_ENT("if-modified-since", "", 39, 2213050793u),
|
|
|
|
MAKE_STATIC_ENT("if-none-match", "", 40, 2536202615u),
|
|
|
|
MAKE_STATIC_ENT("if-range", "", 41, 2340978238u),
|
|
|
|
MAKE_STATIC_ENT("if-unmodified-since", "", 42, 3794814858u),
|
|
|
|
MAKE_STATIC_ENT("last-modified", "", 43, 3226950251u),
|
|
|
|
MAKE_STATIC_ENT("link", "", 44, 232457833u),
|
|
|
|
MAKE_STATIC_ENT("location", "", 45, 200649126u),
|
|
|
|
MAKE_STATIC_ENT("max-forwards", "", 46, 1826162134u),
|
|
|
|
MAKE_STATIC_ENT("proxy-authenticate", "", 47, 2709445359u),
|
|
|
|
MAKE_STATIC_ENT("proxy-authorization", "", 48, 2686392507u),
|
|
|
|
MAKE_STATIC_ENT("range", "", 49, 4208725202u),
|
|
|
|
MAKE_STATIC_ENT("referer", "", 50, 3969579366u),
|
|
|
|
MAKE_STATIC_ENT("refresh", "", 51, 3572655668u),
|
|
|
|
MAKE_STATIC_ENT("retry-after", "", 52, 3336180598u),
|
|
|
|
MAKE_STATIC_ENT("server", "", 53, 1085029842u),
|
|
|
|
MAKE_STATIC_ENT("set-cookie", "", 54, 1848371000u),
|
|
|
|
MAKE_STATIC_ENT("strict-transport-security", "", 55, 4138147361u),
|
|
|
|
MAKE_STATIC_ENT("transfer-encoding", "", 56, 3719590988u),
|
|
|
|
MAKE_STATIC_ENT("user-agent", "", 57, 606444526u),
|
|
|
|
MAKE_STATIC_ENT("vary", "", 58, 1085005381u),
|
|
|
|
MAKE_STATIC_ENT("via", "", 59, 1762798611u),
|
|
|
|
MAKE_STATIC_ENT("www-authenticate", "", 60, 779865858u),
|
2014-09-29 14:58:37 +02:00
|
|
|
};
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int memeq(const void *s1, const void *s2, size_t n) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return memcmp(s1, s2, n) == 0;
|
2014-01-07 13:51:48 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:32:11 +02:00
|
|
|
/*
|
|
|
|
* This function was generated by genlibtokenlookup.py. Inspired by
|
|
|
|
* h2o header lookup. https://github.com/h2o/h2o
|
|
|
|
*/
|
2016-03-12 05:23:12 +01:00
|
|
|
static int32_t lookup_token(const uint8_t *name, size_t namelen) {
|
2015-04-15 15:32:11 +02:00
|
|
|
switch (namelen) {
|
|
|
|
case 2:
|
|
|
|
switch (name[1]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("t", name, 1)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_TE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
switch (name[2]) {
|
|
|
|
case 'a':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("vi", name, 2)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_VIA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("ag", name, 2)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_AGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
switch (name[3]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("dat", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_DATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("eta", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ETAG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'k':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("lin", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_LINK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("fro", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_FROM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("hos", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_HOST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'y':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("var", name, 3)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_VARY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
switch (name[4]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("rang", name, 4)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_RANGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq(":pat", name, 4)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN__PATH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'w':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("allo", name, 4)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ALLOW;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
switch (name[5]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("cooki", name, 5)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_COOKIE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("serve", name, 5)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_SERVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("accep", name, 5)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCEPT;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("expec", name, 5)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_EXPECT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
switch (name[6]) {
|
|
|
|
case 'd':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq(":metho", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN__METHOD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq(":schem", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN__SCHEME;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("upgrad", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_UPGRADE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("refres", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_REFRESH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("refere", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_REFERER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq(":statu", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN__STATUS;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("expire", name, 6)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_EXPIRES;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
switch (name[7]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("if-rang", name, 7)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_IF_RANGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("if-matc", name, 7)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_IF_MATCH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("locatio", name, 7)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_LOCATION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
switch (name[9]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("keep-aliv", name, 9)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_KEEP_ALIVE;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("set-cooki", name, 9)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_SET_COOKIE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("connectio", name, 9)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONNECTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("user-agen", name, 9)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_USER_AGENT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'y':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq(":authorit", name, 9)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN__AUTHORITY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
switch (name[10]) {
|
|
|
|
case 'r':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("retry-afte", name, 10)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_RETRY_AFTER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
switch (name[11]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-typ", name, 11)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_TYPE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("max-forward", name, 11)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_MAX_FORWARDS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
switch (name[12]) {
|
|
|
|
case 'd':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("last-modifie", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_LAST_MODIFIED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-rang", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_RANGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("if-none-matc", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_IF_NONE_MATCH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("cache-contro", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CACHE_CONTROL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("authorizatio", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_AUTHORIZATION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("accept-range", name, 12)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCEPT_RANGES;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
switch (name[13]) {
|
|
|
|
case 'h':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-lengt", name, 13)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("accept-charse", name, 13)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCEPT_CHARSET;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
switch (name[14]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("accept-languag", name, 14)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCEPT_LANGUAGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("accept-encodin", name, 14)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCEPT_ENCODING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
switch (name[15]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-languag", name, 15)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_LANGUAGE;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("www-authenticat", name, 15)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_WWW_AUTHENTICATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-encodin", name, 15)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-locatio", name, 15)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_LOCATION;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("proxy-connectio", name, 15)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_PROXY_CONNECTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
switch (name[16]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("if-modified-sinc", name, 16)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_IF_MODIFIED_SINCE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("transfer-encodin", name, 16)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_TRANSFER_ENCODING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
switch (name[17]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("proxy-authenticat", name, 17)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_PROXY_AUTHENTICATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 19:
|
|
|
|
switch (name[18]) {
|
|
|
|
case 'e':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("if-unmodified-sinc", name, 18)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("content-dispositio", name, 18)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_CONTENT_DISPOSITION;
|
|
|
|
}
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("proxy-authorizatio", name, 18)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_PROXY_AUTHORIZATION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 25:
|
|
|
|
switch (name[24]) {
|
|
|
|
case 'y':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("strict-transport-securit", name, 24)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
switch (name[26]) {
|
|
|
|
case 'n':
|
2016-08-09 06:15:38 +02:00
|
|
|
if (memeq("access-control-allow-origi", name, 26)) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-07 14:43:34 +01:00
|
|
|
}
|
2015-04-15 15:32:11 +02:00
|
|
|
return -1;
|
2014-01-07 14:43:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
void nghttp2_hd_entry_init(nghttp2_hd_entry *ent, nghttp2_hd_nv *nv) {
|
|
|
|
ent->nv = *nv;
|
|
|
|
ent->cnv.name = nv->name->base;
|
|
|
|
ent->cnv.namelen = nv->name->len;
|
|
|
|
ent->cnv.value = nv->value->base;
|
|
|
|
ent->cnv.valuelen = nv->value->len;
|
|
|
|
ent->cnv.flags = nv->flags;
|
2015-09-14 18:05:41 +02:00
|
|
|
ent->next = NULL;
|
|
|
|
ent->hash = 0;
|
2014-08-06 15:00:12 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_rcbuf_incref(ent->nv.name);
|
|
|
|
nghttp2_rcbuf_incref(ent->nv.value);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
void nghttp2_hd_entry_free(nghttp2_hd_entry *ent) {
|
|
|
|
nghttp2_rcbuf_decref(ent->nv.value);
|
|
|
|
nghttp2_rcbuf_decref(ent->nv.name);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
static int name_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
|
|
|
|
return a->name->len == b->namelen &&
|
|
|
|
memeq(a->name->base, b->name, b->namelen);
|
2015-09-14 18:05:41 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
static int value_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
|
|
|
|
return a->value->len == b->valuelen &&
|
|
|
|
memeq(a->value->base, b->value, b->valuelen);
|
2015-09-14 18:05:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t name_hash(const nghttp2_nv *nv) {
|
|
|
|
/* 32 bit FNV-1a: http://isthe.com/chongo/tech/comp/fnv/ */
|
2015-09-24 17:03:38 +02:00
|
|
|
uint32_t h = 2166136261u;
|
2015-09-14 18:05:41 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < nv->namelen; ++i) {
|
|
|
|
h ^= nv->name[i];
|
|
|
|
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hd_map_init(nghttp2_hd_map *map) {
|
|
|
|
memset(map, 0, sizeof(nghttp2_hd_map));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hd_map_insert(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
|
|
|
|
nghttp2_hd_entry **bucket;
|
|
|
|
|
|
|
|
bucket = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
|
|
|
|
|
|
|
|
if (*bucket == NULL) {
|
|
|
|
*bucket = ent;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lower index is linked near the root */
|
|
|
|
ent->next = *bucket;
|
|
|
|
*bucket = ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nghttp2_hd_entry *hd_map_find(nghttp2_hd_map *map, int *exact_match,
|
2016-03-12 05:23:12 +01:00
|
|
|
const nghttp2_nv *nv, int32_t token,
|
2016-08-07 17:27:16 +02:00
|
|
|
uint32_t hash, int name_only) {
|
2015-09-14 18:05:41 +02:00
|
|
|
nghttp2_hd_entry *p;
|
|
|
|
nghttp2_hd_entry *res = NULL;
|
|
|
|
|
|
|
|
*exact_match = 0;
|
|
|
|
|
|
|
|
for (p = map->table[hash & (HD_MAP_SIZE - 1)]; p; p = p->next) {
|
2016-03-12 05:23:12 +01:00
|
|
|
if (token != p->nv.token ||
|
2016-02-19 16:48:18 +01:00
|
|
|
(token == -1 && (hash != p->hash || !name_eq(&p->nv, nv)))) {
|
2015-09-14 18:05:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!res) {
|
|
|
|
res = p;
|
2016-08-07 17:27:16 +02:00
|
|
|
if (name_only) {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-14 18:05:41 +02:00
|
|
|
}
|
|
|
|
if (value_eq(&p->nv, nv)) {
|
|
|
|
res = p;
|
|
|
|
*exact_match = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hd_map_remove(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
|
2016-05-14 04:34:51 +02:00
|
|
|
nghttp2_hd_entry **dst;
|
2015-09-14 18:05:41 +02:00
|
|
|
|
2016-05-14 04:34:51 +02:00
|
|
|
dst = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
|
2015-09-14 18:05:41 +02:00
|
|
|
|
2016-05-14 04:34:51 +02:00
|
|
|
for (; *dst; dst = &(*dst)->next) {
|
|
|
|
if (*dst != ent) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-14 18:05:41 +02:00
|
|
|
|
2016-05-14 04:34:51 +02:00
|
|
|
*dst = ent->next;
|
2015-09-14 18:05:41 +02:00
|
|
|
ent->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
|
|
|
|
nghttp2_mem *mem) {
|
2013-10-12 14:49:01 +02:00
|
|
|
size_t size;
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size = 1; size < bufsize; size <<= 1)
|
|
|
|
;
|
2014-12-07 15:07:13 +01:00
|
|
|
ringbuf->buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ringbuf->buffer == NULL) {
|
2013-10-12 14:49:01 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
ringbuf->mask = size - 1;
|
|
|
|
ringbuf->first = 0;
|
|
|
|
ringbuf->len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static nghttp2_hd_entry *hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
|
|
|
|
size_t idx) {
|
2014-05-27 22:28:12 +02:00
|
|
|
assert(idx < ringbuf->len);
|
|
|
|
return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
|
|
|
|
nghttp2_mem *mem) {
|
2013-10-25 18:01:28 +02:00
|
|
|
size_t i;
|
|
|
|
size_t size;
|
|
|
|
nghttp2_hd_entry **buffer;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ringbuf->mask + 1 >= bufsize) {
|
2013-10-25 18:01:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size = 1; size < bufsize; size <<= 1)
|
|
|
|
;
|
2014-12-07 15:07:13 +01:00
|
|
|
buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (buffer == NULL) {
|
2013-10-25 18:01:28 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < ringbuf->len; ++i) {
|
2014-05-08 16:54:07 +02:00
|
|
|
buffer[i] = hd_ringbuf_get(ringbuf, i);
|
2013-10-25 18:01:28 +02:00
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, ringbuf->buffer);
|
2013-10-25 18:01:28 +02:00
|
|
|
ringbuf->buffer = buffer;
|
|
|
|
ringbuf->mask = size - 1;
|
|
|
|
ringbuf->first = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf, nghttp2_mem *mem) {
|
2013-10-12 14:49:01 +02:00
|
|
|
size_t i;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ringbuf == NULL) {
|
2013-10-12 14:49:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < ringbuf->len; ++i) {
|
2014-05-08 16:54:07 +02:00
|
|
|
nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
nghttp2_hd_entry_free(ent);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, ent);
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, ringbuf->buffer);
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 06:39:17 +02:00
|
|
|
static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_hd_entry *ent, nghttp2_mem *mem) {
|
2014-06-22 06:39:17 +02:00
|
|
|
int rv;
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem);
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-06-22 06:39:17 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
|
|
|
|
++ringbuf->len;
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2013-10-12 14:49:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) {
|
2013-10-12 14:49:01 +02:00
|
|
|
assert(ringbuf->len > 0);
|
|
|
|
--ringbuf->len;
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
static int hd_context_init(nghttp2_hd_context *context, nghttp2_mem *mem) {
|
2013-10-12 14:49:01 +02:00
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
context->mem = mem;
|
2013-07-25 18:34:28 +02:00
|
|
|
context->bad = 0;
|
2013-10-25 18:01:28 +02:00
|
|
|
context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = hd_ringbuf_init(&context->hd_table, context->hd_table_bufsize_max /
|
2014-12-07 15:07:13 +01:00
|
|
|
NGHTTP2_HD_ENTRY_OVERHEAD,
|
|
|
|
mem);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-10-12 14:49:01 +02:00
|
|
|
return rv;
|
2013-08-22 18:28:01 +02:00
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
|
2013-07-19 18:27:26 +02:00
|
|
|
context->hd_table_bufsize = 0;
|
2015-09-14 18:05:41 +02:00
|
|
|
context->next_seq = 0;
|
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void hd_context_free(nghttp2_hd_context *context) {
|
2014-12-07 15:07:13 +01:00
|
|
|
hd_ringbuf_free(&context->hd_table, context->mem);
|
2014-04-30 04:32:05 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) {
|
|
|
|
return nghttp2_hd_deflate_init2(
|
|
|
|
deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem);
|
2013-10-13 12:24:21 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
|
2016-09-11 15:25:01 +02:00
|
|
|
size_t max_deflate_dynamic_table_size,
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem) {
|
2014-01-26 09:53:04 +01:00
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = hd_context_init(&deflater->ctx, mem);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-26 09:53:04 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-05-11 14:25:27 +02:00
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
hd_map_init(&deflater->map);
|
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
if (max_deflate_dynamic_table_size < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
|
2014-05-11 14:25:27 +02:00
|
|
|
deflater->notify_table_size_change = 1;
|
2016-09-11 15:25:01 +02:00
|
|
|
deflater->ctx.hd_table_bufsize_max = max_deflate_dynamic_table_size;
|
2014-05-11 14:25:27 +02:00
|
|
|
} else {
|
|
|
|
deflater->notify_table_size_change = 0;
|
|
|
|
}
|
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
deflater->deflate_hd_table_bufsize_max = max_deflate_dynamic_table_size;
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max = UINT32_MAX;
|
2014-05-11 14:25:27 +02:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
return 0;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem) {
|
2014-01-25 10:24:15 +01:00
|
|
|
int rv;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = hd_context_init(&inflater->ctx, mem);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-04-30 04:32:05 +02:00
|
|
|
goto fail;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
inflater->settings_hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
2015-10-24 10:27:24 +02:00
|
|
|
inflater->min_hd_table_bufsize_max = UINT32_MAX;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
inflater->nv_name_keep = NULL;
|
|
|
|
inflater->nv_value_keep = NULL;
|
2014-01-26 09:53:04 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
|
2015-07-21 17:11:23 +02:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
nghttp2_buf_init(&inflater->namebuf);
|
|
|
|
nghttp2_buf_init(&inflater->valuebuf);
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->namercbuf = NULL;
|
|
|
|
inflater->valuercbuf = NULL;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded = 0;
|
|
|
|
inflater->index = 0;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->index_required = 0;
|
2014-04-01 18:25:44 +02:00
|
|
|
inflater->no_index = 0;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
2014-04-30 04:32:05 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
fail:
|
2014-04-30 04:32:05 +02:00
|
|
|
return rv;
|
2013-08-22 20:45:26 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) {
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_rcbuf_decref(inflater->nv_value_keep);
|
|
|
|
nghttp2_rcbuf_decref(inflater->nv_name_keep);
|
2014-12-07 15:07:13 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
inflater->nv_value_keep = NULL;
|
|
|
|
inflater->nv_name_keep = NULL;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2013-10-14 16:38:12 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater) {
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_context_free(&deflater->ctx);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater) {
|
2016-03-12 05:23:12 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_rcbuf_decref(inflater->valuercbuf);
|
|
|
|
nghttp2_rcbuf_decref(inflater->namercbuf);
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_context_free(&inflater->ctx);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t entry_room(size_t namelen, size_t valuelen) {
|
2013-07-19 09:50:31 +02:00
|
|
|
return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
|
|
|
|
}
|
|
|
|
|
2016-08-23 15:40:14 +02:00
|
|
|
static void emit_header(nghttp2_hd_nv *nv_out, nghttp2_hd_nv *nv) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: header emission: %s: %s\n", nv->name->base,
|
|
|
|
nv->value->base);
|
2014-07-12 11:47:55 +02:00
|
|
|
/* ent->ref may be 0. This happens if the encoder emits literal
|
|
|
|
block larger than header table capacity with indexing. */
|
2014-01-16 15:41:13 +01:00
|
|
|
*nv_out = *nv;
|
2013-08-23 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t count_encoded_length(size_t n, size_t prefix) {
|
2015-09-23 11:07:38 +02:00
|
|
|
size_t k = (size_t)((1 << prefix) - 1);
|
2013-07-19 09:50:31 +02:00
|
|
|
size_t len = 0;
|
2014-11-20 17:59:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (n < k) {
|
2013-10-18 12:27:15 +02:00
|
|
|
return 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-11-20 17:59:20 +01:00
|
|
|
|
|
|
|
n -= k;
|
|
|
|
++len;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; n >= 128; n >>= 7, ++len)
|
|
|
|
;
|
2014-11-20 17:59:20 +01:00
|
|
|
|
|
|
|
return len + 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t encode_length(uint8_t *buf, size_t n, size_t prefix) {
|
2015-09-23 11:07:38 +02:00
|
|
|
size_t k = (size_t)((1 << prefix) - 1);
|
2014-11-20 17:59:20 +01:00
|
|
|
uint8_t *begin = buf;
|
2014-07-26 11:19:25 +02:00
|
|
|
|
2015-09-23 11:07:38 +02:00
|
|
|
*buf = (uint8_t)(*buf & ~k);
|
2014-07-26 11:19:25 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (n < k) {
|
2015-09-23 11:07:38 +02:00
|
|
|
*buf = (uint8_t)(*buf | n);
|
2013-10-18 12:27:15 +02:00
|
|
|
return 1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-26 11:19:25 +02:00
|
|
|
|
2015-09-23 11:07:38 +02:00
|
|
|
*buf = (uint8_t)(*buf | k);
|
|
|
|
++buf;
|
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
n -= k;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; n >= 128; n >>= 7) {
|
2015-09-23 11:07:38 +02:00
|
|
|
*buf++ = (uint8_t)((1 << 7) | (n & 0x7f));
|
2014-11-20 17:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*buf++ = (uint8_t)n;
|
|
|
|
|
|
|
|
return (size_t)(buf - begin);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-08-03 07:07:35 +02:00
|
|
|
* Decodes |prefix| prefixed integer stored from |in|. The |last|
|
2013-07-19 09:50:31 +02:00
|
|
|
* represents the 1 beyond the last of the valid contiguous memory
|
2014-07-22 15:38:18 +02:00
|
|
|
* region from |in|. The decoded integer must be less than or equal
|
|
|
|
* to UINT32_MAX.
|
2013-07-19 09:50:31 +02:00
|
|
|
*
|
2014-01-25 10:24:15 +01:00
|
|
|
* If the |initial| is nonzero, it is used as a initial value, this
|
|
|
|
* function assumes the |in| starts with intermediate data.
|
|
|
|
*
|
2016-10-15 11:25:13 +02:00
|
|
|
* An entire integer is decoded successfully, decoded, the |*fin| is
|
2014-01-25 10:24:15 +01:00
|
|
|
* set to nonzero.
|
|
|
|
*
|
2014-07-22 15:38:18 +02:00
|
|
|
* This function stores the decoded integer in |*res| if it succeed,
|
|
|
|
* including partial decoding (in this case, number of shift to make
|
|
|
|
* in the next call will be stored in |*shift_ptr|) and returns number
|
|
|
|
* of bytes processed, or returns -1, indicating decoding error.
|
2013-07-19 09:50:31 +02:00
|
|
|
*/
|
2016-10-15 11:25:13 +02:00
|
|
|
static ssize_t decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
|
2016-05-14 11:25:20 +02:00
|
|
|
uint32_t initial, size_t shift, const uint8_t *in,
|
|
|
|
const uint8_t *last, size_t prefix) {
|
2015-09-23 11:07:38 +02:00
|
|
|
uint32_t k = (uint8_t)((1 << prefix) - 1);
|
2014-07-22 15:38:18 +02:00
|
|
|
uint32_t n = initial;
|
2016-05-14 11:25:20 +02:00
|
|
|
const uint8_t *start = in;
|
2014-07-21 18:50:29 +02:00
|
|
|
|
|
|
|
*shift_ptr = 0;
|
2016-10-15 11:25:13 +02:00
|
|
|
*fin = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (n == 0) {
|
|
|
|
if ((*in & k) != k) {
|
2014-01-25 10:24:15 +01:00
|
|
|
*res = (*in) & k;
|
2016-10-15 11:25:13 +02:00
|
|
|
*fin = 1;
|
2014-07-22 15:38:18 +02:00
|
|
|
return 1;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
n = k;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (++in == last) {
|
2014-02-26 13:44:48 +01:00
|
|
|
*res = n;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in - start);
|
2014-02-26 13:44:48 +01:00
|
|
|
}
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; in != last; ++in, shift += 7) {
|
2014-07-22 15:38:18 +02:00
|
|
|
uint32_t add = *in & 0x7f;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((UINT32_MAX >> shift) < add) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflate: integer overflow on shift\n");
|
2014-07-22 15:38:18 +02:00
|
|
|
return -1;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
add <<= shift;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (UINT32_MAX - add < n) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflate: integer overflow on addition\n");
|
2014-07-22 15:38:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += add;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((*in & (1 << 7)) == 0) {
|
2013-07-19 09:50:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 18:50:29 +02:00
|
|
|
|
|
|
|
*shift_ptr = shift;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (in == last) {
|
2014-01-25 10:24:15 +01:00
|
|
|
*res = n;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in - start);
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-21 18:52:51 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
*res = n;
|
2016-10-15 11:25:13 +02:00
|
|
|
*fin = 1;
|
2014-08-04 09:04:29 +02:00
|
|
|
return (ssize_t)(in + 1 - start);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
|
2014-02-13 15:22:52 +01:00
|
|
|
int rv;
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: emit table_size=%zu\n", table_size);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-07-12 11:47:55 +02:00
|
|
|
blocklen = count_encoded_length(table_size, 5);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sizeof(sb) < blocklen) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-04-01 18:25:44 +02:00
|
|
|
*bufp = 0x20u;
|
|
|
|
|
2014-07-12 11:47:55 +02:00
|
|
|
encode_length(bufp, table_size, 5);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-11-16 09:05:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx) {
|
2013-07-19 09:50:31 +02:00
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
blocklen = count_encoded_length(idx + 1, 7);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: emit indexed index=%zu, %zu bytes\n", idx, blocklen);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sizeof(sb) < blocklen) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
|
|
|
*bufp = 0x80u;
|
2014-05-27 22:28:12 +02:00
|
|
|
encode_length(bufp, idx + 1, 7);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len) {
|
2014-06-11 16:37:16 +02:00
|
|
|
int rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
|
|
|
uint8_t *bufp;
|
|
|
|
size_t blocklen;
|
2014-07-26 11:19:25 +02:00
|
|
|
size_t enclen;
|
|
|
|
int huffman = 0;
|
|
|
|
|
|
|
|
enclen = nghttp2_hd_huff_encode_count(str, len);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (enclen < len) {
|
2014-07-26 11:19:25 +02:00
|
|
|
huffman = 1;
|
|
|
|
} else {
|
|
|
|
enclen = len;
|
|
|
|
}
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
blocklen = count_encoded_length(enclen, 7);
|
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("deflatehd: emit string str=%.*s, length=%zu, huffman=%d, "
|
|
|
|
"encoded_length=%zu\n",
|
|
|
|
(int)len, (const char *)str, len, huffman, enclen);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sizeof(sb) < blocklen) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufp = sb;
|
|
|
|
*bufp = huffman ? 1 << 7 : 0;
|
2014-04-30 08:54:02 +02:00
|
|
|
encode_length(bufp, enclen, 7);
|
2014-03-13 14:11:02 +01:00
|
|
|
|
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (huffman) {
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_hd_huff_encode(bufs, str, len);
|
2013-10-20 17:22:14 +02:00
|
|
|
} else {
|
|
|
|
assert(enclen == len);
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, str, len);
|
2013-10-20 17:22:14 +02:00
|
|
|
}
|
2014-03-13 14:11:02 +01:00
|
|
|
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2013-10-20 17:22:14 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
static uint8_t pack_first_byte(int indexing_mode) {
|
|
|
|
switch (indexing_mode) {
|
|
|
|
case NGHTTP2_HD_WITH_INDEXING:
|
2014-04-01 18:25:44 +02:00
|
|
|
return 0x40u;
|
2015-04-14 18:21:27 +02:00
|
|
|
case NGHTTP2_HD_WITHOUT_INDEXING:
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_HD_NEVER_INDEXING:
|
2014-04-01 18:25:44 +02:00
|
|
|
return 0x10u;
|
2015-04-14 18:21:27 +02:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-04-01 18:25:44 +02:00
|
|
|
}
|
2015-04-17 16:46:19 +02:00
|
|
|
/* This is required to compile with android NDK r10d +
|
|
|
|
--enable-werror */
|
|
|
|
return 0;
|
2014-04-01 18:25:44 +02:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
|
2015-04-14 18:21:27 +02:00
|
|
|
const nghttp2_nv *nv, int indexing_mode) {
|
2013-07-19 09:50:31 +02:00
|
|
|
int rv;
|
|
|
|
uint8_t *bufp;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t blocklen;
|
2014-03-13 14:11:02 +01:00
|
|
|
uint8_t sb[16];
|
2014-04-01 18:25:44 +02:00
|
|
|
size_t prefixlen;
|
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
|
2014-04-01 18:25:44 +02:00
|
|
|
prefixlen = 6;
|
|
|
|
} else {
|
|
|
|
prefixlen = 4;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("deflatehd: emit indname index=%zu, valuelen=%zu, indexing_mode=%d\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
idx, nv->valuelen, indexing_mode);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
blocklen = count_encoded_length(idx + 1, prefixlen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sizeof(sb) < blocklen) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
bufp = sb;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
*bufp = pack_first_byte(indexing_mode);
|
2014-04-01 18:25:44 +02:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
encode_length(bufp, idx + 1, prefixlen);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = nghttp2_bufs_add(bufs, sb, blocklen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->value, nv->valuelen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:43:14 +02:00
|
|
|
static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
|
2015-04-14 18:21:27 +02:00
|
|
|
int indexing_mode) {
|
2013-07-19 09:50:31 +02:00
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF(
|
|
|
|
"deflatehd: emit newname namelen=%zu, valuelen=%zu, indexing_mode=%d\n",
|
|
|
|
nv->namelen, nv->valuelen, indexing_mode);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
rv = nghttp2_bufs_addb(bufs, pack_first_byte(indexing_mode));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-12 14:48:00 +02:00
|
|
|
return rv;
|
2014-03-13 14:11:02 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->name, nv->namelen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-19 09:50:31 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
rv = emit_string(bufs, nv->value, nv->valuelen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
static int add_hd_table_incremental(nghttp2_hd_context *context,
|
|
|
|
nghttp2_hd_nv *nv, nghttp2_hd_map *map,
|
|
|
|
uint32_t hash) {
|
2013-09-06 14:53:28 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_hd_entry *new_ent;
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t room;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = context->mem;
|
2016-03-12 05:23:12 +01:00
|
|
|
room = entry_room(nv->name->len, nv->value->len);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
while (context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
|
|
|
|
context->hd_table.len > 0) {
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx = context->hd_table.len - 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
context->hd_table_bufsize -=
|
|
|
|
entry_room(ent->nv.name->len, ent->nv.value->len);
|
2014-07-12 11:47:55 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("hpack: remove item from header table: %s: %s\n",
|
|
|
|
(char *)ent->nv.name->base, (char *)ent->nv.value->base);
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_ringbuf_pop_back(&context->hd_table);
|
2015-09-14 18:05:41 +02:00
|
|
|
if (map) {
|
|
|
|
hd_map_remove(map, ent);
|
|
|
|
}
|
2013-10-25 18:01:28 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_entry_free(ent);
|
|
|
|
nghttp2_mem_free(mem, ent);
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (room > context->hd_table_bufsize_max) {
|
2013-09-06 14:53:28 +02:00
|
|
|
/* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
|
2016-03-12 05:23:12 +01:00
|
|
|
immediately evicted. So we don't allocate memory for it. */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
new_ent = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry));
|
|
|
|
if (new_ent == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-07-15 15:47:04 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_entry_init(new_ent, nv);
|
2014-07-15 15:47:04 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = hd_ringbuf_push_front(&context->hd_table, new_ent, mem);
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
nghttp2_hd_entry_free(new_ent);
|
|
|
|
nghttp2_mem_free(mem, new_ent);
|
2014-06-22 06:39:17 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2015-09-14 18:05:41 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
new_ent->seq = context->next_seq++;
|
|
|
|
new_ent->hash = hash;
|
2015-09-14 18:05:41 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
if (map) {
|
|
|
|
hd_map_insert(map, new_ent);
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
context->hd_table_bufsize += room;
|
|
|
|
|
|
|
|
return 0;
|
2013-09-06 14:53:28 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
typedef struct {
|
|
|
|
ssize_t index;
|
|
|
|
/* Nonzero if both name and value are matched. */
|
2016-08-07 17:27:16 +02:00
|
|
|
int name_value_match;
|
2013-12-18 13:45:15 +01:00
|
|
|
} search_result;
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
static search_result search_static_table(const nghttp2_nv *nv, int32_t token,
|
2016-08-07 17:27:16 +02:00
|
|
|
int name_only) {
|
2015-04-15 15:32:11 +02:00
|
|
|
search_result res = {token, 0};
|
|
|
|
int i;
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_static_entry *ent;
|
2015-04-15 15:32:11 +02:00
|
|
|
|
2016-08-07 17:27:16 +02:00
|
|
|
if (name_only) {
|
2015-04-15 15:32:11 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = token;
|
|
|
|
i <= NGHTTP2_TOKEN_WWW_AUTHENTICATE && static_table[i].token == token;
|
|
|
|
++i) {
|
2016-03-12 05:23:12 +01:00
|
|
|
ent = &static_table[i];
|
|
|
|
if (ent->value.len == nv->valuelen &&
|
|
|
|
memcmp(ent->value.base, nv->value, nv->valuelen) == 0) {
|
2015-04-15 15:32:11 +02:00
|
|
|
res.index = i;
|
|
|
|
res.name_value_match = 1;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
static search_result search_hd_table(nghttp2_hd_context *context,
|
2016-03-12 05:23:12 +01:00
|
|
|
const nghttp2_nv *nv, int32_t token,
|
2015-09-14 18:05:41 +02:00
|
|
|
int indexing_mode, nghttp2_hd_map *map,
|
|
|
|
uint32_t hash) {
|
2014-11-27 15:39:04 +01:00
|
|
|
search_result res = {-1, 0};
|
2015-09-14 18:05:41 +02:00
|
|
|
nghttp2_hd_entry *ent;
|
|
|
|
int exact_match;
|
2016-08-07 17:27:16 +02:00
|
|
|
int name_only = indexing_mode == NGHTTP2_HD_NEVER_INDEXING;
|
2015-04-15 15:32:11 +02:00
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
exact_match = 0;
|
2016-08-07 17:27:16 +02:00
|
|
|
ent = hd_map_find(map, &exact_match, nv, token, hash, name_only);
|
|
|
|
|
|
|
|
if (!exact_match && token >= 0 && token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
|
|
|
|
return search_static_table(nv, token, name_only);
|
2015-09-14 18:05:41 +02:00
|
|
|
}
|
2014-09-27 16:08:14 +02:00
|
|
|
|
2016-08-07 17:27:16 +02:00
|
|
|
if (ent == NULL) {
|
2015-09-14 18:05:41 +02:00
|
|
|
return res;
|
|
|
|
}
|
2014-09-27 16:08:14 +02:00
|
|
|
|
2015-09-24 18:45:28 +02:00
|
|
|
res.index =
|
|
|
|
(ssize_t)(context->next_seq - 1 - ent->seq + NGHTTP2_STATIC_TABLE_LENGTH);
|
2016-08-07 17:27:16 +02:00
|
|
|
res.name_value_match = exact_match;
|
2014-09-27 16:08:14 +02:00
|
|
|
|
2013-12-18 13:45:15 +01:00
|
|
|
return res;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
static void hd_context_shrink_table_size(nghttp2_hd_context *context,
|
|
|
|
nghttp2_hd_map *map) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = context->mem;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
while (context->hd_table_bufsize > context->hd_table_bufsize_max &&
|
|
|
|
context->hd_table.len > 0) {
|
2014-05-27 22:28:12 +02:00
|
|
|
size_t idx = context->hd_table.len - 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
|
2016-03-12 05:23:12 +01:00
|
|
|
context->hd_table_bufsize -=
|
|
|
|
entry_room(ent->nv.name->len, ent->nv.value->len);
|
2014-05-08 16:54:07 +02:00
|
|
|
hd_ringbuf_pop_back(&context->hd_table);
|
2015-09-14 18:05:41 +02:00
|
|
|
if (map) {
|
|
|
|
hd_map_remove(map, ent);
|
|
|
|
}
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
nghttp2_hd_entry_free(ent);
|
|
|
|
nghttp2_mem_free(mem, ent);
|
2013-10-25 18:01:28 +02:00
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
int nghttp2_hd_deflate_change_table_size(
|
|
|
|
nghttp2_hd_deflater *deflater, size_t settings_max_dynamic_table_size) {
|
|
|
|
size_t next_bufsize = nghttp2_min(settings_max_dynamic_table_size,
|
2014-02-13 15:22:52 +01:00
|
|
|
deflater->deflate_hd_table_bufsize_max);
|
|
|
|
|
2014-05-11 14:25:27 +02:00
|
|
|
deflater->ctx.hd_table_bufsize_max = next_bufsize;
|
|
|
|
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max =
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_min(deflater->min_hd_table_bufsize_max, next_bufsize);
|
2014-07-25 15:55:45 +02:00
|
|
|
|
2014-05-11 14:25:27 +02:00
|
|
|
deflater->notify_table_size_change = 1;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
hd_context_shrink_table_size(&deflater->ctx, &deflater->map);
|
2014-02-13 15:22:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
int nghttp2_hd_inflate_change_table_size(
|
|
|
|
nghttp2_hd_inflater *inflater, size_t settings_max_dynamic_table_size) {
|
2015-07-21 17:11:23 +02:00
|
|
|
switch (inflater->state) {
|
|
|
|
case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
|
|
|
|
case NGHTTP2_HD_STATE_INFLATE_START:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NGHTTP2_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2015-10-24 10:27:24 +02:00
|
|
|
/* It seems that encoder is not required to send dynamic table size
|
|
|
|
update if the table size is not changed after applying
|
|
|
|
SETTINGS_HEADER_TABLE_SIZE. RFC 7541 is ambiguous here, but this
|
|
|
|
is the intention of the editor. If new maximum table size is
|
|
|
|
strictly smaller than the current negotiated maximum size,
|
|
|
|
encoder must send dynamic table size update. In other cases, we
|
|
|
|
cannot expect it to do so. */
|
2016-09-11 15:25:01 +02:00
|
|
|
if (inflater->ctx.hd_table_bufsize_max > settings_max_dynamic_table_size) {
|
2015-10-24 10:27:24 +02:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE;
|
|
|
|
/* Remember minimum value, and validate that encoder sends the
|
|
|
|
value less than or equal to this. */
|
2016-09-11 15:25:01 +02:00
|
|
|
inflater->min_hd_table_bufsize_max = settings_max_dynamic_table_size;
|
2015-10-24 10:27:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
inflater->settings_hd_table_bufsize_max = settings_max_dynamic_table_size;
|
2015-10-24 10:27:24 +02:00
|
|
|
|
2016-09-11 15:25:01 +02:00
|
|
|
inflater->ctx.hd_table_bufsize_max = settings_max_dynamic_table_size;
|
2015-10-24 10:27:24 +02:00
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
hd_context_shrink_table_size(&inflater->ctx, NULL);
|
2013-10-25 18:01:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
#define INDEX_RANGE_VALID(context, idx) \
|
2014-07-16 16:04:00 +02:00
|
|
|
((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
|
2013-10-24 15:09:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t get_max_index(nghttp2_hd_context *context) {
|
2016-08-04 03:26:13 +02:00
|
|
|
return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv nghttp2_hd_table_get(nghttp2_hd_context *context, size_t idx) {
|
2014-06-16 18:52:11 +02:00
|
|
|
assert(INDEX_RANGE_VALID(context, idx));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
|
2016-03-12 05:23:12 +01:00
|
|
|
return hd_ringbuf_get(&context->hd_table, idx - NGHTTP2_STATIC_TABLE_LENGTH)
|
|
|
|
->nv;
|
2013-10-12 14:49:01 +02:00
|
|
|
} else {
|
2016-03-13 16:54:02 +01:00
|
|
|
nghttp2_hd_static_entry *ent = &static_table[idx];
|
|
|
|
nghttp2_hd_nv nv = {&ent->name, &ent->value, ent->token,
|
|
|
|
NGHTTP2_NV_FLAG_NONE};
|
|
|
|
return nv;
|
2016-03-12 05:23:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const nghttp2_nv *nghttp2_hd_table_get2(nghttp2_hd_context *context,
|
|
|
|
size_t idx) {
|
|
|
|
assert(INDEX_RANGE_VALID(context, idx));
|
|
|
|
if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
|
|
|
|
return &hd_ringbuf_get(&context->hd_table,
|
2016-10-15 11:36:04 +02:00
|
|
|
idx - NGHTTP2_STATIC_TABLE_LENGTH)
|
|
|
|
->cnv;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
return &static_table[idx].cnv;
|
2013-08-23 16:38:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
static int hd_deflate_decide_indexing(nghttp2_hd_deflater *deflater,
|
2016-03-12 05:23:12 +01:00
|
|
|
const nghttp2_nv *nv, int32_t token) {
|
2015-04-15 15:32:11 +02:00
|
|
|
if (token == NGHTTP2_TOKEN__PATH || token == NGHTTP2_TOKEN_AGE ||
|
|
|
|
token == NGHTTP2_TOKEN_CONTENT_LENGTH || token == NGHTTP2_TOKEN_ETAG ||
|
|
|
|
token == NGHTTP2_TOKEN_IF_MODIFIED_SINCE ||
|
|
|
|
token == NGHTTP2_TOKEN_IF_NONE_MATCH || token == NGHTTP2_TOKEN_LOCATION ||
|
|
|
|
token == NGHTTP2_TOKEN_SET_COOKIE ||
|
|
|
|
entry_room(nv->namelen, nv->valuelen) >
|
|
|
|
deflater->ctx.hd_table_bufsize_max * 3 / 4) {
|
2015-04-14 18:21:27 +02:00
|
|
|
return NGHTTP2_HD_WITHOUT_INDEXING;
|
2014-01-21 13:48:25 +01:00
|
|
|
}
|
2015-04-15 15:32:11 +02:00
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
return NGHTTP2_HD_WITH_INDEXING;
|
2013-11-19 13:41:02 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
|
|
|
|
const nghttp2_nv *nv) {
|
2013-08-24 15:58:26 +02:00
|
|
|
int rv;
|
2013-12-18 13:45:15 +01:00
|
|
|
search_result res;
|
2014-10-30 15:31:28 +01:00
|
|
|
ssize_t idx;
|
2015-04-14 18:21:27 +02:00
|
|
|
int indexing_mode;
|
2016-03-12 05:23:12 +01:00
|
|
|
int32_t token;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2016-02-19 16:48:18 +01:00
|
|
|
uint32_t hash = 0;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("deflatehd: deflating %.*s: %.*s\n", (int)nv->namelen, nv->name,
|
|
|
|
(int)nv->valuelen, nv->value);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = deflater->ctx.mem;
|
|
|
|
|
2015-04-15 15:32:11 +02:00
|
|
|
token = lookup_token(nv->name, nv->namelen);
|
2016-02-19 16:48:18 +01:00
|
|
|
if (token == -1) {
|
2015-09-14 18:05:41 +02:00
|
|
|
hash = name_hash(nv);
|
2016-02-19 16:48:18 +01:00
|
|
|
} else if (token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
|
2015-09-14 18:05:41 +02:00
|
|
|
hash = static_table[token].hash;
|
|
|
|
}
|
2015-04-15 15:32:11 +02:00
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
/* Don't index authorization header field since it may contain low
|
|
|
|
entropy secret data (e.g., id/password). Also cookie header
|
|
|
|
field with less than 20 bytes value is also never indexed. This
|
|
|
|
is the same criteria used in Firefox codebase. */
|
2015-04-15 15:32:11 +02:00
|
|
|
indexing_mode =
|
|
|
|
token == NGHTTP2_TOKEN_AUTHORIZATION ||
|
|
|
|
(token == NGHTTP2_TOKEN_COOKIE && nv->valuelen < 20) ||
|
|
|
|
(nv->flags & NGHTTP2_NV_FLAG_NO_INDEX)
|
|
|
|
? NGHTTP2_HD_NEVER_INDEXING
|
|
|
|
: hd_deflate_decide_indexing(deflater, nv, token);
|
2015-04-14 18:21:27 +02:00
|
|
|
|
2015-09-14 18:05:41 +02:00
|
|
|
res = search_hd_table(&deflater->ctx, nv, token, indexing_mode,
|
|
|
|
&deflater->map, hash);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
idx = res.index;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (res.name_value_match) {
|
2014-07-26 11:19:25 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: name/value match index=%zd\n", idx);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
rv = emit_indexed_block(bufs, (size_t)idx);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-07-16 16:04:00 +02:00
|
|
|
return rv;
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-07-26 11:19:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (res.index != -1) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: name match index=%zd\n", res.index);
|
2014-07-26 11:19:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv hd_nv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (idx != -1 && idx < (ssize_t)NGHTTP2_STATIC_TABLE_LENGTH) {
|
2016-03-12 05:23:12 +01:00
|
|
|
hd_nv.name = nghttp2_hd_table_get(&deflater->ctx, (size_t)idx).name;
|
|
|
|
nghttp2_rcbuf_incref(hd_nv.name);
|
2013-08-24 15:58:26 +02:00
|
|
|
} else {
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = nghttp2_rcbuf_new2(&hd_nv.name, nv->name, nv->namelen, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
rv = nghttp2_rcbuf_new2(&hd_nv.value, nv->value, nv->valuelen, mem);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
nghttp2_rcbuf_decref(hd_nv.name);
|
|
|
|
return rv;
|
2014-07-26 11:19:25 +02:00
|
|
|
}
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
hd_nv.token = token;
|
|
|
|
hd_nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
|
|
|
|
rv = add_hd_table_incremental(&deflater->ctx, &hd_nv, &deflater->map, hash);
|
|
|
|
|
|
|
|
nghttp2_rcbuf_decref(hd_nv.value);
|
|
|
|
nghttp2_rcbuf_decref(hd_nv.name);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
2013-08-24 15:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (idx == -1) {
|
2015-04-14 18:21:27 +02:00
|
|
|
rv = emit_newname_block(bufs, nv, indexing_mode);
|
2014-07-26 11:19:25 +02:00
|
|
|
} else {
|
2015-09-23 07:41:53 +02:00
|
|
|
rv = emit_indname_block(bufs, (size_t)idx, nv, indexing_mode);
|
2014-07-26 11:19:25 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-07-26 11:19:25 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-08-24 15:58:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:42:55 +02:00
|
|
|
int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_bufs *bufs, const nghttp2_nv *nv,
|
|
|
|
size_t nvlen) {
|
2014-03-10 17:47:38 +01:00
|
|
|
size_t i;
|
2013-07-25 18:34:28 +02:00
|
|
|
int rv = 0;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (deflater->ctx.bad) {
|
2013-07-25 18:34:28 +02:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (deflater->notify_table_size_change) {
|
2014-07-25 15:55:45 +02:00
|
|
|
size_t min_hd_table_bufsize_max;
|
|
|
|
|
|
|
|
min_hd_table_bufsize_max = deflater->min_hd_table_bufsize_max;
|
2014-05-11 14:25:27 +02:00
|
|
|
|
|
|
|
deflater->notify_table_size_change = 0;
|
2014-07-25 15:55:45 +02:00
|
|
|
deflater->min_hd_table_bufsize_max = UINT32_MAX;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (deflater->ctx.hd_table_bufsize_max > min_hd_table_bufsize_max) {
|
2014-07-25 15:55:45 +02:00
|
|
|
|
|
|
|
rv = emit_table_size(bufs, min_hd_table_bufsize_max);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-07-25 15:55:45 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2014-05-11 14:25:27 +02:00
|
|
|
|
|
|
|
rv = emit_table_size(bufs, deflater->ctx.hd_table_bufsize_max);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-13 15:22:52 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < nvlen; ++i) {
|
2014-03-13 14:11:02 +01:00
|
|
|
rv = deflate_nv(deflater, bufs, &nv[i]);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-24 15:58:26 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: all input name/value pairs were deflated\n");
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
return 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
fail:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("deflatehd: error return %d\n", rv);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
deflater->ctx.bad = 1;
|
2013-07-25 18:34:28 +02:00
|
|
|
return rv;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf,
|
|
|
|
size_t buflen, const nghttp2_nv *nv,
|
|
|
|
size_t nvlen) {
|
2014-05-13 16:42:55 +02:00
|
|
|
nghttp2_bufs bufs;
|
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = deflater->ctx.mem;
|
2014-05-13 16:42:55 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen, mem);
|
2014-05-13 16:42:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-13 16:42:55 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
|
|
|
|
|
|
|
|
buflen = nghttp2_bufs_len(&bufs);
|
|
|
|
|
|
|
|
nghttp2_bufs_wrap_free(&bufs);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
|
2014-05-13 16:42:55 +02:00
|
|
|
return NGHTTP2_ERR_INSUFF_BUFSIZE;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-05-13 16:42:55 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)buflen;
|
2014-05-13 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 03:55:51 +02:00
|
|
|
ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
|
2016-08-16 06:36:38 +02:00
|
|
|
const nghttp2_vec *vec, size_t veclen,
|
2016-08-16 03:55:51 +02:00
|
|
|
const nghttp2_nv *nv, size_t nvlen) {
|
2016-08-15 12:28:45 +02:00
|
|
|
nghttp2_bufs bufs;
|
|
|
|
int rv;
|
|
|
|
nghttp2_mem *mem;
|
2016-08-16 06:36:38 +02:00
|
|
|
size_t buflen;
|
2016-08-15 12:28:45 +02:00
|
|
|
|
|
|
|
mem = deflater->ctx.mem;
|
|
|
|
|
2016-08-18 14:23:17 +02:00
|
|
|
rv = nghttp2_bufs_wrap_init2(&bufs, vec, veclen, mem);
|
2016-08-15 12:28:45 +02:00
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
|
|
|
|
|
2016-08-16 06:36:38 +02:00
|
|
|
buflen = nghttp2_bufs_len(&bufs);
|
2016-08-15 12:28:45 +02:00
|
|
|
|
|
|
|
nghttp2_bufs_wrap_free(&bufs);
|
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
|
|
|
|
return NGHTTP2_ERR_INSUFF_BUFSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ssize_t)buflen;
|
|
|
|
}
|
|
|
|
|
2014-10-19 15:19:33 +02:00
|
|
|
size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater _U_,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_nv *nva, size_t nvlen) {
|
2014-07-12 11:47:55 +02:00
|
|
|
size_t n = 0;
|
2014-05-13 16:42:55 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Possible Maximum Header Table Size Change. Encoding (1u << 31) -
|
2014-07-25 18:12:19 +02:00
|
|
|
1 using 4 bit prefix requires 6 bytes. We may emit this at most
|
|
|
|
twice. */
|
|
|
|
n += 12;
|
2014-05-13 16:42:55 +02:00
|
|
|
|
|
|
|
/* Use Literal Header Field without indexing - New Name, since it is
|
|
|
|
most space consuming format. Also we choose the less one between
|
|
|
|
non-huffman and huffman, so using literal byte count is
|
|
|
|
sufficient for upper bound.
|
|
|
|
|
|
|
|
Encoding (1u << 31) - 1 using 7 bit prefix requires 6 bytes. We
|
|
|
|
need 2 of this for |nvlen| header fields. */
|
|
|
|
n += 6 * 2 * nvlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < nvlen; ++i) {
|
2014-05-13 16:42:55 +02:00
|
|
|
n += nva[i].namelen + nva[i].valuelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t deflate_hd_table_bufsize_max) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_hd_deflate_new2(deflater_ptr, deflate_hd_table_bufsize_max,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
|
|
|
|
size_t deflate_hd_table_bufsize_max,
|
|
|
|
nghttp2_mem *mem) {
|
2014-06-25 14:26:47 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_hd_deflater *deflater;
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
if (mem == NULL) {
|
|
|
|
mem = nghttp2_mem_default();
|
|
|
|
}
|
|
|
|
|
|
|
|
deflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_deflater));
|
2014-05-13 16:42:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (deflater == NULL) {
|
2014-05-13 16:42:55 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max, mem);
|
2014-06-25 14:26:47 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, deflater);
|
2014-07-17 17:21:15 +02:00
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*deflater_ptr = deflater;
|
|
|
|
|
|
|
|
return 0;
|
2014-05-13 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = deflater->ctx.mem;
|
|
|
|
|
2014-05-13 16:42:55 +02:00
|
|
|
nghttp2_hd_deflate_free(deflater);
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, deflater);
|
2014-05-13 16:42:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *in) {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->huffman_encoded = (*in & (1 << 7)) != 0;
|
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Decodes the integer from the range [in, last). The result is
|
|
|
|
* assigned to |inflater->left|. If the |inflater->left| is 0, then
|
|
|
|
* it performs variable integer decoding from scratch. Otherwise, it
|
|
|
|
* uses the |inflater->left| as the initial value and continues to
|
|
|
|
* decode assuming that [in, last) begins with intermediary sequence.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Integer decoding failed
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static ssize_t hd_inflate_read_len(nghttp2_hd_inflater *inflater, int *rfin,
|
2016-05-14 11:25:20 +02:00
|
|
|
const uint8_t *in, const uint8_t *last,
|
|
|
|
size_t prefix, size_t maxlen) {
|
2014-07-22 15:38:18 +02:00
|
|
|
ssize_t rv;
|
|
|
|
uint32_t out;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
*rfin = 0;
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
rv = decode_length(&out, &inflater->shift, rfin, (uint32_t)inflater->left,
|
|
|
|
inflater->shift, in, last, prefix);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == -1) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: integer decoding failed\n");
|
2014-01-25 10:24:15 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (out > maxlen) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: integer exceeded the maximum value %zu\n", maxlen);
|
2013-07-25 18:34:28 +02:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
|
|
|
inflater->left = out;
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: decoded integer is %u\n", out);
|
2014-08-02 03:40:25 +02:00
|
|
|
|
2014-07-22 15:38:18 +02:00
|
|
|
return rv;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Reads |inflater->left| bytes from the range [in, last) and performs
|
|
|
|
* huffman decoding against them and pushes the result into the
|
|
|
|
* |buffer|.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Huffman decoding failed
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static ssize_t hd_inflate_read_huff(nghttp2_hd_inflater *inflater,
|
2016-05-14 11:25:20 +02:00
|
|
|
nghttp2_buf *buf, const uint8_t *in,
|
|
|
|
const uint8_t *last) {
|
2014-06-11 16:37:16 +02:00
|
|
|
ssize_t readlen;
|
2016-10-15 11:25:13 +02:00
|
|
|
int fin = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((size_t)(last - in) >= inflater->left) {
|
2014-01-25 10:24:15 +01:00
|
|
|
last = in + inflater->left;
|
2016-10-15 11:25:13 +02:00
|
|
|
fin = 1;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2016-03-10 17:52:55 +01:00
|
|
|
readlen = nghttp2_hd_huff_decode(&inflater->huff_decode_ctx, buf, in,
|
2016-10-15 11:25:13 +02:00
|
|
|
(size_t)(last - in), fin);
|
2014-05-12 14:48:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen < 0) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: huffman decoding failed\n");
|
2014-06-11 16:37:16 +02:00
|
|
|
return readlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
inflater->left -= (size_t)readlen;
|
2014-06-11 16:37:16 +02:00
|
|
|
return readlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reads |inflater->left| bytes from the range [in, last) and copies
|
|
|
|
* them into the |buffer|.
|
|
|
|
*
|
|
|
|
* This function returns the number of bytes read if it succeeds, or
|
|
|
|
* one of the following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Header decompression failed
|
|
|
|
*/
|
2016-03-10 17:52:55 +01:00
|
|
|
static ssize_t hd_inflate_read(nghttp2_hd_inflater *inflater, nghttp2_buf *buf,
|
2016-05-14 11:25:20 +02:00
|
|
|
const uint8_t *in, const uint8_t *last) {
|
2014-07-22 15:38:18 +02:00
|
|
|
size_t len = nghttp2_min((size_t)(last - in), inflater->left);
|
2016-03-10 17:52:55 +01:00
|
|
|
|
|
|
|
buf->last = nghttp2_cpymem(buf->last, in, len);
|
|
|
|
|
2014-07-22 15:38:18 +02:00
|
|
|
inflater->left -= len;
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)len;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-11 04:33:09 +02:00
|
|
|
* Finalize indexed header representation reception. The referenced
|
|
|
|
* header is always emitted, and |*nv_out| is filled with that value.
|
2014-01-25 10:24:15 +01:00
|
|
|
*/
|
2016-08-11 04:33:09 +02:00
|
|
|
static void hd_inflate_commit_indexed(nghttp2_hd_inflater *inflater,
|
|
|
|
nghttp2_hd_nv *nv_out) {
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
|
2014-07-12 11:47:55 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
emit_header(nv_out, &nv);
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
/*
|
|
|
|
* Finalize literal header representation - new name- reception. If
|
|
|
|
* header is emitted, |*nv_out| is filled with that value and 0 is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv *nv_out) {
|
|
|
|
nghttp2_hd_nv nv;
|
|
|
|
int rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->no_index) {
|
2014-04-01 18:25:44 +02:00
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
|
|
|
|
} else {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
}
|
2014-04-01 16:17:50 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nv.name = inflater->namercbuf;
|
|
|
|
nv.value = inflater->valuercbuf;
|
|
|
|
nv.token = lookup_token(inflater->namercbuf->base, inflater->namercbuf->len);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
if (inflater->index_required) {
|
|
|
|
rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
emit_header(nv_out, &nv);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
inflater->nv_name_keep = nv.name;
|
|
|
|
inflater->nv_value_keep = nv.value;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->namercbuf = NULL;
|
|
|
|
inflater->valuercbuf = NULL;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finalize literal header representation - indexed name-
|
|
|
|
* reception. If header is emitted, |*nv_out| is filled with that
|
|
|
|
* value and 0 is returned.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2014-01-26 09:53:04 +01:00
|
|
|
static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv *nv_out) {
|
|
|
|
nghttp2_hd_nv nv;
|
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->no_index) {
|
2014-04-01 18:25:44 +02:00
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
|
|
|
|
} else {
|
|
|
|
nv.flags = NGHTTP2_NV_FLAG_NONE;
|
|
|
|
}
|
2014-04-01 16:17:50 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_rcbuf_incref(nv.name);
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nv.value = inflater->valuercbuf;
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
if (inflater->index_required) {
|
|
|
|
rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
|
|
|
|
if (rv != 0) {
|
|
|
|
nghttp2_rcbuf_decref(nv.name);
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2015-04-26 11:43:24 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
emit_header(nv_out, &nv);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->nv_name_keep = nv.name;
|
2016-03-10 17:52:55 +01:00
|
|
|
inflater->nv_value_keep = nv.value;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->valuercbuf = NULL;
|
|
|
|
|
2014-03-11 13:18:28 +01:00
|
|
|
return 0;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out,
|
|
|
|
int *inflate_flags, uint8_t *in, size_t inlen,
|
|
|
|
int in_final) {
|
2016-05-14 11:25:20 +02:00
|
|
|
return nghttp2_hd_inflate_hd2(inflater, nv_out, inflate_flags, in, inlen,
|
|
|
|
in_final);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
|
|
|
|
nghttp2_nv *nv_out, int *inflate_flags,
|
|
|
|
const uint8_t *in, size_t inlen, int in_final) {
|
2016-03-12 05:23:12 +01:00
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_hd_nv hd_nv;
|
|
|
|
|
2016-05-14 11:25:20 +02:00
|
|
|
rv = nghttp2_hd_inflate_hd_nv(inflater, &hd_nv, inflate_flags, in, inlen,
|
|
|
|
in_final);
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
|
|
|
|
nv_out->name = hd_nv.name->base;
|
|
|
|
nv_out->namelen = hd_nv.name->len;
|
|
|
|
|
|
|
|
nv_out->value = hd_nv.value->base;
|
|
|
|
nv_out->valuelen = hd_nv.value->len;
|
|
|
|
|
|
|
|
nv_out->flags = hd_nv.flags;
|
|
|
|
}
|
2015-04-15 15:32:11 +02:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
return rv;
|
2015-04-15 15:32:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 11:25:20 +02:00
|
|
|
ssize_t nghttp2_hd_inflate_hd_nv(nghttp2_hd_inflater *inflater,
|
|
|
|
nghttp2_hd_nv *nv_out, int *inflate_flags,
|
|
|
|
const uint8_t *in, size_t inlen,
|
|
|
|
int in_final) {
|
2014-01-25 10:24:15 +01:00
|
|
|
ssize_t rv = 0;
|
2016-05-14 11:25:20 +02:00
|
|
|
const uint8_t *first = in;
|
|
|
|
const uint8_t *last = in + inlen;
|
2014-01-25 10:24:15 +01:00
|
|
|
int rfin = 0;
|
2015-03-23 15:25:57 +01:00
|
|
|
int busy = 0;
|
2016-03-10 17:52:55 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = inflater->ctx.mem;
|
2014-01-25 10:24:15 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->ctx.bad) {
|
2014-02-26 15:20:52 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: start state=%d\n", inflater->state);
|
2014-02-13 15:22:52 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2014-01-25 10:24:15 +01:00
|
|
|
*inflate_flags = NGHTTP2_HD_INFLATE_NONE;
|
2015-03-23 15:25:57 +01:00
|
|
|
for (; in != last || busy;) {
|
|
|
|
busy = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (inflater->state) {
|
2015-07-21 17:11:23 +02:00
|
|
|
case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
|
|
|
|
if ((*in & 0xe0u) != 0x20u) {
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("inflatehd: header table size change was expected, but saw "
|
|
|
|
"0x%02x as first byte",
|
2016-10-14 15:33:08 +02:00
|
|
|
*in);
|
2015-07-21 17:11:23 +02:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case NGHTTP2_HD_STATE_INFLATE_START:
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_OPCODE:
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((*in & 0xe0u) == 0x20u) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: header table size change\n");
|
2015-07-21 17:11:23 +02:00
|
|
|
if (inflater->state == NGHTTP2_HD_STATE_OPCODE) {
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("inflatehd: header table size change must appear at the head "
|
|
|
|
"of header block\n");
|
2015-07-21 17:11:23 +02:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-04-01 18:25:44 +02:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_TABLE_SIZE;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (*in & 0x80u) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: indexed repr\n");
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (*in == 0x40u || *in == 0 || *in == 0x10u) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: literal header repr - new name\n");
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_NEWNAME;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN;
|
|
|
|
} else {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: literal header repr - indexed name\n");
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->opcode = NGHTTP2_HD_OPCODE_INDNAME;
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
|
|
|
|
}
|
2014-04-01 18:25:44 +02:00
|
|
|
inflater->index_required = (*in & 0x40) != 0;
|
2014-07-31 16:08:51 +02:00
|
|
|
inflater->no_index = (*in & 0xf0u) == 0x10u;
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: indexing required=%d, no_index=%d\n",
|
|
|
|
inflater->index_required, inflater->no_index);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
2014-01-25 10:24:15 +01:00
|
|
|
++in;
|
|
|
|
}
|
2013-11-16 09:05:18 +01:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2014-02-13 15:22:52 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_READ_TABLE_SIZE:
|
|
|
|
rfin = 0;
|
2015-10-24 10:27:24 +02:00
|
|
|
rv = hd_inflate_read_len(
|
|
|
|
inflater, &rfin, in, last, 5,
|
|
|
|
nghttp2_min(inflater->min_hd_table_bufsize_max,
|
|
|
|
inflater->settings_hd_table_bufsize_max));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2014-02-13 15:22:52 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
in += rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!rfin) {
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: table_size=%zu\n", inflater->left);
|
2015-10-24 10:27:24 +02:00
|
|
|
inflater->min_hd_table_bufsize_max = UINT32_MAX;
|
2014-02-13 15:22:52 +01:00
|
|
|
inflater->ctx.hd_table_bufsize_max = inflater->left;
|
2015-09-14 18:05:41 +02:00
|
|
|
hd_context_shrink_table_size(&inflater->ctx, NULL);
|
2015-07-21 17:11:23 +02:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
|
2014-02-13 15:22:52 +01:00
|
|
|
break;
|
2014-04-01 18:25:44 +02:00
|
|
|
case NGHTTP2_HD_STATE_READ_INDEX: {
|
|
|
|
size_t prefixlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
|
2014-04-01 18:25:44 +02:00
|
|
|
prefixlen = 7;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (inflater->index_required) {
|
2014-04-01 18:25:44 +02:00
|
|
|
prefixlen = 6;
|
|
|
|
} else {
|
|
|
|
prefixlen = 4;
|
|
|
|
}
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
rfin = 0;
|
2014-04-01 18:25:44 +02:00
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, prefixlen,
|
2016-08-04 03:26:13 +02:00
|
|
|
get_max_index(&inflater->ctx));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2013-08-22 18:28:01 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-05-01 02:06:54 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-05-01 02:06:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!rfin) {
|
2014-07-21 19:09:01 +02:00
|
|
|
goto almost_ok;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->left == 0) {
|
2014-05-01 02:06:54 +02:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: index=%zu\n", inflater->left);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->index = inflater->left;
|
|
|
|
--inflater->index;
|
2014-07-21 19:09:01 +02:00
|
|
|
|
2016-08-11 04:00:43 +02:00
|
|
|
hd_inflate_commit_indexed(inflater, nv_out);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
2016-08-11 04:00:43 +02:00
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
|
|
|
return (ssize_t)(in - first);
|
2014-01-25 10:24:15 +01:00
|
|
|
} else {
|
|
|
|
inflater->index = inflater->left;
|
|
|
|
--inflater->index;
|
2014-07-21 19:09:01 +02:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2013-10-24 14:52:02 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
2014-04-01 18:25:44 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN:
|
|
|
|
hd_inflate_set_huffman_encoded(inflater, in);
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
|
2014-11-27 15:39:04 +01:00
|
|
|
/* Fall through */
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN:
|
|
|
|
rfin = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
|
|
|
|
if (rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!rfin) {
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("inflatehd: integer not fully decoded. current=%zu\n",
|
|
|
|
inflater->left);
|
2014-03-13 16:51:26 +01:00
|
|
|
|
|
|
|
goto almost_ok;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->huffman_encoded) {
|
2014-02-13 15:22:52 +01:00
|
|
|
nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF;
|
2016-03-12 05:23:12 +01:00
|
|
|
|
|
|
|
rv = nghttp2_rcbuf_new(&inflater->namercbuf, inflater->left * 2 + 1,
|
|
|
|
mem);
|
2013-10-20 16:59:15 +02:00
|
|
|
} else {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAME;
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = nghttp2_rcbuf_new(&inflater->namercbuf, inflater->left + 1, mem);
|
2013-08-25 10:39:29 +02:00
|
|
|
}
|
2016-03-10 17:52:55 +01:00
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_buf_wrap_init(&inflater->namebuf, inflater->namercbuf->base,
|
|
|
|
inflater->namercbuf->len);
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF:
|
2016-03-10 17:52:55 +01:00
|
|
|
rv = hd_inflate_read_huff(inflater, &inflater->namebuf, in, last);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2013-10-20 16:59:15 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: %zd bytes read\n", rv);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->left) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
*inflater->namebuf.last = '\0';
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_NEWNAME_READ_NAME:
|
2016-03-10 17:52:55 +01:00
|
|
|
rv = hd_inflate_read(inflater, &inflater->namebuf, in, last);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: %zd bytes read\n", rv);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->left) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2013-10-14 16:38:12 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
*inflater->namebuf.last = '\0';
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_CHECK_VALUELEN:
|
|
|
|
hd_inflate_set_huffman_encoded(inflater, in);
|
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUELEN;
|
|
|
|
inflater->left = 0;
|
2014-07-21 18:50:29 +02:00
|
|
|
inflater->shift = 0;
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
|
2014-11-27 15:39:04 +01:00
|
|
|
/* Fall through */
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_READ_VALUELEN:
|
|
|
|
rfin = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
|
|
|
|
if (rv < 0) {
|
2014-01-25 10:24:15 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!rfin) {
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: valuelen=%zu\n", inflater->left);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->huffman_encoded) {
|
2014-02-13 15:22:52 +01:00
|
|
|
nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUEHUFF;
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = nghttp2_rcbuf_new(&inflater->valuercbuf, inflater->left * 2 + 1,
|
|
|
|
mem);
|
2013-07-19 09:50:31 +02:00
|
|
|
} else {
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_READ_VALUE;
|
2016-03-10 17:52:55 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = nghttp2_rcbuf_new(&inflater->valuercbuf, inflater->left + 1, mem);
|
2016-03-10 17:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_buf_wrap_init(&inflater->valuebuf, inflater->valuercbuf->base,
|
|
|
|
inflater->valuercbuf->len);
|
|
|
|
|
2015-03-23 15:25:57 +01:00
|
|
|
busy = 1;
|
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HD_STATE_READ_VALUEHUFF:
|
2016-03-10 17:52:55 +01:00
|
|
|
rv = hd_inflate_read_huff(inflater, &inflater->valuebuf, in, last);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: %zd bytes read\n", rv);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->left) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
*inflater->valuebuf.last = '\0';
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = hd_inflate_commit_newname(inflater, nv_out);
|
2014-01-25 10:24:15 +01:00
|
|
|
} else {
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = hd_inflate_commit_indname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-01-25 10:24:15 +01:00
|
|
|
case NGHTTP2_HD_STATE_READ_VALUE:
|
2016-03-10 17:52:55 +01:00
|
|
|
rv = hd_inflate_read(inflater, &inflater->valuebuf, in, last);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("inflatehd: value read failure %zd: %s\n", rv,
|
|
|
|
nghttp2_strerror((int)rv));
|
2013-10-14 16:38:12 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
in += rv;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: %zd bytes read\n", rv);
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->left) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
|
2014-03-13 16:51:26 +01:00
|
|
|
goto almost_ok;
|
2014-01-25 10:24:15 +01:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2016-03-10 17:52:55 +01:00
|
|
|
*inflater->valuebuf.last = '\0';
|
2016-03-12 05:23:12 +01:00
|
|
|
inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
|
2015-03-23 15:25:57 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = hd_inflate_commit_newname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
} else {
|
2016-03-12 05:23:12 +01:00
|
|
|
rv = hd_inflate_commit_indname(inflater, nv_out);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-25 18:34:28 +02:00
|
|
|
goto fail;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_OPCODE;
|
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
|
2014-03-11 13:18:28 +01:00
|
|
|
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-01-25 10:24:15 +01:00
|
|
|
assert(in == last);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: all input bytes were processed\n");
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (in_final) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: in_final set\n");
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2015-10-24 10:27:24 +02:00
|
|
|
if (inflater->state != NGHTTP2_HD_STATE_OPCODE &&
|
|
|
|
inflater->state != NGHTTP2_HD_STATE_INFLATE_START) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: unacceptable state=%d\n", inflater->state);
|
2014-02-11 08:00:59 +01:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-02-11 08:00:59 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-01-25 10:24:15 +01:00
|
|
|
*inflate_flags |= NGHTTP2_HD_INFLATE_FINAL;
|
2013-10-12 14:49:01 +02:00
|
|
|
}
|
2014-06-16 19:17:49 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-03-13 16:51:26 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
almost_ok:
|
2015-10-24 10:27:24 +02:00
|
|
|
if (in_final) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: input ended prematurely\n");
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
rv = NGHTTP2_ERR_HEADER_COMP;
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-03-13 16:51:26 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-08-02 15:42:55 +02:00
|
|
|
return (ssize_t)(in - first);
|
2014-03-13 16:51:26 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
fail:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("inflatehd: error return %zd\n", rv);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-01-26 09:53:04 +01:00
|
|
|
inflater->ctx.bad = 1;
|
2013-07-25 18:34:28 +02:00
|
|
|
return rv;
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) {
|
2014-01-16 15:41:13 +01:00
|
|
|
hd_inflate_keep_free(inflater);
|
2015-07-21 17:11:23 +02:00
|
|
|
inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
|
2013-07-19 09:50:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_hd_inflate_new2(inflater_ptr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
|
|
|
|
nghttp2_mem *mem) {
|
2014-06-25 14:26:47 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_hd_inflater *inflater;
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
if (mem == NULL) {
|
|
|
|
mem = nghttp2_mem_default();
|
|
|
|
}
|
|
|
|
|
|
|
|
inflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_inflater));
|
2014-04-29 08:53:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflater == NULL) {
|
2014-04-30 15:16:21 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-04-29 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = nghttp2_hd_inflate_init(inflater, mem);
|
2014-06-25 14:26:47 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, inflater);
|
2014-07-17 17:21:15 +02:00
|
|
|
|
2014-06-25 14:26:47 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*inflater_ptr = inflater;
|
|
|
|
|
|
|
|
return 0;
|
2014-04-29 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = inflater->ctx.mem;
|
2014-04-29 08:53:46 +02:00
|
|
|
nghttp2_hd_inflate_free(inflater);
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, inflater);
|
2014-04-29 08:53:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:28:12 +02:00
|
|
|
int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,
|
2015-04-14 18:21:27 +02:00
|
|
|
nghttp2_nv *nv, int indexing_mode) {
|
2014-04-01 18:25:44 +02:00
|
|
|
|
2015-04-14 18:21:27 +02:00
|
|
|
return emit_indname_block(bufs, idx, nv, indexing_mode);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
|
2015-04-14 18:21:27 +02:00
|
|
|
int indexing_mode) {
|
|
|
|
return emit_newname_block(bufs, nv, indexing_mode);
|
2014-02-13 15:22:52 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return emit_table_size(bufs, table_size);
|
2013-07-19 09:50:31 +02:00
|
|
|
}
|
2014-07-22 15:38:18 +02:00
|
|
|
|
2016-10-15 11:25:13 +02:00
|
|
|
ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t initial, size_t shift, uint8_t *in,
|
|
|
|
uint8_t *last, size_t prefix) {
|
2016-10-15 11:25:13 +02:00
|
|
|
return decode_length(res, shift_ptr, fin, initial, shift, in, last, prefix);
|
2014-07-22 15:38:18 +02:00
|
|
|
}
|
2015-10-21 17:57:00 +02:00
|
|
|
|
|
|
|
static size_t hd_get_num_table_entries(nghttp2_hd_context *context) {
|
|
|
|
return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const nghttp2_nv *hd_get_table_entry(nghttp2_hd_context *context,
|
|
|
|
size_t idx) {
|
|
|
|
if (idx == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
--idx;
|
|
|
|
|
|
|
|
if (!INDEX_RANGE_VALID(context, idx)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-12 05:23:12 +01:00
|
|
|
return nghttp2_hd_table_get2(context, idx);
|
2015-10-21 17:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater) {
|
|
|
|
return hd_get_num_table_entries(&deflater->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nghttp2_nv *
|
|
|
|
nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx) {
|
|
|
|
return hd_get_table_entry(&deflater->ctx, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater) {
|
|
|
|
return deflater->ctx.hd_table_bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater) {
|
|
|
|
return deflater->ctx.hd_table_bufsize_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater) {
|
|
|
|
return hd_get_num_table_entries(&inflater->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nghttp2_nv *
|
|
|
|
nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx) {
|
|
|
|
return hd_get_table_entry(&inflater->ctx, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater) {
|
|
|
|
return inflater->ctx.hd_table_bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater) {
|
|
|
|
return inflater->ctx.hd_table_bufsize_max;
|
|
|
|
}
|