module PrettyProxy::Utils

api private Don’t use the methods of this class. They are for internal use only.

Public Class Methods

parse_html_or_xhtml(doc, mime_type) click to toggle source
# File lib/pretty_proxy.rb, line 458
def self.parse_html_or_xhtml(doc, mime_type)
  # If you parse XHTML as HTML with Nokogiri, and use to_s after, the markup
  # can be messed up, breaking the structural integrity of the xml
  #
  # Example:     <meta name="description" content="not important" />
  #   becomes    <meta name="description" content="not important" >
  #
  # In the other side if you parse HTML as a XML, and use to_s after, the
  # Nokogiri make empty content tags self-close
  #
  # Example:    <script type="text/javascript" src="/ballonizer.js"></script>
  #   becomes:  <script type="text/javascript" src="/ballonizer.js" />
  #
  # What's even worse than the contrary (xml as html)
  parsed_doc = nil

  case mime_type
  when /text\/html/i
    parsed_doc = Nokogiri::HTML(doc)
  when /application\/xhtml\+xml/i
    options = Nokogiri::XML::ParseOptions::DEFAULT_XML &
                Nokogiri::XML::ParseOptions::STRICT &
                Nokogiri::XML::ParseOptions::NONET
    begin
      parsed_doc = Nokogiri::XML::Document.parse(doc, nil, nil, options)
    rescue
      return nil
    end
  else
    fail ProxyError, "the only mime-types accepted are text/html and" +
                     " application/xhtml+xml, the passed argument was " +
                     "'#{mime_type}'"
  end

  parsed_doc
end
same_domain?(u1, u2) click to toggle source

TODO: check if isn’t the case of change this for ‘u1.site == u2.site’

# File lib/pretty_proxy.rb, line 452
def self.same_domain?(u1, u2)
  u1.normalized_scheme == u2.normalized_scheme &&
    u1.normalized_host == u2.normalized_host &&
    u1.inferred_port == u2.inferred_port
end
validate_original_domain_and_paths(original_domain, original_paths) click to toggle source
# File lib/pretty_proxy.rb, line 506
def self.validate_original_domain_and_paths(original_domain, original_paths)
  fail ConfigError, 'original_paths is empty' if original_paths.empty?

  original_domain = Addressable::URI.parse(original_domain)
  fail ConfigError, 'the original_domain has to have no query or fragment' if original_domain.query || original_domain.fragment

  # can raise errors
  test_uri = original_domain.clone
  if original_paths.respond_to?(:each)
    original_paths.each do | path |
      fail ConfigError, "original_paths value '#{path}' don't start with a '/'" unless path.start_with? '/'
      test_uri.path = path
    end
  else
    fail ConfigError, "original_paths value '#{original_paths}' don't start with a '/'" unless original_paths.start_with? '/'
    test_uri.path = original_paths
  end

rescue Addressable::URI::InvalidURIError => e
  raise ConfigError, "the message of the URI exception: '#{e.message}'"
end
validate_proxy_path(proxy_path) click to toggle source
# File lib/pretty_proxy.rb, line 494
def self.validate_proxy_path(proxy_path)
  fail ConfigError, "proxy_path argument don't start with a '/'" unless proxy_path.start_with? '/'
  fail ConfigError, "proxy_path argument don't end with a '/'" unless proxy_path.end_with? '/'
  # NOTE: if the user want to proxify 'www.site.net', and not 'www.site.net/'?
  # Well, majority of the internet answers for this are 'the right way is to use the trailing slash'
  # See:  http://tim-stanley.com/post/pretty-good-urls/
  #       http://www.w3.org/Provider/Style/URI.html
  #       http://stackoverflow.com/questions/7355305/preventing-trailing-slash-on-domain-name
  #       http://alistapart.com/article/slashforward
  #       http://www.searchenginejournal.com/linking-issues-why-a-trailing-slash-in-the-url-does-matter/13021/?ModPagespeed=noscript
end