• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • Free and easy way to convert Word docs to PDFs

    If you’re like me, you’ve run into situation after situation in which you’d like to convert a Word document to a PDF. While Googling for a tool to do this, though, I inevitably end up drowning in page after page of supposedly “free” converters which cost $30+ to unlock. Come on, $30 to do a simple conversion? This is the era of open source. There has to be a better way.

    Actually, there is. One way would be to install OpenOffice. It allows you to save a document directly to a PDF. This makes so much sense, doesn’t it? Which brings to mind another question… when will Microsoft add a similar feature to Word?

    If you don’t want to wait around for them to do that (and don’t have the inclination to install OpenOffice), check out these instructions for converting to a PDF by printing to a file and using a couple of free tools. It was quick and easy for me, and now I have my PDF and my $30.

  • Think video games are just for the young?

    Read about a 69-year-old grandmother who has two passions: cooking and… gaming. Yes, gaming. There are 17 game consoles in her residence. Just goes to show you that no matter how old you get, you can always find a good creative way to waste extraordinary amounts of time!

  • Reconstructing web sites from cached data

    This paper written by a quartet of guys from the CS department at Old Dominion is quite fascinating. They present a software solution for recovering and reconstructing web sites from various Internet archive sources and cached search engine data. The tool is called Warrick and, while I expect I’ll never have to use it, it’s nice to know it’s there if needed.

    If nothing else, the paper gives a good overview of how web site caching actually works. And their description of the tool is quite intriguing. They must have spent a good deal of time on it.

  • Watir tips and tricks

    I’ve been using Watir for a week or so now and have (naturally) run into a couple of problems. I’ve also come up with a handy way to reference fields on a page. I thought I’d share these tips so others won’t have to spend their precious time working through ’em as I had to do.

    Problem: Watir doesn’t respect maxLength

    The first problem I found is that Watir doesn’t respect the maxLength attribute on a text field. It will happily enter 20 characters into a text field with a maxLength of 10 without complaining. This can cause problems with assertions and such. I was able to override Watir’s set(..) method with code which will throw an exception if the string to be entered into the text field exceeds the maxLength.

    Just add this code to your script if you want the same warning to show up:

    class Watir::TextField
      alias_method :set_value, :set
        def set(value)
          if (value.size > maxLength)
            fail(“The length of the string < "#{value}"> exceeds ” +
              “the max length (#{maxLength}) of the “#{name}” text field.”)
          end
        set_value(value)
      end
    end

    Problem: Watir doesn’t yet handle JavaScript alerts

    Another problem I ran into was Watir’s inability to handle JavaScript alerts and pop-ups. (It can handle pop-up browser windows fine, just not windows created with JavaScript’s alert(..) function.) There currently isn’t a very good workaround for this, but the Watir folks are working on a solution for the next release. In the meantime, they suggest using a tool call AutoIt in conjunction with Watir to automate interaction with the pop-ups. I haven’t tried this method yet.

    Tip: Abstract out textual references to page elements

    The site I’m using Watir to test changes quite frequently. I have no guarantee that a specific page element will be named the same thing for any length of time. Why, then, would I want to scatter references to the element throughout my Watir tests and be forced to change all of the references anytime the element itself is changed? My solution was to abstract out the common attributes of the element so that each element is only defined once. Then, whenever an element changes, I only have to change a single reference.

    Ruby’s “method_missing” idiom made it very, very easy to do this. I simply defined a parent for all of my test cases and added the following method definition to it:

    def method_missing(method)
      key = method.id2name
      super unless page_objects.has_key?(key)
      args = page_objects[key]
      return @ie.send(args[0], args[1], args[2])
    end

    Do you see what this does? Whenever a method which doesn’t exist in a subclass is called, method_missing is passed the name of the (non-existent) method. I take that name and use it as a key in a hash which I expect to be returned by a method called “page_objects” which is defined in a subclass. If the key exists, I grab the object from the hash and use it to call a method on Watir’s IE object (Ruby’s “send” method is the equivalent of an “exec” in other langages… it just allows strings to be interpreted as method calls and arguments).

    I then define the “page_objects” method in each of my subclasses like so:

    def page_objects
      {
      “field1” => [:text_field, :id, “my_field”],
      “field2” => [:text_field, :id, “another_field”]
      }
    end

    I can then simply reference “field1” and “field2” as if they were instance variables of my subclass. For example:

    def test_example
      field1.setContents(“Hello world”)
      assert_equal(“Booyah”, field2.getContents)
    end

    The calls to “field1” and “field2” are handled by “method_missing” since those methods don’t exist, and the magic then proceeds from there. If the actual ID of either field changes, I simply make the change in my “page_objects” method and my existing tests should all continue working. This indirection follows the DRY principle (Don’t Repeat Yourself).

    This trick would almost impossible to accomplish in Java without a large amount of bulky reflection code. Just another example of why dynamic typing is so nice.

    Hope you find this idiom as useful as I have. Happy Watir-ing!