Class: Mayfly::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/mayfly.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, port, lives, secure = nil, passwd = nil, verbose = false) ⇒ Server

Returns a new instance of Server.



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mayfly.rb', line 25

def initialize(file, port, lives, secure = nil, passwd = nil, verbose = false)
  
  local_variables.each {|v| eval("@#{v}=eval(v)")}
  
  if (!File.exists?(@file)) 
    raise FileNotFoundException, "#{@file} does not exist"
  end
  
  logger.mayfly_log("#{file} will be served #{lives} times from: " +
                    "http#{(secure) ? 's' : ''}://#{local_ip}:#{port}/mayfly/")

  logger.mayfly_log("Using HTTP Authentication with the username 'mayfly'" +
                    " and the password you specified") if @passwd

  
  logger.info("Starting mayfly with the following settings: ")
  logger.info("file=#{file}")      
  logger.info("port=#{port}")            
  logger.info("lives=#{lives}")                  
  logger.info("secure=#{secure}")                        
  logger.info("passwd=#{passwd}")
  
   @config = {
      :Port => port,
      :Logger => logger,
      :AccessLog => [[$stdout, "%t Request from %h, response code = %s"]],
    }

    if (secure)

      cert, key = get_cert

      @config.merge!({
        :SSLEnable => true,
        :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate => cert,
        :SSLPrivateKey => key
      })

    end
  
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



23
24
25
# File 'lib/mayfly.rb', line 23

def config
  @config
end

Instance Method Details

#get_certObject

Generate SSL Cert, and hack stderr to not output some ugly crap from webrick



82
83
84
85
86
87
88
# File 'lib/mayfly.rb', line 82

def get_cert
  old_stderr = $stderr
  $stderr = StringIO.new
  cert, key = WEBrick::Utils.create_self_signed_cert(1024, [["C","GB"], ["O","#{local_ip}"], ["CN", "Mayfly"]], "Generated by Ruby/OpenSSL")
  $stderr = old_stderr
  return cert, key      
end

#loggerObject



90
91
92
# File 'lib/mayfly.rb', line 90

def logger
  @logger ||= Log.new((@verbose) ? $stdout : nil, WEBrick::Log::INFO)
end

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mayfly.rb', line 68

def start
  
  server = WEBrick::HTTPServer.new(@config)
  server.mount('/mayfly', Mayfly::Servlet, @file, @lives, @passwd)

  ['INT', 'TERM'].each { |signal|
     trap(signal){server.shutdown} 
  }

  server.start
  
end