Skip to content

Instantly share code, notes, and snippets.

@sprsquish
Created October 5, 2009 14:20
Show Gist options
  • Select an option

  • Save sprsquish/202150 to your computer and use it in GitHub Desktop.

Select an option

Save sprsquish/202150 to your computer and use it in GitHub Desktop.
config.middleware.use "Hostname", %x"hostname".chomp
class Hostname
TITLE_REGEXP = /(<title>)([^<]*)(<\/title>)/i
def initialize(app, hostname="")
@app = app
@title_suffix = " - on #{hostname}"
end
def call(env)
status, headers, response = @app.call(env)
add_hostname(response, headers) if headers["Content-Type"] =~ %r{text/html}
[status, headers, response]
end
def add_hostname(response, headers)
response.each{|s| s.sub!(TITLE_REGEXP, "\\1\\2#{@title_suffix}\\3") if s =~ TITLE_REGEXP}
headers["Content-Length"] = (headers["Content-Length"].to_i + @title_suffix.length).to_s
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment