Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Ruby/JavaScript Developer at Liaison. Previously, I was a Developer at Continuity and Hedgeye, a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize TechCorridor.io, ICRuby, OpenHack Iowa City, and previously organized NewHaven.rb. I have an amazing wife named Danielle Oakes.

Easy request logging on OSX

by Ben

Here’s a great way to capture HTTP request/response traffic on OSX.

Source: Easy request logging on OSX

Mockbin by Mashape

by Ben

Mockbin allows you to generate custom endpoints to test, mock, and track HTTP requests & responses between libraries, sockets and APIs. Made with Love by Mashape.

Source: Mockbin by Mashape

HTTP Status Codes — httpstatuses.com

by Ben

HTTP Status Code directory, with definitions, details and helpful code references.

Source: HTTP Status Codes — httpstatuses.com

HTTP/2 finished, coming to browsers within weeks

by Ben

HTTP/2 finished, coming to browsers within weeks.

HTTP/2 is based in large part on Google’s SPDY. SPDY did some things that HTTP/2 does not. It required the use of TLS (Transport Layer Security) to enhance privacy and security. HTTP/2 makes this optional; it can operate over TLS or over plain TCP. Some vendors, however, have said that their implementations will only support HTTP/2 over encrypted TLS connections to regain these privacy benefits.

Okay, so some browsers will require TLS and others won’t? No wonder I’m confused. If I were to guess, I’d imagine we’ll see most browsers support unencrypted HTTP/2 in a few years, provided enough of them support unencrypted connections initially. (Who would want to be the vendor of the only browser that’s forced to use the slower HTTP/1.1 on some sites?)

That’s not to say TLS is a bad idea. I just don’t think this requirement will stand the test of time given that it’s already giving way.

Ruby Simple HTTP Server, minimalist Rake

by Ben

I use a really simple HTTP server all the time. It happens to be written in Python:

python -m SimpleHTTPServer 5000

That serves all the files in the current directory over HTTP on port 5000. Honestly, it works just fine, but I’ve always wondered if Ruby had an equivalent.

Here it is:

ruby -run -e httpd . -p 5000

(from Aaron Patterson’s tweet found via Zach Morek)

It’s pretty much the same, except it’s written in Ruby. More often than not, that’s not a big difference — except I can understand the code behind it.

#
# = un.rb
#
# Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
#
# This program is free software.
# You can distribute/modify this program under the same terms of Ruby.

# [...]

##
# Run WEBrick HTTP server.
#
# ruby -run -e httpd -- [OPTION] DocumentRoot
#
# --bind-address=ADDR address to bind
# --port=NUM listening port number
# --max-clients=MAX max number of simultaneous clients
# --temp-dir=DIR temporary directory
# --do-not-reverse-lookup disable reverse lookup
# --request-timeout=SECOND request timeout in seconds
# --http-version=VERSION HTTP version
# -v verbose
#

def httpd
  setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR",
        "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do
    |argv, options|
    require 'webrick'
    opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i
    [:Port, :MaxClients].each do |name|
      opt = options[name] and (options[name] = Integer(opt)) rescue nil
    end
    unless argv.size == 1
      raise ArgumentError, "DocumentRoot is mandatory"
    end
    options[:DocumentRoot] = argv.shift
    s = WEBrick::HTTPServer.new(options)
    shut = proc {s.shutdown}
    siglist = %w"TERM QUIT"
    siglist.concat(%w"HUP INT") if STDIN.tty?
    siglist &= Signal.list.keys
    siglist.each do |sig|
      Signal.trap(sig, shut)
    end
    s.start
  end
end

So how does it work? It’s actually a little surprising. Here’s the command again for reference:

ruby -run -e httpd . -p 5000

In order:

While that code is probably too clever, it’s nice to have a simple HTTP server wherever I have Ruby.

Even more, the concept is reusable:

# File: ake.rb
# Minimalist rake.  :)
def greet
  puts "Hello, #{ ARGV[0] }!"
end

Here’s the output

$ ruby -I . -rake -e greet Ben
Hello, Ben!
$ ruby -r ./ake -e greet Ben
Hello, Ben!

That could be a nice minimalist way to write some helper scripts without Rake or Thor.