elixir-mint / mint

Functional HTTP client for Elixir with support for HTTP/1 and HTTP/2 🌱
Apache License 2.0
1.37k stars 112 forks source link

Mint.HTTP1.Request private fn validate_header_value throws Erlang error: {:bad_generator, nil} when value is nil #393

Closed marinac-dev closed 1 year ago

marinac-dev commented 1 year ago

If the value of header is nil it will just throw error.

** (ErlangError) Erlang error: {:bad_generator, nil}
    (mint 1.5.1) lib/mint/http1/request.ex:85: Mint.HTTP1.Request."-validate_header_value!/2-lc$^0/1-0-"/3
    (mint 1.5.1) lib/mint/http1/request.ex:85: Mint.HTTP1.Request.validate_header_value!/2
    (mint 1.5.1) lib/mint/http1/request.ex:27: anonymous fn/2 in Mint.HTT
    # . . . Stack strace

I think I could make a bug reproducing repo in free time, but what caused this in my case is a dependency. In dependency, there is macro @api_key Application.compile_env(:my_app, :api_key) (yes I know I shouldn't use that, I will change it) which requires setting it in somewhere in config.exs but if I don't do that header value becomes nil and bum bug. :)

In my config, I use System.get_env to fetch header value I am using Tesla with Finch adapter

ericmj commented 1 year ago

We do not accept nil in the headers as seen in the typespec: https://hexdocs.pm/mint/Mint.Types.html#t:headers/0. Handling nil would mean we have to make the decision if the header should be removed or the value should be "". This decision will be different for different applications so mint cannot make it.

If you are using System.get_env it takes a second argument where you can give a default value that isn't nil.