add nghttp3 wrapping with futhark (#1)

This commit is contained in:
vladopajic
2025-04-16 14:28:15 +02:00
committed by GitHub
parent fe55b477f7
commit a53ade70e5
13 changed files with 2410 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@@ -0,0 +1,16 @@
# Git submodules dir
!libs/
!libs/*
# Ignore all test build files in tests folder (auto generated when running tests),
# by ignoring anything that does not have following file name scheme:
# has extension or is Dockerfile...
/tests/*
!/tests/*.*
# nim
nimble.develop
nimble.paths
# Build binary
generate_nghttp3

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "libs/nghttp3"]
path = libs/nghttp3
url = https://github.com/ngtcp2/nghttp3

1
.tool-versions Normal file
View File

@@ -0,0 +1 @@
nim 1.6.20

25
LICENSE-MIT Normal file
View File

@@ -0,0 +1,25 @@
nim-http3 is licensed under the MIT License
Copyright (c) 2025 Status Research & Development GmbH
-----------------------------------------------------
The MIT License (MIT)
Copyright (c) 2018 Status Research & Development GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8
Makefile Normal file
View File

@@ -0,0 +1,8 @@
.PHONY: build-source
build-source:
./build.sh
.PHONY: build-source-and-test
build-source-and-test:
./build.sh
nim c -d:debug --styleCheck:usages --styleCheck:error -r tests/test_nghttp3.nim

27
build.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
root=$(dirname "$0")
sources=${root}/libs
# Step 1: create new nghttp3.nim file
rm -f nghttp3.nim
touch nghttp3.nim
# Step 2: add prelude
cat "${root}/nghttp3_prelude.nim" >> nghttp3.nim
# Step 3: add compile directive for all c files
for file in `ls "${sources}/nghttp3/lib"/*.c`; do
echo "{.compile: \"$file\".}" >> nghttp3.nim
done
echo "{.compile: \"${sources}/nghttp3/lib/sfparse/sfparse.c\".}" >> nghttp3.nim
# Step 4: generate nghttp3 source and append it to nghttp3.nim
# generate nghttp3_source.nim using futhark
nimble install futhark@0.15.0
nim c --maxLoopIterationsVM:100000000 generate_nghttp3.nim
# removes absolute path prefix from comments "Generated based on"
sed -i 's/Generated based on.*\/nim-http3\/libs\//Generated based on \/nim-http3\/libs\//g' nghttp3_source.nim
# copy source to nghttp3.nim file
cat "${root}/nghttp3_source.nim" >> nghttp3.nim
# remove autogenerated nghttp3_source.nim file
rm -f nghttp3_source.nim

5
config.nims Normal file
View File

@@ -0,0 +1,5 @@
--styleCheck:usages
if (NimMajor, NimMinor) < (1, 6):
--styleCheck:hint
else:
--styleCheck:error

8
generate_nghttp3.nim Normal file
View File

@@ -0,0 +1,8 @@
import futhark, strformat
from os import parentDir, `/`
importc:
outputPath currentSourcePath.parentDir / "nghttp3_source.nim"
path currentSourcePath.parentDir/"libs/nghttp3/lib/includes"
"nghttp3/nghttp3.h"
"nghttp3/version.h"

7
http3.nimble Normal file
View File

@@ -0,0 +1,7 @@
packageName = "http3"
version = "0.0.1"
author = "Status Research & Development GmbH"
description = "http3 protocol implementation"
license = "MIT"
requires "nim >= 1.6.0"

2256
nghttp3.nim Normal file

File diff suppressed because it is too large Load Diff

3
nghttp3_prelude.nim Normal file
View File

@@ -0,0 +1,3 @@
########################################################
# This is auto generated file.
########################################################

1
tests/nim.cfg Normal file
View File

@@ -0,0 +1 @@
--path=".."

50
tests/test_nghttp3.nim Normal file
View File

@@ -0,0 +1,50 @@
import unittest
import nghttp3
test "basic http3 client request":
# callbacks
var callbacks: nghttp3_callbacks
zeroMem(addr callbacks, sizeof(callbacks))
callbacks.stream_close = proc (
conn: ptr nghttp3_conn,
stream_id: int64,
app_error_code: uint64,
user_data: pointer,
stream_user_data: pointer
): cint {.cdecl.} =
echo "Stream ", stream_id, " closed with code ", app_error_code
return 0
callbacks.recv_data = proc (
conn: ptr nghttp3_conn,
stream_id: int64,
buf: ptr uint8,
buflen: csize_t,
user_data: pointer,
stream_user_data: pointer
): cint {.cdecl.} =
let data = cast[string](buf)
echo "Received data (", buflen, " bytes) on stream ", stream_id, ": ", data
return 0
# settings
var settings: nghttp3_settings
nghttp3_settings_default_versioned(NGHTTP3_SETTINGS_V1, addr settings)
settings.qpack_max_dtable_capacity = 4096
settings.qpack_blocked_streams = 100
settings.max_field_section_size = 16384
var conn: ptr nghttp3_conn
let result = nghttp3_conn_client_new_versioned(
addr conn,
NGHTTP3_CALLBACKS_V1, addr callbacks,
NGHTTP3_SETTINGS_V1, addr settings,
nil, # mem
nil # user data
)
if result != 0:
echo "Failed to initialize nghttp3 client"
defer:
nghttp3_conn_del(conn)
echo "nghttp3 client connection created"