rails/rails — Issue #4379: Certain URLs trigger encoding errors when using action caching
Author: purcell
Created: 2012-01-07 15:04:37+00:00
State: open

Generate a fresh rails app, with a resource "things", and a things_controller.rb like this:

```ruby
class ThingsController < ApplicationController
caches_action :show

def show
render :text => 'hi there'
end
end
```

Now start the app in production mode (so caching is active), and request a URL like this:

curl 'http://localhost:3000/things/1.%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%82%C2%A8%C3%83%C2%A2%C3%A2%E2%80%9A%C2%AC%C3%82%C2%A8'

The result is the stacktrace at bottom. This is with Rails 3.1, but I've seen the problem in production for several Rails versions, apparently because some crawlers (-cough- bing) request invalid links which incorporate trailing punctuation.

Apologies that I have no idea how to reproduce this with a test right now, but if I'm pointed in the right direction, I'll be happy to help further.

```
Encoding::CompatibilityError in ThingsController#show

incompatible character encodings: ASCII-8BIT and UTF-8

activesupport (3.1.3) lib/active_support/core_ext/uri.rb:18:in `gsub'
activesupport (3.1.3) lib/active_support/core_ext/uri.rb:18:in `unescape'
actionpack (3.1.3) lib/action_controller/caching/actions.rb:179:in `normalize!'
actionpack (3.1.3) lib/action_controller/caching/actions.rb:172:in `initialize'
actionpack (3.1.3) lib/action_controller/caching/actions.rb:140:in `new'
actionpack (3.1.3) lib/action_controller/caching/actions.rb:140:in `filter'
activesupport (3.1.3) lib/active_support/callbacks.rb:313:in `around'
activesupport (3.1.3) lib/active_support/callbacks.rb:302:in `_callback_around_7'
activesupport (3.1.3) lib/active_support/callbacks.rb:206:in `_conditional_callback_around_18'
activesupport (3.1.3) lib/active_support/callbacks.rb:416:in `_run__758917200118941995__process_action__1057316068177780704__callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.3) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.3) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.3) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.3) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.3) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.3) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:89:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:532:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/flash.rb:247:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.3) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.1.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.3) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
rack-cache (1.1) lib/rack/cache/context.rb:132:in `forward'
rack-cache (1.1) lib/rack/cache/context.rb:241:in `fetch'
rack-cache (1.1) lib/rack/cache/context.rb:181:in `lookup'
rack-cache (1.1) lib/rack/cache/context.rb:65:in `call!'
rack-cache (1.1) lib/rack/cache/context.rb:50:in `call'
railties (3.1.3) lib/rails/engine.rb:456:in `call'
rack (1.3.6) lib/rack/content_length.rb:14:in `call'
railties (3.1.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/Users/steve/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/steve/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/steve/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Request

Parameters:

{"id"=>"1",
"format"=>"Ã¢â‚¬Â¨Ã¢â‚¬Â¨"}
```