Class: UU::OS::Util::EmbeddedHTTPServer
- Inherits:
-
Object
- Object
- UU::OS::Util::EmbeddedHTTPServer
- 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.
Direct Known Subclasses
Constant Summary
Instance Attribute Summary (collapse)
-
- (String) base_url
readonly
Server base URL (without context path).
-
- (String) context
readonly
Server context path.
-
- (String) host
readonly
Server host.
-
- (Fixnum) port
readonly
Server port.
Instance Method Summary (collapse)
-
- (EmbeddedHTTPServer) initialize(options = {})
constructor
Creates new instance of server.
-
- (void) register_servlet(servlet_class, *path)
Registers servlet to be invoked for given path.
-
- (void) shutdown
Stops server and releases all listening sockets.
-
- (void) start
Starts server.
-
- (Symbol) status
Returns server status.
-
- (void) stop
Stops server.
Constructor Details
- (EmbeddedHTTPServer) initialize(options = {})
Creates new instance of server.
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( = {}) @host = [:host] || DEFAULT_HOST @port = [:port] || empty_port @context = [:context] || '' @base_url = "#{[:use_ssl] ? "https" : "http"}://#{@host}:#{@port}" config = {BindAddress: @host, Port: @port} config[:Logger] = WEBrick::Log.new([:log_file], ([:log_level] || 2)) if [:access_log_file] log = [: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 ![:access_log] config[:AccessLog] = [] end if [:use_ssl] config[:SSLEnable] = true cert = [:ssl_cert] pkey = [: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).
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.
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.
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.
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.
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.
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 |