Here's why WEBrick doesn't work on Glitch

Background

If you try to serve a page from WEBrick, it fails with this message like this in the log:

bad URI `/'.

And there’s nothing particularly bad or unparsable about that, so it’s mysterious.
This post is about why that is.

Why

Our Glitch containers run Ubuntu 16.04, with the distro’s version of Ruby, 2.3.1.
https://packages.ubuntu.com/xenial/ruby
https://packages.ubuntu.com/xenial/ruby2.3

In that version of Ruby, the error comes from the HTTP request parser here ruby/lib/webrick/httprequest.rb at v2_3_1 · ruby/ruby · GitHub.

I found with some debugging that the line that raises is this one calling parse_uri ruby/lib/webrick/httprequest.rb at v2_3_1 · ruby/ruby · GitHub, and within that, this line that sets the incoming request URI’s scheme ruby/lib/webrick/httprequest.rb at v2_3_1 · ruby/ruby · GitHub.

The forwarded proto comes from an HTTP header ruby/lib/webrick/httprequest.rb at v2_3_1 · ruby/ruby · GitHub, which we do indeed receive from Glitch’s router.
We receive something like https,http,http, indicating that the request came from your browser over HTTPS, then the routing system forwarded it two times over HTTP.
But this string is an invalid value for the URI class’s scheme.
This raises an exception, which WEBrick swallows, providing its own unhelpful message.

What happened after Xenial’s feature freeze

Later, Ruby 2.4 came out with an enhancement to WEBrick to support comma separated X-Forwarded-Proto Multiple values X-Forwarded-Proto in webrick · ruby/ruby@978ee6d · GitHub.

Concluding words

tsk, grr

3 Likes