sprsquish (owner)

Revisions

gist: 202150 Download_button fork
public
Public Clone URL: git://gist.github.com/202150.git
Embed All Files: show embed
environment.rb #
1
config.middleware.use "Hostname", %x"hostname".chomp
hostname.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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