* [PATCH] remove optimizations which only made sense in old Rubies
@ 2016-03-17 23:47 Eric Wong
0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-03-17 23:47 UTC (permalink / raw)
To: unxf-public; +Cc: Eric Wong
Constants waste memory and inline cache lookups are not free,
either. Rely on frozen string literals in Ruby 2.3+, but continue
optimizing for opt_str_freeze in Ruby 2.1+ when literal strings are
explicitly frozen.
---
lib/unxf.rb | 25 ++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)
diff --git a/lib/unxf.rb b/lib/unxf.rb
index cf1f5e9..1f6a066 100644
--- a/lib/unxf.rb
+++ b/lib/unxf.rb
@@ -1,19 +1,10 @@
# -*- encoding: binary -*-
+# -*- frozen_string_literal: true -*-
require 'rpatricia'
# Rack middleware to remove "HTTP_X_FORWARDED_FOR" in the Rack environment and
# replace "REMOTE_ADDR" with the value of the original client address.
class UnXF
- # :stopdoc:
- # reduce garbage overhead by using constant strings
- REMOTE_ADDR = "REMOTE_ADDR".freeze
- HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR"
- HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO"
- RACK_URL_SCHEME = "rack.url_scheme".freeze
- UNXF_FOR = "unxf.for".freeze
- UNXF_PROTO = "unxf.proto".freeze
- HTTPS = "https"
- # :startdoc:
# local LAN addresses described in RFC 1918
RFC_1918 = %w(10.0.0.0/8 172.16.0.0/12 192.168.0.0/16)
@@ -58,10 +49,10 @@ class UnXF
# This allows existing applications to use UnXF without putting it
# into the middleware stack (to avoid increasing stack depth and GC time)
def unxf!(env)
- if xff_str = env.delete(HTTP_X_FORWARDED_FOR)
- env[UNXF_FOR] = xff_str
+ if xff_str = env.delete('HTTP_X_FORWARDED_FOR'.freeze)
+ env['unxf.for'] = xff_str
xff = xff_str.split(/\s*,\s*/)
- addr = env[REMOTE_ADDR]
+ addr = env['REMOTE_ADDR']
begin
while (/:/ =~ addr ? @trusted6 : @trusted).include?(addr) &&
tmp = xff.pop
@@ -74,10 +65,10 @@ class UnXF
# it's stupid to have https at any point other than the first
# proxy in the chain, so we don't support that
if xff.empty?
- env[REMOTE_ADDR] = addr
- if xfp = env.delete(HTTP_X_FORWARDED_PROTO)
- env[UNXF_PROTO] = xfp
- /\Ahttps\b/ =~ xfp and env[RACK_URL_SCHEME] = HTTPS
+ env['REMOTE_ADDR'] = addr
+ if xfp = env.delete('HTTP_X_FORWARDED_PROTO'.freeze)
+ env['unxf.proto'] = xfp
+ /\Ahttps\b/ =~ xfp and env['rack.url_scheme'] = 'https'.freeze
end
else
return on_untrusted_addr(env, xff_str)
--
EW
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2016-03-17 23:47 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-17 23:47 [PATCH] remove optimizations which only made sense in old Rubies Eric Wong
Code repositories for project(s) associated with this public inbox
https://yhbt.net/unxf.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).