Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
LOCAL_CLEAN=$(shell git diff-index --quiet HEAD -- && echo 1)

test:
mix test
@mix deps.get
mix test

release:
ifeq ($(LOCAL_CLEAN), 1)
Expand Down
33 changes: 25 additions & 8 deletions lib/grpc/server/adapters/cowboy/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
"""
@spec init(:cowboy_req.req(), state :: init_state) :: init_result
def init(req, {endpoint, {_name, server}, route, opts} = state) do
http_method =
req
|> :cowboy_req.method()
|> String.downcase()
|> String.to_existing_atom()
http_method = extract_http_method(req) |> String.to_existing_atom()

with {:ok, access_mode, sub_type, content_type} <- find_content_type_subtype(req),
{:ok, codec} <- find_codec(sub_type, content_type, server),
Expand Down Expand Up @@ -136,11 +132,31 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
content_type
end

{:ok, access_mode, subtype} = extract_subtype(content_type)
http_method = extract_http_method(req)

{:ok, access_mode, subtype} =
case extract_subtype(content_type) do
{:ok, :unknown, "unknown"} ->
if http_method == "post" do
{:ok, :grpc, "proto"}
else
{:ok, :http_transcoding, "json"}
end

resp ->
resp
end

access_mode = resolve_access_mode(req, access_mode, subtype)
{:ok, access_mode, subtype, content_type}
end

defp extract_http_method(req) do
req
|> :cowboy_req.method()
|> String.downcase()
end

defp find_compressor(req, server) do
encoding = :cowboy_req.header("grpc-encoding", req)

Expand Down Expand Up @@ -627,8 +643,9 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
defp extract_subtype("application/grpc-web-text+" <> rest), do: {:ok, :grpcweb, rest}

defp extract_subtype(type) do
Logger.warning("Got unknown content-type #{type}, please create an issue.")
{:ok, :grpc, "proto"}
Logger.warning("Got unknown content-type #{type}, please create an issue. ")

{:ok, :unknown, "unknown"}
end

defp resolve_access_mode(%{version: "HTTP/1.1"}, _detected_access_mode, _type_subtype),
Expand Down