Sha256: f5934b9429097c6d569dc479b79ee7e9035c8d2f44b26350bbd123fdc4d935c6

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# Copyright (C) 2012 Sourcefire, Inc.

require 'thin'

module Panoptimon
class HTTP

  include Logger

  def initialize (args={})
    @match = []
    @mount = []
    # TODO args[:config].http_port, ssl, etc
    @http = Thin::Server.new('0.0.0.0', 8080, self);
  end

  def start
    @http.backend.start
  end

  def hostport
    "#{@http.host}:#{@http.port}"
  end

  def call (env)
    path = env['PATH_INFO']
    return favicon(env) if path == '/favicon.ico'
    # logger.debug { "#{path} => " + env.inspect }
    if    go = @match.find {|x| path =~ x[0]}
    elsif go = @mount.find {|x| path =~ %r{^#{x[0]}(/|$)}}
      env['SCRIPT_NAME'] = go[0]
      env['PATH_INFO'] = path.sub(%r{^#{go[0]}}, '')
    else
      return [404, {'Content-Type' => 'text/html'},
        '<html><head><title>Not Found</title></head>' +
        '<body><p>nope</p></body></html>']
    end
    env['rack.logger'] = logger
    begin
      return go[1].call(env)
    rescue => ex
      logger.error { "error: #{ex.message} #{ex.backtrace.join("\n  ")}" }
      return [500, {'Content-Type' => 'text/html'}, ['bah']]
    end
  end

  def favicon(env)
    # TODO bundle / configure favicon?
    # NOTE why doesn't rack/thin support .to_path per spec?
    return [200, {'Content-Type' => 'image/x-icon'},
      Pathname.new('/tmp/favicon.ico').open]
  end

  # regexp-match
  def match (regexp, app)
    regexp = %r{^#{regexp}} if regexp.class == String
    @match.push([regexp, app])
  end

  # path prefix
  def mount (point, app)
    point.sub(%r{/*$}, '')
    @mount.push([point, app])
  end

end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
panoptimon-0.4.5 lib/panoptimon/http.rb
panoptimon-0.4.4 lib/panoptimon/http.rb
panoptimon-0.4.2 lib/panoptimon/http.rb
panoptimon-0.4.1 lib/panoptimon/http.rb
panoptimon-0.4.0 lib/panoptimon/http.rb
panoptimon-0.3.0 lib/panoptimon/http.rb