I’m building a rack server app that has to filter domains that can access the data.
This is currently how i do that and it works fine:
authorized_domains = /domain1.com|domain2.com|domain3.com/
return [417, {"Content-Type" => "text/html"}, ["Expectation Failed"]] unless env['HTTP_REFERER'].match(authorized_domains)
The problem is that i would get probably half a million request a day
or even more, and on top of that i would have about 1000-3000 domains on the list.
Is this the most efficient filtering there is? doing a regex on the list of domains?
Store the domains as a
Setin a class constant so that it doesn’t get instantiated on each request. Then get the domain.tld from the HTTP_REFERER and do a look-up in the set.Something like
@pguardiario argued that the above is slower than his regex solution. To benchmark is to know.
user system total real pguardiario regex 8.437000 0.000000 8.437000 ( 8.538086) jonelf original 2.719000 0.000000 2.719000 ( 2.734375) Only the set look-up 0.047000 0.000000 0.047000 ( 0.040039) A tweaked solution 2.203000 0.000000 2.203000 ( 2.222656)