Class: UU::OS::Util::EmbeddedHTTPServer

Inherits:
Object
  • Object
show all
Defined in:
uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb

Overview

Simple HTTP server based on WEBrick to be used mainly for testing purposes. Additionally when this class is required, WEBrick::HTTPRequest object is extended with methods #body_stream and #body_multipart to ease processing of incoming requests.

Examples:

Basic usage:

require 'uu/os/con/util/embedded_http_server'

server = UU::OS::CON::Util::EmbeddedHTTPServer.new
server.register_servlet(My::Module::MyServlet, '/path/to/my/servlet')
server.start
...
server.shutdown

Direct Known Subclasses

EmbeddedApplicationServer

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (EmbeddedHTTPServer) initialize(options = {})

Creates new instance of server.

Parameters:

  • options (Hash) (defaults to: {})

    Server options:

Options Hash (options):

  • :host (String)

    Server host (defaults to localhost).

  • :port (Fixnum)

    Server port (defaults to first available unused port).

  • :context (String)

    Server context path (defaults to empty path).

  • :log_file (String, IO)

    Path to server log file or existing IO (defaults to $stderr).

  • :log_level (Fixnum)

    Log level (possible values 0=OFF, 1=FATAL, 2=ERROR, 3=WARN, 4=INFO, 5=DEBUG; defaults to 2=ERROR).

  • :access_log_file (String, IO)

    Path to server access log file or existing IO (defaults to $stderr).

  • :access_log (TrueClass, FalseClass)

    Flag if access log is enabled (defaults to false).

  • :use_ssl (TrueClass, FalseClass)

    Flag if server should use SSL (default to false). If options :ssl_cert and :ssl_pkey are not provided, server uses self signed certificate generated with each start.

  • :ssl_cert (String, IO)

    Path to SSL certificate or existing IO (defaults to nil)

  • :ssl_pkey (String, IO)

    Path to SSL certificate private key or existing IO (defaults to nil)



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 211

def initialize(options = {})
  @host = options[:host] || DEFAULT_HOST
  @port = options[:port] || empty_port
  @context = options[:context] || ''
  @base_url = "#{options[:use_ssl] ? "https" : "http"}://#{@host}:#{@port}"
  config = {BindAddress: @host, Port: @port}
  config[:Logger] = WEBrick::Log.new(options[:log_file], (options[:log_level] || 2))
  if options[:access_log_file]
    log = options[:access_log_file]
    log = File.open(log, 'a') if log.kind_of?String
    config[:AccessLog] = [[log, AccessLog::COMMON_LOG_FORMAT], [log, AccessLog::REFERER_LOG_FORMAT]]
  elsif !options[:access_log]
    config[:AccessLog] = []
  end
  if options[:use_ssl]
    config[:SSLEnable] = true
    cert = options[:ssl_cert]
    pkey = options[:ssl_pkey]
    if cert.nil? || pkey.nil?
      config[:SSLCertName] = [['CN', @host]]
    else
      cert = File.open(cert, 'rb') if cert.kind_of?String
      pkey = File.open(pkey, 'rb') if pkey.kind_of?String
      config[:SSLCertificate] = OpenSSL::X509::Certificate.new(cert.read) if !cert.nil?
      config[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(pkey.read) if !pkey.nil?
    end
  end
  @server = WEBrick::HTTPServer.new(config)
end

Instance Attribute Details

- (String) base_url (readonly)

Server base URL (without context path).

Returns:

  • (String)

    Server base URL.



196
197
198
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 196

def base_url
  @base_url
end

- (String) context (readonly)

Server context path.

Returns:

  • (String)

    Server context path.



192
193
194
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 192

def context
  @context
end

- (String) host (readonly)

Server host.

Returns:

  • (String)

    Server host.



184
185
186
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 184

def host
  @host
end

- (Fixnum) port (readonly)

Server port.

Returns:

  • (Fixnum)

    Server port.



188
189
190
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 188

def port
  @port
end

Instance Method Details

- (void) register_servlet(servlet_class, *path)

This method returns an undefined value.

Registers servlet to be invoked for given path.

Parameters:

  • servlet_class (WEBrick::HTTPServlet::AbstractServlet)

    Any class inherited from AbstractServlet.

  • path (Array<String>)

    Servlet path (can be given as separate path elements). If no path is given, servlet is registered to root of context.



246
247
248
249
250
251
252
253
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 246

def register_servlet(servlet_class, *path)
  if path.size > 0
    deploy_path = concat_path('', @context, *path)
  else
    deploy_path = concat_path('', @context, '/')
  end
  @server.mount(deploy_path, servlet_class)
end

- (void) shutdown

This method returns an undefined value.

Stops server and releases all listening sockets. Server stopped via this method cannot be restarted (new server instance must be created).



283
284
285
286
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 283

def shutdown
  @server.shutdown
  return
end

- (void) start

This method returns an undefined value.

Starts server.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 257

def start
  @thread = Thread.new {
    Thread.current.abort_on_exception = true
    @server.start
  }
  while @server.status != :Running
    Thread.pass
    unless @thread.alive?
      @thread.join
      raise
    end
  end
  return
end

- (Symbol) status

Returns server status.

Returns:

  • (Symbol)

    Server status.



290
291
292
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 290

def status
  @server.status
end

- (void) stop

This method returns an undefined value.

Stops server.



274
275
276
277
# File 'uu_os_connection-2.2.4/lib/uu/os/util/embedded_http_server.rb', line 274

def stop
  @server.stop
  return
end