Skip to content

Commit 7519a7a

Browse files
committed
Properly close stream on HTTP2 failure
1 parent af02b68 commit 7519a7a

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ asan: CFLAGS+=-fsanitize=address
2222
asan: LDFLAGS+=-fsanitize=address
2323
asan: dohd
2424

25-
dohd: url64.o libevquick.o mempool.o odoh.o proxy_auth.o dohd.o
25+
dohd: url64.o libevquick.o mempool.o odoh.o proxy_auth.o h2_session.o dohd.o
2626
gcc -o $@ $^ $(LDFLAGS)
2727
clean:
2828
rm -f *.o dohd

src/dohd.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "mempool.h"
3838
#include "odoh.h"
3939
#include "proxy_auth.h"
40+
#include "h2_session.h"
4041
#include <nghttp2/nghttp2.h>
4142
#include <netinet/tcp.h>
4243
#include <fcntl.h>
@@ -1018,13 +1019,7 @@ static ssize_t h2_cb_send(nghttp2_session *session, const uint8_t *data,
10181019
struct client_data *cd = (struct client_data *)user_data;
10191020
(void)session;
10201021
(void)flags;
1021-
int ret;
1022-
ret = client_ssl_write(cd, data, length);
1023-
1024-
if (nghttp2_session_want_write(session)) {
1025-
nghttp2_session_send(session);
1026-
}
1027-
return ret;
1022+
return client_ssl_write(cd, data, length);
10281023
}
10291024

10301025

@@ -1111,12 +1106,9 @@ static int h2_cb_on_stream_close(nghttp2_session *session, int32_t stream_id,
11111106
struct client_data *cd = (struct client_data *)user_data;
11121107
struct req_slot *req =
11131108
nghttp2_session_get_stream_user_data(session, stream_id);
1114-
if (!req)
1115-
return -1;
1116-
if (cd != req->owner)
1117-
return -1;
11181109
(void)error_code;
1119-
if (req) {
1110+
if (dohd_h2_stream_close_action(req != NULL,
1111+
req != NULL && cd == req->owner) == DOHD_H2_STREAM_CLOSE_DESTROY) {
11201112
dohd_destroy_request(req);
11211113
}
11221114
return 0;
@@ -1281,12 +1273,15 @@ static void tls_read(__attribute__((unused)) int fd, short __attribute__((unused
12811273
readlen = nghttp2_session_mem_recv(cd->h2_session, buff, ret);
12821274
if (readlen < 0) {
12831275
dohprint(DOH_WARN, "NGHTTP2 error: %s\n", nghttp2_strerror((int)readlen));
1276+
dohd_destroy_client(cd);
12841277
return;
12851278
}
12861279
while (nghttp2_session_want_write(cd->h2_session)) {
12871280
ret = nghttp2_session_send(cd->h2_session);
12881281
if (ret < 0) {
12891282
dohprint(DOH_WARN, "NGHTTP2 error: %s\n", nghttp2_strerror((int)ret));
1283+
dohd_destroy_client(cd);
1284+
return;
12901285
}
12911286
}
12921287
} else {

test/Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CFLAGS := -I../src -O0 -ggdb -Wall -Wextra -Wno-sign-compare \
88
CFLAGS += $(if $(shell ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1), -D_MUSL_,)
99

1010
# Default target: build all unit tests
11-
all: dohd_url64_test dohd_url64_extended_test dohd_heap_test dohd_dns_parser_test dohd_mempool_test
11+
all: dohd_url64_test dohd_url64_extended_test dohd_heap_test dohd_dns_parser_test dohd_mempool_test dohd_h2_session_test
1212

1313
# Run all unit tests
1414
check: all
@@ -29,6 +29,9 @@ check: all
2929
@echo "--- Memory Pool Tests ---"
3030
./dohd_mempool_test
3131
@echo ""
32+
@echo "--- HTTP/2 Session Tests ---"
33+
./dohd_h2_session_test
34+
@echo ""
3235
@echo "=== All Unit Tests Passed ==="
3336

3437
# Original url64 test
@@ -51,6 +54,10 @@ dohd_dns_parser_test: test_dns_parser.o
5154
dohd_mempool_test: mempool.o test_mempool.o
5255
${CC} -o dohd_mempool_test mempool.o test_mempool.o
5356

57+
# HTTP/2 session helper test
58+
dohd_h2_session_test: h2_session.o test_h2_session.o
59+
${CC} -o dohd_h2_session_test h2_session.o test_h2_session.o
60+
5461
# Object files
5562
url64.o: ../src/url64.c
5663
${CC} ${CFLAGS} -c -o url64.o ../src/url64.c
@@ -73,9 +80,15 @@ test_dns_parser.o: test_dns_parser.c
7380
mempool.o: ../src/mempool.c ../src/mempool.h
7481
${CC} ${CFLAGS} -c -o mempool.o ../src/mempool.c
7582

83+
h2_session.o: ../src/h2_session.c ../src/h2_session.h
84+
${CC} ${CFLAGS} -c -o h2_session.o ../src/h2_session.c
85+
7686
test_mempool.o: test_mempool.c ../src/mempool.h
7787
${CC} ${CFLAGS} -c -o test_mempool.o test_mempool.c
7888

89+
test_h2_session.o: test_h2_session.c ../src/h2_session.h
90+
${CC} ${CFLAGS} -c -o test_h2_session.o test_h2_session.c
91+
7992
# Integration tests (require running dohd)
8093
integration: gen-certs
8194
@echo "=== Running Integration Tests ==="
@@ -126,7 +139,7 @@ gen-certs:
126139

127140
clean:
128141
rm -f *.o
129-
rm -f dohd_url64_test dohd_url64_extended_test dohd_heap_test dohd_dns_parser_test dohd_mempool_test
142+
rm -f dohd_url64_test dohd_url64_extended_test dohd_heap_test dohd_dns_parser_test dohd_mempool_test dohd_h2_session_test
130143
rm -f test.crt test.key valgrind.log
131144

132145
.PHONY: all check integration valgrind gen-certs clean

0 commit comments

Comments
 (0)