4

So I've got a simple Sinatra site:

# app.rb         
require 'sinatra'

get '/' do       
  'Hello world!' 
end              

And I can set up rack to serve it over HTTP:

# config.ru              
require './app'          
run Sinatra::Application 

# vim: ft=ruby           

And it works fine when I browse to http://localhost:9292:

% rackup
[2013-01-22 10:27:52] INFO  WEBrick 1.3.1
[2013-01-22 10:27:52] INFO  ruby 1.9.2 (2011-02-18) [x86_64-darwin10.7.4]
[2013-01-22 10:27:52] INFO  WEBrick::HTTPServer#start: pid=7525 port=9292
127.0.0.1 - - [22/Jan/2013 10:28:05] "GET / HTTP/1.1" 200 12 0.0177            
127.0.0.1 - - [22/Jan/2013 10:28:06] "GET /favicon.ico HTTP/1.1" 404 447 0.0007

I've generated a self-signed certificate, and after some googling, thought I found a way of setting up my rack file to use the cert and serve HTTPS:

# config.ru                                                                     
require './app'                                                                 
require 'webrick'                                                               

Rack::Server.start(                                                             
  :Port             => 9292,                                                    
  :Logger           => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),         
  :app              => Sinatra::Application,                                    
  :SSLEnable        => true,                                                    
  :SSLVerifyClient  => OpenSSL::SSL::VERIFY_NONE,                               
  :SSLPrivateKey    => OpenSSL::PKey::RSA.new( File.read "server.key" ),        
  :SSLCertificate   => OpenSSL::X509::Certificate.new( File.read "server.crt" ),
  :SSLCertName      => [["CN", WEBrick::Utils::getservername]]                  
)                                                                               

# vim: ft=ruby                                                                  

But when I visit https://localhost:9292 I get Error code: ssl_error_rx_record_too_long and webrick is ... unhappy:

% rackup
[2013-01-22 10:46:28] DEBUG TCPServer.new(0.0.0.0, 9292)
[2013-01-22 10:46:28] DEBUG TCPServer.new(::, 9292)
[2013-01-22 10:46:28] DEBUG Rack::Handler::WEBrick is mounted on /.
[2013-01-22 10:46:28] INFO  WEBrick::HTTPServer#start: pid=7660 port=9292
[2013-01-22 10:46:33] DEBUG accept: 127.0.0.1:62185
[2013-01-22 10:46:33] ERROR bad URI `?w~Rx03I?\x06?\x00\x00H\x00??'.
localhost - - [22/Jan/2013:10:46:33 EST] "\x16\x03\x01\x00?\x01\x00\x00?\x03\x01P??Y?KOƛ\x0Ew/yO;P\x1E\x13 ?w~Rx03I?\x06?\x00\x00H\x00??" 400 316
- -> ?w~Rx03I?\x06?\x00\x00H\x00??
[2013-01-22 10:46:33] DEBUG close: 127.0.0.1:62185

Feel free to try it yourself, all the files you need to run it (config.ru, app.rb, server.crt, server.key) are in this gist.

So I'm obviously doing something wrong. How can I fix it?

Community
  • 1
  • 1
rampion
  • 87,131
  • 49
  • 199
  • 315
  • I tried this before too, I gave up. I'll be interested to see if you can find a way to get it to work! – ian Jan 22 '13 at 17:22

1 Answers1

0

You need to add

require "webrick/https"

to make it working

NoBody
  • 17
  • 1
  • Doesn't work for me - I just added that to `config.ru`, and now webrick spits `!! Invalid request` and the browser still says `Error code: ssl_error_rx_record_too_long`. – rampion Feb 02 '13 at 15:03