{"id":147,"date":"2008-12-17T17:33:28","date_gmt":"2008-12-17T07:33:28","guid":{"rendered":"http:\/\/spiffystores.com.au\/blog\/?p=147"},"modified":"2021-09-01T07:08:59","modified_gmt":"2021-08-31T21:08:59","slug":"rails-222-memoization-gotchas","status":"publish","type":"post","link":"https:\/\/www.spiffystores.com.au\/blog\/2008\/12\/17\/rails-222-memoization-gotchas\/","title":{"rendered":"Rails 2.2.2 memoization gotchas"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">For various reasons, we are still running on Apache 1.3 which is fine, except for the fact that there is no easy way of determining whether a request is via an SSL session or not.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unfortunately, this process was broken by the upgrade to Rails 2.2.2.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">No probs. We just rewrite the URL to add an &#8220;ssl&#8221; prefix before it gets passed to the Rails engine.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"apacheconf\" class=\"language-apacheconf\">RewriteEngine On\nRewriteCond %{DOCUMENT_ROOT}\/%{REQUEST_FILENAME} !-f\nRewriteRule ^\/(.*)$ http:\/\/127.0.0.1:8000\/ssl\/$1 [P,L]\nProxyPassReverse \/ http:\/\/127.0.0.1:8000\/ssl\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">From there, a simple route will pass the request on to our SSL Proxy controller.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">map.connect 'ssl\/*suburi',\n            :controller => 'ssl_proxy',\n            :action => 'reprocess'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now all our &#8216;reprocess&#8217; method has to do is take the request, rewrite the URI to its original form, and set an environment variable to let us know it&#8217;s an SSL request.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby line-numbers\">class SslProxyController &lt; ActionController::Base\n  def reprocess\n    request.env[\"REQUEST_URI\"] = \"\/#{params[:suburi].join('\/')}\"\n    request.env[\"HTTPS\"] = \"on\"\n\n    controller = ActionController::Routing::Routes.recognize(request)\n    controller.process(request, response)\n    @performed_render = true\n  end\nend<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This all broke down with the upgrade to Rails 2.2.2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the new features of 2.2.2 is the addition of memoization to various system classes, including the &#8216;request&#8217; object. This means that &#8216;request_uri&#8217; is now memoized, and that even if we update the environment variable &#8216;REQUEST_URI&#8217;, the memoized value doesn&#8217;t change. When the route is recognized, it resolves to &#8216;reprocess&#8217; again, and we end up in an endless loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The answer turns to reasonably easy to implement. All we need to do is add the following line at the start of the &#8216;reprocess&#8217; method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">request.unmemoize_all if request.respond_to? :unmemoize_all<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code simply clears all the memoized variables, so they can all be loaded again using our new, proper URI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For various reasons, we are still running on Apache 1.3 which is fine, except for the fact that there is no easy way of determining whether a request is via an SSL session or not. Unfortunately, this process was broken &hellip; <a href=\"https:\/\/www.spiffystores.com.au\/blog\/2008\/12\/17\/rails-222-memoization-gotchas\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,1],"tags":[23,24,22,25,20,12,26,11,21],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-geek-stuff","category-show-all","tag-23","tag-24","tag-apache","tag-apache-13","tag-memoize","tag-rails","tag-rails-222","tag-ruby","tag-ssl"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pgk4DF-2n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":3228,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/147\/revisions\/3228"}],"wp:attachment":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}