Module: Clarity::Server

Includes:
BasicAuth, ChunkHttp, EventMachine::HttpServer
Defined in:
lib/clarity/server.rb

Constant Summary

Constants included from ChunkHttp

ChunkHttp::LeadIn

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChunkHttp

#logfiles, #params, #path, #public_file, #render, #respond_with, #respond_with_chunks, #template

Methods included from BasicAuth

#authentication_data, #decode_credentials, #user_name_and_password

Instance Attribute Details

#log_filesObject

Returns the value of attribute log_files.



19
20
21
# File 'lib/clarity/server.rb', line 19

def log_files
  @log_files
end

#relative_rootObject

Returns the value of attribute relative_root.



18
19
20
# File 'lib/clarity/server.rb', line 18

def relative_root
  @relative_root
end

#required_passwordObject

Returns the value of attribute required_password.



18
19
20
# File 'lib/clarity/server.rb', line 18

def required_password
  @required_password
end

#required_usernameObject

Returns the value of attribute required_username.



18
19
20
# File 'lib/clarity/server.rb', line 18

def required_username
  @required_username
end

Class Method Details

.run(options) ⇒ Object



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
47
48
49
50
# File 'lib/clarity/server.rb', line 21

def self.run(options)

  EventMachine::run do
    EventMachine.epoll
    EventMachine::start_server(options[:address], options[:port], self) do |a|
      a.log_files = options[:log_files]
      a.required_username = options[:username]
      a.required_password = options[:password]
      a.relative_root = options[:relative_root] || ""
    end

    STDERR.puts "Clarity #{Clarity::VERSION} starting up."
    STDERR.puts " * listening on #{options[:address]}:#{options[:port]}"

    if options[:user]
      STDERR.puts " * Running as user #{options[:user]}"
      EventMachine.set_effective_user(options[:user])
    end

    STDERR.puts " * Log mask(s): #{options[:log_files].join(', ')}"

    if options[:username].nil? or options[:password].nil?
      STDERR.puts " * WARNING: No username/password specified. This is VERY insecure."
    end

    STDERR.puts


  end
end

Instance Method Details

#error_page(error) ⇒ Object



106
107
108
109
# File 'lib/clarity/server.rb', line 106

def error_page(error)
  @error = error
  render "error.html.erb"
end

#process_http_requestObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/clarity/server.rb', line 52

def process_http_request
  authenticate!

  puts "action: #{path}"
  puts "params: #{params.inspect}"

  @hostname = HostnameCommandBuilder.command

  case path
  when '/'
    respond_with(200, welcome_page)

  when '/perform'
    if params.empty?
      respond_with(200, welcome_page)
    else
      # get command
      command = case params['tool']
        when 'grep' then GrepCommandBuilder.new(params).command
        when 'tail' then TailCommandBuilder.new(params).command
        else raise InvalidParameterError, "Invalid Tool parameter"
      end
      response = respond_with_chunks
      response.chunk results_page # display page header

      puts "Running: #{command}"

      EventMachine::popen(command, GrepRenderer) do |grepper|
        @grepper = grepper
        @grepper.response = response
      end
    end

  when '/test'
    response = respond_with_chunks
    EventMachine::add_periodic_timer(1) do
      response.chunk "Lorem ipsum dolor sit amet<br/>"
      response.send_chunks
    end

  else
    respond_with(200, public_file(path), :content_type => Mime.for(path))
  end

rescue InvalidParameterError => e
  respond_with(500, error_page(e))
rescue NotFoundError => e
  respond_with(404, "<h1>Not Found</h1>")
rescue NotAuthenticatedError => e
  puts "Could not authenticate user"
  headers = { "WWW-Authenticate" => %(Basic realm="Clarity")}
  respond_with(401, "HTTP Basic: Access denied.\n", :content_type => 'text/plain', :headers => headers)
end

#results_pageObject



115
116
117
# File 'lib/clarity/server.rb', line 115

def results_page
  render "index.html.erb"
end

#unbindObject



119
120
121
122
# File 'lib/clarity/server.rb', line 119

def unbind
  @grepper.close_connection if @grepper
  close_connection
end

#welcome_pageObject



111
112
113
# File 'lib/clarity/server.rb', line 111

def welcome_page
  render "index.html.erb"
end