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 https://github.com/ruby/ruby/blob/v2_3_1/lib/webrick/httprequest.rb#L226.

I found with some debugging that the line that raises is this one calling parse_uri https://github.com/ruby/ruby/blob/v2_3_1/lib/webrick/httprequest.rb#L217, and within that, this line that sets the incoming request URI’s scheme https://github.com/ruby/ruby/blob/v2_3_1/lib/webrick/httprequest.rb#L461.

The forwarded proto comes from an HTTP header https://github.com/ruby/ruby/blob/v2_3_1/lib/webrick/httprequest.rb#L570, 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 a 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 https://github.com/ruby/ruby/commit/978ee6d1ef62b36c143103f7a229bfb8bf0f99c6.

Concluding words

tsk, grr

2 Likes