From c07fea1aa5fa8147a4f43929fff6d71ec17f01fb Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Wed, 10 Apr 2019 19:28:09 +0100 Subject: [PATCH] HTTP1 example server: don't crash on empty file served (#962) Motivation: The file-io implementation in the example HTTP1 server just crashed on empty file ;). Modifications: - fix the crash - add integration test Result: fewer crashes (cherry picked from commit c90b159b4fd0ea56d4a5f0032d3b85b184032e7e) --- IntegrationTests/tests_01_http/test_01_get_file.sh | 9 ++++++--- Sources/NIOHTTP1Server/main.swift | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/IntegrationTests/tests_01_http/test_01_get_file.sh b/IntegrationTests/tests_01_http/test_01_get_file.sh index 6c8307ee3..ef6a19017 100644 --- a/IntegrationTests/tests_01_http/test_01_get_file.sh +++ b/IntegrationTests/tests_01_http/test_01_get_file.sh @@ -19,8 +19,11 @@ token=$(create_token) start_server "$token" htdocs=$(get_htdocs "$token") echo FOO BAR > "$htdocs/some_file.txt" -for method in sendfile fileio; do - do_curl "$token" "http://foobar.com/$method/some_file.txt" > "$tmp/out.txt" - assert_equal_files "$htdocs/some_file.txt" "$tmp/out.txt" +touch "$htdocs/empty" +for file in some_file.txt empty; do + for method in sendfile fileio; do + do_curl "$token" "http://foobar.com/$method/$file" > "$tmp/out.txt" + assert_equal_files "$htdocs/$file" "$tmp/out.txt" + done done stop_server "$token" diff --git a/Sources/NIOHTTP1Server/main.swift b/Sources/NIOHTTP1Server/main.swift index 771979a91..3a577b504 100644 --- a/Sources/NIOHTTP1Server/main.swift +++ b/Sources/NIOHTTP1Server/main.swift @@ -333,6 +333,10 @@ private final class HTTPHandler: ChannelInboundHandler { case .nonblockingFileIO: var responseStarted = false let response = responseHead(request: request, fileRegion: region) + if region.readableBytes == 0 { + responseStarted = true + context.write(self.wrapOutboundOut(.head(response)), promise: nil) + } return self.fileIO.readChunked(fileRegion: region, chunkSize: 32 * 1024, allocator: context.channel.allocator,