ActiveSupport Option Hashes

A small blog item, for remembering this method...

When using a (normal) Hash in Ruby on Rails accessing an element with a symbol key or string key results in a different item.

Example
(Notice the use of a string and symbol to access a key)

normal = {'foo'=>"lish"} 
normal[:foo] = 'bar'

results in the hash:

{"foo"=>"lish", :foo=>"bar"}

To solve this issue you can convert every Hash to HashWithInDifferentAccess

special = {'foo'=>'lish'}.with_indifferent_access
special[:foo] = 'bar'

Results in

{"foo"=>"bar"}
special.class => ActiveSupport::HashWithIndifferentAccess

that more like it :-)

Reopended my Blog

After a few months of offline time I finally reopened my Blog.

I didn't post often but the last few months I had several moments I would love to blog some technical solutions I found to certain problems. But I didn't have a place to put it...

I quickly choose a standard layout, maybe I will update it later.

So see you in the next post...

Apache 2.2.13 upgrade breaks my SSL configuration

Today I upgraded my Apache installation to 2.2.13 on my FreeBSD server. (Thankfully I first upgraded my internal server before upgrading the server this blog is on).
The server didn't start:

[error] Server should be SSL-aware but has no certificate configured [Hint: SSLCertificateFile]

To solve this problem I usually fall back to google...

Finding some discussions about this problem I tried the following:
Moving the SSLCerticateFile and SSLCertificateKey outside a virtualhost tag as suggested, I've got the following error:

 [error] Illegal attempt to re-initialise SSL for server (theoretically shouldn't happen!)

More info wasn't available...

After some debuggin/playing I found out I must remove ALL SSL stuff out of my directives except for the line SSLEngine ON.
So in short my config file :

# Those two lines are placed OUTSIDE the <virtualhost> tag
SSLCertificateFile /usr/local/etc/apache22/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/etc/apache22/ssl.key/server.key

# in a virtual host file:
&lt;VirtualHost *:443&gt;
  ServerName sample.com
  DocumentRoot /usr/local/www/sample

    SSLEngine on
  
   &lt;Directory /usr/local/www/sample &gt;
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
  &lt;/Directory&gt;
 &lt;/VirtualHost&gt;

Finally the sites are up again. Besides the Apache warning about SSL and virtual hosts everything runs find. PHeeew!

[warn] Init: You should not use name-based virtual hosts in conjunction with SSL!!

Now I must update this server, so If you can read this, the upgrade was succesful ;-)

Freebsd Ports – Environmental variables

I'm a big fan of the FreeBSD port system. But the last few weeks
I had problems updating my freebsd ports.

A growing number of ports were giving the following error: "cc: /sbin:/bin:/...:/root/bin: No such file or directory"

A full sample:

libtool: link: cc -shared  .libs/lqr_gradient.o .libs/lqr_rwindow.o .libs/lqr_energy.o .libs/lqr_cursor.o .libs/lqr_carver.o .libs/lqr_carver_list.o .libs/lqr_carver_bias.o .libs/lqr_carver_rigmask.o .libs/lqr_vmap.o .libs/lqr_vmap_list.o .libs/lqr_progress.o   -Wl,-rpath -Wl,/usr/local/lib -Wl,-rpath -Wl,/usr/local/lib -L/usr/local/lib /usr/local/lib/libglib-2.0.so /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:/root/bin /usr/local/lib/libintl.so /usr/local/lib/libiconv.so /usr/local/lib/libpcre.so -lm    -Wl,-soname -Wl,liblqr-1.so.3 -o .libs/liblqr-1.so.3
cc: /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:/root/bin: No such file or directory
gmake[2]: *** [liblqr-1.la] Error 1
gmake[2]: Leaving directory `/usr/ports/graphics/liblqr-1/work/liblqr-1-0.4.1/lqr'
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/usr/ports/graphics/liblqr-1/work/liblqr-1-0.4.1'
gmake: *** [all] Error 2
*** Error code 1

After a lof of debugging and digging deeply in the /work directories of the broken ports. I found out
that libtool was generating a 'wrong' cc command. The part -L /sbin:/bin/usr/bin ... etc was wrong. The path seperator ':' should not be used. I thought libtool was broken. (Rule X pragmatic programmer: "SELECT Isn't broken" ;-) )

After dinging deeply in this script I saw the $path variable was placed in this command...

After typing 'set' to see all environment variables I found out I defined a custom $path variable.
Probably by a typing mistake. Because the real environment variable for path is PATH (uppercase...)

After remove this variable 'path' ("unset path" in bash), all my problems were gone.... Yess !

So some words of wisdom: NEVER define an environment variable 'path' on freebsd !! (And be VERY careful with your other environment variables!)

Production gotcha: Rails send_file seems to corrupt files

What's wrong with the following Rails code?

class ResourceController < ApplicationController  
  DEFAULT_OPTIONS = { :disposition => 'inline' }

  def send_file1
    send_file 'flash1.swf', DEFAULT_OPTIONS
  end

  def send_file2
    send_file 'flash2.swf', DEFAULT_OPTIONS
  end
end

This code works perfectly in development mode. In production mode retrieving the two different files the second file gets corrupt / wrong...

After a long search I looked in the rails code:

  def send_file(path, options = {}) #:doc:
    raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)
 
    options[:length]   ||= File.size(path)
    options[:filename] ||= File.basename(path) unless options[:url_based_filename] 
    send_file_headers! options  
    #.... 
  end

OOops... the send_file code modifies my class constant!
And after the first call the length and filename is placed in the class constant...
This is no problem in development mode because the classes are reloaded every time. In production mode every mongrel server has it's own instance...