Class: Puppeteer::Bidi::HTTPResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/bidi/http_response.rb,
sig/puppeteer/bidi/http_response.rbs

Overview

HTTPResponse represents a response to an HTTP request.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, request:) ⇒ HTTPResponse

Returns a new instance of HTTPResponse.

Parameters:

  • data: (Hash[String, untyped])
  • request: (HTTPRequest, nil)


45
46
47
48
# File 'lib/puppeteer/bidi/http_response.rb', line 45

def initialize(data:, request:)
  @data = data
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.

Returns:

  • (Object)


40
41
42
# File 'lib/puppeteer/bidi/http_response.rb', line 40

def request
  @request
end

Class Method Details

.for_bfcache(url:, status: 200) ⇒ HTTPResponse

Create a synthetic response for BFCache restoration

Parameters:

  • url: (String)
  • status: (Integer) (defaults to: 200)

Returns:



29
30
31
32
33
34
35
36
37
38
# File 'lib/puppeteer/bidi/http_response.rb', line 29

def self.for_bfcache(url:, status: 200)
  data = {
    "url" => url,
    "status" => status,
    "statusText" => "OK",
    "headers" => [],
    "fromCache" => true
  }
  new(data: data, request: nil)
end

.from(data, request) ⇒ HTTPResponse

Parameters:

Returns:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppeteer/bidi/http_response.rb', line 13

def self.from(data, request)
  existing = request.response
  if existing
    existing.send(:update_data, data)
    return existing
  end

  response = new(data: data, request: request)
  response.send(:initialize_response)
  response
end

Instance Method Details

#bufferString

Returns:

  • (String)


131
132
133
# File 'lib/puppeteer/bidi/http_response.rb', line 131

def buffer
  content
end

#contentString

Returns:

  • (String)


126
127
128
# File 'lib/puppeteer/bidi/http_response.rb', line 126

def content
  @request.get_response_content
end

#frameFrame?

Returns:



161
162
163
# File 'lib/puppeteer/bidi/http_response.rb', line 161

def frame
  @request.frame
end

#from_cache?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/puppeteer/bidi/http_response.rb', line 151

def from_cache?
  @data["fromCache"] == true
end

#from_service_worker?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/puppeteer/bidi/http_response.rb', line 156

def from_service_worker?
  false
end

#headersHash[String, String]

Returns:

  • (Hash[String, String])


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppeteer/bidi/http_response.rb', line 71

def headers
  headers = {}
  (@data["headers"] || []).each do |header|
    value = header["value"]
    next unless value.is_a?(Hash)
    next unless value["type"] == "string"

    header_name = header["name"].to_s.downcase
    headers[header_name] = HTTPUtils.normalize_header_value(header_name, value["value"])
  end
  headers
end

#initialize_responseObject

Returns:

  • (Object)


167
168
169
170
171
172
173
174
# File 'lib/puppeteer/bidi/http_response.rb', line 167

def initialize_response
  if from_cache?
    @request.instance_variable_set(:@from_memory_cache, true)
    @request.frame.page.emit(:requestservedfromcache, @request)
  end

  @request.frame.page.emit(:response, self)
end

#jsonObject

Returns:

  • (Object)


146
147
148
# File 'lib/puppeteer/bidi/http_response.rb', line 146

def json
  JSON.parse(text)
end

#ok?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/puppeteer/bidi/http_response.rb', line 85

def ok?
  current_status = status
  current_status == 0 || (current_status >= 200 && current_status <= 299)
end

#remote_addressHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


51
52
53
# File 'lib/puppeteer/bidi/http_response.rb', line 51

def remote_address
  { ip: "", port: -1 }
end

#security_detailsHash[String, untyped]?

Returns:

  • (Hash[String, untyped], nil)


91
92
93
# File 'lib/puppeteer/bidi/http_response.rb', line 91

def security_details
  nil
end

#statusInteger

Returns:

  • (Integer)


61
62
63
# File 'lib/puppeteer/bidi/http_response.rb', line 61

def status
  @data["status"]
end

#status_textString

Returns:

  • (String)


66
67
68
# File 'lib/puppeteer/bidi/http_response.rb', line 66

def status_text
  @data["statusText"]
end

#textString

Returns:

  • (String)


136
137
138
139
140
141
142
143
# File 'lib/puppeteer/bidi/http_response.rb', line 136

def text
  text = content.dup.force_encoding("UTF-8")
  unless text.valid_encoding?
    raise Encoding::InvalidByteSequenceError, "Response body is not a valid UTF-8 string"
  end

  text
end

#timingHash[String, Numeric]?

Returns:

  • (Hash[String, Numeric], nil)


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
# File 'lib/puppeteer/bidi/http_response.rb', line 96

def timing
  bidi_timing = @request.timing
  return nil if bidi_timing.nil? || bidi_timing.empty?

  {
    "requestTime" => bidi_timing["requestTime"],
    "proxyStart" => -1,
    "proxyEnd" => -1,
    "dnsStart" => bidi_timing["dnsStart"],
    "dnsEnd" => bidi_timing["dnsEnd"],
    "connectStart" => bidi_timing["connectStart"],
    "connectEnd" => bidi_timing["connectEnd"],
    "sslStart" => bidi_timing["tlsStart"],
    "sslEnd" => -1,
    "workerStart" => -1,
    "workerReady" => -1,
    "workerFetchStart" => -1,
    "workerRespondWithSettled" => -1,
    "workerRouterEvaluationStart" => -1,
    "workerCacheLookupStart" => -1,
    "sendStart" => bidi_timing["requestStart"],
    "sendEnd" => -1,
    "pushStart" => -1,
    "pushEnd" => -1,
    "receiveHeadersStart" => bidi_timing["responseStart"],
    "receiveHeadersEnd" => bidi_timing["responseEnd"],
  }
end

#update_data(data) ⇒ Object

Parameters:

  • data (Object)

Returns:

  • (Object)


176
177
178
# File 'lib/puppeteer/bidi/http_response.rb', line 176

def update_data(data)
  @data = data
end

#urlString

Returns:

  • (String)


56
57
58
# File 'lib/puppeteer/bidi/http_response.rb', line 56

def url
  @data["url"]
end