Class: Ferrum::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/network.rb,
lib/ferrum/network/error.rb,
lib/ferrum/network/request.rb,
lib/ferrum/network/exchange.rb,
lib/ferrum/network/response.rb,
lib/ferrum/network/intercepted_request.rb

Defined Under Namespace

Classes: Error, Exchange, InterceptedRequest, Request, Response

Constant Summary collapse

CLEAR_TYPE =
%i[traffic cache].freeze
AUTHORIZE_TYPE =
%i[server proxy].freeze
RESOURCE_TYPES =
%w[Document Stylesheet Image Media Font Script TextTrack
XHR Fetch EventSource WebSocket Manifest
SignedExchange Ping CSPViolationReport Other].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Network

Returns a new instance of Network.



16
17
18
19
20
# File 'lib/ferrum/network.rb', line 16

def initialize(page)
  @page = page
  @traffic = []
  @exchange = nil
end

Instance Attribute Details

#trafficObject (readonly)

Returns the value of attribute traffic.



14
15
16
# File 'lib/ferrum/network.rb', line 14

def traffic
  @traffic
end

Instance Method Details

#authorize(user:, password:, type: :server) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ferrum/network.rb', line 57

def authorize(user:, password:, type: :server)
  unless AUTHORIZE_TYPE.include?(type)
    raise ArgumentError, ":type should be in #{AUTHORIZE_TYPE}"
  end

  @authorized_ids ||= {}
  @authorized_ids[type] ||= []

  intercept

  @page.on(:request) do |request, index, total|
    if request.auth_challenge?(type)
      response = authorized_response(@authorized_ids[type],
                                     request.interception_id,
                                     user, password)

      @authorized_ids[type] << request.interception_id
      request.continue(authChallengeResponse: response)
    elsif index + 1 < total
      next # There are other callbacks that can handle this
    else
      request.continue
    end
  end
end

#authorized_response(ids, interception_id, username, password) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ferrum/network.rb', line 121

def authorized_response(ids, interception_id, username, password)
  if ids.include?(interception_id)
    { response: "CancelAuth" }
  elsif username && password
    { response: "ProvideCredentials",
      username: username,
      password: password }
  else
    { response: "CancelAuth" }
  end
end

#clear(type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ferrum/network.rb', line 34

def clear(type)
  unless CLEAR_TYPE.include?(type)
    raise ArgumentError, ":type should be in #{CLEAR_TYPE}"
  end

  if type == :traffic
    @traffic.clear
  else
    @page.command("Network.clearBrowserCache")
  end

  true
end

#first_by(request_id) ⇒ Object



133
134
135
# File 'lib/ferrum/network.rb', line 133

def first_by(request_id)
  @traffic.find { |e| e.request.id == request_id }
end

#intercept(pattern: "*", resource_type: nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ferrum/network.rb', line 48

def intercept(pattern: "*", resource_type: nil)
  pattern = { urlPattern: pattern }
  if resource_type && RESOURCE_TYPES.include?(resource_type.to_s)
    pattern[:resourceType] = resource_type
  end

  @page.command("Network.setRequestInterception", patterns: [pattern])
end

#last_by(request_id) ⇒ Object



137
138
139
# File 'lib/ferrum/network.rb', line 137

def last_by(request_id)
  @traffic.select { |e| e.request.id == request_id }.last
end

#requestObject



22
23
24
# File 'lib/ferrum/network.rb', line 22

def request
  @exchange&.request
end

#responseObject



26
27
28
# File 'lib/ferrum/network.rb', line 26

def response
  @exchange&.response
end

#statusObject



30
31
32
# File 'lib/ferrum/network.rb', line 30

def status
  response&.status
end

#subscribeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ferrum/network.rb', line 83

def subscribe
  @page.on("Network.requestWillBeSent") do |params|
    # On redirects Chrome doesn't change `requestId` and there's no
    # `Network.responseReceived` event for such request. If there's already
    # exchange object with this id then we got redirected and params has
    # `redirectResponse` key which contains the response.
    if exchange = first_by(params["requestId"])
      exchange.build_response(params)
    end

    exchange = Network::Exchange.new(params)
    @exchange = exchange if exchange.navigation_request?(@page.frame_id)
    @traffic << exchange
  end

  @page.on("Network.responseReceived") do |params|
    if exchange = last_by(params["requestId"])
      exchange.build_response(params)
    end
  end

  @page.on("Network.loadingFinished") do |params|
    exchange = last_by(params["requestId"])
    if exchange && exchange.response
      exchange.response.body_size = params["encodedDataLength"]
    end
  end

  @page.on("Log.entryAdded") do |params|
    entry = params["entry"] || {}
    if entry["source"] == "network" &&
        entry["level"] == "error" &&
        exchange = last_by(entry["networkRequestId"])
      exchange.build_error(entry)
    end
  end
end