ruby / ruby/webrick

`WEBrick::HTTPUtils.escape(nil)` raises a confusing exception

Open
#96 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
327
Forks
112
Avg merge
4h 2m
Merged PRs (30d)
1

Description

% irb
> RUBY_VERSION
=> "3.1.2"

> require 'webrick'
> WEBrick::HTTPUtils.escape(nil)
/Users/katsuhiko.yoshida/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/webrick-1.7.0/lib/webrick/httputils.rb:444:in `_escape': undefined method `b' for nil:NilClass (NoMethodError)

      str = str.b
               ^^

(omit)
%irb
> RUBY_VERSION
=> "2.7.6"

> WEBrick::HTTPUtils.escape(nil)
Traceback (most recent call last):
        7: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/bin/irb:23:in `<main>'
        6: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/bin/irb:23:in `load'
        5: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
        4: from (irb):2
        3: from (irb):3:in `rescue in irb_binding'
        2: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/2.7.0/webrick/httputils.rb:467:in `escape'
        1: from /Users/katsuhiko.yoshida/.rbenv/versions/2.7.6/lib/ruby/2.7.0/webrick/httputils.rb:443:in `_escape'
NoMethodError (undefined method `b' for nil:NilClass)

WEBrick::HTTPUtils.escape_form(nil) is also the same. If the exception was raised in complex situations (e.g. in third party library), it will be difficult to determine the cause (parameter is nil).

My suggestions are following.

  1. Raises a builtin exception
    • CGI.escape is this behavior (raises a TypeError)
  2. Raises a custom exception
    • Like a EscapeError
  3. Returns ""
    • URI.encode_www_form_component and ERB::Util.url_encode are this behavior
    • But, BREAKING CHANGE (doesn't stop)

Thank you for your great work.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/webrick/httputils.rb around HTTPUtils.escape and _escape, then compare the reported nil behavior with escape_form(nil). Review the three proposed outcomes against CGI.escape, URI.encode_www_form_component, and ERB::Util.url_encode; done requires a maintainer decision on the intended nil behavior and corresponding coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.