2

Sinatra application, Ubuntu 12

#config.ru
require 'sinatra/base'
require 'openssl'
require 'webrick'
require 'webrick/https'

class App1 < Sinatra::Base
  get '/' do
    'app1'
  end
end

class App2 < Sinatra::Base
  get '/' do
    'app2'
  end
end

app = Rack::Builder.new do 
  map '/app1' do
    run App1
  end
  map '/app2' do
    run App2
  end
end

webrick_options = {
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stdout, WEBrick::Log::DEBUG),
  :DocumentRoot       => "./public",
  :SSLEnable          => true,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open("/etc/ssl/certs/ssl-cert-snakeoil.pem").read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open("/etc/ssl/private/ssl-cert-snakeoil.key").read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ]
}

Rack::Handler::WEBrick.run app, webrick_options

Then I run it

sudo rackup

it runs and it seems to be ok:

[2012-11-06 16:10:14] INFO  WEBrick 1.3.1
[2012-11-06 16:10:14] INFO  ruby 1.9.3 (2012-04-20) [x86_64-linux]
[2012-11-06 16:10:14] DEBUG TCPServer.new(0.0.0.0, 8443)
[2012-11-06 16:10:14] DEBUG TCPServer.new(::, 8443)
.......

but when I go to http://localhost:8443/app1 I get

[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'
[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'
[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'

and the label "This web page is not available" in the page.

What am I doing wrong?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

1 Answers1

0

WEBrick is interpreting the browser's HTTP request as a badly-formed HTTPS request.

To issue a HTTPS request instead, visit https://localhost:8443/app1.

You could always run a second server, say on port 8080, which redirects HTTP requests to the HTTPS URL.

joews
  • 29,767
  • 10
  • 79
  • 91