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/auth_request.rb,
lib/ferrum/network/intercepted_request.rb

Defined Under Namespace

Classes: AuthRequest, 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.



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

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

Instance Attribute Details

#trafficObject (readonly)

Returns the value of attribute traffic.



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

def traffic
  @traffic
end

Instance Method Details

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



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

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|
    request.continue
  end

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

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

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



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ferrum/network.rb', line 126

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

#clear(type) ⇒ Object



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

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



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

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

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



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

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("Fetch.enable", handleAuthRequests: true, patterns: [pattern])
end

#last_by(request_id) ⇒ Object



142
143
144
# File 'lib/ferrum/network.rb', line 142

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

#requestObject



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

def request
  @exchange&.request
end

#responseObject



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

def response
  @exchange&.response
end

#statusObject



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

def status
  response&.status
end

#subscribeObject



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
120
121
122
123
124
# File 'lib/ferrum/network.rb', line 88

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(@page, params)
    @exchange = exchange if exchange.navigation_request?(@page.main_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