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

Add support for gleam v1.0.0 #20

Merged
merged 4 commits into from
Mar 20, 2024
Merged

Add support for gleam v1.0.0 #20

merged 4 commits into from
Mar 20, 2024

Conversation

BradLewis
Copy link
Contributor

This PR contains the necessary fixes to have gleam_cowboy work with gleam v1.0.0.

In particular it changes:

  • replace gleam/map with gleam/list
  • replace gleam/bit_builder with gleam/bytes_builder
  • lock cowboy to 2.10 due to some changes with the management of cookie headers in the cowboy library. I wasn't able to figure out how to allow for cowboy > 2.0 but < 2.11, so let me know if there's a better way to do this!

I've also updated the example in the README reflect these changes.

Let me know what you think!

Resolves: #19

gleam.toml Outdated
@@ -14,7 +14,7 @@ links = [
gleam_stdlib = "~> 0.31"
gleam_http = "~> 3.0"
gleam_otp = "~> 0.7"
cowboy = "~> 2.0"
cowboy = "~> 2.10.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've downgraded the requirement here to be very pessimistic. Before it was installing 2.x, now it's 2.10.x. Could you revert this please? 🙏

Copy link
Contributor Author

@BradLewis BradLewis Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I addressed this in the third dot point, but basically with cowboy 2.11.0 they changed the way they handled cookie headers. In particular, you get this error when running the tests:

=ERROR REPORT==== 17-Mar-2024::16:54:14.917416 ===
Ranch listener {gleam_cowboy,#Ref<0.2594036081.850657288.229963>}, connection process <0.573.0>, stream 1 had its request process <0.574.0> exit with reason {response_error,invalid_header,'Response cookies must be set using cowboy_req:set_resp_cookie/3,4.'} and stacktrace [{cowboy_req,reply,4,[{file,"/Users/bradlewis/repos/cowboy/build/dev/erlang/cowboy/src/cowboy_req.erl"},{line,813}]},{gleam_cowboy_native,init,2,[{file,"/Users/bradlewis/repos/cowboy/build/dev/erlang/gleam_cowboy/_gleam_artefacts/gleam_cowboy_native.erl"},{line,22}]},{cowboy_handler,execute,2,[{file,"/Users/bradlewis/repos/cowboy/build/dev/erlang/cowboy/src/cowboy_handler.erl"},{line,37}]},{cowboy_stream_h,execute,3,[{file,"/Users/bradlewis/repos/cowboy/build/dev/erlang/cowboy/src/cowboy_stream_h.erl"},{line,306}]},{cowboy_stream_h,request_process,3,[{file,"/Users/bradlewis/repos/cowboy/build/dev/erlang/cowboy/src/cowboy_stream_h.erl"},{line,295}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,241}]}]

I'll have a quick look into seeing if I can update the lib to support cowboy_req:set_resp_cookie instead to keep the requirement as is.

For context, it's the same problem as described for plug elixir-plug/plug_cowboy#98

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roger. Let's go for ~> 2.11 to be on the right side of that breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a bit of trial and error I managed to get it to work. I needed to update the CowboyRequest object directly, which is the path that was suggested here: ninenines/cowboy#1624 (comment) and the solution chosen by the elixir cowboy project.

This lets us keep the requirement back to just ~> 2.0. Let me know what you think of this.

FWIW I did look into using the cowboy_req:set_resp_cookies function, but I was unable to get that to work, both due to parsing the cookie string dropping some information, and not being able to pass the opts easily.

Cowboy 2.11.0 removed the option of setting cookie headers in `cowboy_req:reply`, `cowboy_req:set_resp_header` or `cowboy_req:set_resp_headers`
To solve this we update the CowboyRequest headers directly, as suggested here: ninenines/cowboy#1624 (comment)
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the CI version too? The build will fail otherwise.

Does this approach to headers work for older versions of Cowboy too?

@BradLewis
Copy link
Contributor Author

Yeah this works with all of the older versions. Though after testing these older versions, there are other issues with versions < 2.5.0, so I'll update the dependencies to reflect that.

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@lpil lpil merged commit d61776f into gleam-lang:main Mar 20, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support gleam 1.0.0
2 participants