Class: HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerprinter/core/http_client.rb

Overview

HTTP Client used to perform some requests (check for some content for example) or as a fallback in some browser processing (less resource intensive than using a Chrome instance but won’t provide access to the DOM for example)

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



9
10
11
12
# File 'lib/fingerprinter/core/http_client.rb', line 9

def initialize
  Typhoeus::Config.user_agent = ScanOptions.user_agent
  @hydra = Typhoeus::Hydra.new(max_concurrency: ScanOptions.http_concurrency)
end

Instance Method Details

#get(urls, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fingerprinter/core/http_client.rb', line 38

def get(urls, options = {})
  responses = {}

  urls = [urls] if urls.is_a?(String)
  urls.each do |url|
    http_request = Typhoeus::Request.new(url, request_options(:get, options))

    http_request.on_complete { |response| responses[url] = response }

    @hydra.queue(http_request)
  end

  @hydra.run

  responses
end

#post(urls, body, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fingerprinter/core/http_client.rb', line 55

def post(urls, body, options = {})
  responses = {}

  urls = [urls] if urls.is_a?(String)
  urls.each do |url|
    http_request = Typhoeus::Request.new(url, request_options(:post, options, body))

    http_request.on_complete { |response| responses[url] = response }

    @hydra.queue(http_request)
  end

  @hydra.run

  responses
end

#request_options(method, options, body = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fingerprinter/core/http_client.rb', line 14

def request_options(method, options, body = nil)
  req_options = {
    ssl_verifypeer: false,
    ssl_verifyhost: 0,
    followlocation: options[:follow_location] || false,
    method: method,
    headers: options[:headers] || {},
    body: body
  }

  req_options[:headers].merge!({
                                 'Priority' => 'u=0, i',
                                 'Accept-Encoding' => 'gzip, deflate, br',
                                 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'
                               })

  req_options[:params] = options[:params] if options[:params]

  req_options[:proxy] = ScanOptions.proxy_url if ScanOptions.proxy?
  req_options[:timeout] = ScanOptions.timeout

  req_options
end