Skip to content

Commit

Permalink
url encode/decode status-message header (#271)
Browse files Browse the repository at this point in the history
Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
  • Loading branch information
drowzy and polvalente authored Oct 12, 2022
1 parent f21e2e1 commit b85a393
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/grpc/client/adapters/gun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ defmodule GRPC.Client.Adapters.Gun do
case :gun.await(conn_pid, stream_ref, timeout) do
{:response, :fin, status, headers} ->
if status == 200 do
headers = Enum.into(headers, %{})
headers = GRPC.Transport.HTTP2.decode_headers(headers)

case headers["grpc-status"] do
nil ->
Expand Down Expand Up @@ -245,7 +245,7 @@ defmodule GRPC.Client.Adapters.Gun do

{:response, :nofin, status, headers} ->
if status == 200 do
headers = Enum.into(headers, %{})
headers = GRPC.Transport.HTTP2.decode_headers(headers)

if headers["grpc-status"] && headers["grpc-status"] != "0" do
{:error,
Expand Down
10 changes: 8 additions & 2 deletions lib/grpc/transport/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule GRPC.Transport.HTTP2 do
def server_trailers(status \\ Status.ok(), message \\ "") do
%{
"grpc-status" => Integer.to_string(status),
"grpc-message" => message
"grpc-message" => URI.encode(message)
}
end

Expand Down Expand Up @@ -78,7 +78,7 @@ defmodule GRPC.Transport.HTTP2 do
if is_metadata(k) do
decode_metadata({k, v})
else
{k, v}
decode_reserved({k, v})
end
end)
end
Expand Down Expand Up @@ -142,6 +142,12 @@ defmodule GRPC.Transport.HTTP2 do
{key, val}
end

defp decode_reserved({"grpc-message" = key, val}) do
{key, URI.decode(val)}
end

defp decode_reserved(kv), do: kv

defp is_reserved_header(":" <> _), do: true
defp is_reserved_header("grpc-" <> _), do: true
defp is_reserved_header("content-type"), do: true
Expand Down
14 changes: 13 additions & 1 deletion test/grpc/transport/http2_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule GRPC.Transport.HTTP2Test do
use ExUnit.Case, async: true
alias GRPC.Channel
alias GRPC.{Channel, Status}
alias GRPC.Transport.HTTP2

alias GRPC.Client.Stream
Expand Down Expand Up @@ -113,4 +113,16 @@ defmodule GRPC.Transport.HTTP2Test do
HTTP2.server_headers(stream)
end
end

test "decode_headers/2 url decodes grpc-message" do
trailers = HTTP2.server_trailers(Status.unknown(), "Unknown error")
assert %{"grpc-message" => "Unknown error"} = HTTP2.decode_headers(trailers)
end

test "server_trailers/3 sets url encoded grpc-message" do
assert %{"grpc-message" => "Ok"} = HTTP2.server_trailers(Status.ok(), "Ok")

assert %{"grpc-message" => "Unknown%20error"} =
HTTP2.server_trailers(Status.unknown(), "Unknown error")
end
end

0 comments on commit b85a393

Please sign in to comment.