rack

Ruby Rack: startup and teardown operations (Tokyo Cabinet connection)

半城伤御伤魂 提交于 2019-12-01 06:02:20
问题 I have built a pretty simple REST service in Sinatra, on Rack. It's backed by 3 Tokyo Cabinet/Table datastores, which have connections that need to be opened and closed. I have two model classes written in straight Ruby that currently simply connect, get or put what they need, and then disconnect. Obviously, this isn't going to work long-term. I also have some Rack middleware like Warden that rely on these model classes. What's the best way to manage opening and closing the connections? Rack

Rack throwing EOFError (bad content body)

ぃ、小莉子 提交于 2019-12-01 04:39:57
We're using Ruby 2.1.2, Rails 3.2.19 with JQuery 1.11, asset pipeline is not being used (so not using JQuery-ujs, but are using relevant rails.js explicitly). For most users, JQuery and other related common plugins are being pulled from Google CDN (including jquery.form 3.50). The web server affected is Ubuntu 14.04, Nginx 1.6, and Passenger 4.x. For about 10% of users on our production server (loads seem to not matter), we're getting a stack dump like this, primarily for one URL but there are others: /gems/rack-1.4.5/lib/rack/multipart/parser.rb:74 in "block in fast_forward_to_first_boundary"

Changing HTTP status message using Sinatra

陌路散爱 提交于 2019-12-01 04:11:18
问题 I'm writing a simple Sinatra app, and given a user posts a request with an specific data, I want to return an error '453' (custom error code) with a message CLIENT_ERROR, or something similar. The problem is: looking into the Sinatra documentation and doing some testing I couldn't find a way to setup the response error message, only the response status. So, if a set the Sinatra response get '/' do response.status = 453 end I get the error code right: curl -v localhost:4567 * About to connect(

How do I configure WEBrick to use an intermediate certificate with HTTPS?

不问归期 提交于 2019-12-01 03:23:04
I am currently using the following options in my Rails app to enable HTTPS with WEBrick: { :Port => 3000, :environment => (ENV['RAILS_ENV'] || "development").dup, :daemonize => false, :debugger => false, :pid => File.expand_path("tmp/pids/server.pid"), :config => File.expand_path("config.ru"), :SSLEnable => true, :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, :SSLPrivateKey => OpenSSL::PKey::RSA.new( File.open("certificates/https/key.pem").read), :SSLCertificate => OpenSSL::X509::Certificate.new( File.open("certificates/https/cert.pem").read), :SSLCertName => [["CN", WEBrick::Utils:

Start and call Ruby HTTP server in the same script

白昼怎懂夜的黑 提交于 2019-12-01 01:44:27
I wonder how I could start a Ruby Rack application (such as Sinatra) and call it with Net::HTTP or similar in the same script. Of couse I could do something like... require 'sinatra/base' require 'net/http' t = Thread.new do class App < Sinatra::Base get '/' do 'Hi!' end end App.run! :host => 'localhost', :port => 1234 end sleep 2 puts Net::HTTP.start('localhost', 1234) { |http| http.get('/') }.body t.join puts 'Bye!' ...but it doesn't feel optimal to sleep for two seconds, waiting for Thin to start. What I need is some kind of callback when the server has started or does anybody have any

Multipart response in Ruby/Rack

笑着哭i 提交于 2019-12-01 00:02:10
I want my server to send a multipart response (multipart/x-mixed-replace). I'd prefer some kind of solution using the Sinatra framework or a generic Rack app, but any example in ruby would be nice. Here's the equivalent of what I'm trying to do, in PHP: <?php header('Content-type: multipart/x-mixed-replace;boundary="rn9012"'); print "--rn9012\n"; print "Content-type: application/xml\n\n"; print "<?xml version='1.0'?>\n"; print "<content>First Part</content>\n"; print "--rn9012\n"; flush(); sleep(5); print "Content-type: application/xml\n\n"; print "<?xml version='1.0'?>\n"; print "<content

Start and call Ruby HTTP server in the same script

落爺英雄遲暮 提交于 2019-11-30 20:46:43
问题 I wonder how I could start a Ruby Rack application (such as Sinatra) and call it with Net::HTTP or similar in the same script. Of couse I could do something like... require 'sinatra/base' require 'net/http' t = Thread.new do class App < Sinatra::Base get '/' do 'Hi!' end end App.run! :host => 'localhost', :port => 1234 end sleep 2 puts Net::HTTP.start('localhost', 1234) { |http| http.get('/') }.body t.join puts 'Bye!' ...but it doesn't feel optimal to sleep for two seconds, waiting for Thin

Rails/Rack: “ArgumentError: invalid %-encoding” for POST data

扶醉桌前 提交于 2019-11-30 18:43:34
Our ruby on rails site has a URI that one of our partners POSTs XML data to. Since we don't want to deal with XML, we literally just stuff the raw data into a database column and don't go any further with processing it. However, one of the posts we received gave us this error in airbrake: ArgumentError: invalid %-encoding ("http://ns.hr-xml.org/2004-08-02" userId="" password=""><BackgroundReportPackage type="report"> <ProviderReferenceId>.... With backtrace: vendor/ruby-1.9.3/lib/ruby/1.9.1/uri/common.rb:898:in `decode_www_form_component' vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/utils

Calling a Sinatra app instance method from TestCase

痴心易碎 提交于 2019-11-30 18:27:02
I have an util method into a Sinatra application and I would like to tested from my TestCase . The problem is that I don't know how to invoke it, if I just use app.util_method I have the error NameError: undefined local variable or method 'util_method' for #<Sinatra::ExtendedRack:0x007fc0c43305b8> my_app.rb: class MyApp < Sinatra::Base # [...] routes methods # utils methods def util_method return "hi" end end my_app_test.rb: require "my_app.rb" require "test/unit" require "rack/test" class MyAppTest < Test::Unit::TestCase include Rack::Test::Methods def app MyApp.new end # [...] routes methods

Rails 4.2 server; private and public ip not working

吃可爱长大的小学妹 提交于 2019-11-30 18:06:59
I recently updated my rails 4.1.8 to 4.2 I'm not able to access rails app using private ip 192.168.1.x:3000 and also with my public-ip address . Rails app is working with lvh.me:3000 , 0.0.0.0:3000 , localhost:3000 and 127.0.0.1:3000 . But it looks all the the address are pointing to 127.0.0.1:3000 in my server log rails-issue . It was working fine in 4.1 I tried adding following in environments/development.rb , but nothing changed. TRUSTED_PROXIES = %r{ ^127\.0\.0\.1$ | # localhost ^(10 | # private IP 10.x.x.x 172\.(1[6-9]|2[0-9]|3[0-1]) | # private IP in the range 172.16.0.0 .. 172.31.255