codegourmet

savory code and other culinary highlights

URL::InvalidURIError: The Scheme Http Does Not Accept Registry Part

| Comments

Mocking a web request with the excellent WebMock library lead to the following error:

1
2
URI::InvalidURIError: the scheme http does not accept registry part:↵
test_service.invalid (or bad hostname?)

Symptoms

This error occured whenever a domain contained _, e.g. an underscore character.

Solution

I was dealing with a mock URL anyway, I was able to replace the underscore with a hyphen -.

At the end of this rubyforums discussion you can find a relatively simple solution which solves this problem for cases where you have no control over the domain name:

1
2
uri_parser = URI::Parser.new(:HOSTNAME => "(?:[a-zA-Z0-9\\-._~]|%\\h\\h)+")
uri = uri_parser.parse("http://www_w.example.com")

Discussion

The aforementioned discussion has some interesting insights, on which I will give a brief summary here:

RFC 1738, section 3.1. explains the host scheme:

1
2
each domain label starting and ending with an alphanumerical character and
possibly also containing "-" characters

In short, URI won’t parse domain names with underscores. Those are officially invalid, even though they are used sometimes. Some people don’t have the luxury to just change the URI since some domain names contain an underscore and need to be accessed.

If you have no control over the domain, you can either

Happy Coding! – codegourmet

Comments