-
Notifications
You must be signed in to change notification settings - Fork 237
Support grpcweb trailers encoded in the message #481
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
267fe01
5997da7
51af767
054f370
f695359
2713352
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,9 +11,11 @@ defmodule GRPC.Message do | |||||
| Message -> *{binary octet} | ||||||
| """ | ||||||
|
|
||||||
| import Bitwise | ||||||
| alias GRPC.RPCError | ||||||
|
|
||||||
| @max_message_length Bitwise.bsl(1, 32 - 1) | ||||||
| @max_message_length bsl(1, 32 - 1) | ||||||
| @trailers_flag 0b1000_0000 | ||||||
|
|
||||||
| @doc """ | ||||||
| Transforms Protobuf data into a gRPC body binary. | ||||||
|
|
@@ -46,20 +48,21 @@ defmodule GRPC.Message do | |||||
| iolist = opts[:iolist] | ||||||
| codec = opts[:codec] | ||||||
| max_length = opts[:max_message_length] || @max_message_length | ||||||
| additional_flags = opts[:message_flag] || 0 | ||||||
|
|
||||||
| {compress_flag, message} = | ||||||
| {flag, message} = | ||||||
| if compressor do | ||||||
| {1, compressor.compress(message)} | ||||||
| {1 ||| additional_flags, compressor.compress(message)} | ||||||
| else | ||||||
| {0, message} | ||||||
| {0 ||| additional_flags, message} | ||||||
| end | ||||||
|
|
||||||
| length = IO.iodata_length(message) | ||||||
|
|
||||||
| if length > max_length do | ||||||
| {:error, "Encoded message is too large (#{length} bytes)"} | ||||||
| else | ||||||
| result = [compress_flag, <<length::size(4)-unit(8)>>, message] | ||||||
| result = [flag, <<length::size(4)-unit(8)>>, message] | ||||||
|
|
||||||
| result = | ||||||
| if function_exported?(codec, :pack_for_channel, 1), | ||||||
|
|
@@ -78,12 +81,14 @@ defmodule GRPC.Message do | |||||
| ## Examples | ||||||
|
|
||||||
| iex> GRPC.Message.from_data(<<0, 0, 0, 0, 8, 1, 2, 3, 4, 5, 6, 7, 8>>) | ||||||
| <<1, 2, 3, 4, 5, 6, 7, 8>> | ||||||
| {<<1, 2, 3, 4, 5, 6, 7, 8>>, <<>>} | ||||||
| """ | ||||||
| @spec from_data(binary) :: binary | ||||||
| @spec from_data(binary) :: {message :: binary, rest :: binary} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is technically a breaking change. I don't think it impacts as much if we merge before releasing 1.0, but we need to keep this in mind. |
||||||
| def from_data(data) do | ||||||
| <<_flag::unsigned-integer-size(8), _length::bytes-size(4), message::binary>> = data | ||||||
| message | ||||||
| <<_flag::unsigned-integer-size(8), length::big-32, message::bytes-size(length), rest::binary>> = | ||||||
| data | ||||||
|
|
||||||
| {message, rest} | ||||||
| end | ||||||
|
|
||||||
| @doc """ | ||||||
|
|
@@ -92,13 +97,16 @@ defmodule GRPC.Message do | |||||
| ## Examples | ||||||
|
|
||||||
| iex> GRPC.Message.from_data(%{compressor: nil}, <<0, 0, 0, 0, 8, 1, 2, 3, 4, 5, 6, 7, 8>>) | ||||||
| {:ok, <<1, 2, 3, 4, 5, 6, 7, 8>>} | ||||||
| {:ok, <<1, 2, 3, 4, 5, 6, 7, 8>>, <<>>} | ||||||
| """ | ||||||
| @spec from_data(map, binary) :: {:ok, binary} | {:error, GRPC.RPCError.t()} | ||||||
| @spec from_data(map, binary) :: | ||||||
| {:ok, message :: binary, rest :: binary} | ||||||
| | {:trailers, map, rest :: binary} | ||||||
| | {:error, GRPC.RPCError.t()} | ||||||
| def from_data(%{compressor: nil}, data) do | ||||||
| case data do | ||||||
| <<0, _length::bytes-size(4), message::binary>> -> | ||||||
| {:ok, message} | ||||||
| <<0, length::big-32, message::bytes-size(length), rest::binary>> -> | ||||||
| {:ok, message, rest} | ||||||
|
|
||||||
| <<1, _length::bytes-size(4), _::binary>> -> | ||||||
| {:error, | ||||||
|
|
@@ -107,24 +115,39 @@ defmodule GRPC.Message do | |||||
| message: "Compressed flag is set, but not specified in headers." | ||||||
| )} | ||||||
|
|
||||||
| <<@trailers_flag, length::big-32, message::bytes-size(length), rest::binary>> -> | ||||||
| {:trailers, parse_trailers(message), rest} | ||||||
|
|
||||||
| _ -> | ||||||
| {:error, RPCError.exception(status: :invalid_argument, message: "Message is malformed.")} | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def from_data(%{compressor: compressor}, data) do | ||||||
| case data do | ||||||
| <<1, _length::bytes-size(4), message::binary>> -> | ||||||
| {:ok, compressor.decompress(message)} | ||||||
| <<1, length::big-32, message::bytes-size(length), rest::binary>> -> | ||||||
| {:ok, compressor.decompress(message), rest} | ||||||
|
|
||||||
| <<0, _length::bytes-size(4), message::binary>> -> | ||||||
| {:ok, message} | ||||||
| <<0, length::big-32, message::bytes-size(length), rest::binary>> -> | ||||||
| {:ok, message, rest} | ||||||
|
|
||||||
| <<@trailers_flag, length::big-32, message::bytes-size(length), rest::binary>> -> | ||||||
| {:trailers, parse_trailers(message), rest} | ||||||
|
|
||||||
| _ -> | ||||||
| {:error, RPCError.exception(status: :invalid_argument, message: "Message is malformed.")} | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| defp parse_trailers(data) do | ||||||
| data | ||||||
| |> String.split("\r\n") | ||||||
| |> Enum.reduce(%{}, fn line, acc -> | ||||||
| [k, v] = String.split(line, ":") | ||||||
|
||||||
| [k, v] = String.split(line, ":") | |
| [k, v] = String.split(line, ":", parts: 2) |
Otherwise this can raise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see 054f370
Uh oh!
There was an error while loading. Please reload this page.