“Error bad URI” when trying to get WEBrick to accept HTTPS

久未见 提交于 2020-02-28 05:18:23

问题


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?


回答1:


You need to add

require "webrick/https"

to make it working



来源:https://stackoverflow.com/questions/14462558/error-bad-uri-when-trying-to-get-webrick-to-accept-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!