Sha256: f6e76b593cf7e45b9688ae824fa50a82208e1bcb37666d204faa6c385b4ce631

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require "net/http"
require "openssl"
require "base64"

module Apullo
  module Fingerprint
    class Favicon
      attr_reader :uri

      def initialize(url)
        @uri = URI(url)
      end

      def results
        data = b64_favicon_data
        return {} unless data

        hash = Hash.new(data.b)
        {
          md5: hash.md5,
          mmh3: hash.mmh3,
          sha1: hash.sha1,
          sha256: hash.sha256,
          meta: {
            url: uri.to_s,
          }
        }
      end

      private

      def b64_favicon_data
        @b64_favicon_data ||= [].tap do |out|
          data = get(uri.path)
          next unless data

          b64 = Base64.strict_encode64(data)
          out << b64.chars.each_slice(76).map(&:join).join("\n") + "\n"
        end.first
      end

      def get(path)
        http = build_http
        path = path.empty? ? "/" : path
        request = Net::HTTP::Get.new(path)
        response = http.request(request)

        response.code.to_i == 200 ? response.body : nil
      rescue Errno::ECONNREFUSED, Net::HTTPError, OpenSSL::OpenSSLError, Timeout::Error => _e
        nil
      end

      def build_http
        if uri.scheme == "http"
          Net::HTTP.start(uri.host, uri.port)
        else
          Net::HTTP.start(uri.host, uri.port, use_ssl: true)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apullo-0.2.0 lib/apullo/fingerprints/favicon.rb