├── .clang-format ├── .clusterfuzzlite ├── Dockerfile ├── build.sh └── project.yaml ├── .github ├── dependabot.yml └── workflows │ ├── build.yml │ ├── cflite_batch.yaml │ ├── cflite_cron.yaml │ └── stale.yaml ├── .gitignore ├── .gitmodules ├── AUTHORS ├── CMakeLists.txt ├── CMakeOptions.txt ├── COPYING ├── ChangeLog ├── Makefile.am ├── NEWS ├── README ├── README.rst ├── SECURITY.md ├── cmake ├── PickyWarningsC.cmake ├── PickyWarningsCXX.cmake └── Version.cmake ├── cmakeconfig.h.in ├── configure.ac ├── doc ├── .gitignore ├── Makefile.am ├── make.bat ├── mkapiref.py └── source │ ├── .gitignore │ ├── conf.py.in │ ├── index.rst │ ├── programmers-guide.rst │ └── qpack-howto.rst ├── examples ├── .gitignore ├── CMakeLists.txt ├── Makefile.am ├── qpack.cc ├── qpack.h ├── qpack_decode.cc ├── qpack_decode.h ├── qpack_encode.cc ├── qpack_encode.h ├── template.h ├── util.cc └── util.h ├── fuzz ├── corpus │ ├── fuzz_http3serverreq │ │ └── curl │ └── fuzz_qpackdecoder │ │ └── netbsd-hq.out.256.100.1 ├── fuzz_http3serverreq.cc └── fuzz_qpackdecoder.cc ├── genchartbl.py ├── genlibtokenlookup.py ├── gennmchartbl.py ├── lib ├── CMakeLists.txt ├── Makefile.am ├── config.cmake.in ├── includes │ ├── CMakeLists.txt │ ├── Makefile.am │ └── nghttp3 │ │ ├── nghttp3.h │ │ └── version.h.in ├── libnghttp3.pc.in ├── nghttp3_balloc.c ├── nghttp3_balloc.h ├── nghttp3_buf.c ├── nghttp3_buf.h ├── nghttp3_conn.c ├── nghttp3_conn.h ├── nghttp3_conv.c ├── nghttp3_conv.h ├── nghttp3_debug.c ├── nghttp3_debug.h ├── nghttp3_err.c ├── nghttp3_err.h ├── nghttp3_frame.c ├── nghttp3_frame.h ├── nghttp3_gaptr.c ├── nghttp3_gaptr.h ├── nghttp3_http.c ├── nghttp3_http.h ├── nghttp3_idtr.c ├── nghttp3_idtr.h ├── nghttp3_ksl.c ├── nghttp3_ksl.h ├── nghttp3_macro.h ├── nghttp3_map.c ├── nghttp3_map.h ├── nghttp3_mem.c ├── nghttp3_mem.h ├── nghttp3_objalloc.c ├── nghttp3_objalloc.h ├── nghttp3_opl.c ├── nghttp3_opl.h ├── nghttp3_pq.c ├── nghttp3_pq.h ├── nghttp3_qpack.c ├── nghttp3_qpack.h ├── nghttp3_qpack_huffman.c ├── nghttp3_qpack_huffman.h ├── nghttp3_qpack_huffman_data.c ├── nghttp3_range.c ├── nghttp3_range.h ├── nghttp3_rcbuf.c ├── nghttp3_rcbuf.h ├── nghttp3_ringbuf.c ├── nghttp3_ringbuf.h ├── nghttp3_str.c ├── nghttp3_str.h ├── nghttp3_stream.c ├── nghttp3_stream.h ├── nghttp3_tnode.c ├── nghttp3_tnode.h ├── nghttp3_unreachable.c ├── nghttp3_unreachable.h ├── nghttp3_vec.c ├── nghttp3_vec.h └── nghttp3_version.c ├── m4 ├── ax_check_compile_flag.m4 └── ax_cxx_compile_stdcxx.m4 ├── makerelease.sh ├── mkhufftbl.py ├── mkstatichdtbl.py ├── qifs-check.sh ├── qifs.sh └── tests ├── .gitignore ├── CMakeLists.txt ├── Makefile.am ├── main.c ├── nghttp3_conn_test.c ├── nghttp3_conn_test.h ├── nghttp3_conv_test.c ├── nghttp3_conv_test.h ├── nghttp3_http_test.c ├── nghttp3_http_test.h ├── nghttp3_qpack_test.c ├── nghttp3_qpack_test.h ├── nghttp3_stream_test.c ├── nghttp3_stream_test.h ├── nghttp3_test_helper.c ├── nghttp3_test_helper.h ├── nghttp3_tnode_test.c └── nghttp3_tnode_test.h /.clusterfuzzlite/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gcr.io/oss-fuzz-base/base-builder:v1 2 | RUN apt-get update && apt-get install -y make autoconf automake libtool pkg-config 3 | COPY . $SRC/nghttp3 4 | WORKDIR nghttp3 5 | COPY .clusterfuzzlite/build.sh $SRC/ 6 | -------------------------------------------------------------------------------- /.clusterfuzzlite/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eu 2 | 3 | autoreconf -i 4 | ./configure --disable-dependency-tracking 5 | make -j$(nproc) 6 | 7 | $CXX $CXXFLAGS -std=c++20 -Ilib/includes -Ilib \ 8 | fuzz/fuzz_http3serverreq.cc -o $OUT/fuzz_http3serverreq \ 9 | $LIB_FUZZING_ENGINE lib/.libs/libnghttp3.a 10 | 11 | $CXX $CXXFLAGS -std=c++20 -Ilib/includes -Ilib \ 12 | fuzz/fuzz_qpackdecoder.cc -o $OUT/fuzz_qpackdecoder \ 13 | $LIB_FUZZING_ENGINE lib/.libs/libnghttp3.a 14 | 15 | zip -j $OUT/fuzz_http3serverreq_seed_corpus.zip fuzz/corpus/fuzz_http3serverreq/* 16 | zip -j $OUT/fuzz_qpackdecoder_seed_corpus.zip fuzz/corpus/fuzz_qpackdecoder/* 17 | -------------------------------------------------------------------------------- /.clusterfuzzlite/project.yaml: -------------------------------------------------------------------------------- 1 | language: c++ 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | -------------------------------------------------------------------------------- /.github/workflows/cflite_batch.yaml: -------------------------------------------------------------------------------- 1 | name: ClusterFuzzLite batch fuzzing 2 | on: 3 | schedule: 4 | - cron: '0 0/6 * * *' 5 | permissions: read-all 6 | jobs: 7 | BatchFuzzing: 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | sanitizer: 13 | - address 14 | - undefined 15 | - memory 16 | steps: 17 | - name: LLVM workaround 18 | run: | 19 | # https://github.com/actions/runner-images/issues/9491#issuecomment-1989718917 20 | # Asan in llvm 14 provided in ubuntu 22.04 is incompatible with 21 | # high-entropy ASLR in much newer kernels that GitHub runners are 22 | # using leading to random crashes: https://reviews.llvm.org/D148280 23 | sudo sysctl vm.mmap_rnd_bits=28 24 | - name: Build Fuzzers (${{ matrix.sanitizer }}) 25 | id: build 26 | uses: google/clusterfuzzlite/actions/build_fuzzers@v1 27 | with: 28 | sanitizer: ${{ matrix.sanitizer }} 29 | - name: Run Fuzzers (${{ matrix.sanitizer }}) 30 | id: run 31 | uses: google/clusterfuzzlite/actions/run_fuzzers@v1 32 | with: 33 | github-token: ${{ secrets.GITHUB_TOKEN }} 34 | fuzz-seconds: 3600 35 | mode: 'batch' 36 | sanitizer: ${{ matrix.sanitizer }} 37 | -------------------------------------------------------------------------------- /.github/workflows/cflite_cron.yaml: -------------------------------------------------------------------------------- 1 | name: ClusterFuzzLite cron tasks 2 | on: 3 | schedule: 4 | - cron: '0 0 * * *' 5 | permissions: read-all 6 | jobs: 7 | Pruning: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Build Fuzzers 11 | id: build 12 | uses: google/clusterfuzzlite/actions/build_fuzzers@v1 13 | - name: Run Fuzzers 14 | id: run 15 | uses: google/clusterfuzzlite/actions/run_fuzzers@v1 16 | with: 17 | github-token: ${{ secrets.GITHUB_TOKEN }} 18 | fuzz-seconds: 600 19 | mode: 'prune' 20 | Coverage: 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Build Fuzzers 24 | id: build 25 | uses: google/clusterfuzzlite/actions/build_fuzzers@v1 26 | with: 27 | language: c++ 28 | sanitizer: coverage 29 | - name: Run Fuzzers 30 | id: run 31 | uses: google/clusterfuzzlite/actions/run_fuzzers@v1 32 | with: 33 | github-token: ${{ secrets.GITHUB_TOKEN }} 34 | fuzz-seconds: 600 35 | mode: 'coverage' 36 | sanitizer: 'coverage' 37 | -------------------------------------------------------------------------------- /.github/workflows/stale.yaml: -------------------------------------------------------------------------------- 1 | name: 'Close stale issues' 2 | 3 | on: 4 | schedule: 5 | - cron: '30 1 * * *' 6 | 7 | permissions: 8 | issues: write 9 | 10 | jobs: 11 | stale: 12 | runs-on: ubuntu-24.04 13 | 14 | steps: 15 | - uses: actions/stale@v9 16 | with: 17 | stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.' 18 | days-before-stale: 30 19 | days-before-close: 7 20 | exempt-all-milestones: true 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # emacs backup file 2 | *~ 3 | 4 | # autotools 5 | *.la 6 | *.lo 7 | *.m4 8 | *.o 9 | *.pyc 10 | .dirstamp 11 | .deps/ 12 | .libs/ 13 | INSTALL 14 | Makefile 15 | Makefile.in 16 | autom4te.cache/ 17 | compile 18 | config.guess 19 | config.h 20 | config.h.in 21 | config.log 22 | config.status 23 | config.sub 24 | configure 25 | depcomp 26 | install-sh 27 | libtool 28 | ltmain.sh 29 | missing 30 | stamp-h1 31 | test-driver 32 | 33 | # test logs generated by `make check` 34 | *.log 35 | *.trs 36 | 37 | # autotools derivatives 38 | /lib/includes/nghttp3/version.h 39 | /lib/libnghttp3.pc 40 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests/munit"] 2 | path = tests/munit 3 | url = https://github.com/ngtcp2/munit 4 | [submodule "lib/sfparse"] 5 | path = lib/sfparse 6 | url = https://github.com/ngtcp2/sfparse 7 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Alexis La Goutte 2 | Amir Livneh 3 | Bruno S Marques 4 | Bryan Call 5 | Cheng Zhao 6 | Daniel Bevenius 7 | Daniel Stenberg 8 | Deel 9 | Dimitris Apostolou 10 | Don 11 | Don Olmstead 12 | Dusk_NM02 13 | Force Charlie 14 | James M Snell 15 | Javier Blazquez 16 | Li Xinwei 17 | Marek Ludha 18 | Nishant Nori 19 | Ondřej Koláček 20 | Peter Wu 21 | Tal Regev 22 | Tatsuhiro Tsujikawa 23 | Tim Gates 24 | Toni Uhlig 25 | Valère Plantevin 26 | Viktor Szakats 27 | Your Name 28 | lhuang04 29 | mbuhl 30 | -------------------------------------------------------------------------------- /CMakeOptions.txt: -------------------------------------------------------------------------------- 1 | # Features that can be enabled for cmake (see CMakeLists.txt) 2 | 3 | option(ENABLE_WERROR "Make compiler warnings fatal" OFF) 4 | option(ENABLE_DEBUG "Turn on debug output") 5 | option(ENABLE_ASAN "Enable AddressSanitizer (ASAN)" OFF) 6 | option(ENABLE_LIB_ONLY "Build libnghttp3 only" OFF) 7 | option(ENABLE_STATIC_LIB "Build libnghttp3 as a static library" ON) 8 | option(ENABLE_SHARED_LIB "Build libnghttp3 as a shared library" ON) 9 | option(ENABLE_STATIC_CRT "Build libnghttp3 against the MS LIBCMT[d]") 10 | option(ENABLE_POPCNT "Enable popcnt instruction" ON) 11 | cmake_dependent_option(BUILD_TESTING "Enable tests" ON "ENABLE_STATIC_LIB" OFF) 12 | 13 | # vim: ft=cmake: 14 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2019 nghttp3 contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngtcp2/nghttp3/52e35f1f72806c95a8b0bdd3ee8629fd4ef04d80/ChangeLog -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2016 ngtcp2 contributors 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | SUBDIRS = lib tests doc examples 25 | 26 | ACLOCAL_AMFLAGS = -I m4 27 | 28 | dist_doc_DATA = README.rst 29 | 30 | EXTRA_DIST = \ 31 | cmakeconfig.h.in \ 32 | CMakeLists.txt \ 33 | CMakeOptions.txt \ 34 | cmake/PickyWarningsC.cmake \ 35 | cmake/PickyWarningsCXX.cmake \ 36 | cmake/Version.cmake 37 | 38 | # Format source files using clang-format. Don't format source files 39 | # under third-party directory since we are not responsible for their 40 | # coding style. 41 | clang-format: 42 | CLANGFORMAT=`git config --get clangformat.binary`; \ 43 | test -z $${CLANGFORMAT} && CLANGFORMAT="clang-format"; \ 44 | $${CLANGFORMAT} -i lib/*.{c,h} tests/*.{c,h} lib/includes/nghttp3/*.h \ 45 | examples/*.{cc,h} fuzz/*.cc 46 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngtcp2/nghttp3/52e35f1f72806c95a8b0bdd3ee8629fd4ef04d80/NEWS -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | See README.rst 2 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | nghttp3 2 | ======= 3 | 4 | nghttp3 is an implementation of `RFC 9114 5 | `_ HTTP/3 mapping over 6 | QUIC and `RFC 9204 `_ 7 | QPACK in C. 8 | 9 | It does not depend on any particular QUIC transport implementation. 10 | 11 | Documentation 12 | ------------- 13 | 14 | `Online documentation `_ is available. 15 | 16 | Build from git 17 | --------------- 18 | 19 | .. code-block:: shell 20 | 21 | $ git clone https://github.com/ngtcp2/nghttp3 22 | $ cd nghttp3 23 | $ git submodule update --init 24 | $ autoreconf -i 25 | $ ./configure 26 | $ make -j$(nproc) check 27 | 28 | HTTP/3 29 | ------ 30 | 31 | This library implements `RFC 9114 32 | `_ HTTP/3. It does not 33 | support server push. 34 | 35 | The following extensions have been implemented: 36 | 37 | - `Extensible Prioritization Scheme for HTTP 38 | `_ 39 | - `Bootstrapping WebSockets with HTTP/3 40 | `_ 41 | 42 | It can also send and receive `SETTINGS_H3_DATAGRAM` from `HTTP 43 | Datagrams and the Capsule Protocol 44 | `_. 45 | 46 | QPACK 47 | ----- 48 | 49 | This library implements `RFC 9204 50 | `_ QPACK. It supports 51 | dynamic table. 52 | 53 | Optimizations 54 | ------------- 55 | 56 | This library optionally uses AVX2, if available, to optimize its 57 | performance. To compile with AVX2, add ``-mavx2`` to CFLAGS. Note 58 | that by default, CFLAGS is set to ``-g -O2``. When specifying CFLAGS, 59 | include them as well (e.g., ``-g -O2 -mavx2``). 60 | 61 | Examples 62 | -------- 63 | 64 | - client: https://github.com/ngtcp2/ngtcp2/blob/main/examples/client.cc 65 | - server: https://github.com/ngtcp2/ngtcp2/blob/main/examples/server.cc 66 | - curl: https://github.com/curl/curl/blob/master/lib/vquic/curl_ngtcp2.c 67 | 68 | License 69 | ------- 70 | 71 | The MIT License 72 | 73 | Copyright (c) 2019 nghttp3 contributors 74 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Process 2 | 3 | If you find a vulnerability in our software, please report it via 4 | GitHub "Private vulnerability reporting" feature at 5 | https://github.com/ngtcp2/nghttp3/security instead of submitting 6 | issues on github issue page. It is a standard practice not to 7 | disclose vulnerability information publicly until a fixed version is 8 | released, or mitigation is worked out. 9 | 10 | If we identify that the reported issue is really a vulnerability, we 11 | open a new security advisory draft using [GitHub security 12 | feature](https://github.com/ngtcp2/nghttp3/security) and discuss the 13 | mitigation and bug fixes there. The fixes are committed to the 14 | private repository. 15 | 16 | We write the security advisory and get CVE number from GitHub 17 | privately. We also discuss the disclosure date to the public. 18 | 19 | We make a new release with the fix at the same time when the 20 | vulnerability is disclosed to public. 21 | 22 | At least 7 days before the public disclosure date, we open a new issue 23 | on [nghttp3 issue tracker](https://github.com/ngtcp2/nghttp3/issues) 24 | which notifies that the upcoming release will have a security fix. 25 | The `SECURITY` label is attached to this kind of issue. The issue is 26 | not opened if a vulnerability is already disclosed, and it is publicly 27 | known that nghttp3 is affected by that. The issue might not be 28 | created if CVE only affects the latest version released very recently. 29 | In this case, we would like to release a fix without waiting a week to 30 | avoid widespread use of the version. 31 | 32 | Before few hours of new release, we merge the fixes to the master 33 | branch (and/or a release branch if necessary) and make a new release. 34 | Security advisory is disclosed on GitHub. 35 | -------------------------------------------------------------------------------- /cmake/Version.cmake: -------------------------------------------------------------------------------- 1 | # Converts a version such as 1.2.255 to 0x0102ff 2 | function(HexVersion version_hex_var major minor patch) 3 | math(EXPR version_dec "${major} * 256 * 256 + ${minor} * 256 + ${patch}") 4 | set(version_hex "0x") 5 | foreach(i RANGE 5 0 -1) 6 | math(EXPR num "(${version_dec} >> (4 * ${i})) & 15") 7 | string(SUBSTRING "0123456789abcdef" ${num} 1 num_hex) 8 | set(version_hex "${version_hex}${num_hex}") 9 | endforeach() 10 | set(${version_hex_var} "${version_hex}" PARENT_SCOPE) 11 | endfunction() 12 | -------------------------------------------------------------------------------- /cmakeconfig.h.in: -------------------------------------------------------------------------------- 1 | 2 | /* Define to `int' if does not define. */ 3 | #cmakedefine ssize_t @ssize_t@ 4 | 5 | /* Define to 1 to enable debug output. */ 6 | #cmakedefine DEBUGBUILD 1 7 | 8 | /* Define to 1 if you have the header file. */ 9 | #cmakedefine HAVE_ARPA_INET_H 1 10 | 11 | /* Define to 1 if you have the header file. */ 12 | #cmakedefine HAVE_UNISTD_H 1 13 | 14 | /* Define to 1 if you have the header file. */ 15 | #cmakedefine HAVE_SYS_ENDIAN_H 1 16 | 17 | /* Define to 1 if you have the header file. */ 18 | #cmakedefine HAVE_ENDIAN_H 1 19 | 20 | /* Define to 1 if you have the header file. */ 21 | #cmakedefine HAVE_BYTESWAP_H 1 22 | 23 | /* Define to 1 if you have the `be64toh' function, otherwise 0. */ 24 | #cmakedefine01 HAVE_DECL_BE64TOH 25 | 26 | /* Define to 1 if you have the `bswap_64' function, otherwise 0. */ 27 | #cmakedefine01 HAVE_DECL_BSWAP_64 28 | 29 | /* Define WORDS_BIGENDIAN to 1 if target architecture is big 30 | endian. */ 31 | #cmakedefine WORDS_BIGENDIAN 1 32 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /doc/Makefile.am: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | 3 | # Copyright (c) 2020 nghttp3 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | # Minimal makefile for Sphinx documentation 25 | # 26 | 27 | # You can set these variables from the command line, and also 28 | # from the environment for the first two. 29 | SPHINXOPTS ?= 30 | SPHINXBUILD ?= sphinx-build 31 | SOURCEDIR = source 32 | BUILDDIR = build 33 | 34 | # Put it first so that "make" without argument is like "make help". 35 | help: 36 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 37 | 38 | .PHONY: help html apiref 39 | 40 | apiref: mkapiref.py \ 41 | $(top_builddir)/lib/includes/nghttp3/version.h \ 42 | $(top_srcdir)/lib/includes/nghttp3/nghttp3.h 43 | $(top_srcdir)/doc/mkapiref.py \ 44 | source/apiref.rst source/macros.rst source/enums.rst source/types.rst \ 45 | source $(wordlist 2, $(words $^), $^) 46 | 47 | html-local: apiref 48 | @$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 49 | 50 | clean-local: 51 | -rm -rf "$(BUILDDIR)" 52 | -------------------------------------------------------------------------------- /doc/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | 13 | if "%1" == "" goto help 14 | 15 | %SPHINXBUILD% >NUL 2>NUL 16 | if errorlevel 9009 ( 17 | echo. 18 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 19 | echo.installed, then set the SPHINXBUILD environment variable to point 20 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 21 | echo.may add the Sphinx directory to PATH. 22 | echo. 23 | echo.If you don't have Sphinx installed, grab it from 24 | echo.http://sphinx-doc.org/ 25 | exit /b 1 26 | ) 27 | 28 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 29 | goto end 30 | 31 | :help 32 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% 33 | 34 | :end 35 | popd 36 | -------------------------------------------------------------------------------- /doc/source/.gitignore: -------------------------------------------------------------------------------- 1 | /conf.py 2 | /apiref.rst 3 | /enums.rst 4 | /macros.rst 5 | /types.rst 6 | -------------------------------------------------------------------------------- /doc/source/conf.py.in: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | 3 | # Copyright (c) 2020 nghttp3 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | # Configuration file for the Sphinx documentation builder. 25 | # 26 | # This file only contains a selection of the most common options. For a full 27 | # list see the documentation: 28 | # https://www.sphinx-doc.org/en/master/usage/configuration.html 29 | 30 | # -- Path setup -------------------------------------------------------------- 31 | 32 | # If extensions (or modules to document with autodoc) are in another directory, 33 | # add these directories to sys.path here. If the directory is relative to the 34 | # documentation root, use os.path.abspath to make it absolute, like shown here. 35 | # 36 | # import os 37 | # import sys 38 | # sys.path.insert(0, os.path.abspath('.')) 39 | 40 | 41 | # -- Project information ----------------------------------------------------- 42 | 43 | project = 'nghttp3' 44 | copyright = '2020, nghttp3 contributors' 45 | author = 'nghttp3 contributors' 46 | 47 | 48 | # -- General configuration --------------------------------------------------- 49 | 50 | # Add any Sphinx extension module names here, as strings. They can be 51 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 52 | # ones. 53 | extensions = [ 54 | ] 55 | 56 | # Add any paths that contain templates here, relative to this directory. 57 | templates_path = ['_templates'] 58 | 59 | # List of patterns, relative to source directory, that match files and 60 | # directories to ignore when looking for source files. 61 | # This pattern also affects html_static_path and html_extra_path. 62 | exclude_patterns = [] 63 | 64 | 65 | # -- Options for HTML output ------------------------------------------------- 66 | 67 | # The theme to use for HTML and HTML Help pages. See the documentation for 68 | # a list of builtin themes. 69 | # 70 | html_theme = 'sphinx_rtd_theme' 71 | 72 | # Add any paths that contain custom static files (such as style sheets) here, 73 | # relative to this directory. They are copied after the builtin static files, 74 | # so a file named "default.css" will overwrite the builtin "default.css". 75 | html_static_path = ['_static'] 76 | 77 | # The reST default role (used for this markup: `text`) to use for all documents. 78 | default_role = 'c:func' 79 | primary_domain = 'c' 80 | 81 | # manpage URL pattern 82 | manpages_url = 'https://man7.org/linux/man-pages/man{section}/{page}.{section}.html' 83 | 84 | # The default language to highlight source code in. 85 | highlight_language = 'c' 86 | 87 | # The version info for the project you're documenting, acts as replacement for 88 | # |version| and |release|, also used in various other places throughout the 89 | # built documents. 90 | # 91 | # The short X.Y version. 92 | version = '@PACKAGE_VERSION@' 93 | # The full version, including alpha/beta/rc tags. 94 | release = '@PACKAGE_VERSION@' 95 | -------------------------------------------------------------------------------- /doc/source/index.rst: -------------------------------------------------------------------------------- 1 | .. nghttp3 documentation master file, created by 2 | sphinx-quickstart on Mon Nov 30 22:15:12 2020. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to nghttp3's documentation! 7 | =================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 1 11 | :caption: Contents: 12 | 13 | programmers-guide 14 | qpack-howto 15 | apiref 16 | 17 | Indices and tables 18 | ================== 19 | 20 | * :ref:`genindex` 21 | * :ref:`modindex` 22 | * :ref:`search` 23 | -------------------------------------------------------------------------------- /doc/source/qpack-howto.rst: -------------------------------------------------------------------------------- 1 | QPACK How-To 2 | ============ 3 | 4 | Using QPACK encoder 5 | ------------------- 6 | 7 | Firstly, create QPACK encoder by calling `nghttp3_qpack_encoder_new`. 8 | It requires *hard_max_dtable_size* parameter. When in doubt, pass 9 | 4096 for this tutorial. Optionally, call 10 | `nghttp3_qpack_encoder_set_max_dtable_capacity` to set the maximum 11 | size of dynamic table. You can also call 12 | `nghttp3_qpack_encoder_set_max_blocked_streams` to set the maximum 13 | number of streams that can be blocked. 14 | 15 | In order to encode HTTP header fields, they must be stored in an array 16 | of :type:`nghttp3_nv`. Then call `nghttp3_qpack_encoder_encode`. It 17 | writes 3 buffers; *pbuf*, *rbuf*, and *ebuf*. They are a header block 18 | prefix, request stream, and encoder stream respectively. A header 19 | block prefix and request stream must be sent in this order to a stream 20 | denoted by *stream_id* passed to the function. Encoder stream must be 21 | sent to the encoder stream you setup. 22 | 23 | In order to read decoder stream, call 24 | `nghttp3_qpack_encoder_read_decoder`. 25 | 26 | Once QPACK encoder is no longer used, call `nghttp3_qpack_encoder_del` 27 | to free up memory allocated for it. 28 | 29 | Using QPACK decoder 30 | ------------------- 31 | 32 | `nghttp3_qpack_decoder_new` will create new QPACK decoder. It 33 | requires *hard_max_dtable_size* and *max_blocked* parameters. When in 34 | doubt, pass 4096 and 0 respectively for this tutorial. 35 | 36 | In order to read encoder stream, call 37 | `nghttp3_qpack_decoder_read_encoder`. This might update dynamic 38 | table, but does not emit any header fields. 39 | 40 | In order to read request stream, call 41 | `nghttp3_qpack_decoder_read_request`. This is the function to emit 42 | header fields. *sctx* stores a per-stream decoder state and must be 43 | created by `nghttp3_qpack_stream_context_new`. It identifies a single 44 | encoded header block in a particular request stream. *fin* must be 45 | nonzero if and only if a passed data contains the last part of encoded 46 | header block. 47 | 48 | The scope of :type:`nghttp3_qpack_stream_context` is per header block, 49 | but `nghttp3_qpack_stream_context_reset` resets its state and can be 50 | reused for an another header block in the same stream. In general, 51 | you can reset it when you see that 52 | :macro:`NGHTTP3_QPACK_DECODE_FLAG_FINAL` is set in *\*pflags*. When 53 | :type:`nghttp3_qpack_stream_context` is no longer necessary, call 54 | `nghttp3_qpack_stream_context_del` to free up its resource. 55 | 56 | `nghttp3_qpack_decoder_read_request` succeeds, *\*pflags* is assigned. 57 | If it has :macro:`NGHTTP3_QPACK_DECODE_FLAG_EMIT` set, a header field 58 | is emitted and stored in the buffer pointed by *nv*. If *\*pflags* 59 | has :macro:`NGHTTP3_QPACK_DECODE_FLAG_FINAL` set, all header fields 60 | have been successfully decoded. If *\*pflags* has 61 | :macro:`NGHTTP3_QPACK_DECODE_FLAG_BLOCKED` set, decoding is blocked 62 | due to required insert count, which means that more data must be read 63 | by `nghttp3_qpack_decoder_read_encoder`. 64 | 65 | `nghttp3_qpack_decoder_read_request` returns the number of bytes read. 66 | When a header field is emitted, it might read data partially. 67 | Application has to call the function repeatedly by adjusting the 68 | pointer to data and its length until the function consumes all data or 69 | :macro:`NGHTTP3_QPACK_DECODE_FLAG_BLOCKED` is set in *\*pflags*. 70 | 71 | If *nv* is assigned, its :member:`nv->name ` 72 | and :member:`nv->value ` are reference counted 73 | and already incremented by 1. If application finishes processing 74 | these values, it must call `nghttp3_rcbuf_decref(nv->name) 75 | ` and `nghttp3_rcbuf_decref(nv->value) 76 | `. 77 | 78 | If an application has no interest to decode header fields for a 79 | particular stream, call `nghttp3_qpack_decoder_cancel_stream`. 80 | 81 | In order to tell decoding state to an encoder, QPACK decoder has to 82 | write decoder stream by calling `nghttp3_qpack_decoder_write_decoder`. 83 | 84 | Once QPACK decoder is no longer used, call `nghttp3_qpack_decoder_del` 85 | to free up memory allocated for it. 86 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | /qpack 2 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ngtcp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2017 ngtcp2 contributors 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | if(ENABLE_EXAMPLES) 26 | include_directories( 27 | ${CMAKE_SOURCE_DIR}/lib/includes 28 | ${CMAKE_BINARY_DIR}/lib/includes 29 | ) 30 | 31 | if(ENABLE_SHARED_LIB) 32 | link_libraries( 33 | nghttp3 34 | ) 35 | else() 36 | link_libraries( 37 | nghttp3_static 38 | ) 39 | endif() 40 | 41 | set(qpack_SOURCES 42 | qpack.cc 43 | qpack_encode.cc 44 | qpack_decode.cc 45 | util.cc 46 | ) 47 | 48 | add_executable(qpack ${qpack_SOURCES}) 49 | set_target_properties(qpack PROPERTIES 50 | COMPILE_FLAGS "${WARNCXXFLAGS}" 51 | CXX_STANDARD 17 52 | CXX_STANDARD_REQUIRED ON 53 | ) 54 | 55 | # TODO prevent qpack example from being installed? 56 | endif() 57 | -------------------------------------------------------------------------------- /examples/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2017 ngtcp2 contributors 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | EXTRA_DIST = CMakeLists.txt 25 | 26 | if ENABLE_EXAMPLES 27 | 28 | AM_CFLAGS = $(WARNCFLAGS) $(DEBUGCFLAGS) 29 | AM_CXXFLAGS = $(WARNCXXFLAGS) $(DEBUGCFLAGS) 30 | AM_CPPFLAGS = \ 31 | -I$(top_srcdir)/lib/includes \ 32 | -I$(top_builddir)/lib/includes \ 33 | @DEFS@ 34 | AM_LDFLAGS = -no-install 35 | LDADD = $(top_builddir)/lib/libnghttp3.la 36 | 37 | noinst_PROGRAMS = qpack 38 | 39 | qpack_SOURCES = \ 40 | qpack.cc qpack.h \ 41 | qpack_encode.cc qpack_encode.h \ 42 | qpack_decode.cc qpack_decode.h \ 43 | template.h \ 44 | util.cc util.h 45 | 46 | endif # ENABLE_EXAMPLES 47 | -------------------------------------------------------------------------------- /examples/qpack.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "qpack.h" 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #include "qpack_encode.h" 34 | #include "qpack_decode.h" 35 | 36 | namespace nghttp3 { 37 | 38 | Config config{}; 39 | 40 | namespace { 41 | void print_usage() { 42 | std::cerr << "Usage: qpack [OPTIONS] " 43 | << std::endl; 44 | } 45 | } // namespace 46 | 47 | namespace { 48 | void print_help() { 49 | print_usage(); 50 | 51 | std::cerr << R"( 52 | "encode" or "decode" 53 | Path to an input file 54 | Path to an output file 55 | Options: 56 | -h, --help Display this help and exit. 57 | -m, --max-blocked= 58 | The maximum number of streams which are permitted to be blocked. 59 | -s, --max-dtable-size= 60 | The maximum size of dynamic table. 61 | -a, --immediate-ack 62 | Turn on immediate acknowledgement. 63 | )"; 64 | } 65 | } // namespace 66 | 67 | int main(int argc, char **argv) { 68 | for (;;) { 69 | static int flag = 0; 70 | (void)flag; 71 | constexpr static option long_opts[] = { 72 | {"help", no_argument, nullptr, 'h'}, 73 | {"max-blocked", required_argument, nullptr, 'm'}, 74 | {"max-dtable-size", required_argument, nullptr, 's'}, 75 | {"immediate-ack", no_argument, nullptr, 'a'}, 76 | {nullptr, 0, nullptr, 0}, 77 | }; 78 | 79 | auto optidx = 0; 80 | auto c = getopt_long(argc, argv, "hm:s:a", long_opts, &optidx); 81 | if (c == -1) { 82 | break; 83 | } 84 | switch (c) { 85 | case 'h': 86 | // --help 87 | print_help(); 88 | exit(EXIT_SUCCESS); 89 | case 'm': { 90 | // --max-blocked 91 | config.max_blocked = strtoul(optarg, nullptr, 10); 92 | break; 93 | } 94 | case 's': { 95 | // --max-dtable-size 96 | config.max_dtable_size = strtoul(optarg, nullptr, 10); 97 | break; 98 | } 99 | case 'a': 100 | // --immediate-ack 101 | config.immediate_ack = true; 102 | break; 103 | case '?': 104 | print_usage(); 105 | exit(EXIT_FAILURE); 106 | case 0: 107 | break; 108 | default: 109 | break; 110 | }; 111 | } 112 | 113 | if (argc - optind < 3) { 114 | std::cerr << "Too few arguments" << std::endl; 115 | print_usage(); 116 | exit(EXIT_FAILURE); 117 | } 118 | 119 | auto command = std::string_view(argv[optind++]); 120 | auto infile = std::string_view(argv[optind++]); 121 | auto outfile = std::string_view(argv[optind++]); 122 | 123 | int rv; 124 | if (command == "encode") { 125 | rv = encode(outfile, infile); 126 | } else if (command == "decode") { 127 | rv = decode(outfile, infile); 128 | } else { 129 | std::cerr << "Unrecognized command: " << command << std::endl; 130 | print_usage(); 131 | exit(EXIT_FAILURE); 132 | } 133 | 134 | if (rv != 0) { 135 | exit(EXIT_FAILURE); 136 | } 137 | 138 | return 0; 139 | } 140 | 141 | } // namespace nghttp3 142 | 143 | int main(int argc, char **argv) { return nghttp3::main(argc, argv); } 144 | -------------------------------------------------------------------------------- /examples/qpack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef QPACK_H 26 | #define QPACK_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // defined(HAVE_CONFIG_H) 31 | 32 | #include 33 | 34 | namespace nghttp3 { 35 | 36 | struct Config { 37 | size_t max_blocked; 38 | size_t max_dtable_size; 39 | bool immediate_ack; 40 | }; 41 | 42 | } // namespace nghttp3 43 | 44 | #endif // !defined(QPACK_H) 45 | -------------------------------------------------------------------------------- /examples/qpack_decode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef QPACK_DECODE_H 26 | #define QPACK_DECODE_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // defined(HAVE_CONFIG_H) 31 | 32 | #include 33 | 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | namespace nghttp3 { 42 | struct Request { 43 | Request(int64_t stream_id, const nghttp3_buf *buf); 44 | ~Request(); 45 | 46 | nghttp3_buf buf; 47 | nghttp3_qpack_stream_context *sctx; 48 | int64_t stream_id; 49 | }; 50 | } // namespace nghttp3 51 | 52 | namespace std { 53 | template <> struct greater> { 54 | bool operator()(const std::shared_ptr &lhs, 55 | const std::shared_ptr &rhs) const { 56 | return nghttp3_qpack_stream_context_get_ricnt(lhs->sctx) > 57 | nghttp3_qpack_stream_context_get_ricnt(rhs->sctx); 58 | } 59 | }; 60 | } // namespace std 61 | 62 | namespace nghttp3 { 63 | 64 | using Headers = std::vector>; 65 | 66 | class Decoder { 67 | public: 68 | Decoder(size_t max_dtable_size, size_t max_blocked); 69 | ~Decoder(); 70 | 71 | int init(); 72 | int read_encoder(nghttp3_buf *buf); 73 | std::tuple read_request(nghttp3_buf *buf, int64_t stream_id); 74 | std::tuple read_request(Request &req); 75 | std::tuple process_blocked(); 76 | size_t get_num_blocked() const; 77 | 78 | private: 79 | const nghttp3_mem *mem_; 80 | nghttp3_qpack_decoder *dec_; 81 | std::priority_queue, 82 | std::vector>, 83 | std::greater>> 84 | blocked_reqs_; 85 | size_t max_dtable_size_; 86 | size_t max_blocked_; 87 | }; 88 | 89 | int decode(const std::string_view &outfile, const std::string_view &infile); 90 | 91 | } // namespace nghttp3 92 | 93 | #endif // !defined(QPACK_DECODE_H) 94 | -------------------------------------------------------------------------------- /examples/qpack_encode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef QPACK_ENCODE_H 26 | #define QPACK_ENCODE_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // defined(HAVE_CONFIG_H) 31 | 32 | #include 33 | 34 | #include 35 | 36 | namespace nghttp3 { 37 | 38 | class Encoder { 39 | public: 40 | Encoder(size_t max_dtable_size, size_t max_blocked, bool immediate_ack); 41 | ~Encoder(); 42 | 43 | int init(); 44 | int encode(nghttp3_buf *pbuf, nghttp3_buf *rbuf, nghttp3_buf *ebuf, 45 | int64_t stream_id, const nghttp3_nv *nva, size_t len); 46 | 47 | private: 48 | const nghttp3_mem *mem_; 49 | nghttp3_qpack_encoder *enc_; 50 | size_t max_dtable_size_; 51 | size_t max_blocked_; 52 | bool immediate_ack_; 53 | }; 54 | 55 | int encode(const std::string_view &outfile, const std::string_view &infile); 56 | 57 | } // namespace nghttp3 58 | 59 | #endif // !defined(QPACK_ENCODE_H) 60 | -------------------------------------------------------------------------------- /examples/template.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2015 ngttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifndef TEMPLATE_H 28 | #define TEMPLATE_H 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace nghttp3 { 35 | 36 | // inspired by , but our 37 | // template can take functions returning other than void. 38 | template struct Defer { 39 | Defer(F &&f, T &&...t) 40 | : f(std::bind(std::forward(f), std::forward(t)...)) {} 41 | Defer(Defer &&o) noexcept : f(std::move(o.f)) {} 42 | ~Defer() { f(); } 43 | 44 | using ResultType = std::invoke_result_t; 45 | std::function f; 46 | }; 47 | 48 | template Defer defer(F &&f, T &&...t) { 49 | return Defer(std::forward(f), std::forward(t)...); 50 | } 51 | 52 | template constexpr size_t array_size(T (&)[N]) { 53 | return N; 54 | } 55 | 56 | template constexpr size_t str_size(T (&)[N]) { 57 | return N - 1; 58 | } 59 | 60 | // User-defined literals for K, M, and G (powers of 1024) 61 | 62 | constexpr unsigned long long operator"" _k(unsigned long long k) { 63 | return k * 1024; 64 | } 65 | 66 | constexpr unsigned long long operator"" _m(unsigned long long m) { 67 | return m * 1024 * 1024; 68 | } 69 | 70 | constexpr unsigned long long operator"" _g(unsigned long long g) { 71 | return g * 1024 * 1024 * 1024; 72 | } 73 | 74 | } // namespace nghttp3 75 | 76 | #endif // !defined(TEMPLATE_H) 77 | -------------------------------------------------------------------------------- /examples/util.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "util.h" 26 | 27 | namespace nghttp3 {} // namespace nghttp3 28 | -------------------------------------------------------------------------------- /examples/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef UTIL_H 26 | #define UTIL_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // defined(HAVE_CONFIG_H) 31 | 32 | #include 33 | 34 | #ifdef HAVE_ARPA_INET_H 35 | # include 36 | #endif // defined(HAVE_ARPA_INET_H) 37 | 38 | #ifdef HAVE_NETINET_IN_H 39 | # include 40 | #endif // defined(HAVE_NETINET_IN_H) 41 | 42 | #ifdef HAVE_ENDIAN_H 43 | # include 44 | #endif // defined(HAVE_ENDIAN_H)x 45 | 46 | #ifdef HAVE_SYS_ENDIAN_H 47 | # include 48 | #endif // defined(HAVE_SYS_ENDIAN_H) 49 | 50 | namespace nghttp3 { 51 | 52 | #if HAVE_DECL_BE64TOH 53 | # define nghttp3_ntohl64(N) be64toh(N) 54 | # define nghttp3_htonl64(N) htobe64(N) 55 | #else // !HAVE_DECL_BE64TOH 56 | # ifdef WORDS_BIGENDIAN 57 | # define nghttp3_ntohl64(N) (N) 58 | # define nghttp3_htonl64(N) (N) 59 | # else // !defined(WORDS_BIGENDIAN) 60 | # define nghttp3_bswap64(N) \ 61 | ((uint64_t)(ntohl((uint32_t)(N))) << 32 | ntohl((uint32_t)((N) >> 32))) 62 | # define nghttp3_ntohl64(N) nghttp3_bswap64(N) 63 | # define nghttp3_htonl64(N) nghttp3_bswap64(N) 64 | # endif // !defined(WORDS_BIGENDIAN) 65 | #endif // !HAVE_DECL_BE64TOH 66 | 67 | } // namespace nghttp3 68 | 69 | #endif // !defined(UTIL_H) 70 | -------------------------------------------------------------------------------- /fuzz/corpus/fuzz_http3serverreq/curl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngtcp2/nghttp3/52e35f1f72806c95a8b0bdd3ee8629fd4ef04d80/fuzz/corpus/fuzz_http3serverreq/curl -------------------------------------------------------------------------------- /fuzz/corpus/fuzz_qpackdecoder/netbsd-hq.out.256.100.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngtcp2/nghttp3/52e35f1f72806c95a8b0bdd3ee8629fd4ef04d80/fuzz/corpus/fuzz_qpackdecoder/netbsd-hq.out.256.100.1 -------------------------------------------------------------------------------- /genchartbl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import sys 3 | import string 4 | 5 | def name(i): 6 | if i < 0x21: 7 | return \ 8 | ['NUL ', 'SOH ', 'STX ', 'ETX ', 'EOT ', 'ENQ ', 'ACK ', 'BEL ', 9 | 'BS ', 'HT ', 'LF ', 'VT ', 'FF ', 'CR ', 'SO ', 'SI ', 10 | 'DLE ', 'DC1 ', 'DC2 ', 'DC3 ', 'DC4 ', 'NAK ', 'SYN ', 'ETB ', 11 | 'CAN ', 'EM ', 'SUB ', 'ESC ', 'FS ', 'GS ', 'RS ', 'US ', 12 | 'SPC '][i] 13 | if i == 0x7f: 14 | return 'DEL ' 15 | 16 | def gentbl(tblname, pred): 17 | sys.stdout.write('''\ 18 | /* Generated by genchartbl.py */ 19 | static const int {}[] = {{ 20 | '''.format(tblname)) 21 | 22 | for i in range(256): 23 | if pred(chr(i)): 24 | v = 1 25 | else: 26 | v = 0 27 | 28 | if 0x21 <= i and i < 0x7f: 29 | sys.stdout.write('{} /* {} */, '.format(v, chr(i))) 30 | elif 0x80 <= i: 31 | sys.stdout.write('{} /* {} */, '.format(v, hex(i))) 32 | else: 33 | sys.stdout.write('{} /* {} */, '.format(v, name(i))) 34 | if (i + 1)%4 == 0: 35 | sys.stdout.write('\n') 36 | 37 | sys.stdout.write('};\n') 38 | 39 | def sf_key(): 40 | gentbl('SF_KEY_CHARS', lambda c: c in string.ascii_lowercase or 41 | c in string.digits or c in '_-.*') 42 | 43 | def sf_dquote(): 44 | gentbl('SF_DQUOTE_CHARS', lambda c: (0x20 <= ord(c) and ord(c) <= 0x21) or 45 | (0x23 <= ord(c) and ord(c) <= 0x5b) or 46 | (0x5d <= ord(c) and ord(c) <= 0x7e)) 47 | 48 | def sf_token(): 49 | gentbl('SF_TOKEN_CHARS', lambda c: c in "!#$%&'*+-.^_`|~:/" or 50 | c in string.digits or c in string.ascii_letters) 51 | 52 | def sf_byteseq(): 53 | gentbl('SF_BYTESEQ_CHARS', lambda c: c in string.ascii_letters or 54 | c in string.digits or c in '+/=') 55 | 56 | sf_key() 57 | sys.stdout.write('\n') 58 | 59 | sf_dquote() 60 | sys.stdout.write('\n') 61 | 62 | sf_token() 63 | sys.stdout.write('\n') 64 | 65 | sf_byteseq() 66 | sys.stdout.write('\n') 67 | -------------------------------------------------------------------------------- /gennmchartbl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import sys 3 | 4 | def name(i): 5 | if i < 0x21: 6 | return \ 7 | ['NUL ', 'SOH ', 'STX ', 'ETX ', 'EOT ', 'ENQ ', 'ACK ', 'BEL ', 8 | 'BS ', 'HT ', 'LF ', 'VT ', 'FF ', 'CR ', 'SO ', 'SI ', 9 | 'DLE ', 'DC1 ', 'DC2 ', 'DC3 ', 'DC4 ', 'NAK ', 'SYN ', 'ETB ', 10 | 'CAN ', 'EM ', 'SUB ', 'ESC ', 'FS ', 'GS ', 'RS ', 'US ', 11 | 'SPC '][i] 12 | elif i == 0x7f: 13 | return 'DEL ' 14 | 15 | for i in range(256): 16 | if chr(i) in ["!" , "#" , "$" , "%" , "&" , "'" , "*", 17 | "+" , "-" , "." , "^" , "_" , "`" , "|" , "~"] or \ 18 | ('0' <= chr(i) and chr(i) <= '9') or \ 19 | ('a' <= chr(i) and chr(i) <= 'z'): 20 | sys.stdout.write('1 /* {} */, '.format(chr(i))) 21 | elif ('A' <= chr(i) and chr(i) <= 'Z'): 22 | sys.stdout.write('-1 /* {} */, '.format(chr(i))) 23 | elif (0x21 <= i and i < 0x7f): 24 | sys.stdout.write('0 /* {} */, '.format(chr(i))) 25 | elif 0x80 <= i: 26 | sys.stdout.write('0 /* {} */, '.format(hex(i))) 27 | else: 28 | sys.stdout.write('0 /* {} */, '.format(name(i))) 29 | if (i + 1)%4 == 0: 30 | sys.stdout.write('\n') 31 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 4 | # Copyright (c) 2016 ngtcp2 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | add_subdirectory(includes) 26 | 27 | include_directories( 28 | "${CMAKE_CURRENT_SOURCE_DIR}/includes" 29 | "${CMAKE_CURRENT_BINARY_DIR}/includes" 30 | ) 31 | 32 | add_definitions(-DBUILDING_NGHTTP3) 33 | 34 | set(nghttp3_SOURCES 35 | nghttp3_rcbuf.c 36 | nghttp3_mem.c 37 | nghttp3_str.c 38 | nghttp3_conv.c 39 | nghttp3_buf.c 40 | nghttp3_ringbuf.c 41 | nghttp3_pq.c 42 | nghttp3_map.c 43 | nghttp3_ksl.c 44 | nghttp3_qpack.c 45 | nghttp3_qpack_huffman.c 46 | nghttp3_qpack_huffman_data.c 47 | nghttp3_err.c 48 | nghttp3_debug.c 49 | nghttp3_conn.c 50 | nghttp3_stream.c 51 | nghttp3_frame.c 52 | nghttp3_tnode.c 53 | nghttp3_vec.c 54 | nghttp3_gaptr.c 55 | nghttp3_idtr.c 56 | nghttp3_range.c 57 | nghttp3_http.c 58 | nghttp3_version.c 59 | nghttp3_balloc.c 60 | nghttp3_opl.c 61 | nghttp3_objalloc.c 62 | nghttp3_unreachable.c 63 | sfparse/sfparse.c 64 | ) 65 | 66 | set(NGHTTP3_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") 67 | set(NGHTTP3_VERSION_CONFIG "${NGHTTP3_GENERATED_DIR}/${PROJECT_NAME}ConfigVersion.cmake") 68 | set(NGHTTP3_PROJECT_CONFIG "${NGHTTP3_GENERATED_DIR}/${PROJECT_NAME}Config.cmake") 69 | set(NGHTTP3_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets") 70 | set(NGHTTP3_CONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}") 71 | set(NGHTTP3_NAMESPACE "${PROJECT_NAME}::") 72 | set(NGHTTP3_VERSION ${PROJECT_VERSION}) 73 | 74 | include(CMakePackageConfigHelpers) 75 | write_basic_package_version_file( 76 | "${NGHTTP3_VERSION_CONFIG}" VERSION ${NGHTTP3_VERSION} COMPATIBILITY SameMajorVersion 77 | ) 78 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in" "${NGHTTP3_PROJECT_CONFIG}" @ONLY) 79 | 80 | # Install cmake config files 81 | install( 82 | FILES "${NGHTTP3_PROJECT_CONFIG}" "${NGHTTP3_VERSION_CONFIG}" 83 | DESTINATION "${NGHTTP3_CONFIG_INSTALL_DIR}") 84 | 85 | # Public shared library 86 | if(ENABLE_SHARED_LIB) 87 | add_library(nghttp3 SHARED ${nghttp3_SOURCES}) 88 | set_target_properties(nghttp3 PROPERTIES 89 | COMPILE_FLAGS "${WARNCFLAGS}" 90 | VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION} 91 | C_VISIBILITY_PRESET hidden 92 | ) 93 | 94 | target_include_directories(nghttp3 INTERFACE $) 95 | 96 | install(TARGETS nghttp3 97 | EXPORT ${NGHTTP3_TARGETS_EXPORT_NAME} 98 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 99 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 100 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") 101 | endif() 102 | 103 | if(ENABLE_STATIC_LIB) 104 | # Public static library 105 | add_library(nghttp3_static STATIC ${nghttp3_SOURCES}) 106 | set_target_properties(nghttp3_static PROPERTIES 107 | COMPILE_FLAGS "${WARNCFLAGS}" 108 | VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION} 109 | ARCHIVE_OUTPUT_NAME nghttp3${STATIC_LIB_SUFFIX} 110 | ) 111 | 112 | target_include_directories(nghttp3_static INTERFACE $) 113 | 114 | target_compile_definitions(nghttp3_static PUBLIC "-DNGHTTP3_STATICLIB") 115 | 116 | install(TARGETS nghttp3_static 117 | EXPORT ${NGHTTP3_TARGETS_EXPORT_NAME} 118 | DESTINATION "${CMAKE_INSTALL_LIBDIR}") 119 | endif() 120 | 121 | install( 122 | EXPORT "${NGHTTP3_TARGETS_EXPORT_NAME}" 123 | NAMESPACE "${NGHTTP3_NAMESPACE}" 124 | DESTINATION "${NGHTTP3_CONFIG_INSTALL_DIR}") 125 | 126 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libnghttp3.pc" 127 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 128 | -------------------------------------------------------------------------------- /lib/Makefile.am: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 4 | # Copyright (c) 2016 ngtcp2 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | SUBDIRS = includes 25 | 26 | EXTRA_DIST = CMakeLists.txt sfparse/COPYING config.cmake.in 27 | 28 | AM_CFLAGS = $(WARNCFLAGS) $(DEBUGCFLAGS) $(EXTRACFLAG) 29 | AM_CPPFLAGS = -I$(srcdir)/includes -I$(builddir)/includes -DBUILDING_NGHTTP3 30 | 31 | pkgconfigdir = $(libdir)/pkgconfig 32 | pkgconfig_DATA = libnghttp3.pc 33 | DISTCLEANFILES = $(pkgconfig_DATA) 34 | 35 | lib_LTLIBRARIES = libnghttp3.la 36 | 37 | OBJECTS = \ 38 | nghttp3_rcbuf.c \ 39 | nghttp3_mem.c \ 40 | nghttp3_str.c \ 41 | nghttp3_conv.c \ 42 | nghttp3_buf.c \ 43 | nghttp3_ringbuf.c \ 44 | nghttp3_pq.c \ 45 | nghttp3_map.c \ 46 | nghttp3_ksl.c \ 47 | nghttp3_qpack.c \ 48 | nghttp3_qpack_huffman.c \ 49 | nghttp3_qpack_huffman_data.c \ 50 | nghttp3_err.c \ 51 | nghttp3_debug.c \ 52 | nghttp3_conn.c \ 53 | nghttp3_stream.c \ 54 | nghttp3_frame.c \ 55 | nghttp3_tnode.c \ 56 | nghttp3_vec.c \ 57 | nghttp3_gaptr.c \ 58 | nghttp3_idtr.c \ 59 | nghttp3_range.c \ 60 | nghttp3_http.c \ 61 | nghttp3_version.c \ 62 | nghttp3_balloc.c \ 63 | nghttp3_opl.c \ 64 | nghttp3_objalloc.c \ 65 | nghttp3_unreachable.c \ 66 | sfparse/sfparse.c 67 | HFILES = \ 68 | nghttp3_rcbuf.h \ 69 | nghttp3_mem.h \ 70 | nghttp3_str.h \ 71 | nghttp3_conv.h \ 72 | nghttp3_buf.h \ 73 | nghttp3_ringbuf.h \ 74 | nghttp3_pq.h \ 75 | nghttp3_map.h \ 76 | nghttp3_ksl.h \ 77 | nghttp3_qpack.h \ 78 | nghttp3_qpack_huffman.h \ 79 | nghttp3_err.h \ 80 | nghttp3_debug.h \ 81 | nghttp3_conn.h \ 82 | nghttp3_stream.h \ 83 | nghttp3_frame.h \ 84 | nghttp3_tnode.h \ 85 | nghttp3_vec.h \ 86 | nghttp3_gaptr.h \ 87 | nghttp3_idtr.h \ 88 | nghttp3_range.h \ 89 | nghttp3_http.h \ 90 | nghttp3_balloc.h \ 91 | nghttp3_opl.h \ 92 | nghttp3_objalloc.h \ 93 | nghttp3_unreachable.h \ 94 | sfparse/sfparse.h \ 95 | nghttp3_macro.h 96 | 97 | libnghttp3_la_SOURCES = $(HFILES) $(OBJECTS) 98 | libnghttp3_la_LDFLAGS = -no-undefined \ 99 | -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) 100 | -------------------------------------------------------------------------------- /lib/config.cmake.in: -------------------------------------------------------------------------------- 1 | include(CMakeFindDependencyMacro) 2 | 3 | include("${CMAKE_CURRENT_LIST_DIR}/@NGHTTP3_TARGETS_EXPORT_NAME@.cmake") 4 | -------------------------------------------------------------------------------- /lib/includes/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | install(FILES 2 | nghttp3/nghttp3.h 3 | "${CMAKE_CURRENT_BINARY_DIR}/nghttp3/version.h" 4 | DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/nghttp3") 5 | -------------------------------------------------------------------------------- /lib/includes/Makefile.am: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2016 ngtcp2 contributors 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | EXTRA_DIST = CMakeLists.txt 25 | 26 | nobase_include_HEADERS = nghttp3/nghttp3.h nghttp3/version.h 27 | -------------------------------------------------------------------------------- /lib/includes/nghttp3/version.h.in: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_VERSION_H 27 | #define NGHTTP3_VERSION_H 28 | 29 | /** 30 | * @macro 31 | * 32 | * Version number of the nghttp3 library release. 33 | */ 34 | #define NGHTTP3_VERSION "@PACKAGE_VERSION@" 35 | 36 | /** 37 | * @macro 38 | * 39 | * Numerical representation of the version number of the nghttp3 40 | * library release. This is a 24 bit number with 8 bits for major 41 | * number, 8 bits for minor and 8 bits for patch. Version 1.2.3 42 | * becomes 0x010203. 43 | */ 44 | #define NGHTTP3_VERSION_NUM @PACKAGE_VERSION_NUM@ 45 | 46 | #endif /* !defined(NGHTTP3_VERSION_H) */ 47 | -------------------------------------------------------------------------------- /lib/libnghttp3.pc.in: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2016 ngtcp2 contributors 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | prefix=@prefix@ 25 | exec_prefix=@exec_prefix@ 26 | libdir=@libdir@ 27 | includedir=@includedir@ 28 | 29 | Name: libnghttp3 30 | Description: nghttp3 library 31 | URL: https://github.com/ngtcp2/nghttp3 32 | Version: @VERSION@ 33 | Libs: -L${libdir} -lnghttp3 34 | Cflags: -I${includedir} 35 | -------------------------------------------------------------------------------- /lib/nghttp3_balloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_balloc.h" 27 | 28 | #include 29 | 30 | #include "nghttp3_mem.h" 31 | 32 | void nghttp3_balloc_init(nghttp3_balloc *balloc, size_t blklen, 33 | const nghttp3_mem *mem) { 34 | assert((blklen & 0xfu) == 0); 35 | 36 | balloc->mem = mem; 37 | balloc->blklen = blklen; 38 | balloc->head = NULL; 39 | nghttp3_buf_wrap_init(&balloc->buf, (void *)"", 0); 40 | } 41 | 42 | void nghttp3_balloc_free(nghttp3_balloc *balloc) { 43 | if (balloc == NULL) { 44 | return; 45 | } 46 | 47 | nghttp3_balloc_clear(balloc); 48 | } 49 | 50 | void nghttp3_balloc_clear(nghttp3_balloc *balloc) { 51 | nghttp3_memblock_hd *p, *next; 52 | 53 | for (p = balloc->head; p; p = next) { 54 | next = p->next; 55 | nghttp3_mem_free(balloc->mem, p); 56 | } 57 | 58 | balloc->head = NULL; 59 | nghttp3_buf_wrap_init(&balloc->buf, (void *)"", 0); 60 | } 61 | 62 | int nghttp3_balloc_get(nghttp3_balloc *balloc, void **pbuf, size_t n) { 63 | uint8_t *p; 64 | nghttp3_memblock_hd *hd; 65 | 66 | assert(n <= balloc->blklen); 67 | 68 | if (nghttp3_buf_left(&balloc->buf) < n) { 69 | p = nghttp3_mem_malloc(balloc->mem, 70 | sizeof(nghttp3_memblock_hd) + 0x8u + balloc->blklen); 71 | if (p == NULL) { 72 | return NGHTTP3_ERR_NOMEM; 73 | } 74 | 75 | hd = (nghttp3_memblock_hd *)(void *)p; 76 | hd->next = balloc->head; 77 | balloc->head = hd; 78 | nghttp3_buf_wrap_init( 79 | &balloc->buf, 80 | (uint8_t *)(((uintptr_t)p + sizeof(nghttp3_memblock_hd) + 0xfu) & 81 | ~(uintptr_t)0xfu), 82 | balloc->blklen); 83 | } 84 | 85 | assert(((uintptr_t)balloc->buf.last & 0xfu) == 0); 86 | 87 | *pbuf = balloc->buf.last; 88 | balloc->buf.last += (n + 0xfu) & ~(uintptr_t)0xfu; 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /lib/nghttp3_balloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_BALLOC_H 27 | #define NGHTTP3_BALLOC_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include "nghttp3_buf.h" 36 | 37 | typedef struct nghttp3_memblock_hd nghttp3_memblock_hd; 38 | 39 | /* 40 | * nghttp3_memblock_hd is the header of memory block. 41 | */ 42 | struct nghttp3_memblock_hd { 43 | union { 44 | nghttp3_memblock_hd *next; 45 | uint64_t pad; 46 | }; 47 | }; 48 | 49 | /* 50 | * nghttp3_balloc is a custom memory allocator. It allocates |blklen| 51 | * bytes of memory at once on demand, and returns its slice when the 52 | * allocation is requested. 53 | */ 54 | typedef struct nghttp3_balloc { 55 | /* mem is the underlying memory allocator. */ 56 | const nghttp3_mem *mem; 57 | /* blklen is the size of memory block. */ 58 | size_t blklen; 59 | /* head points to the list of memory block allocated so far. */ 60 | nghttp3_memblock_hd *head; 61 | /* buf wraps the current memory block for allocation requests. */ 62 | nghttp3_buf buf; 63 | } nghttp3_balloc; 64 | 65 | /* 66 | * nghttp3_balloc_init initializes |balloc| with |blklen| which is the 67 | * size of memory block. 68 | */ 69 | void nghttp3_balloc_init(nghttp3_balloc *balloc, size_t blklen, 70 | const nghttp3_mem *mem); 71 | 72 | /* 73 | * nghttp3_balloc_free releases all allocated memory blocks. 74 | */ 75 | void nghttp3_balloc_free(nghttp3_balloc *balloc); 76 | 77 | /* 78 | * nghttp3_balloc_get allocates |n| bytes of memory and assigns its 79 | * pointer to |*pbuf|. 80 | * 81 | * It returns 0 if it succeeds, or one of the following negative error 82 | * codes: 83 | * 84 | * NGHTTP3_ERR_NOMEM 85 | * Out of memory. 86 | */ 87 | int nghttp3_balloc_get(nghttp3_balloc *balloc, void **pbuf, size_t n); 88 | 89 | /* 90 | * nghttp3_balloc_clear releases all allocated memory blocks and 91 | * initializes its state. 92 | */ 93 | void nghttp3_balloc_clear(nghttp3_balloc *balloc); 94 | 95 | #endif /* !defined(NGHTTP3_BALLOC_H) */ 96 | -------------------------------------------------------------------------------- /lib/nghttp3_buf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_buf.h" 27 | 28 | void nghttp3_buf_init(nghttp3_buf *buf) { 29 | buf->begin = buf->end = buf->pos = buf->last = NULL; 30 | } 31 | 32 | void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) { 33 | buf->begin = buf->pos = buf->last = src; 34 | buf->end = buf->begin + len; 35 | } 36 | 37 | void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem) { 38 | nghttp3_mem_free(mem, buf->begin); 39 | } 40 | 41 | size_t nghttp3_buf_left(const nghttp3_buf *buf) { 42 | return (size_t)(buf->end - buf->last); 43 | } 44 | 45 | size_t nghttp3_buf_len(const nghttp3_buf *buf) { 46 | return (size_t)(buf->last - buf->pos); 47 | } 48 | 49 | size_t nghttp3_buf_cap(const nghttp3_buf *buf) { 50 | return (size_t)(buf->end - buf->begin); 51 | } 52 | 53 | size_t nghttp3_buf_offset(const nghttp3_buf *buf) { 54 | return (size_t)(buf->pos - buf->begin); 55 | } 56 | 57 | void nghttp3_buf_reset(nghttp3_buf *buf) { buf->pos = buf->last = buf->begin; } 58 | 59 | int nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem) { 60 | uint8_t *p; 61 | nghttp3_ssize pos_offset, last_offset; 62 | 63 | if ((size_t)(buf->end - buf->begin) >= size) { 64 | return 0; 65 | } 66 | 67 | pos_offset = buf->pos - buf->begin; 68 | last_offset = buf->last - buf->begin; 69 | 70 | p = nghttp3_mem_realloc(mem, buf->begin, size); 71 | if (p == NULL) { 72 | return NGHTTP3_ERR_NOMEM; 73 | } 74 | 75 | buf->begin = p; 76 | buf->end = p + size; 77 | buf->pos = p + pos_offset; 78 | buf->last = p + last_offset; 79 | 80 | return 0; 81 | } 82 | 83 | void nghttp3_buf_swap(nghttp3_buf *a, nghttp3_buf *b) { 84 | nghttp3_buf c = *a; 85 | 86 | *a = *b; 87 | *b = c; 88 | } 89 | 90 | void nghttp3_typed_buf_init(nghttp3_typed_buf *tbuf, const nghttp3_buf *buf, 91 | nghttp3_buf_type type) { 92 | tbuf->buf = *buf; 93 | tbuf->type = type; 94 | tbuf->buf.begin = tbuf->buf.pos; 95 | } 96 | 97 | void nghttp3_typed_buf_shared_init(nghttp3_typed_buf *tbuf, 98 | const nghttp3_buf *chunk) { 99 | tbuf->buf = *chunk; 100 | tbuf->type = NGHTTP3_BUF_TYPE_SHARED; 101 | tbuf->buf.begin = tbuf->buf.pos = tbuf->buf.last; 102 | } 103 | -------------------------------------------------------------------------------- /lib/nghttp3_buf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_BUF_H 27 | #define NGHTTP3_BUF_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include "nghttp3_mem.h" 36 | 37 | void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len); 38 | 39 | /* 40 | * nghttp3_buf_cap returns the capacity of the buffer. In other 41 | * words, it returns buf->end - buf->begin. 42 | */ 43 | size_t nghttp3_buf_cap(const nghttp3_buf *buf); 44 | 45 | /* 46 | * nghttp3_buf_offset returns the distance from tbuf->begin to 47 | * tbuf->pos. In other words, it returns buf->pos - buf->begin. 48 | */ 49 | size_t nghttp3_buf_offset(const nghttp3_buf *buf); 50 | 51 | int nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem); 52 | 53 | /* 54 | * nghttp3_buf_swap swaps |a| and |b|. 55 | */ 56 | void nghttp3_buf_swap(nghttp3_buf *a, nghttp3_buf *b); 57 | 58 | typedef enum nghttp3_buf_type { 59 | /* NGHTTP3_BUF_TYPE_PRIVATE indicates that memory is allocated for 60 | this buffer only and should be freed after its use. */ 61 | NGHTTP3_BUF_TYPE_PRIVATE, 62 | /* NGHTTP3_BUF_TYPE_SHARED indicates that buffer points to shared 63 | memory. */ 64 | NGHTTP3_BUF_TYPE_SHARED, 65 | /* NGHTTP3_BUF_TYPE_ALIEN indicates that the buffer points to a 66 | memory which comes from outside of the library. */ 67 | NGHTTP3_BUF_TYPE_ALIEN, 68 | } nghttp3_buf_type; 69 | 70 | typedef struct nghttp3_typed_buf { 71 | nghttp3_buf buf; 72 | nghttp3_buf_type type; 73 | } nghttp3_typed_buf; 74 | 75 | void nghttp3_typed_buf_init(nghttp3_typed_buf *tbuf, const nghttp3_buf *buf, 76 | nghttp3_buf_type type); 77 | 78 | /* 79 | * nghttp3_typed_buf_shared_init initializes |tbuf| of type 80 | * NGHTTP3_BUF_TYPE_SHARED. 81 | */ 82 | void nghttp3_typed_buf_shared_init(nghttp3_typed_buf *tbuf, 83 | const nghttp3_buf *chunk); 84 | 85 | void nghttp3_typed_buf_free(nghttp3_typed_buf *tbuf); 86 | 87 | #endif /* !defined(NGHTTP3_BUF_H) */ 88 | -------------------------------------------------------------------------------- /lib/nghttp3_conv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_conv.h" 27 | 28 | #include 29 | #include 30 | 31 | #include "nghttp3_str.h" 32 | #include "nghttp3_unreachable.h" 33 | 34 | const uint8_t *nghttp3_get_varint(int64_t *dest, const uint8_t *p) { 35 | union { 36 | uint8_t n8; 37 | uint16_t n16; 38 | uint32_t n32; 39 | uint64_t n64; 40 | } n; 41 | 42 | switch (*p >> 6) { 43 | case 0: 44 | *dest = *p++; 45 | return p; 46 | case 1: 47 | memcpy(&n, p, 2); 48 | n.n8 &= 0x3f; 49 | *dest = ntohs(n.n16); 50 | 51 | return p + 2; 52 | case 2: 53 | memcpy(&n, p, 4); 54 | n.n8 &= 0x3f; 55 | *dest = ntohl(n.n32); 56 | 57 | return p + 4; 58 | case 3: 59 | memcpy(&n, p, 8); 60 | n.n8 &= 0x3f; 61 | *dest = (int64_t)nghttp3_ntohl64(n.n64); 62 | 63 | return p + 8; 64 | default: 65 | nghttp3_unreachable(); 66 | } 67 | } 68 | 69 | size_t nghttp3_get_varintlen(const uint8_t *p) { 70 | return (size_t)(1u << (*p >> 6)); 71 | } 72 | 73 | uint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n) { 74 | n = nghttp3_htonl64(n); 75 | return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 76 | } 77 | 78 | uint8_t *nghttp3_put_uint32be(uint8_t *p, uint32_t n) { 79 | n = htonl(n); 80 | return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 81 | } 82 | 83 | uint8_t *nghttp3_put_uint16be(uint8_t *p, uint16_t n) { 84 | n = htons(n); 85 | return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 86 | } 87 | 88 | uint8_t *nghttp3_put_varint(uint8_t *p, int64_t n) { 89 | uint8_t *rv; 90 | if (n < 64) { 91 | *p++ = (uint8_t)n; 92 | return p; 93 | } 94 | if (n < 16384) { 95 | rv = nghttp3_put_uint16be(p, (uint16_t)n); 96 | *p |= 0x40; 97 | return rv; 98 | } 99 | if (n < 1073741824) { 100 | rv = nghttp3_put_uint32be(p, (uint32_t)n); 101 | *p |= 0x80; 102 | return rv; 103 | } 104 | assert(n < 4611686018427387904LL); 105 | rv = nghttp3_put_uint64be(p, (uint64_t)n); 106 | *p |= 0xc0; 107 | return rv; 108 | } 109 | 110 | size_t nghttp3_put_varintlen(int64_t n) { 111 | if (n < 64) { 112 | return 1; 113 | } 114 | if (n < 16384) { 115 | return 2; 116 | } 117 | if (n < 1073741824) { 118 | return 4; 119 | } 120 | assert(n < 4611686018427387904LL); 121 | return 8; 122 | } 123 | 124 | uint64_t nghttp3_ord_stream_id(int64_t stream_id) { 125 | return (uint64_t)(stream_id >> 2) + 1; 126 | } 127 | -------------------------------------------------------------------------------- /lib/nghttp3_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_debug.h" 27 | 28 | #include 29 | 30 | #ifdef DEBUGBUILD 31 | 32 | static void nghttp3_default_debug_vfprintf_callback(const char *fmt, 33 | va_list args) { 34 | vfprintf(stderr, fmt, args); 35 | } 36 | 37 | static nghttp3_debug_vprintf_callback static_debug_vprintf_callback = 38 | nghttp3_default_debug_vfprintf_callback; 39 | 40 | void nghttp3_debug_vprintf(const char *format, ...) { 41 | if (static_debug_vprintf_callback) { 42 | va_list args; 43 | va_start(args, format); 44 | static_debug_vprintf_callback(format, args); 45 | va_end(args); 46 | } 47 | } 48 | 49 | void nghttp3_set_debug_vprintf_callback( 50 | nghttp3_debug_vprintf_callback debug_vprintf_callback) { 51 | static_debug_vprintf_callback = debug_vprintf_callback; 52 | } 53 | 54 | #else /* !defined(DEBUGBUILD) */ 55 | 56 | void nghttp3_set_debug_vprintf_callback( 57 | nghttp3_debug_vprintf_callback debug_vprintf_callback) { 58 | (void)debug_vprintf_callback; 59 | } 60 | 61 | #endif /* !defined(DEBUGBUILD) */ 62 | -------------------------------------------------------------------------------- /lib/nghttp3_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_DEBUG_H 27 | #define NGHTTP3_DEBUG_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #ifdef DEBUGBUILD 36 | # define DEBUGF(...) nghttp3_debug_vprintf(__VA_ARGS__) 37 | void nghttp3_debug_vprintf(const char *format, ...); 38 | #else /* !defined(DEBUGBUILD) */ 39 | # define DEBUGF(...) \ 40 | do { \ 41 | } while (0) 42 | #endif /* !defined(DEBUGBUILD) */ 43 | 44 | #endif /* !defined(NGHTTP3_DEBUG_H) */ 45 | -------------------------------------------------------------------------------- /lib/nghttp3_err.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "nghttp3_err.h" 26 | 27 | const char *nghttp3_strerror(int liberr) { 28 | switch (liberr) { 29 | case NGHTTP3_ERR_INVALID_ARGUMENT: 30 | return "ERR_INVALID_ARGUMENT"; 31 | case NGHTTP3_ERR_INVALID_STATE: 32 | return "ERR_INVALID_STATE"; 33 | case NGHTTP3_ERR_WOULDBLOCK: 34 | return "ERR_WOULDBLOCK"; 35 | case NGHTTP3_ERR_STREAM_IN_USE: 36 | return "ERR_STREAM_IN_USE"; 37 | case NGHTTP3_ERR_MALFORMED_HTTP_HEADER: 38 | return "ERR_MALFORMED_HTTP_HEADER"; 39 | case NGHTTP3_ERR_REMOVE_HTTP_HEADER: 40 | return "ERR_REMOVE_HTTP_HEADER"; 41 | case NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING: 42 | return "ERR_MALFORMED_HTTP_MESSAGING"; 43 | case NGHTTP3_ERR_QPACK_FATAL: 44 | return "ERR_QPACK_FATAL"; 45 | case NGHTTP3_ERR_QPACK_HEADER_TOO_LARGE: 46 | return "ERR_QPACK_HEADER_TOO_LARGE"; 47 | case NGHTTP3_ERR_STREAM_NOT_FOUND: 48 | return "ERR_STREAM_NOT_FOUND"; 49 | case NGHTTP3_ERR_CONN_CLOSING: 50 | return "ERR_CONN_CLOSING"; 51 | case NGHTTP3_ERR_STREAM_DATA_OVERFLOW: 52 | return "ERR_STREAM_DATA_OVERFLOW"; 53 | case NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED: 54 | return "ERR_QPACK_DECOMPRESSION_FAILED"; 55 | case NGHTTP3_ERR_QPACK_ENCODER_STREAM_ERROR: 56 | return "ERR_QPACK_ENCODER_STREAM_ERROR"; 57 | case NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR: 58 | return "ERR_QPACK_DECODER_STREAM_ERROR"; 59 | case NGHTTP3_ERR_H3_FRAME_UNEXPECTED: 60 | return "ERR_H3_FRAME_UNEXPECTED"; 61 | case NGHTTP3_ERR_H3_FRAME_ERROR: 62 | return "ERR_H3_FRAME_ERROR"; 63 | case NGHTTP3_ERR_H3_MISSING_SETTINGS: 64 | return "ERR_H3_MISSING_SETTINGS"; 65 | case NGHTTP3_ERR_H3_INTERNAL_ERROR: 66 | return "ERR_H3_INTERNAL_ERROR"; 67 | case NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM: 68 | return "ERR_CLOSED_CRITICAL_STREAM"; 69 | case NGHTTP3_ERR_H3_GENERAL_PROTOCOL_ERROR: 70 | return "ERR_H3_GENERAL_PROTOCOL_ERROR"; 71 | case NGHTTP3_ERR_H3_ID_ERROR: 72 | return "ERR_H3_ID_ERROR"; 73 | case NGHTTP3_ERR_H3_SETTINGS_ERROR: 74 | return "ERR_H3_SETTINGS_ERROR"; 75 | case NGHTTP3_ERR_H3_STREAM_CREATION_ERROR: 76 | return "ERR_H3_STREAM_CREATION_ERROR"; 77 | case NGHTTP3_ERR_NOMEM: 78 | return "ERR_NOMEM"; 79 | case NGHTTP3_ERR_CALLBACK_FAILURE: 80 | return "ERR_CALLBACK_FAILURE"; 81 | default: 82 | return "(unknown)"; 83 | } 84 | } 85 | 86 | uint64_t nghttp3_err_infer_quic_app_error_code(int liberr) { 87 | switch (liberr) { 88 | case 0: 89 | return NGHTTP3_H3_NO_ERROR; 90 | case NGHTTP3_ERR_QPACK_DECOMPRESSION_FAILED: 91 | return NGHTTP3_QPACK_DECOMPRESSION_FAILED; 92 | case NGHTTP3_ERR_QPACK_ENCODER_STREAM_ERROR: 93 | return NGHTTP3_QPACK_ENCODER_STREAM_ERROR; 94 | case NGHTTP3_ERR_QPACK_DECODER_STREAM_ERROR: 95 | return NGHTTP3_QPACK_DECODER_STREAM_ERROR; 96 | case NGHTTP3_ERR_H3_FRAME_UNEXPECTED: 97 | return NGHTTP3_H3_FRAME_UNEXPECTED; 98 | case NGHTTP3_ERR_H3_FRAME_ERROR: 99 | return NGHTTP3_H3_FRAME_ERROR; 100 | case NGHTTP3_ERR_H3_MISSING_SETTINGS: 101 | return NGHTTP3_H3_MISSING_SETTINGS; 102 | case NGHTTP3_ERR_H3_INTERNAL_ERROR: 103 | case NGHTTP3_ERR_NOMEM: 104 | case NGHTTP3_ERR_CALLBACK_FAILURE: 105 | case NGHTTP3_ERR_QPACK_FATAL: 106 | case NGHTTP3_ERR_QPACK_HEADER_TOO_LARGE: 107 | case NGHTTP3_ERR_STREAM_DATA_OVERFLOW: 108 | return NGHTTP3_H3_INTERNAL_ERROR; 109 | case NGHTTP3_ERR_H3_CLOSED_CRITICAL_STREAM: 110 | return NGHTTP3_H3_CLOSED_CRITICAL_STREAM; 111 | case NGHTTP3_ERR_H3_GENERAL_PROTOCOL_ERROR: 112 | return NGHTTP3_H3_GENERAL_PROTOCOL_ERROR; 113 | case NGHTTP3_ERR_H3_ID_ERROR: 114 | return NGHTTP3_H3_ID_ERROR; 115 | case NGHTTP3_ERR_H3_SETTINGS_ERROR: 116 | return NGHTTP3_H3_SETTINGS_ERROR; 117 | case NGHTTP3_ERR_H3_STREAM_CREATION_ERROR: 118 | return NGHTTP3_H3_STREAM_CREATION_ERROR; 119 | case NGHTTP3_ERR_MALFORMED_HTTP_HEADER: 120 | case NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING: 121 | return NGHTTP3_H3_MESSAGE_ERROR; 122 | default: 123 | return NGHTTP3_H3_GENERAL_PROTOCOL_ERROR; 124 | } 125 | } 126 | 127 | int nghttp3_err_is_fatal(int liberr) { return liberr < NGHTTP3_ERR_FATAL; } 128 | -------------------------------------------------------------------------------- /lib/nghttp3_err.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_ERR_H 26 | #define NGHTTP3_ERR_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #include 33 | 34 | #endif /* !defined(NGHTTP3_ERR_H) */ 35 | -------------------------------------------------------------------------------- /lib/nghttp3_gaptr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_GAPTR_H 27 | #define NGHTTP3_GAPTR_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include "nghttp3_mem.h" 36 | #include "nghttp3_ksl.h" 37 | #include "nghttp3_range.h" 38 | 39 | /* 40 | * nghttp3_gaptr maintains the gap in the range [0, UINT64_MAX). 41 | */ 42 | typedef struct nghttp3_gaptr { 43 | /* gap maintains the range of offset which is not pushed 44 | yet. Initially, its range is [0, UINT64_MAX). "gap" is the range 45 | that is not pushed yet. */ 46 | nghttp3_ksl gap; 47 | /* mem is custom memory allocator */ 48 | const nghttp3_mem *mem; 49 | } nghttp3_gaptr; 50 | 51 | /* 52 | * nghttp3_gaptr_init initializes |gaptr|. 53 | */ 54 | void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem); 55 | 56 | /* 57 | * nghttp3_gaptr_free frees resources allocated for |gaptr|. 58 | */ 59 | void nghttp3_gaptr_free(nghttp3_gaptr *gaptr); 60 | 61 | /* 62 | * nghttp3_gaptr_push pushes the range [offset, offset + datalen). 63 | * 64 | * This function returns 0 if it succeeds, or one of the following 65 | * negative error codes: 66 | * 67 | * NGHTTP3_ERR_NOMEM 68 | * Out of memory 69 | */ 70 | int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset, uint64_t datalen); 71 | 72 | /* 73 | * nghttp3_gaptr_first_gap_offset returns the offset to the first gap. 74 | * If there is no gap, it returns UINT64_MAX. 75 | */ 76 | uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr); 77 | 78 | /* 79 | * nghttp3_gaptr_get_first_gap_after returns the first gap which 80 | * includes or comes after |offset|. 81 | */ 82 | nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr, 83 | uint64_t offset); 84 | 85 | /* 86 | * nghttp3_gaptr_is_pushed returns nonzero if range [offset, offset + 87 | * datalen) is completely pushed into this object. 88 | */ 89 | int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset, 90 | uint64_t datalen); 91 | 92 | /* 93 | * nghttp3_gaptr_drop_first_gap deletes the first gap entirely as if 94 | * the range is pushed. This function assumes that at least one gap 95 | * exists. 96 | */ 97 | void nghttp3_gaptr_drop_first_gap(nghttp3_gaptr *gaptr); 98 | 99 | #endif /* !defined(NGHTTP3_GAPTR_H) */ 100 | -------------------------------------------------------------------------------- /lib/nghttp3_idtr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_idtr.h" 27 | 28 | #include 29 | 30 | void nghttp3_idtr_init(nghttp3_idtr *idtr, const nghttp3_mem *mem) { 31 | nghttp3_gaptr_init(&idtr->gap, mem); 32 | } 33 | 34 | void nghttp3_idtr_free(nghttp3_idtr *idtr) { 35 | if (idtr == NULL) { 36 | return; 37 | } 38 | 39 | nghttp3_gaptr_free(&idtr->gap); 40 | } 41 | 42 | /* 43 | * id_from_stream_id translates |stream_id| to an internal ID. 44 | */ 45 | static uint64_t id_from_stream_id(int64_t stream_id) { 46 | return (uint64_t)(stream_id >> 2); 47 | } 48 | 49 | int nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id) { 50 | uint64_t q; 51 | 52 | q = id_from_stream_id(stream_id); 53 | 54 | if (nghttp3_gaptr_is_pushed(&idtr->gap, q, 1)) { 55 | return NGHTTP3_ERR_STREAM_IN_USE; 56 | } 57 | 58 | return nghttp3_gaptr_push(&idtr->gap, q, 1); 59 | } 60 | 61 | int nghttp3_idtr_is_open(nghttp3_idtr *idtr, int64_t stream_id) { 62 | uint64_t q; 63 | 64 | q = id_from_stream_id(stream_id); 65 | 66 | return nghttp3_gaptr_is_pushed(&idtr->gap, q, 1); 67 | } 68 | -------------------------------------------------------------------------------- /lib/nghttp3_idtr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_IDTR_H 27 | #define NGHTTP3_IDTR_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include "nghttp3_mem.h" 36 | #include "nghttp3_gaptr.h" 37 | 38 | /* 39 | * nghttp3_idtr tracks the usage of stream ID. 40 | */ 41 | typedef struct nghttp3_idtr { 42 | /* gap maintains the range of an internal ID which is not used yet. 43 | Initially, its range is [0, UINT64_MAX). The internal ID and 44 | stream ID are in the different number spaces. See 45 | id_from_stream_id to convert a stream ID to an internal ID. */ 46 | nghttp3_gaptr gap; 47 | } nghttp3_idtr; 48 | 49 | /* 50 | * nghttp3_idtr_init initializes |idtr|. 51 | */ 52 | void nghttp3_idtr_init(nghttp3_idtr *idtr, const nghttp3_mem *mem); 53 | 54 | /* 55 | * nghttp3_idtr_free frees resources allocated for |idtr|. 56 | */ 57 | void nghttp3_idtr_free(nghttp3_idtr *idtr); 58 | 59 | /* 60 | * nghttp3_idtr_open claims that |stream_id| is in use. 61 | * 62 | * It returns 0 if it succeeds, or one of the following negative error 63 | * codes: 64 | * 65 | * NGHTTP3_ERR_STREAM_IN_USE 66 | * |stream_id| has already been used. 67 | * NGHTTP3_ERR_NOMEM 68 | * Out of memory. 69 | */ 70 | int nghttp3_idtr_open(nghttp3_idtr *idtr, int64_t stream_id); 71 | 72 | /* 73 | * nghttp3_idtr_open returns nonzero if |stream_id| is in use. 74 | */ 75 | int nghttp3_idtr_is_open(nghttp3_idtr *idtr, int64_t stream_id); 76 | 77 | #endif /* !defined(NGHTTP3_IDTR_H) */ 78 | -------------------------------------------------------------------------------- /lib/nghttp3_macro.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_MACRO_H 27 | #define NGHTTP3_MACRO_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include 36 | 37 | #define nghttp3_struct_of(ptr, type, member) \ 38 | ((type *)(void *)((char *)(ptr) - offsetof(type, member))) 39 | 40 | #define nghttp3_arraylen(A) (sizeof(A) / sizeof(*(A))) 41 | 42 | #define lstreq(A, B, N) ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0) 43 | 44 | /* NGHTTP3_MAX_VARINT` is the maximum value which can be encoded in 45 | variable-length integer encoding. */ 46 | #define NGHTTP3_MAX_VARINT ((1ULL << 62) - 1) 47 | 48 | #define nghttp3_max_def(SUFFIX, T) \ 49 | static inline T nghttp3_max_##SUFFIX(T a, T b) { return a < b ? b : a; } 50 | 51 | nghttp3_max_def(int8, int8_t) 52 | nghttp3_max_def(int16, int16_t) 53 | nghttp3_max_def(int32, int32_t) 54 | nghttp3_max_def(int64, int64_t) 55 | nghttp3_max_def(uint8, uint8_t) 56 | nghttp3_max_def(uint16, uint16_t) 57 | nghttp3_max_def(uint32, uint32_t) 58 | nghttp3_max_def(uint64, uint64_t) 59 | nghttp3_max_def(size, size_t) 60 | 61 | #define nghttp3_min_def(SUFFIX, T) \ 62 | static inline T nghttp3_min_##SUFFIX(T a, T b) { return a < b ? a : b; } 63 | 64 | nghttp3_min_def(int8, int8_t) 65 | nghttp3_min_def(int16, int16_t) 66 | nghttp3_min_def(int32, int32_t) 67 | nghttp3_min_def(int64, int64_t) 68 | nghttp3_min_def(uint8, uint8_t) 69 | nghttp3_min_def(uint16, uint16_t) 70 | nghttp3_min_def(uint32, uint32_t) 71 | nghttp3_min_def(uint64, uint64_t) 72 | nghttp3_min_def(size, size_t) 73 | 74 | #endif /* !defined(NGHTTP3_MACRO_H) */ 75 | -------------------------------------------------------------------------------- /lib/nghttp3_map.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2012 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifndef NGHTTP3_MAP_H 28 | #define NGHTTP3_MAP_H 29 | 30 | #ifdef HAVE_CONFIG_H 31 | # include 32 | #endif /* defined(HAVE_CONFIG_H) */ 33 | 34 | #include 35 | 36 | #include "nghttp3_mem.h" 37 | 38 | /* Implementation of unordered map */ 39 | 40 | typedef uint64_t nghttp3_map_key_type; 41 | 42 | typedef struct nghttp3_map_bucket { 43 | uint32_t psl; 44 | nghttp3_map_key_type key; 45 | void *data; 46 | } nghttp3_map_bucket; 47 | 48 | typedef struct nghttp3_map { 49 | nghttp3_map_bucket *table; 50 | const nghttp3_mem *mem; 51 | size_t size; 52 | size_t hashbits; 53 | } nghttp3_map; 54 | 55 | /* 56 | * nghttp3_map_init initializes the map |map|. 57 | */ 58 | void nghttp3_map_init(nghttp3_map *map, const nghttp3_mem *mem); 59 | 60 | /* 61 | * nghttp3_map_free deallocates any resources allocated for |map|. 62 | * The stored entries are not freed by this function. Use 63 | * nghttp3_map_each() to free each entry. 64 | */ 65 | void nghttp3_map_free(nghttp3_map *map); 66 | 67 | /* 68 | * nghttp3_map_insert inserts the new |data| with the |key| to the map 69 | * |map|. 70 | * 71 | * This function returns 0 if it succeeds, or one of the following 72 | * negative error codes: 73 | * 74 | * NGHTTP3_ERR_INVALID_ARGUMENT 75 | * The item associated by |key| already exists. 76 | * NGHTTP3_ERR_NOMEM 77 | * Out of memory 78 | */ 79 | int nghttp3_map_insert(nghttp3_map *map, nghttp3_map_key_type key, void *data); 80 | 81 | /* 82 | * nghttp3_map_find returns the entry associated by the key |key|. If 83 | * there is no such entry, this function returns NULL. 84 | */ 85 | void *nghttp3_map_find(const nghttp3_map *map, nghttp3_map_key_type key); 86 | 87 | /* 88 | * nghttp3_map_remove removes the entry associated by the key |key| 89 | * from the |map|. The removed entry is not freed by this function. 90 | * 91 | * This function returns 0 if it succeeds, or one of the following 92 | * negative error codes: 93 | * 94 | * NGHTTP3_ERR_INVALID_ARGUMENT 95 | * The entry associated by |key| does not exist. 96 | */ 97 | int nghttp3_map_remove(nghttp3_map *map, nghttp3_map_key_type key); 98 | 99 | /* 100 | * nghttp3_map_clear removes all entries from |map|. The removed 101 | * entry is not freed by this function. 102 | */ 103 | void nghttp3_map_clear(nghttp3_map *map); 104 | 105 | /* 106 | * nghttp3_map_size returns the number of items stored in the map 107 | * |map|. 108 | */ 109 | size_t nghttp3_map_size(const nghttp3_map *map); 110 | 111 | /* 112 | * nghttp3_map_each applies the function |func| to each entry in the 113 | * |map| with the optional user supplied pointer |ptr|. 114 | * 115 | * If the |func| returns 0, this function calls the |func| with the 116 | * next entry. If the |func| returns nonzero, it will not call the 117 | * |func| for further entries and return the return value of the 118 | * |func| immediately. Thus, this function returns 0 if all the 119 | * invocations of the |func| return 0, or nonzero value which the last 120 | * invocation of |func| returns. 121 | */ 122 | int nghttp3_map_each(const nghttp3_map *map, int (*func)(void *data, void *ptr), 123 | void *ptr); 124 | 125 | #ifndef WIN32 126 | void nghttp3_map_print_distance(const nghttp3_map *map); 127 | #endif /* !defined(WIN32) */ 128 | 129 | #endif /* !defined(NGHTTP3_MAP_H) */ 130 | -------------------------------------------------------------------------------- /lib/nghttp3_mem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2014 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #include "nghttp3_mem.h" 28 | 29 | #include 30 | 31 | static void *default_malloc(size_t size, void *user_data) { 32 | (void)user_data; 33 | 34 | return malloc(size); 35 | } 36 | 37 | static void default_free(void *ptr, void *user_data) { 38 | (void)user_data; 39 | 40 | free(ptr); 41 | } 42 | 43 | static void *default_calloc(size_t nmemb, size_t size, void *user_data) { 44 | (void)user_data; 45 | 46 | return calloc(nmemb, size); 47 | } 48 | 49 | static void *default_realloc(void *ptr, size_t size, void *user_data) { 50 | (void)user_data; 51 | 52 | return realloc(ptr, size); 53 | } 54 | 55 | static nghttp3_mem mem_default = {NULL, default_malloc, default_free, 56 | default_calloc, default_realloc}; 57 | 58 | const nghttp3_mem *nghttp3_mem_default(void) { return &mem_default; } 59 | 60 | #ifndef MEMDEBUG 61 | void *nghttp3_mem_malloc(const nghttp3_mem *mem, size_t size) { 62 | return mem->malloc(size, mem->user_data); 63 | } 64 | 65 | void nghttp3_mem_free(const nghttp3_mem *mem, void *ptr) { 66 | mem->free(ptr, mem->user_data); 67 | } 68 | 69 | void *nghttp3_mem_calloc(const nghttp3_mem *mem, size_t nmemb, size_t size) { 70 | return mem->calloc(nmemb, size, mem->user_data); 71 | } 72 | 73 | void *nghttp3_mem_realloc(const nghttp3_mem *mem, void *ptr, size_t size) { 74 | return mem->realloc(ptr, size, mem->user_data); 75 | } 76 | #else /* defined(MEMDEBUG) */ 77 | void *nghttp3_mem_malloc_debug(const nghttp3_mem *mem, size_t size, 78 | const char *func, const char *file, 79 | size_t line) { 80 | void *nptr = mem->malloc(size, mem->user_data); 81 | 82 | fprintf(stderr, "malloc %p size=%zu in %s at %s:%zu\n", nptr, size, func, 83 | file, line); 84 | 85 | return nptr; 86 | } 87 | 88 | void nghttp3_mem_free_debug(const nghttp3_mem *mem, void *ptr, const char *func, 89 | const char *file, size_t line) { 90 | fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line); 91 | 92 | mem->free(ptr, mem->user_data); 93 | } 94 | 95 | void nghttp3_mem_free2_debug(const nghttp3_free free_func, void *ptr, 96 | void *user_data, const char *func, 97 | const char *file, size_t line) { 98 | fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line); 99 | 100 | free_func(ptr, user_data); 101 | } 102 | 103 | void *nghttp3_mem_calloc_debug(const nghttp3_mem *mem, size_t nmemb, 104 | size_t size, const char *func, const char *file, 105 | size_t line) { 106 | void *nptr = mem->calloc(nmemb, size, mem->user_data); 107 | 108 | fprintf(stderr, "calloc %p nmemb=%zu size=%zu in %s at %s:%zu\n", nptr, nmemb, 109 | size, func, file, line); 110 | 111 | return nptr; 112 | } 113 | 114 | void *nghttp3_mem_realloc_debug(const nghttp3_mem *mem, void *ptr, size_t size, 115 | const char *func, const char *file, 116 | size_t line) { 117 | void *nptr = mem->realloc(ptr, size, mem->user_data); 118 | 119 | fprintf(stderr, "realloc %p ptr=%p size=%zu in %s at %s:%zu\n", nptr, ptr, 120 | size, func, file, line); 121 | 122 | return nptr; 123 | } 124 | #endif /* defined(MEMDEBUG) */ 125 | -------------------------------------------------------------------------------- /lib/nghttp3_mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2014 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifndef NGHTTP3_MEM_H 28 | #define NGHTTP3_MEM_H 29 | 30 | #ifdef HAVE_CONFIG_H 31 | # include 32 | #endif /* defined(HAVE_CONFIG_H) */ 33 | 34 | #include 35 | 36 | /* Convenient wrapper functions to call allocator function in 37 | |mem|. */ 38 | #ifndef MEMDEBUG 39 | void *nghttp3_mem_malloc(const nghttp3_mem *mem, size_t size); 40 | void nghttp3_mem_free(const nghttp3_mem *mem, void *ptr); 41 | void *nghttp3_mem_calloc(const nghttp3_mem *mem, size_t nmemb, size_t size); 42 | void *nghttp3_mem_realloc(const nghttp3_mem *mem, void *ptr, size_t size); 43 | #else /* defined(MEMDEBUG) */ 44 | void *nghttp3_mem_malloc_debug(const nghttp3_mem *mem, size_t size, 45 | const char *func, const char *file, size_t line); 46 | 47 | # define nghttp3_mem_malloc(MEM, SIZE) \ 48 | nghttp3_mem_malloc_debug((MEM), (SIZE), __func__, __FILE__, __LINE__) 49 | 50 | void nghttp3_mem_free_debug(const nghttp3_mem *mem, void *ptr, const char *func, 51 | const char *file, size_t line); 52 | 53 | # define nghttp3_mem_free(MEM, PTR) \ 54 | nghttp3_mem_free_debug((MEM), (PTR), __func__, __FILE__, __LINE__) 55 | 56 | void nghttp3_mem_free2_debug(nghttp3_free free_func, void *ptr, void *user_data, 57 | const char *func, const char *file, size_t line); 58 | 59 | # define nghttp3_mem_free2(FREE_FUNC, PTR, USER_DATA) \ 60 | nghttp3_mem_free2_debug((FREE_FUNC), (PTR), (USER_DATA), __func__, \ 61 | __FILE__, __LINE__) 62 | 63 | void *nghttp3_mem_calloc_debug(const nghttp3_mem *mem, size_t nmemb, 64 | size_t size, const char *func, const char *file, 65 | size_t line); 66 | 67 | # define nghttp3_mem_calloc(MEM, NMEMB, SIZE) \ 68 | nghttp3_mem_calloc_debug((MEM), (NMEMB), (SIZE), __func__, __FILE__, \ 69 | __LINE__) 70 | 71 | void *nghttp3_mem_realloc_debug(const nghttp3_mem *mem, void *ptr, size_t size, 72 | const char *func, const char *file, 73 | size_t line); 74 | 75 | # define nghttp3_mem_realloc(MEM, PTR, SIZE) \ 76 | nghttp3_mem_realloc_debug((MEM), (PTR), (SIZE), __func__, __FILE__, \ 77 | __LINE__) 78 | #endif /* defined(MEMDEBUG) */ 79 | 80 | #endif /* !defined(NGHTTP3_MEM_H) */ 81 | -------------------------------------------------------------------------------- /lib/nghttp3_objalloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_objalloc.h" 27 | 28 | void nghttp3_objalloc_init(nghttp3_objalloc *objalloc, size_t blklen, 29 | const nghttp3_mem *mem) { 30 | nghttp3_balloc_init(&objalloc->balloc, blklen, mem); 31 | nghttp3_opl_init(&objalloc->opl); 32 | } 33 | 34 | void nghttp3_objalloc_free(nghttp3_objalloc *objalloc) { 35 | nghttp3_balloc_free(&objalloc->balloc); 36 | } 37 | 38 | void nghttp3_objalloc_clear(nghttp3_objalloc *objalloc) { 39 | nghttp3_opl_clear(&objalloc->opl); 40 | nghttp3_balloc_clear(&objalloc->balloc); 41 | } 42 | -------------------------------------------------------------------------------- /lib/nghttp3_opl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_opl.h" 27 | 28 | void nghttp3_opl_init(nghttp3_opl *opl) { opl->head = NULL; } 29 | 30 | void nghttp3_opl_push(nghttp3_opl *opl, nghttp3_opl_entry *ent) { 31 | ent->next = opl->head; 32 | opl->head = ent; 33 | } 34 | 35 | nghttp3_opl_entry *nghttp3_opl_pop(nghttp3_opl *opl) { 36 | nghttp3_opl_entry *ent = opl->head; 37 | 38 | if (!ent) { 39 | return NULL; 40 | } 41 | 42 | opl->head = ent->next; 43 | 44 | return ent; 45 | } 46 | 47 | void nghttp3_opl_clear(nghttp3_opl *opl) { opl->head = NULL; } 48 | -------------------------------------------------------------------------------- /lib/nghttp3_opl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_OPL_H 27 | #define NGHTTP3_OPL_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | typedef struct nghttp3_opl_entry nghttp3_opl_entry; 36 | 37 | struct nghttp3_opl_entry { 38 | nghttp3_opl_entry *next; 39 | }; 40 | 41 | /* 42 | * nghttp3_opl is an object memory pool. 43 | */ 44 | typedef struct nghttp3_opl { 45 | nghttp3_opl_entry *head; 46 | } nghttp3_opl; 47 | 48 | /* 49 | * nghttp3_opl_init initializes |opl|. 50 | */ 51 | void nghttp3_opl_init(nghttp3_opl *opl); 52 | 53 | /* 54 | * nghttp3_opl_push inserts |ent| to |opl| head. 55 | */ 56 | void nghttp3_opl_push(nghttp3_opl *opl, nghttp3_opl_entry *ent); 57 | 58 | /* 59 | * nghttp3_opl_pop removes the first nghttp3_opl_entry from |opl| and 60 | * returns it. If |opl| does not have any entry, it returns NULL. 61 | */ 62 | nghttp3_opl_entry *nghttp3_opl_pop(nghttp3_opl *opl); 63 | 64 | void nghttp3_opl_clear(nghttp3_opl *opl); 65 | 66 | #endif /* !defined(NGHTTP3_OPL_H) */ 67 | -------------------------------------------------------------------------------- /lib/nghttp3_pq.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2012 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #include "nghttp3_pq.h" 28 | 29 | #include 30 | 31 | #include "nghttp3_macro.h" 32 | 33 | void nghttp3_pq_init(nghttp3_pq *pq, nghttp3_pq_less less, 34 | const nghttp3_mem *mem) { 35 | pq->q = NULL; 36 | pq->mem = mem; 37 | pq->length = 0; 38 | pq->capacity = 0; 39 | pq->less = less; 40 | } 41 | 42 | void nghttp3_pq_free(nghttp3_pq *pq) { 43 | if (!pq) { 44 | return; 45 | } 46 | 47 | nghttp3_mem_free(pq->mem, pq->q); 48 | } 49 | 50 | static void swap(nghttp3_pq *pq, size_t i, size_t j) { 51 | nghttp3_pq_entry *a = pq->q[i]; 52 | nghttp3_pq_entry *b = pq->q[j]; 53 | 54 | pq->q[i] = b; 55 | b->index = i; 56 | pq->q[j] = a; 57 | a->index = j; 58 | } 59 | 60 | static void bubble_up(nghttp3_pq *pq, size_t index) { 61 | size_t parent; 62 | 63 | while (index) { 64 | parent = (index - 1) / 2; 65 | if (!pq->less(pq->q[index], pq->q[parent])) { 66 | return; 67 | } 68 | 69 | swap(pq, parent, index); 70 | index = parent; 71 | } 72 | } 73 | 74 | int nghttp3_pq_push(nghttp3_pq *pq, nghttp3_pq_entry *item) { 75 | if (pq->capacity <= pq->length) { 76 | void *nq; 77 | size_t ncapacity; 78 | 79 | ncapacity = nghttp3_max_size(4, pq->capacity * 2); 80 | 81 | nq = nghttp3_mem_realloc(pq->mem, pq->q, 82 | ncapacity * sizeof(nghttp3_pq_entry *)); 83 | if (nq == NULL) { 84 | return NGHTTP3_ERR_NOMEM; 85 | } 86 | 87 | pq->capacity = ncapacity; 88 | pq->q = nq; 89 | } 90 | 91 | pq->q[pq->length] = item; 92 | item->index = pq->length; 93 | ++pq->length; 94 | bubble_up(pq, item->index); 95 | 96 | return 0; 97 | } 98 | 99 | nghttp3_pq_entry *nghttp3_pq_top(const nghttp3_pq *pq) { 100 | assert(pq->length); 101 | return pq->q[0]; 102 | } 103 | 104 | static void bubble_down(nghttp3_pq *pq, size_t index) { 105 | size_t i, j, minindex; 106 | 107 | for (;;) { 108 | j = index * 2 + 1; 109 | minindex = index; 110 | 111 | for (i = 0; i < 2; ++i, ++j) { 112 | if (j >= pq->length) { 113 | break; 114 | } 115 | 116 | if (pq->less(pq->q[j], pq->q[minindex])) { 117 | minindex = j; 118 | } 119 | } 120 | 121 | if (minindex == index) { 122 | return; 123 | } 124 | 125 | swap(pq, index, minindex); 126 | index = minindex; 127 | } 128 | } 129 | 130 | void nghttp3_pq_pop(nghttp3_pq *pq) { 131 | assert(pq->length); 132 | 133 | pq->q[0] = pq->q[pq->length - 1]; 134 | pq->q[0]->index = 0; 135 | --pq->length; 136 | bubble_down(pq, 0); 137 | } 138 | 139 | void nghttp3_pq_remove(nghttp3_pq *pq, nghttp3_pq_entry *item) { 140 | assert(pq->q[item->index] == item); 141 | 142 | if (item->index == 0) { 143 | nghttp3_pq_pop(pq); 144 | return; 145 | } 146 | 147 | if (item->index == pq->length - 1) { 148 | --pq->length; 149 | return; 150 | } 151 | 152 | pq->q[item->index] = pq->q[pq->length - 1]; 153 | pq->q[item->index]->index = item->index; 154 | --pq->length; 155 | 156 | if (pq->less(item, pq->q[item->index])) { 157 | bubble_down(pq, item->index); 158 | } else { 159 | bubble_up(pq, item->index); 160 | } 161 | } 162 | 163 | int nghttp3_pq_empty(const nghttp3_pq *pq) { return pq->length == 0; } 164 | 165 | size_t nghttp3_pq_size(const nghttp3_pq *pq) { return pq->length; } 166 | 167 | void nghttp3_pq_clear(nghttp3_pq *pq) { pq->length = 0; } 168 | -------------------------------------------------------------------------------- /lib/nghttp3_pq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2012 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifndef NGHTTP3_PQ_H 28 | #define NGHTTP3_PQ_H 29 | 30 | #ifdef HAVE_CONFIG_H 31 | # include 32 | #endif /* defined(HAVE_CONFIG_H) */ 33 | 34 | #include 35 | 36 | #include "nghttp3_mem.h" 37 | 38 | /* Implementation of priority queue */ 39 | 40 | /* NGHTTP3_PQ_BAD_INDEX is the priority queue index which indicates 41 | that an entry is not queued. Assigning this value to 42 | nghttp3_pq_entry.index can check that the entry is queued or 43 | not. */ 44 | #define NGHTTP3_PQ_BAD_INDEX SIZE_MAX 45 | 46 | typedef struct nghttp3_pq_entry { 47 | size_t index; 48 | } nghttp3_pq_entry; 49 | 50 | /* nghttp3_pq_less is a "less" function, that returns nonzero if |lhs| 51 | is considered to be less than |rhs|. */ 52 | typedef int (*nghttp3_pq_less)(const nghttp3_pq_entry *lhs, 53 | const nghttp3_pq_entry *rhs); 54 | 55 | typedef struct nghttp3_pq { 56 | /* q is a pointer to an array that stores the items. */ 57 | nghttp3_pq_entry **q; 58 | /* mem is a memory allocator. */ 59 | const nghttp3_mem *mem; 60 | /* length is the number of items stored. */ 61 | size_t length; 62 | /* capacity is the maximum number of items this queue can store. 63 | This is automatically extended when length is reached to this 64 | limit. */ 65 | size_t capacity; 66 | /* less is the less function to compare items. */ 67 | nghttp3_pq_less less; 68 | } nghttp3_pq; 69 | 70 | /* 71 | * nghttp3_pq_init initializes |pq| with compare function |cmp|. 72 | */ 73 | void nghttp3_pq_init(nghttp3_pq *pq, nghttp3_pq_less less, 74 | const nghttp3_mem *mem); 75 | 76 | /* 77 | * nghttp3_pq_free deallocates any resources allocated for |pq|. The 78 | * stored items are not freed by this function. 79 | */ 80 | void nghttp3_pq_free(nghttp3_pq *pq); 81 | 82 | /* 83 | * nghttp3_pq_push adds |item| to |pq|. 84 | * 85 | * This function returns 0 if it succeeds, or one of the following 86 | * negative error codes: 87 | * 88 | * NGHTTP3_ERR_NOMEM 89 | * Out of memory. 90 | */ 91 | int nghttp3_pq_push(nghttp3_pq *pq, nghttp3_pq_entry *item); 92 | 93 | /* 94 | * nghttp3_pq_top returns item at the top of |pq|. It is undefined if 95 | * |pq| is empty. 96 | */ 97 | nghttp3_pq_entry *nghttp3_pq_top(const nghttp3_pq *pq); 98 | 99 | /* 100 | * nghttp3_pq_pop pops item at the top of |pq|. The popped item is 101 | * not freed by this function. It is undefined if |pq| is empty. 102 | */ 103 | void nghttp3_pq_pop(nghttp3_pq *pq); 104 | 105 | /* 106 | * nghttp3_pq_empty returns nonzero if |pq| is empty. 107 | */ 108 | int nghttp3_pq_empty(const nghttp3_pq *pq); 109 | 110 | /* 111 | * nghttp3_pq_size returns the number of items |pq| contains. 112 | */ 113 | size_t nghttp3_pq_size(const nghttp3_pq *pq); 114 | 115 | /* 116 | * nghttp3_pq_remove removes |item| from |pq|. |pq| must contain 117 | * |item| otherwise the behavior is undefined. 118 | */ 119 | void nghttp3_pq_remove(nghttp3_pq *pq, nghttp3_pq_entry *item); 120 | 121 | /* 122 | * nghttp3_pq_clear removes all items from |pq|. 123 | */ 124 | void nghttp3_pq_clear(nghttp3_pq *pq); 125 | 126 | #endif /* !defined(NGHTTP3_PQ_H) */ 127 | -------------------------------------------------------------------------------- /lib/nghttp3_qpack_huffman.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2013 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_qpack_huffman.h" 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "nghttp3_conv.h" 33 | 34 | size_t nghttp3_qpack_huffman_encode_count(const uint8_t *src, size_t len) { 35 | size_t i; 36 | size_t nbits = 0; 37 | 38 | for (i = 0; i < len; ++i) { 39 | nbits += huffman_sym_table[src[i]].nbits; 40 | } 41 | /* pad the prefix of EOS (256) */ 42 | return (nbits + 7) / 8; 43 | } 44 | 45 | uint8_t *nghttp3_qpack_huffman_encode(uint8_t *dest, const uint8_t *src, 46 | size_t srclen) { 47 | const nghttp3_qpack_huffman_sym *sym; 48 | const uint8_t *end = src + srclen; 49 | uint64_t code = 0; 50 | size_t nbits = 0; 51 | uint32_t x; 52 | 53 | for (; src != end;) { 54 | sym = &huffman_sym_table[*src++]; 55 | code |= (uint64_t)sym->code << (32 - nbits); 56 | nbits += sym->nbits; 57 | if (nbits < 32) { 58 | continue; 59 | } 60 | x = htonl((uint32_t)(code >> 32)); 61 | memcpy(dest, &x, 4); 62 | dest += 4; 63 | code <<= 32; 64 | nbits -= 32; 65 | } 66 | 67 | for (; nbits >= 8;) { 68 | *dest++ = (uint8_t)(code >> 56); 69 | code <<= 8; 70 | nbits -= 8; 71 | } 72 | 73 | if (nbits) { 74 | *dest++ = (uint8_t)((uint8_t)(code >> 56) | ((1 << (8 - nbits)) - 1)); 75 | } 76 | 77 | return dest; 78 | } 79 | 80 | void nghttp3_qpack_huffman_decode_context_init( 81 | nghttp3_qpack_huffman_decode_context *ctx) { 82 | ctx->fstate = NGHTTP3_QPACK_HUFFMAN_ACCEPTED; 83 | } 84 | 85 | nghttp3_ssize 86 | nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx, 87 | uint8_t *dest, const uint8_t *src, size_t srclen, 88 | int fin) { 89 | uint8_t *p = dest; 90 | const uint8_t *end = src + srclen; 91 | nghttp3_qpack_huffman_decode_node node = { 92 | .fstate = ctx->fstate, 93 | }; 94 | const nghttp3_qpack_huffman_decode_node *t = &node; 95 | uint8_t c; 96 | 97 | /* We use the decoding algorithm described in 98 | - http://graphics.ics.uci.edu/pub/Prefix.pdf [!!! NO LONGER VALID !!!] 99 | - https://ics.uci.edu/~dan/pubs/Prefix.pdf 100 | - https://github.com/nghttp2/nghttp2/files/15141264/Prefix.pdf */ 101 | for (; src != end;) { 102 | c = *src++; 103 | t = &qpack_huffman_decode_table[t->fstate & 0x1ff][c >> 4]; 104 | if (t->fstate & NGHTTP3_QPACK_HUFFMAN_SYM) { 105 | *p++ = t->sym; 106 | } 107 | 108 | t = &qpack_huffman_decode_table[t->fstate & 0x1ff][c & 0xf]; 109 | if (t->fstate & NGHTTP3_QPACK_HUFFMAN_SYM) { 110 | *p++ = t->sym; 111 | } 112 | } 113 | 114 | ctx->fstate = t->fstate; 115 | 116 | if (fin && !(ctx->fstate & NGHTTP3_QPACK_HUFFMAN_ACCEPTED)) { 117 | return NGHTTP3_ERR_QPACK_FATAL; 118 | } 119 | 120 | return p - dest; 121 | } 122 | 123 | int nghttp3_qpack_huffman_decode_failure_state( 124 | nghttp3_qpack_huffman_decode_context *ctx) { 125 | return ctx->fstate == 0x100; 126 | } 127 | -------------------------------------------------------------------------------- /lib/nghttp3_qpack_huffman.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2013 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_QPACK_HUFFMAN_H 27 | #define NGHTTP3_QPACK_HUFFMAN_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | typedef struct nghttp3_qpack_huffman_sym { 36 | /* The number of bits in this code */ 37 | uint32_t nbits; 38 | /* Huffman code aligned to LSB */ 39 | uint32_t code; 40 | } nghttp3_qpack_huffman_sym; 41 | 42 | extern const nghttp3_qpack_huffman_sym huffman_sym_table[]; 43 | 44 | size_t nghttp3_qpack_huffman_encode_count(const uint8_t *src, size_t len); 45 | 46 | uint8_t *nghttp3_qpack_huffman_encode(uint8_t *dest, const uint8_t *src, 47 | size_t srclen); 48 | 49 | typedef enum nghttp3_qpack_huffman_decode_flag { 50 | /* FSA accepts this state as the end of huffman encoding 51 | sequence. */ 52 | NGHTTP3_QPACK_HUFFMAN_ACCEPTED = 1 << 14, 53 | /* This state emits symbol */ 54 | NGHTTP3_QPACK_HUFFMAN_SYM = 1 << 15, 55 | } nghttp3_qpack_huffman_decode_flag; 56 | 57 | typedef struct nghttp3_qpack_huffman_decode_node { 58 | /* fstate is the current huffman decoding state, which is actually 59 | the node ID of internal huffman tree with 60 | nghttp3_qpack_huffman_decode_flag OR-ed. We have 257 leaf nodes, 61 | but they are identical to root node other than emitting a symbol, 62 | so we have 256 internal nodes [1..256], inclusive. The node ID 63 | 256 is a special node and it is a terminal state that means 64 | decoding failed. */ 65 | uint16_t fstate; 66 | /* symbol if NGHTTP3_QPACK_HUFFMAN_SYM flag set */ 67 | uint8_t sym; 68 | } nghttp3_qpack_huffman_decode_node; 69 | 70 | typedef struct nghttp3_qpack_huffman_decode_context { 71 | /* fstate is the current huffman decoding state. */ 72 | uint16_t fstate; 73 | } nghttp3_qpack_huffman_decode_context; 74 | 75 | extern const nghttp3_qpack_huffman_decode_node qpack_huffman_decode_table[][16]; 76 | 77 | void nghttp3_qpack_huffman_decode_context_init( 78 | nghttp3_qpack_huffman_decode_context *ctx); 79 | 80 | /* 81 | * nghttp3_qpack_huffman_decode decodes huffman encoded byte string 82 | * stored in |src| of length |srclen|. |ctx| is a decoding context. 83 | * |ctx| remembers the decoding state, and caller can call this 84 | * function multiple times to feed each chunk of huffman encoded 85 | * substring. |fin| must be nonzero if |src| contains the last chunk 86 | * of huffman string. The decoded string is written to the buffer 87 | * pointed by |dest|. This function assumes that the buffer pointed 88 | * by |dest| contains enough memory to store decoded byte string. 89 | * 90 | * This function returns the number of bytes written to |dest|, or one 91 | * of the following negative error codes: 92 | * 93 | * NGHTTP3_ERR_QPACK_FATAL 94 | * Could not decode huffman string. 95 | */ 96 | nghttp3_ssize 97 | nghttp3_qpack_huffman_decode(nghttp3_qpack_huffman_decode_context *ctx, 98 | uint8_t *dest, const uint8_t *src, size_t srclen, 99 | int fin); 100 | 101 | /* 102 | * nghttp3_qpack_huffman_decode_failure_state returns nonzero if |ctx| 103 | * indicates that huffman decoding context is in failure state. 104 | */ 105 | int nghttp3_qpack_huffman_decode_failure_state( 106 | nghttp3_qpack_huffman_decode_context *ctx); 107 | 108 | #endif /* !defined(NGHTTP3_QPACK_HUFFMAN_H) */ 109 | -------------------------------------------------------------------------------- /lib/nghttp3_range.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_range.h" 27 | #include "nghttp3_macro.h" 28 | 29 | void nghttp3_range_init(nghttp3_range *r, uint64_t begin, uint64_t end) { 30 | r->begin = begin; 31 | r->end = end; 32 | } 33 | 34 | nghttp3_range nghttp3_range_intersect(const nghttp3_range *a, 35 | const nghttp3_range *b) { 36 | nghttp3_range r = {0}; 37 | uint64_t begin = nghttp3_max_uint64(a->begin, b->begin); 38 | uint64_t end = nghttp3_min_uint64(a->end, b->end); 39 | 40 | if (begin < end) { 41 | nghttp3_range_init(&r, begin, end); 42 | } 43 | 44 | return r; 45 | } 46 | 47 | uint64_t nghttp3_range_len(const nghttp3_range *r) { return r->end - r->begin; } 48 | 49 | int nghttp3_range_eq(const nghttp3_range *a, const nghttp3_range *b) { 50 | return a->begin == b->begin && a->end == b->end; 51 | } 52 | 53 | void nghttp3_range_cut(nghttp3_range *left, nghttp3_range *right, 54 | const nghttp3_range *a, const nghttp3_range *b) { 55 | /* Assume that b is included in a */ 56 | left->begin = a->begin; 57 | left->end = b->begin; 58 | right->begin = b->end; 59 | right->end = a->end; 60 | } 61 | 62 | int nghttp3_range_not_after(const nghttp3_range *a, const nghttp3_range *b) { 63 | return a->end <= b->end; 64 | } 65 | -------------------------------------------------------------------------------- /lib/nghttp3_range.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_RANGE_H 27 | #define NGHTTP3_RANGE_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | /* 36 | * nghttp3_range represents half-closed range [begin, end). 37 | */ 38 | typedef struct nghttp3_range { 39 | uint64_t begin; 40 | uint64_t end; 41 | } nghttp3_range; 42 | 43 | /* 44 | * nghttp3_range_init initializes |r| with the range [|begin|, |end|). 45 | */ 46 | void nghttp3_range_init(nghttp3_range *r, uint64_t begin, uint64_t end); 47 | 48 | /* 49 | * nghttp3_range_intersect returns the intersection of |a| and |b|. 50 | * If they do not overlap, it returns empty range. 51 | */ 52 | nghttp3_range nghttp3_range_intersect(const nghttp3_range *a, 53 | const nghttp3_range *b); 54 | 55 | /* 56 | * nghttp3_range_len returns the length of |r|. 57 | */ 58 | uint64_t nghttp3_range_len(const nghttp3_range *r); 59 | 60 | /* 61 | * nghttp3_range_eq returns nonzero if |a| equals |b|, such that 62 | * a->begin == b->begin and a->end == b->end hold. 63 | */ 64 | int nghttp3_range_eq(const nghttp3_range *a, const nghttp3_range *b); 65 | 66 | /* 67 | * nghttp3_range_cut returns the left and right range after removing 68 | * |b| from |a|. This function assumes that |a| completely includes 69 | * |b|. In other words, a->begin <= b->begin and b->end <= a->end 70 | * hold. 71 | */ 72 | void nghttp3_range_cut(nghttp3_range *left, nghttp3_range *right, 73 | const nghttp3_range *a, const nghttp3_range *b); 74 | 75 | /* 76 | * nghttp3_range_not_after returns nonzero if the right edge of |a| 77 | * does not go beyond of the right edge of |b|. 78 | */ 79 | int nghttp3_range_not_after(const nghttp3_range *a, const nghttp3_range *b); 80 | 81 | #endif /* !defined(NGHTTP3_RANGE_H) */ 82 | -------------------------------------------------------------------------------- /lib/nghttp3_rcbuf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_rcbuf.h" 27 | 28 | #include 29 | 30 | #include "nghttp3_mem.h" 31 | #include "nghttp3_str.h" 32 | 33 | int nghttp3_rcbuf_new(nghttp3_rcbuf **rcbuf_ptr, size_t size, 34 | const nghttp3_mem *mem) { 35 | uint8_t *p; 36 | 37 | p = nghttp3_mem_malloc(mem, sizeof(nghttp3_rcbuf) + size); 38 | if (p == NULL) { 39 | return NGHTTP3_ERR_NOMEM; 40 | } 41 | 42 | *rcbuf_ptr = (void *)p; 43 | 44 | (*rcbuf_ptr)->mem = mem; 45 | (*rcbuf_ptr)->base = p + sizeof(nghttp3_rcbuf); 46 | (*rcbuf_ptr)->len = size; 47 | (*rcbuf_ptr)->ref = 1; 48 | 49 | return 0; 50 | } 51 | 52 | int nghttp3_rcbuf_new2(nghttp3_rcbuf **rcbuf_ptr, const uint8_t *src, 53 | size_t srclen, const nghttp3_mem *mem) { 54 | int rv; 55 | uint8_t *p; 56 | 57 | rv = nghttp3_rcbuf_new(rcbuf_ptr, srclen + 1, mem); 58 | if (rv != 0) { 59 | return rv; 60 | } 61 | 62 | (*rcbuf_ptr)->len = srclen; 63 | p = (*rcbuf_ptr)->base; 64 | 65 | if (srclen) { 66 | p = nghttp3_cpymem(p, src, srclen); 67 | } 68 | 69 | *p = '\0'; 70 | 71 | return 0; 72 | } 73 | 74 | /* 75 | * Frees |rcbuf| itself, regardless of its reference cout. 76 | */ 77 | void nghttp3_rcbuf_del(nghttp3_rcbuf *rcbuf) { 78 | nghttp3_mem_free(rcbuf->mem, rcbuf); 79 | } 80 | 81 | void nghttp3_rcbuf_incref(nghttp3_rcbuf *rcbuf) { 82 | if (rcbuf->ref == -1) { 83 | return; 84 | } 85 | 86 | ++rcbuf->ref; 87 | } 88 | 89 | void nghttp3_rcbuf_decref(nghttp3_rcbuf *rcbuf) { 90 | if (rcbuf == NULL || rcbuf->ref == -1) { 91 | return; 92 | } 93 | 94 | assert(rcbuf->ref > 0); 95 | 96 | if (--rcbuf->ref == 0) { 97 | nghttp3_rcbuf_del(rcbuf); 98 | } 99 | } 100 | 101 | nghttp3_vec nghttp3_rcbuf_get_buf(const nghttp3_rcbuf *rcbuf) { 102 | nghttp3_vec res = {rcbuf->base, rcbuf->len}; 103 | return res; 104 | } 105 | 106 | int nghttp3_rcbuf_is_static(const nghttp3_rcbuf *rcbuf) { 107 | return rcbuf->ref == -1; 108 | } 109 | -------------------------------------------------------------------------------- /lib/nghttp3_rcbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_RCBUF_H 27 | #define NGHTTP3_RCBUF_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | struct nghttp3_rcbuf { 36 | /* mem is the memory allocator that allocates memory for this 37 | object. */ 38 | const nghttp3_mem *mem; 39 | /* The pointer to the underlying buffer */ 40 | uint8_t *base; 41 | /* Size of buffer pointed by |base|. */ 42 | size_t len; 43 | /* Reference count */ 44 | int32_t ref; 45 | }; 46 | 47 | /* 48 | * Allocates nghttp3_rcbuf object with |size| as initial buffer size. 49 | * When the function succeeds, the reference count becomes 1. 50 | * 51 | * This function returns 0 if it succeeds, or one of the following 52 | * negative error codes: 53 | * 54 | * NGHTTP3_ERR_NOMEM: 55 | * Out of memory. 56 | */ 57 | int nghttp3_rcbuf_new(nghttp3_rcbuf **rcbuf_ptr, size_t size, 58 | const nghttp3_mem *mem); 59 | 60 | /* 61 | * Like nghttp3_rcbuf_new(), but initializes the buffer with |src| of 62 | * length |srclen|. This function allocates additional byte at the 63 | * end and puts '\0' into it, so that the resulting buffer could be 64 | * used as NULL-terminated string. Still (*rcbuf_ptr)->len equals to 65 | * |srclen|. 66 | * 67 | * This function returns 0 if it succeeds, or one of the following 68 | * negative error codes: 69 | * 70 | * NGHTTP3_ERR_NOMEM: 71 | * Out of memory. 72 | */ 73 | int nghttp3_rcbuf_new2(nghttp3_rcbuf **rcbuf_ptr, const uint8_t *src, 74 | size_t srclen, const nghttp3_mem *mem); 75 | 76 | /* 77 | * Frees |rcbuf| itself, regardless of its reference cout. 78 | */ 79 | void nghttp3_rcbuf_del(nghttp3_rcbuf *rcbuf); 80 | 81 | #endif /* !defined(NGHTTP3_RCBUF_H) */ 82 | -------------------------------------------------------------------------------- /lib/nghttp3_ringbuf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_ringbuf.h" 27 | 28 | #include 29 | #include 30 | #ifdef WIN32 31 | # include 32 | #endif /* defined(WIN32) */ 33 | 34 | #include "nghttp3_macro.h" 35 | 36 | #ifndef NDEBUG 37 | static int ispow2(size_t n) { 38 | # if defined(DISABLE_POPCNT) || \ 39 | (defined(_MSC_VER) && !defined(__clang__) && \ 40 | (defined(_M_ARM) || (defined(_M_ARM64) && _MSC_VER < 1941))) 41 | return n && !(n & (n - 1)); 42 | # elif defined(WIN32) 43 | return 1 == __popcnt((unsigned int)n); 44 | # else /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \ 45 | (defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */ 46 | return 1 == __builtin_popcount((unsigned int)n); 47 | # endif /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \ 48 | (defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */ 49 | } 50 | #endif /* !defined(NDEBUG) */ 51 | 52 | int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size, 53 | const nghttp3_mem *mem) { 54 | if (nmemb) { 55 | assert(ispow2(nmemb)); 56 | 57 | rb->buf = nghttp3_mem_malloc(mem, nmemb * size); 58 | if (rb->buf == NULL) { 59 | return NGHTTP3_ERR_NOMEM; 60 | } 61 | } else { 62 | rb->buf = NULL; 63 | } 64 | 65 | rb->mem = mem; 66 | rb->nmemb = nmemb; 67 | rb->size = size; 68 | rb->first = 0; 69 | rb->len = 0; 70 | 71 | return 0; 72 | } 73 | 74 | void nghttp3_ringbuf_free(nghttp3_ringbuf *rb) { 75 | if (rb == NULL) { 76 | return; 77 | } 78 | 79 | nghttp3_mem_free(rb->mem, rb->buf); 80 | } 81 | 82 | void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb) { 83 | rb->first = (rb->first - 1) & (rb->nmemb - 1); 84 | rb->len = nghttp3_min_size(rb->nmemb, rb->len + 1); 85 | 86 | return (void *)&rb->buf[rb->first * rb->size]; 87 | } 88 | 89 | void *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb) { 90 | size_t offset = (rb->first + rb->len) & (rb->nmemb - 1); 91 | 92 | if (rb->len == rb->nmemb) { 93 | rb->first = (rb->first + 1) & (rb->nmemb - 1); 94 | } else { 95 | ++rb->len; 96 | } 97 | 98 | return (void *)&rb->buf[offset * rb->size]; 99 | } 100 | 101 | void nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb) { 102 | rb->first = (rb->first + 1) & (rb->nmemb - 1); 103 | --rb->len; 104 | } 105 | 106 | void nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb) { 107 | assert(rb->len); 108 | --rb->len; 109 | } 110 | 111 | void nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len) { 112 | assert(len <= rb->nmemb); 113 | rb->len = len; 114 | } 115 | 116 | void *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset) { 117 | assert(offset < rb->len); 118 | offset = (rb->first + offset) & (rb->nmemb - 1); 119 | return &rb->buf[offset * rb->size]; 120 | } 121 | 122 | int nghttp3_ringbuf_full(nghttp3_ringbuf *rb) { return rb->len == rb->nmemb; } 123 | 124 | int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb) { 125 | uint8_t *buf; 126 | 127 | if (rb->nmemb >= nmemb) { 128 | return 0; 129 | } 130 | 131 | assert(ispow2(nmemb)); 132 | 133 | buf = nghttp3_mem_malloc(rb->mem, nmemb * rb->size); 134 | if (buf == NULL) { 135 | return NGHTTP3_ERR_NOMEM; 136 | } 137 | 138 | if (rb->buf != NULL) { 139 | if (rb->first + rb->len <= rb->nmemb) { 140 | memcpy(buf, rb->buf + rb->first * rb->size, rb->len * rb->size); 141 | rb->first = 0; 142 | } else { 143 | memcpy(buf, rb->buf + rb->first * rb->size, 144 | (rb->nmemb - rb->first) * rb->size); 145 | memcpy(buf + (rb->nmemb - rb->first) * rb->size, rb->buf, 146 | (rb->len - (rb->nmemb - rb->first)) * rb->size); 147 | rb->first = 0; 148 | } 149 | 150 | nghttp3_mem_free(rb->mem, rb->buf); 151 | } 152 | 153 | rb->buf = buf; 154 | rb->nmemb = nmemb; 155 | 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /lib/nghttp3_ringbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_RINGBUF_H 27 | #define NGHTTP3_RINGBUF_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #include "nghttp3_mem.h" 36 | 37 | typedef struct nghttp3_ringbuf { 38 | /* buf points to the underlying buffer. */ 39 | uint8_t *buf; 40 | const nghttp3_mem *mem; 41 | /* nmemb is the number of elements that can be stored in this ring 42 | buffer. */ 43 | size_t nmemb; 44 | /* size is the size of each element. */ 45 | size_t size; 46 | /* first is the offset to the first element. */ 47 | size_t first; 48 | /* len is the number of elements actually stored. */ 49 | size_t len; 50 | } nghttp3_ringbuf; 51 | 52 | /* 53 | * nghttp3_ringbuf_init initializes |rb|. |nmemb| is the number of 54 | * elements that can be stored in this buffer. |size| is the size of 55 | * each element. |size| must be power of 2. 56 | * 57 | * This function returns 0 if it succeeds, or one of the following 58 | * negative error codes: 59 | * 60 | * NGHTTP3_ERR_NOMEM 61 | * Out of memory. 62 | */ 63 | int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size, 64 | const nghttp3_mem *mem); 65 | 66 | /* 67 | * nghttp3_ringbuf_free frees resources allocated for |rb|. This 68 | * function does not free the memory pointed by |rb|. 69 | */ 70 | void nghttp3_ringbuf_free(nghttp3_ringbuf *rb); 71 | 72 | /* nghttp3_ringbuf_push_front moves the offset to the first element in 73 | the buffer backward, and returns the pointer to the element. 74 | Caller can store data to the buffer pointed by the returned 75 | pointer. If this action exceeds the capacity of the ring buffer, 76 | the last element is silently overwritten, and rb->len remains 77 | unchanged. */ 78 | void *nghttp3_ringbuf_push_front(nghttp3_ringbuf *rb); 79 | 80 | /* nghttp3_ringbuf_push_back moves the offset to the last element in 81 | the buffer forward, and returns the pointer to the element. Caller 82 | can store data to the buffer pointed by the returned pointer. If 83 | this action exceeds the capacity of the ring buffer, the first 84 | element is silently overwritten, and rb->len remains unchanged. */ 85 | void *nghttp3_ringbuf_push_back(nghttp3_ringbuf *rb); 86 | 87 | /* 88 | * nghttp3_ringbuf_pop_front removes first element in |rb|. 89 | */ 90 | void nghttp3_ringbuf_pop_front(nghttp3_ringbuf *rb); 91 | 92 | /* 93 | * nghttp3_ringbuf_pop_back removes the last element in |rb|. 94 | */ 95 | void nghttp3_ringbuf_pop_back(nghttp3_ringbuf *rb); 96 | 97 | /* nghttp3_ringbuf_resize changes the number of elements stored. This 98 | does not change the capacity of the underlying buffer. */ 99 | void nghttp3_ringbuf_resize(nghttp3_ringbuf *rb, size_t len); 100 | 101 | /* nghttp3_ringbuf_get returns the pointer to the element at 102 | |offset|. */ 103 | void *nghttp3_ringbuf_get(nghttp3_ringbuf *rb, size_t offset); 104 | 105 | /* nghttp3_ringbuf_len returns the number of elements stored. */ 106 | #define nghttp3_ringbuf_len(RB) ((RB)->len) 107 | 108 | /* nghttp3_ringbuf_full returns nonzero if |rb| is full. */ 109 | int nghttp3_ringbuf_full(nghttp3_ringbuf *rb); 110 | 111 | int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb); 112 | 113 | #endif /* !defined(NGHTTP3_RINGBUF_H) */ 114 | -------------------------------------------------------------------------------- /lib/nghttp3_str.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2017 ngtcp2 contributors 6 | * Copyright (c) 2012 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifndef NGHTTP3_STR_H 28 | #define NGHTTP3_STR_H 29 | 30 | #ifdef HAVE_CONFIG_H 31 | # include 32 | #endif /* defined(HAVE_CONFIG_H) */ 33 | 34 | #include 35 | 36 | uint8_t *nghttp3_cpymem(uint8_t *dest, const uint8_t *src, size_t n); 37 | 38 | void nghttp3_downcase(uint8_t *s, size_t len); 39 | 40 | #endif /* !defined(NGHTTP3_STR_H) */ 41 | -------------------------------------------------------------------------------- /lib/nghttp3_tnode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "nghttp3_tnode.h" 26 | 27 | #include 28 | 29 | #include "nghttp3_macro.h" 30 | #include "nghttp3_stream.h" 31 | #include "nghttp3_conn.h" 32 | #include "nghttp3_conv.h" 33 | 34 | void nghttp3_tnode_init(nghttp3_tnode *tnode, int64_t id) { 35 | tnode->pe.index = NGHTTP3_PQ_BAD_INDEX; 36 | tnode->id = id; 37 | tnode->cycle = 0; 38 | tnode->pri.urgency = NGHTTP3_DEFAULT_URGENCY; 39 | tnode->pri.inc = 0; 40 | } 41 | 42 | void nghttp3_tnode_free(nghttp3_tnode *tnode) { (void)tnode; } 43 | 44 | static void tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) { 45 | assert(tnode->pe.index != NGHTTP3_PQ_BAD_INDEX); 46 | 47 | nghttp3_pq_remove(pq, &tnode->pe); 48 | tnode->pe.index = NGHTTP3_PQ_BAD_INDEX; 49 | } 50 | 51 | void nghttp3_tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq) { 52 | if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) { 53 | return; 54 | } 55 | 56 | tnode_unschedule(tnode, pq); 57 | } 58 | 59 | static uint64_t pq_get_first_cycle(nghttp3_pq *pq) { 60 | nghttp3_tnode *top; 61 | 62 | if (nghttp3_pq_empty(pq)) { 63 | return 0; 64 | } 65 | 66 | top = nghttp3_struct_of(nghttp3_pq_top(pq), nghttp3_tnode, pe); 67 | return top->cycle; 68 | } 69 | 70 | int nghttp3_tnode_schedule(nghttp3_tnode *tnode, nghttp3_pq *pq, 71 | uint64_t nwrite) { 72 | uint64_t penalty = nwrite / NGHTTP3_STREAM_MIN_WRITELEN; 73 | 74 | if (tnode->pe.index == NGHTTP3_PQ_BAD_INDEX) { 75 | tnode->cycle = 76 | pq_get_first_cycle(pq) + 77 | ((nwrite == 0 || !tnode->pri.inc) ? 0 : nghttp3_max_uint64(1, penalty)); 78 | } else if (nwrite > 0) { 79 | if (!tnode->pri.inc || nghttp3_pq_size(pq) == 1) { 80 | return 0; 81 | } 82 | 83 | nghttp3_pq_remove(pq, &tnode->pe); 84 | tnode->pe.index = NGHTTP3_PQ_BAD_INDEX; 85 | tnode->cycle += nghttp3_max_uint64(1, penalty); 86 | } else { 87 | return 0; 88 | } 89 | 90 | return nghttp3_pq_push(pq, &tnode->pe); 91 | } 92 | 93 | int nghttp3_tnode_is_scheduled(nghttp3_tnode *tnode) { 94 | return tnode->pe.index != NGHTTP3_PQ_BAD_INDEX; 95 | } 96 | -------------------------------------------------------------------------------- /lib/nghttp3_tnode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_TNODE_H 26 | #define NGHTTP3_TNODE_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #include 33 | 34 | #include "nghttp3_pq.h" 35 | 36 | #define NGHTTP3_TNODE_MAX_CYCLE_GAP (1llu << 24) 37 | 38 | typedef struct nghttp3_tnode { 39 | nghttp3_pq_entry pe; 40 | size_t num_children; 41 | int64_t id; 42 | uint64_t cycle; 43 | /* pri is a stream priority produced by nghttp3_pri_to_uint8. */ 44 | nghttp3_pri pri; 45 | } nghttp3_tnode; 46 | 47 | void nghttp3_tnode_init(nghttp3_tnode *tnode, int64_t id); 48 | 49 | void nghttp3_tnode_free(nghttp3_tnode *tnode); 50 | 51 | void nghttp3_tnode_unschedule(nghttp3_tnode *tnode, nghttp3_pq *pq); 52 | 53 | /* 54 | * nghttp3_tnode_schedule schedules |tnode| using |nwrite| as penalty. 55 | * If |tnode| has already been scheduled, it is rescheduled by the 56 | * amount of |nwrite|. 57 | */ 58 | int nghttp3_tnode_schedule(nghttp3_tnode *tnode, nghttp3_pq *pq, 59 | uint64_t nwrite); 60 | 61 | /* 62 | * nghttp3_tnode_is_scheduled returns nonzero if |tnode| is scheduled. 63 | */ 64 | int nghttp3_tnode_is_scheduled(nghttp3_tnode *tnode); 65 | 66 | #endif /* !defined(NGHTTP3_TNODE_H) */ 67 | -------------------------------------------------------------------------------- /lib/nghttp3_unreachable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_unreachable.h" 27 | 28 | #include 29 | #include 30 | #ifdef HAVE_UNISTD_H 31 | # include 32 | #endif /* defined(HAVE_UNISTD_H) */ 33 | #include 34 | #ifdef WIN32 35 | # include 36 | #endif /* defined(WIN32) */ 37 | 38 | void nghttp3_unreachable_fail(const char *file, int line, const char *func) { 39 | char *buf; 40 | size_t buflen; 41 | int rv; 42 | 43 | #define NGHTTP3_UNREACHABLE_TEMPLATE "%s:%d %s: Unreachable.\n" 44 | 45 | rv = snprintf(NULL, 0, NGHTTP3_UNREACHABLE_TEMPLATE, file, line, func); 46 | if (rv < 0) { 47 | abort(); 48 | } 49 | 50 | /* here we explicitly use system malloc */ 51 | buflen = (size_t)rv + 1; 52 | buf = malloc(buflen); 53 | if (buf == NULL) { 54 | abort(); 55 | } 56 | 57 | rv = snprintf(buf, buflen, NGHTTP3_UNREACHABLE_TEMPLATE, file, line, func); 58 | if (rv < 0) { 59 | abort(); 60 | } 61 | 62 | #ifndef WIN32 63 | while (write(STDERR_FILENO, buf, (size_t)rv) == -1 && errno == EINTR) 64 | ; 65 | #else /* defined(WIN32) */ 66 | _write(_fileno(stderr), buf, (unsigned int)rv); 67 | #endif /* defined(WIN32) */ 68 | 69 | free(buf); 70 | 71 | abort(); 72 | } 73 | -------------------------------------------------------------------------------- /lib/nghttp3_unreachable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2022 nghttp3 contributors 5 | * Copyright (c) 2022 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_UNREACHABLE_H 27 | #define NGHTTP3_UNREACHABLE_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | #ifdef __FILE_NAME__ 36 | # define NGHTTP3_FILE_NAME __FILE_NAME__ 37 | #else /* !defined(__FILE_NAME__) */ 38 | # define NGHTTP3_FILE_NAME "(file)" 39 | #endif /* !defined(__FILE_NAME__) */ 40 | 41 | #define nghttp3_unreachable() \ 42 | nghttp3_unreachable_fail(NGHTTP3_FILE_NAME, __LINE__, __func__) 43 | 44 | #ifdef _MSC_VER 45 | __declspec(noreturn) 46 | #endif /* defined(_MSC_VER) */ 47 | void nghttp3_unreachable_fail(const char *file, int line, const char *func) 48 | #ifndef _MSC_VER 49 | __attribute__((noreturn)) 50 | #endif /* !defined(_MSC_VER) */ 51 | ; 52 | 53 | #endif /* !defined(NGHTTP3_UNREACHABLE_H) */ 54 | -------------------------------------------------------------------------------- /lib/nghttp3_vec.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2018 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "nghttp3_vec.h" 27 | #include "nghttp3_macro.h" 28 | 29 | uint64_t nghttp3_vec_len(const nghttp3_vec *vec, size_t n) { 30 | size_t i; 31 | uint64_t res = 0; 32 | 33 | for (i = 0; i < n; ++i) { 34 | res += vec[i].len; 35 | } 36 | 37 | return res; 38 | } 39 | 40 | int64_t nghttp3_vec_len_varint(const nghttp3_vec *vec, size_t n) { 41 | uint64_t res = 0; 42 | size_t len; 43 | size_t i; 44 | 45 | for (i = 0; i < n; ++i) { 46 | len = vec[i].len; 47 | if (len > NGHTTP3_MAX_VARINT - res) { 48 | return -1; 49 | } 50 | 51 | res += len; 52 | } 53 | 54 | return (int64_t)res; 55 | } 56 | -------------------------------------------------------------------------------- /lib/nghttp3_vec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2018 ngtcp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGHTTP3_VEC_H 27 | #define NGHTTP3_VEC_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* defined(HAVE_CONFIG_H) */ 32 | 33 | #include 34 | 35 | /* 36 | * nghttp3_vec_len_varint is similar to nghttp3_vec_len, but it 37 | * returns -1 if the sum of the length exceeds NGHTTP3_MAX_VARINT. 38 | */ 39 | int64_t nghttp3_vec_len_varint(const nghttp3_vec *vec, size_t n); 40 | 41 | #endif /* !defined(NGHTTP3_VEC_H) */ 42 | -------------------------------------------------------------------------------- /lib/nghttp3_version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifdef HAVE_CONFIG_H 26 | # include 27 | #endif /* defined(HAVE_CONFIG_H) */ 28 | 29 | #include 30 | 31 | static nghttp3_info version = { 32 | .age = NGHTTP3_VERSION_AGE, 33 | .version_num = NGHTTP3_VERSION_NUM, 34 | .version_str = NGHTTP3_VERSION, 35 | }; 36 | 37 | const nghttp3_info *nghttp3_version(int least_version) { 38 | if (least_version > NGHTTP3_VERSION_NUM) { 39 | return NULL; 40 | } 41 | return &version; 42 | } 43 | -------------------------------------------------------------------------------- /m4/ax_check_compile_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's compiler 12 | # or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the current language's default 18 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 19 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 20 | # force the compiler to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_COMPILE_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_COMPILE_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS 65 | _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" 66 | AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_COMPILE_FLAGS 75 | -------------------------------------------------------------------------------- /makerelease.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | TAG=$1 4 | PREV_TAG=$2 5 | 6 | git checkout refs/tags/$TAG 7 | git log --pretty=fuller --date=short refs/tags/$PREV_TAG..HEAD > ChangeLog 8 | 9 | autoreconf -i 10 | ./configure 11 | make dist-bzip2 12 | make dist-gzip 13 | make dist-xz 14 | make distclean 15 | 16 | rm -f checksums.txt 17 | 18 | VERSION=`echo -n $TAG | sed -E 's|^v([0-9]+\.[0-9]+\.[0-9]+(-[^.]+(\.[0-9]+)?)?)$|\1|'` 19 | for f in nghttp3-$VERSION.tar.bz2 nghttp3-$VERSION.tar.gz nghttp3-$VERSION.tar.xz; do 20 | sha256sum $f >> checksums.txt 21 | echo -n "$GPG_PASSPHRASE" | gpg --batch --passphrase-fd 0 --pinentry-mode loopback --armor --detach-sign $f 22 | done 23 | -------------------------------------------------------------------------------- /mkstatichdtbl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | # This scripts reads static table entries [1] and generates 5 | # token_stable and stable. This table is used in lib/nghttp3_qpack.c. 6 | # 7 | # [1] https://datatracker.ietf.org/doc/html/rfc9204#name-static-table-2 8 | 9 | import re, sys 10 | 11 | def hd_map_hash(name): 12 | h = 2166136261 13 | 14 | # FNV hash variant: http://isthe.com/chongo/tech/comp/fnv/ 15 | for c in name: 16 | h ^= ord(c) 17 | h *= 16777619 18 | h &= 0xffffffff 19 | 20 | return h 21 | 22 | class Header: 23 | def __init__(self, idx, name, value): 24 | self.idx = idx 25 | self.name = name 26 | self.value = value 27 | self.token = -1 28 | 29 | entries = [] 30 | for line in sys.stdin: 31 | m = re.match(r'(\d+)\s+(\S+)\s+(\S.*)?', line) 32 | val = m.group(3).strip() if m.group(3) else '' 33 | entries.append(Header(int(m.group(1)), m.group(2), val)) 34 | 35 | token_entries = sorted(entries, key=lambda ent: ent.name) 36 | 37 | token = 0 38 | seq = 0 39 | name = token_entries[0].name 40 | for i, ent in enumerate(token_entries): 41 | if name != ent.name: 42 | name = ent.name 43 | token = seq 44 | seq += 1 45 | ent.token = token 46 | 47 | def to_enum_hd(k): 48 | res = 'NGHTTP3_QPACK_TOKEN_' 49 | for c in k.upper(): 50 | if c == ':' or c == '-': 51 | res += '_' 52 | continue 53 | res += c 54 | return res 55 | 56 | def gen_enum(entries): 57 | used = {} 58 | print('typedef enum nghttp3_qpack_token {') 59 | for ent in entries: 60 | if ent.name in used: 61 | continue 62 | used[ent.name] = True 63 | enumname = to_enum_hd(ent.name) 64 | print('''\ 65 | /** 66 | * :enum:`{enumname}` is a token for ``{name}``. 67 | */'''.format(enumname=enumname, name=ent.name)) 68 | if ent.token is None: 69 | print(' {},'.format(enumname)) 70 | else: 71 | print(' {} = {},'.format(enumname, ent.token)) 72 | print('} nghttp3_qpack_token;') 73 | 74 | gen_enum(entries) 75 | 76 | print() 77 | 78 | print('static nghttp3_qpack_static_entry token_stable[] = {') 79 | for i, ent in enumerate(token_entries): 80 | print('MAKE_STATIC_ENT({}, {}, {}u),'\ 81 | .format(ent.idx, to_enum_hd(ent.name), hd_map_hash(ent.name))) 82 | print('};') 83 | 84 | print() 85 | 86 | print('static nghttp3_qpack_static_header stable[] = {') 87 | for ent in entries: 88 | print('MAKE_STATIC_HD("{}", "{}", {}),'\ 89 | .format(ent.name, ent.value, to_enum_hd(ent.name))) 90 | print('};') 91 | -------------------------------------------------------------------------------- /qifs-check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | for f in qifs/encoded/qpack-06/*/*; do 5 | echo $f 6 | 7 | name=`basename "$f"` 8 | IFS='.' read -ra params <<< "$name" 9 | [ "${params[1]}" = "out" ] || continue 10 | prefix=${params[0]} 11 | maxtablesize=${params[2]} 12 | maxblocked=${params[3]} 13 | immediateack=${params[4]} 14 | 15 | opts="-s$maxtablesize -m$maxblocked" 16 | if [ "$immediateack" = "1" ]; then 17 | opts="$opts -a" 18 | fi 19 | 20 | examples/qpack decode "$f" qpack-check.out $opts 21 | qifs/bin/sort-qif.pl --strip-comments qpack-check.out > qpack-check-canonical.out 22 | diff -u qpack-check-canonical.out "qifs/qifs/$prefix.qif" 23 | done 24 | -------------------------------------------------------------------------------- /qifs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | destdir=qifs/encoded/qpack-05/nghttp3 5 | mkdir -p "$destdir" 6 | 7 | for f in qifs/qifs/*.qif; do 8 | prefix=`basename ${f%.qif}` 9 | 10 | for maxtablesize in 0 256 512 4096; do 11 | for maxblocked in 0 100; do 12 | echo $f $maxtablesize $maxblocked 0 13 | outprefix=$prefix.out.$maxtablesize.$maxblocked 14 | examples/qpack encode "$f" "$destdir/$outprefix.0" -s$maxtablesize -m$maxblocked 15 | echo $f $maxtablesize $maxblocked 1 16 | examples/qpack encode "$f" "$destdir/$outprefix.1" -s$maxtablesize -m$maxblocked -a 17 | done 18 | done 19 | done 20 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | /main -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2016 ngtcp2 contributors 5 | # Copyright (c) 2012 nghttp2 contributors 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining 8 | # a copy of this software and associated documentation files (the 9 | # "Software"), to deal in the Software without restriction, including 10 | # without limitation the rights to use, copy, modify, merge, publish, 11 | # distribute, sublicense, and/or sell copies of the Software, and to 12 | # permit persons to whom the Software is furnished to do so, subject to 13 | # the following conditions: 14 | # 15 | # The above copyright notice and this permission notice shall be 16 | # included in all copies or substantial portions of the Software. 17 | # 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | include_directories( 27 | "${CMAKE_SOURCE_DIR}/lib" 28 | "${CMAKE_SOURCE_DIR}/lib/includes" 29 | "${CMAKE_SOURCE_DIR}/tests/munit" 30 | "${CMAKE_BINARY_DIR}/lib/includes" 31 | ) 32 | 33 | set(main_SOURCES 34 | main.c 35 | nghttp3_qpack_test.c 36 | nghttp3_conn_test.c 37 | nghttp3_stream_test.c 38 | nghttp3_tnode_test.c 39 | nghttp3_http_test.c 40 | nghttp3_conv_test.c 41 | nghttp3_test_helper.c 42 | munit/munit.c 43 | ) 44 | 45 | add_executable(main EXCLUDE_FROM_ALL 46 | ${main_SOURCES} 47 | ) 48 | # FIXME enable and fix warnings 49 | #set_target_properties(main PROPERTIES COMPILE_FLAGS "${WARNCFLAGS}") 50 | target_link_libraries(main 51 | nghttp3_static 52 | ) 53 | add_test(main main) 54 | add_dependencies(check main) 55 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | # nghttp3 2 | # 3 | # Copyright (c) 2019 nghttp3 contributors 4 | # Copyright (c) 2016 ngtcp2 contributors 5 | # Copyright (c) 2012 nghttp2 contributors 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining 8 | # a copy of this software and associated documentation files (the 9 | # "Software"), to deal in the Software without restriction, including 10 | # without limitation the rights to use, copy, modify, merge, publish, 11 | # distribute, sublicense, and/or sell copies of the Software, and to 12 | # permit persons to whom the Software is furnished to do so, subject to 13 | # the following conditions: 14 | # 15 | # The above copyright notice and this permission notice shall be 16 | # included in all copies or substantial portions of the Software. 17 | # 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | EXTRA_DIST = CMakeLists.txt munit/munit.c munit/munit.h munit/COPYING 26 | 27 | check_PROGRAMS = main 28 | 29 | OBJECTS = \ 30 | main.c \ 31 | nghttp3_qpack_test.c \ 32 | nghttp3_conn_test.c \ 33 | nghttp3_stream_test.c \ 34 | nghttp3_tnode_test.c \ 35 | nghttp3_http_test.c \ 36 | nghttp3_conv_test.c \ 37 | nghttp3_test_helper.c \ 38 | munit/munit.c 39 | HFILES = \ 40 | nghttp3_qpack_test.h \ 41 | nghttp3_conn_test.h \ 42 | nghttp3_stream_test.h \ 43 | nghttp3_tnode_test.h \ 44 | nghttp3_http_test.h \ 45 | nghttp3_conv_test.h \ 46 | nghttp3_test_helper.h \ 47 | munit/munit.h 48 | 49 | main_SOURCES = $(HFILES) $(OBJECTS) 50 | 51 | # With static lib disabled and symbol hiding enabled, we have to link object 52 | # files directly because the tests use symbols not included in public API. 53 | main_LDADD = ${top_builddir}/lib/.libs/*.o \ 54 | ${top_builddir}/lib/sfparse/.libs/*.o 55 | main_LDFLAGS = -static 56 | 57 | AM_CFLAGS = $(WARNCFLAGS) \ 58 | -I${top_srcdir}/lib \ 59 | -I${top_srcdir}/lib/includes \ 60 | -I${top_srcdir}/tests/munit \ 61 | -I${top_builddir}/lib/includes \ 62 | -DBUILDING_NGHTTP3 \ 63 | @DEFS@ 64 | AM_LDFLAGS = -no-install 65 | 66 | TESTS = main 67 | -------------------------------------------------------------------------------- /tests/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * Copyright (c) 2016 ngtcp2 contributors 6 | * Copyright (c) 2012 nghttp2 contributors 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | */ 27 | #ifdef HAVE_CONFIG_H 28 | # include 29 | #endif /* defined(HAVE_CONFIG_H) */ 30 | 31 | #include "munit.h" 32 | 33 | /* include test cases' include files here */ 34 | #include "nghttp3_qpack_test.h" 35 | #include "nghttp3_conn_test.h" 36 | #include "nghttp3_stream_test.h" 37 | #include "nghttp3_tnode_test.h" 38 | #include "nghttp3_http_test.h" 39 | #include "nghttp3_conv_test.h" 40 | 41 | int main(int argc, char **argv) { 42 | const MunitSuite suites[] = { 43 | qpack_suite, conn_suite, stream_suite, tnode_suite, http_suite, {0}, 44 | }; 45 | const MunitSuite suite = { 46 | .prefix = "", 47 | .suites = suites, 48 | .iterations = 1, 49 | }; 50 | 51 | return munit_suite_main(&suite, NULL, argc, argv); 52 | } 53 | -------------------------------------------------------------------------------- /tests/nghttp3_conn_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_CONN_TEST_H 26 | #define NGHTTP3_CONN_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #define MUNIT_ENABLE_ASSERT_ALIASES 33 | 34 | #include "munit.h" 35 | 36 | extern const MunitSuite conn_suite; 37 | 38 | munit_void_test_decl(test_nghttp3_conn_read_control) 39 | munit_void_test_decl(test_nghttp3_conn_write_control) 40 | munit_void_test_decl(test_nghttp3_conn_submit_request) 41 | munit_void_test_decl(test_nghttp3_conn_http_request) 42 | munit_void_test_decl(test_nghttp3_conn_http_resp_header) 43 | munit_void_test_decl(test_nghttp3_conn_http_req_header) 44 | munit_void_test_decl(test_nghttp3_conn_http_content_length) 45 | munit_void_test_decl(test_nghttp3_conn_http_content_length_mismatch) 46 | munit_void_test_decl(test_nghttp3_conn_http_non_final_response) 47 | munit_void_test_decl(test_nghttp3_conn_http_trailers) 48 | munit_void_test_decl(test_nghttp3_conn_http_ignore_content_length) 49 | munit_void_test_decl(test_nghttp3_conn_http_record_request_method) 50 | munit_void_test_decl(test_nghttp3_conn_http_error) 51 | munit_void_test_decl(test_nghttp3_conn_qpack_blocked_stream) 52 | munit_void_test_decl(test_nghttp3_conn_qpack_decoder_cancel_stream) 53 | munit_void_test_decl(test_nghttp3_conn_just_fin) 54 | munit_void_test_decl(test_nghttp3_conn_submit_response_read_blocked) 55 | munit_void_test_decl(test_nghttp3_conn_submit_info) 56 | munit_void_test_decl(test_nghttp3_conn_recv_uni) 57 | munit_void_test_decl(test_nghttp3_conn_recv_goaway) 58 | munit_void_test_decl(test_nghttp3_conn_shutdown_server) 59 | munit_void_test_decl(test_nghttp3_conn_shutdown_client) 60 | munit_void_test_decl(test_nghttp3_conn_priority_update) 61 | munit_void_test_decl(test_nghttp3_conn_request_priority) 62 | munit_void_test_decl(test_nghttp3_conn_set_stream_priority) 63 | munit_void_test_decl(test_nghttp3_conn_shutdown_stream_read) 64 | munit_void_test_decl(test_nghttp3_conn_stream_data_overflow) 65 | munit_void_test_decl(test_nghttp3_conn_get_frame_payload_left) 66 | munit_void_test_decl(test_nghttp3_conn_update_ack_offset) 67 | munit_void_test_decl(test_nghttp3_conn_set_client_stream_priority) 68 | munit_void_test_decl(test_nghttp3_conn_rx_http_state) 69 | munit_void_test_decl(test_nghttp3_conn_push) 70 | 71 | #endif /* !defined(NGHTTP3_CONN_TEST_H) */ 72 | -------------------------------------------------------------------------------- /tests/nghttp3_conv_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2020 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "nghttp3_conv_test.h" 26 | 27 | #include 28 | #include 29 | 30 | #include "nghttp3_conv.h" 31 | #include "nghttp3_test_helper.h" 32 | -------------------------------------------------------------------------------- /tests/nghttp3_conv_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2020 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_CONV_TEST_H 26 | #define NGHTTP3_CONV_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #endif /* !defined(NGHTTP3_CONV_TEST_H) */ 33 | -------------------------------------------------------------------------------- /tests/nghttp3_http_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2020 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_HTTP_TEST_H 26 | #define NGHTTP3_HTTP_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #define MUNIT_ENABLE_ASSERT_ALIASES 33 | 34 | #include "munit.h" 35 | 36 | extern const MunitSuite http_suite; 37 | 38 | munit_void_test_decl(test_nghttp3_http_parse_priority) 39 | munit_void_test_decl(test_nghttp3_check_header_value) 40 | munit_void_test_decl(test_nghttp3_check_header_name) 41 | 42 | #endif /* !defined(NGHTTP3_HTTP_TEST_H) */ 43 | -------------------------------------------------------------------------------- /tests/nghttp3_qpack_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_QPACK_TEST_H 26 | #define NGHTTP3_QPACK_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #define MUNIT_ENABLE_ASSERT_ALIASES 33 | 34 | #include "munit.h" 35 | 36 | extern const MunitSuite qpack_suite; 37 | 38 | munit_void_test_decl(test_nghttp3_qpack_encoder_encode) 39 | munit_void_test_decl(test_nghttp3_qpack_encoder_encode_try_encode) 40 | munit_void_test_decl(test_nghttp3_qpack_encoder_still_blocked) 41 | munit_void_test_decl(test_nghttp3_qpack_encoder_set_dtable_cap) 42 | munit_void_test_decl(test_nghttp3_qpack_decoder_feedback) 43 | munit_void_test_decl(test_nghttp3_qpack_decoder_stream_overflow) 44 | munit_void_test_decl(test_nghttp3_qpack_huffman) 45 | munit_void_test_decl(test_nghttp3_qpack_huffman_decode_failure_state) 46 | munit_void_test_decl(test_nghttp3_qpack_decoder_reconstruct_ricnt) 47 | munit_void_test_decl(test_nghttp3_qpack_decoder_read_encoder) 48 | munit_void_test_decl(test_nghttp3_qpack_encoder_read_decoder) 49 | 50 | #endif /* !defined(NGHTTP3_QPACK_TEST_H) */ 51 | -------------------------------------------------------------------------------- /tests/nghttp3_stream_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2025 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_STREAM_TEST_H 26 | #define NGHTTP3_STREAM_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #define MUNIT_ENABLE_ASSERT_ALIASES 33 | 34 | #include "munit.h" 35 | 36 | extern const MunitSuite stream_suite; 37 | 38 | munit_void_test_decl(test_nghttp3_read_varint) 39 | 40 | #endif /* !defined(NGHTTP3_STREAM_TEST_H) */ 41 | -------------------------------------------------------------------------------- /tests/nghttp3_test_helper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_TEST_HELPER 26 | #define NGHTTP3_TEST_HELPER 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #include "nghttp3_buf.h" 33 | #include "nghttp3_frame.h" 34 | #include "nghttp3_qpack.h" 35 | 36 | #define MAKE_NV(NAME, VALUE) \ 37 | { \ 38 | .name = (uint8_t *)(NAME), \ 39 | .value = (uint8_t *)(VALUE), \ 40 | .namelen = sizeof((NAME)) - 1, \ 41 | .valuelen = sizeof((VALUE)) - 1, \ 42 | } 43 | 44 | /* 45 | * strsize macro returns the length of string literal |S|. 46 | */ 47 | #define strsize(S) (sizeof(S) - 1) 48 | 49 | /* 50 | * nghttp3_write_frame writes |fr| to |dest|. This function 51 | * calculates the payload length and assigns it to fr->hd.length; 52 | */ 53 | void nghttp3_write_frame(nghttp3_buf *dest, nghttp3_frame *fr); 54 | 55 | /* 56 | * nghttp3_write_frame_qpack writes |fr| to |dest|. |fr| is supposed 57 | * to be a frame which uses QPACK encoder |qenc|. |qenc| must be 58 | * configured so that it does not use dynamic table. This function 59 | * calculates the payload length and assigns it to fr->hd.length; 60 | */ 61 | void nghttp3_write_frame_qpack(nghttp3_buf *dest, nghttp3_qpack_encoder *qenc, 62 | int64_t stream_id, nghttp3_frame *fr); 63 | 64 | /* 65 | * nghttp3_write_frame_qpack_dyn is similar to 66 | * nghttp3_write_frame_qpack but it can use dynamic table. The it 67 | * will write encoder stream to |ebuf|. 68 | */ 69 | void nghttp3_write_frame_qpack_dyn(nghttp3_buf *dest, nghttp3_buf *ebuf, 70 | nghttp3_qpack_encoder *qenc, 71 | int64_t stream_id, nghttp3_frame *fr); 72 | 73 | /* 74 | * nghttp3_write_frame_data writes DATA frame which has |len| bytes of 75 | * payload. 76 | */ 77 | void nghttp3_write_frame_data(nghttp3_buf *dest, size_t len); 78 | 79 | /* 80 | * nghttp3_decode_frame_hd decodes frame header out of |vec| of length 81 | * |veccnt|. It returns the number of bytes read if it succeeds, or 82 | * negative error code. 83 | */ 84 | nghttp3_ssize nghttp3_decode_frame_hd(nghttp3_frame_hd *hd, 85 | const nghttp3_vec *vec, size_t veccnt); 86 | 87 | /* 88 | * nghttp3_decode_priority_update_frame decodes PRIORITY_UPDATE frame 89 | * out of |vec| of length |veccnt|. It returns the number of bytes 90 | * read if it succeeds, or a negative error code. 91 | */ 92 | nghttp3_ssize 93 | nghttp3_decode_priority_update_frame(nghttp3_frame_priority_update *fr, 94 | const nghttp3_vec *vec, size_t veccnt); 95 | 96 | /* 97 | * nghttp3_decode_settings_frame decodes SETTINGS frame out of |vec| 98 | * of length |veccnt|. |fr| should have enough space to store 99 | * settings. This function does not produce more than 16 settings. 100 | * If the given buffer contains more than 16 settings, this function 101 | * returns NGHTTP3_ERR_INVALID_ARGUMENT. It returns the number of 102 | * bytes read if it succeeds, or a negative error code. 103 | */ 104 | nghttp3_ssize nghttp3_decode_settings_frame(nghttp3_frame_settings *fr, 105 | const nghttp3_vec *vec, 106 | size_t veccnt); 107 | 108 | #endif /* !defined(NGHTTP3_TEST_HELPER) */ 109 | -------------------------------------------------------------------------------- /tests/nghttp3_tnode_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "nghttp3_tnode_test.h" 26 | 27 | #include 28 | 29 | #include "nghttp3_tnode.h" 30 | #include "nghttp3_macro.h" 31 | #include "nghttp3_test_helper.h" 32 | 33 | static const MunitTest tests[] = { 34 | munit_void_test(test_nghttp3_tnode_schedule), 35 | munit_test_end(), 36 | }; 37 | 38 | const MunitSuite tnode_suite = { 39 | .prefix = "/tnode", 40 | .tests = tests, 41 | }; 42 | 43 | static int cycle_less(const nghttp3_pq_entry *lhsx, 44 | const nghttp3_pq_entry *rhsx) { 45 | const nghttp3_tnode *lhs = nghttp3_struct_of(lhsx, nghttp3_tnode, pe); 46 | const nghttp3_tnode *rhs = nghttp3_struct_of(rhsx, nghttp3_tnode, pe); 47 | 48 | if (lhs->cycle == rhs->cycle) { 49 | return lhs->id < rhs->id; 50 | } 51 | 52 | return rhs->cycle - lhs->cycle <= NGHTTP3_TNODE_MAX_CYCLE_GAP; 53 | } 54 | 55 | void test_nghttp3_tnode_schedule(void) { 56 | const nghttp3_mem *mem = nghttp3_mem_default(); 57 | nghttp3_tnode node, node2; 58 | nghttp3_pq pq; 59 | int rv; 60 | nghttp3_pq_entry *ent; 61 | nghttp3_tnode *p; 62 | 63 | /* Schedule node with incremental enabled */ 64 | nghttp3_tnode_init(&node, 0); 65 | node.pri.inc = 1; 66 | 67 | nghttp3_pq_init(&pq, cycle_less, mem); 68 | 69 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 70 | 71 | assert_int(0, ==, rv); 72 | assert_uint64(0, ==, node.cycle); 73 | 74 | /* Schedule another node */ 75 | nghttp3_tnode_init(&node2, 1); 76 | node.pri.inc = 1; 77 | 78 | rv = nghttp3_tnode_schedule(&node2, &pq, 0); 79 | 80 | assert_int(0, ==, rv); 81 | 82 | /* Rescheduling node with nwrite > 0 */ 83 | 84 | rv = nghttp3_tnode_schedule(&node, &pq, 1000); 85 | 86 | assert_int(0, ==, rv); 87 | assert_uint64(1, ==, node.cycle); 88 | 89 | /* Rescheduling node with nwrite == 0 */ 90 | 91 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 92 | 93 | assert_int(0, ==, rv); 94 | assert_uint64(1, ==, node.cycle); 95 | 96 | nghttp3_pq_free(&pq); 97 | 98 | /* Schedule node without incremental */ 99 | nghttp3_tnode_init(&node, 0); 100 | 101 | nghttp3_pq_init(&pq, cycle_less, mem); 102 | 103 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 104 | 105 | assert_int(0, ==, rv); 106 | assert_uint64(0, ==, node.cycle); 107 | 108 | /* Schedule another node */ 109 | nghttp3_tnode_init(&node2, 1); 110 | 111 | rv = nghttp3_tnode_schedule(&node2, &pq, 0); 112 | 113 | assert_int(0, ==, rv); 114 | 115 | /* Rescheduling node with nwrite > 0 */ 116 | 117 | rv = nghttp3_tnode_schedule(&node, &pq, 1000); 118 | 119 | assert_int(0, ==, rv); 120 | assert_uint64(0, ==, node.cycle); 121 | 122 | /* Rescheduling node with nwrit == 0 */ 123 | 124 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 125 | 126 | assert_int(0, ==, rv); 127 | assert_uint64(0, ==, node.cycle); 128 | 129 | nghttp3_pq_free(&pq); 130 | 131 | /* Stream with lower stream ID takes precedence */ 132 | nghttp3_pq_init(&pq, cycle_less, mem); 133 | 134 | nghttp3_tnode_init(&node2, 1); 135 | 136 | rv = nghttp3_tnode_schedule(&node2, &pq, 0); 137 | 138 | assert_int(0, ==, rv); 139 | 140 | nghttp3_tnode_init(&node, 0); 141 | 142 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 143 | 144 | assert_int(0, ==, rv); 145 | 146 | ent = nghttp3_pq_top(&pq); 147 | 148 | p = nghttp3_struct_of(ent, nghttp3_tnode, pe); 149 | 150 | assert_int64(0, ==, p->id); 151 | 152 | nghttp3_pq_free(&pq); 153 | 154 | /* Check the same reversing push order */ 155 | nghttp3_pq_init(&pq, cycle_less, mem); 156 | 157 | nghttp3_tnode_init(&node, 0); 158 | 159 | rv = nghttp3_tnode_schedule(&node, &pq, 0); 160 | 161 | assert_int(0, ==, rv); 162 | 163 | nghttp3_tnode_init(&node2, 1); 164 | 165 | rv = nghttp3_tnode_schedule(&node2, &pq, 0); 166 | 167 | assert_int(0, ==, rv); 168 | 169 | ent = nghttp3_pq_top(&pq); 170 | 171 | p = nghttp3_struct_of(ent, nghttp3_tnode, pe); 172 | 173 | assert_int64(0, ==, p->id); 174 | 175 | nghttp3_pq_free(&pq); 176 | } 177 | -------------------------------------------------------------------------------- /tests/nghttp3_tnode_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nghttp3 3 | * 4 | * Copyright (c) 2019 nghttp3 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGHTTP3_TNODE_TEST_H 26 | #define NGHTTP3_TNODE_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* defined(HAVE_CONFIG_H) */ 31 | 32 | #define MUNIT_ENABLE_ASSERT_ALIASES 33 | 34 | #include "munit.h" 35 | 36 | extern const MunitSuite tnode_suite; 37 | 38 | munit_void_test_decl(test_nghttp3_tnode_schedule) 39 | 40 | #endif /* !defined(NGHTTP3_TNODE_TEST_H) */ 41 | --------------------------------------------------------------------------------