Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url encode/decode status-message header #271

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/grpc/client/adapters/gun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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 @@ -215,7 +215,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