From 70c976bdd85bb8515fea01d6ad6074ef472fc2e0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 14 Nov 2015 02:47:24 +0000 Subject: reduce constant lookup dependencies Unicorn 5 removes some constants we were using, and constant lookups + inline caching are waste of time anyways on newer Rubies with the opt_str_freeze bytecode instruction. This may reduce performance for folks on older Rubies (probably not noticeable); but improves performance for folks on newer Rubies. --- lib/rainbows/reverse_proxy.rb | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'lib/rainbows/reverse_proxy.rb') diff --git a/lib/rainbows/reverse_proxy.rb b/lib/rainbows/reverse_proxy.rb index c1f1dc2..16a6f65 100644 --- a/lib/rainbows/reverse_proxy.rb +++ b/lib/rainbows/reverse_proxy.rb @@ -47,15 +47,6 @@ class Rainbows::ReverseProxy autoload :EventMachine, 'rainbows/reverse_proxy/event_machine' autoload :EvClient, 'rainbows/reverse_proxy/ev_client' - HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR" - REMOTE_ADDR = "REMOTE_ADDR" - REQUEST_METHOD = "REQUEST_METHOD" - REQUEST_URI = "REQUEST_URI" - CRLF = "\r\n" - TR = %w(_ -) - CONTENT_LENGTH = "CONTENT_LENGTH" - HTTP_TRANSFER_ENCODING = "HTTP_TRANSFER_ENCODING" - RackInput = "rack.input" E502 = [ 502, [ %w(Content-Length 0), %w(Content-Type text/plain) ], [] ] def initialize(opts) @@ -113,24 +104,23 @@ class Rainbows::ReverseProxy # returns request headers for sending to the upstream as a string def build_headers(env, input) - remote_addr = env[REMOTE_ADDR] - xff = env[HTTP_X_FORWARDED_FOR] + remote_addr = env['REMOTE_ADDR'] + xff = env['HTTP_X_FORWARDED_FOR'] xff = xff ? "#{xff},#{remote_addr}" : remote_addr - req = "#{env[REQUEST_METHOD]} #{env[REQUEST_URI]} HTTP/1.0\r\n" \ + req = "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} HTTP/1.0\r\n" \ "Connection: close\r\n" \ "X-Forwarded-For: #{xff}\r\n" - uscore, dash = *TR env.each do |key, value| %r{\AHTTP_(\w+)\z} =~ key or next key = $1 next if %r{\A(?:VERSION|CONNECTION|KEEP_ALIVE|X_FORWARDED_FOR)\z}x =~ key - key.tr!(uscore, dash) + key.tr!('_'.freeze, '-'.freeze) req << "#{key}: #{value}\r\n" end input and req << (input.respond_to?(:size) ? "Content-Length: #{input.size}\r\n" : - "Transfer-Encoding: chunked\r\n") - req << CRLF + "Transfer-Encoding: chunked\r\n".freeze) + req << "\r\n".freeze end def pick_upstream(env) # +env+ is reserved for future expansion @@ -139,16 +129,16 @@ class Rainbows::ReverseProxy end def prepare_input!(env) - if cl = env[CONTENT_LENGTH] + if cl = env['CONTENT_LENGTH'] size = cl.to_i size > 0 or return - elsif %r{\Achunked\z}i =~ env.delete(HTTP_TRANSFER_ENCODING) + elsif %r{\Achunked\z}i =~ env.delete('HTTP_TRANSFER_ENCODING') # do people use multiple transfer-encodings? else return end - input = env[RackInput] + input = env['rack.input'] if input.respond_to?(:rewind) if input.respond_to?(:size) input.size # TeeInput-specific behavior -- cgit v1.2.3-24-ge0c7