From 12afcb98edc34f1ea243b2121126afc48dd70ec9 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 7 Aug 2025 15:26:03 +0200 Subject: [PATCH 1/2] Improve error message on invalid headers --- lib/hpax.ex | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/hpax.ex b/lib/hpax.ex index 91c9401..bc271f9 100644 --- a/lib/hpax.ex +++ b/lib/hpax.ex @@ -298,7 +298,8 @@ defmodule HPAX do end defp encode_headers([{action, name, value} | rest], table, acc) - when action in @valid_header_actions and is_binary(name) and is_binary(value) do + when action in @valid_header_actions do + validate_header(name, value) huffman? = table.huffman_encoding == :always {encoded, table} = @@ -330,6 +331,15 @@ defmodule HPAX do encode_headers(rest, table, [acc, encoded]) end + defp validate_header(name, value) when is_binary(name) and is_binary(value) do + :ok + end + + defp validate_header(name, value) do + raise ArgumentError, + "expected header name/value to be strings, got: #{inspect(name)}/#{inspect(value)}" + end + defp encode_indexed_header(index) do <<1::1, Types.encode_integer(index, 7)::bitstring>> end From 138451c6e3952dc77e541617d229a7e614bc0940 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Thu, 7 Aug 2025 17:34:50 +0200 Subject: [PATCH 2/2] Add test --- test/hpax_test.exs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/hpax_test.exs b/test/hpax_test.exs index 72bcc68..5d1da24 100644 --- a/test/hpax_test.exs +++ b/test/hpax_test.exs @@ -95,6 +95,22 @@ defmodule HPAXTest do assert dec_table.entries == [{"b", "B"}, {"a", "A"}] end + test "encode/3 with invalid headers" do + table = HPAX.new(1000) + + assert_raise ArgumentError, + ~s(expected header name/value to be strings, got: :foo/"bar"), + fn -> + HPAX.encode([{:store, :foo, "bar"}], table) + end + + assert_raise ArgumentError, + ~s(expected header name/value to be strings, got: "foo"/:bar), + fn -> + HPAX.encode([{:store, "foo", :bar}], table) + end + end + property "encode/3 with a single action" do table = HPAX.new(500)