Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6628925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:11:13+00:00 2026-05-25T22:11:13+00:00

I’m building a rack server app that has to filter domains that can access

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:11:14+00:00Added an answer on May 25, 2026 at 10:11 pm

    Store the domains as a Set in 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

    require 'rubygems'
    require 'rack'
    require 'set'
    require 'URI'
    
    class FontServer
      DOMAINS = %w[domain1.com domain2.com domain3.com].to_set
      def call(env)
        uri = URI.parse(env['HTTP_REFERER'])
        domain_tld = uri.host.split('.')[-2..-1].join('.')
        return [417, {"Content-Type" => "text/html"}, ["Expectation Failed"]] unless DOMAINS.include? domain
      end
    end
    

    @pguardiario argued that the above is slower than his regex solution. To benchmark is to know.

    require 'rubygems'
    require 'rack'
    require 'set'
    require 'URI'
    require 'benchmark'
    
    domains=[]
    3000.times {|i| domains<<"domain#{i}.com"}
    DOMAINS = domains.to_set
    re = Regexp.new('^https?:\/\/(' + domains.join('|') + ')')
    env = {'HTTP_REFERER' => 'http://domain4711.com/'}
    
    Benchmark.bm do |benchmark|
    puts "pguardiario regex"
    benchmark.report do
        100000.times do
          env['HTTP_REFERER'] =~ re
        end
      end
      # Parsing the URL with the URI gem and getting domain.tld
      # About three times faster than the regex solution
      puts "jonelf original"
      benchmark.report do
        100000.times do
          uri = URI.parse(env['HTTP_REFERER'])
          domain_tld = uri.host.split('.')[-2..-1].join('.')
          DOMAINS.include? domain_tld
        end
      end
      # Set::include? is really fast. If we only got a
      # fast way to get the domain it would rock.
      puts "Only the set look-up"
      benchmark.report do
        domain_tld="domain4711.com"
        100000.times do
          DOMAINS.include? domain_tld
        end
      end
      # Gets the full host instead of just domain.tld
      # so that it does the same as pguardiario did
      # About 3.8 times faster than the regex on my machine.
      puts "A tweaked solution"
      benchmark.report do
        100000.times do
          DOMAINS.include? URI.parse(env['HTTP_REFERER']).host
        end
      end
    end
    
          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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.