@@ -51,11 +51,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
5151 """
5252 @ spec init ( :cowboy_req . req ( ) , state :: init_state ) :: init_result
5353 def init ( req , { endpoint , { _name , server } , route , opts } = state ) do
54- http_method =
55- req
56- |> :cowboy_req . method ( )
57- |> String . downcase ( )
58- |> String . to_existing_atom ( )
54+ http_method = extract_http_method ( req ) |> String . to_existing_atom ( )
5955
6056 with { :ok , access_mode , sub_type , content_type } <- find_content_type_subtype ( req ) ,
6157 { :ok , codec } <- find_codec ( sub_type , content_type , server ) ,
@@ -136,11 +132,31 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
136132 content_type
137133 end
138134
139- { :ok , access_mode , subtype } = extract_subtype ( content_type )
135+ http_method = extract_http_method ( req )
136+
137+ { :ok , access_mode , subtype } =
138+ case extract_subtype ( content_type ) do
139+ { :ok , :unknown , "unknown" } ->
140+ if http_method == "post" do
141+ { :ok , :grpc , "proto" }
142+ else
143+ { :ok , :http_transcoding , "json" }
144+ end
145+
146+ resp ->
147+ resp
148+ end
149+
140150 access_mode = resolve_access_mode ( req , access_mode , subtype )
141151 { :ok , access_mode , subtype , content_type }
142152 end
143153
154+ defp extract_http_method ( req ) do
155+ req
156+ |> :cowboy_req . method ( )
157+ |> String . downcase ( )
158+ end
159+
144160 defp find_compressor ( req , server ) do
145161 encoding = :cowboy_req . header ( "grpc-encoding" , req )
146162
@@ -627,8 +643,9 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
627643 defp extract_subtype ( "application/grpc-web-text+" <> rest ) , do: { :ok , :grpcweb , rest }
628644
629645 defp extract_subtype ( type ) do
630- Logger . warning ( "Got unknown content-type #{ type } , please create an issue." )
631- { :ok , :grpc , "proto" }
646+ Logger . warning ( "Got unknown content-type #{ type } , please create an issue. " )
647+
648+ { :ok , :unknown , "unknown" }
632649 end
633650
634651 defp resolve_access_mode ( % { version: "HTTP/1.1" } , _detected_access_mode , _type_subtype ) ,
0 commit comments