Module: WEBrick::Utils

Defined in:
lib/webrick/ssl.rb,
lib/webrick/utils.rb

Defined Under Namespace

Classes: TimeoutHandler

Constant Summary collapse

RAND_CHARS =

Characters used to generate random strings

"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789" +
"abcdefghijklmnopqrstuvwxyz"

Class Method Summary collapse

Class Method Details

.create_listeners(address, port, logger = nil) ⇒ Object

Creates TCP server sockets bound to address:port and returns them.

It will create IPV4 and IPV6 sockets on all interfaces.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/webrick/utils.rb', line 66

def create_listeners(address, port, logger=nil)
  unless port
    raise ArgumentError, "must specify port"
  end
  sockets = Socket.tcp_server_sockets(address, port)
  sockets = sockets.map {|s|
    s.autoclose = false
    ts = TCPServer.for_fd(s.fileno)
    s.close
    ts
  }
  return sockets
end

.create_self_signed_cert(bits, cn, comment) ⇒ Object

Creates a self-signed certificate with the given number of bits, the issuer cn and a comment to be stored in the certificate.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/webrick/ssl.rb', line 91

def create_self_signed_cert(bits, cn, comment)
  rsa = OpenSSL::PKey::RSA.new(bits){|p, n|
    case p
    when 0; $stderr.putc "."  # BN_generate_prime
    when 1; $stderr.putc "+"  # BN_generate_prime
    when 2; $stderr.putc "*"  # searching good prime,
                              # n = #of try,
                              # but also data from BN_generate_prime
    when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
                              # but also data from BN_generate_prime
    else;   $stderr.putc "*"  # BN_generate_prime
    end
  }
  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = 1
  name = OpenSSL::X509::Name.new(cn)
  cert.subject = name
  cert.issuer = name
  cert.not_before = Time.now
  cert.not_after = Time.now + (365*24*60*60)
  cert.public_key = rsa.public_key

  ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
  ef.issuer_certificate = cert
  cert.extensions = [
    ef.create_extension("basicConstraints","CA:FALSE"),
    ef.create_extension("keyUsage", "keyEncipherment"),
    ef.create_extension("subjectKeyIdentifier", "hash"),
    ef.create_extension("extendedKeyUsage", "serverAuth"),
    ef.create_extension("nsComment", comment),
  ]
  aki = ef.create_extension("authorityKeyIdentifier",
                            "keyid:always,issuer:always")
  cert.add_extension(aki)
  cert.sign(rsa, OpenSSL::Digest::SHA1.new)

  return [ cert, rsa ]
end

.getservernameObject

The server hostname



52
53
54
55
56
57
58
59
# File 'lib/webrick/utils.rb', line 52

def getservername
  host = Socket::gethostname
  begin
    Socket::gethostbyname(host)[0]
  rescue
    host
  end
end

.random_string(len) ⇒ Object

Generates a random string of length len



89
90
91
92
93
94
# File 'lib/webrick/utils.rb', line 89

def random_string(len)
  rand_max = RAND_CHARS.bytesize
  ret = ""
  len.times{ ret << RAND_CHARS[rand(rand_max)] }
  ret
end

.set_close_on_exec(io) ⇒ Object

Sets the close on exec flag for io



30
31
32
33
34
# File 'lib/webrick/utils.rb', line 30

def set_close_on_exec(io)
  if defined?(Fcntl::FD_CLOEXEC)
    io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
  end
end

.set_non_blocking(io) ⇒ Object

Sets IO operations on io to be non-blocking



19
20
21
22
23
24
25
# File 'lib/webrick/utils.rb', line 19

def set_non_blocking(io)
  flag = File::NONBLOCK
  if defined?(Fcntl::F_GETFL)
    flag |= io.fcntl(Fcntl::F_GETFL)
  end
  io.fcntl(Fcntl::F_SETFL, flag)
end

.su(user) ⇒ Object

Changes the process’s uid and gid to the ones of user



39
40
41
42
43
44
45
46
47
# File 'lib/webrick/utils.rb', line 39

def su(user)
  if pw = Etc.getpwnam(user)
    Process::initgroups(user, pw.gid)
    Process::Sys::setgid(pw.gid)
    Process::Sys::setuid(pw.uid)
  else
    warn("WEBrick::Utils::su doesn't work on this platform")
  end
end

.timeout(seconds, exception = Timeout::Error) ⇒ Object

Executes the passed block and raises exception if execution takes more than seconds.

If seconds is zero or nil, simply executes the block



218
219
220
221
222
223
224
225
226
227
# File 'lib/webrick/utils.rb', line 218

def timeout(seconds, exception=Timeout::Error)
  return yield if seconds.nil? or seconds.zero?
  # raise ThreadError, "timeout within critical session" if Thread.critical
  id = TimeoutHandler.register(seconds, exception)
  begin
    yield(seconds)
  ensure
    TimeoutHandler.cancel(id)
  end
end