Class: RushHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/rush/server.rb

Overview

Mongrel handler that translates the incoming HTTP request into a Rush::Connection::Local call. The results are sent back across the wire to be decoded by Rush::Connection::Remote on the other side.

Instance Method Summary collapse

Instance Method Details

#authorize(auth) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rush/server.rb', line 48

def authorize(auth)
  unless m = auth.match(/^Basic (.+)$/)
    log "Request with no authorization data"
    return false
  end

  decoded = Base64.decode64(m[1])
  user, password = decoded.split(':', 2)

  if user.nil? or user.length == 0 or password.nil? or password.length == 0
    log "Malformed user or password"
    return false
  end

  if password == config.passwords[user]
    return true
  else
    log "Access denied to #{user}"
    return false
  end
end

#boxObject



70
71
72
# File 'lib/rush/server.rb', line 70

def box
  @box ||= Rush::Box.new('localhost')
end

#configObject



74
75
76
# File 'lib/rush/server.rb', line 74

def config
  @config ||= Rush::Config.new
end

#log(msg) ⇒ Object



78
79
80
81
82
# File 'lib/rush/server.rb', line 78

def log(msg)
  File.open('rushd.log', 'a') do |f|
    f.puts "#{Time.now.strftime('%Y-%m-%d %H:%I:%S')} :: #{msg}"
  end
end

#process(request, response) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rush/server.rb', line 9

def process(request, response)
  params = {}
  request.params['QUERY_STRING'].split("?").last.split("&").each do |tuple|
    key, value = tuple.split("=")
    params[key.to_sym] = value
  end

  unless authorize(request.params['HTTP_AUTHORIZATION'])
    response.start(401) do |head, out|
    end
  else
    payload = request.body.read

    without_action = params
    without_action.delete(params[:action])

    msg = sprintf "%-20s", params[:action]
    msg += without_action.inspect
    msg += " + #{payload.size} bytes of payload" if payload.size > 0
    log msg

    params[:payload] = payload

    begin
      result = box.connection.receive(params)

      response.start(200) do |head, out|
        out.write result
      end
    rescue Rush::Exception => e
      response.start(400) do |head, out|
        out.write "#{e.class}\n#{e.message}\n"
      end
    end
  end
rescue Exception => e
  log e.full_display
end