Class: Puppeteer::Bidi::HTTPResponse
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::HTTPResponse
- 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
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
-
.for_bfcache(url:, status: 200) ⇒ HTTPResponse
Create a synthetic response for BFCache restoration.
- .from(data, request) ⇒ HTTPResponse
Instance Method Summary collapse
- #buffer ⇒ String
- #content ⇒ String
- #frame ⇒ Frame?
- #from_cache? ⇒ Boolean
- #from_service_worker? ⇒ Boolean
- #headers ⇒ Hash[String, String]
-
#initialize(data:, request:) ⇒ HTTPResponse
constructor
A new instance of HTTPResponse.
- #initialize_response ⇒ Object
- #json ⇒ Object
- #ok? ⇒ Boolean
- #remote_address ⇒ Hash[Symbol, untyped]
- #security_details ⇒ Hash[String, untyped]?
- #status ⇒ Integer
- #status_text ⇒ String
- #text ⇒ String
- #timing ⇒ Hash[String, Numeric]?
- #update_data(data) ⇒ Object
- #url ⇒ String
Constructor Details
#initialize(data:, request:) ⇒ HTTPResponse
Returns a new instance of HTTPResponse.
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
#request ⇒ Object (readonly)
Returns the value of attribute request.
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
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
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
#buffer ⇒ String
131 132 133 |
# File 'lib/puppeteer/bidi/http_response.rb', line 131 def buffer content end |
#content ⇒ String
126 127 128 |
# File 'lib/puppeteer/bidi/http_response.rb', line 126 def content @request.get_response_content end |
#frame ⇒ Frame?
161 162 163 |
# File 'lib/puppeteer/bidi/http_response.rb', line 161 def frame @request.frame end |
#from_cache? ⇒ Boolean
151 152 153 |
# File 'lib/puppeteer/bidi/http_response.rb', line 151 def from_cache? @data["fromCache"] == true end |
#from_service_worker? ⇒ Boolean
156 157 158 |
# File 'lib/puppeteer/bidi/http_response.rb', line 156 def from_service_worker? false end |
#headers ⇒ 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_response ⇒ 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 |
#json ⇒ Object
146 147 148 |
# File 'lib/puppeteer/bidi/http_response.rb', line 146 def json JSON.parse(text) end |
#ok? ⇒ 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_address ⇒ Hash[Symbol, untyped]
51 52 53 |
# File 'lib/puppeteer/bidi/http_response.rb', line 51 def remote_address { ip: "", port: -1 } end |
#security_details ⇒ Hash[String, untyped]?
91 92 93 |
# File 'lib/puppeteer/bidi/http_response.rb', line 91 def security_details nil end |
#status ⇒ Integer
61 62 63 |
# File 'lib/puppeteer/bidi/http_response.rb', line 61 def status @data["status"] end |
#status_text ⇒ String
66 67 68 |
# File 'lib/puppeteer/bidi/http_response.rb', line 66 def status_text @data["statusText"] end |
#text ⇒ 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 |
#timing ⇒ Hash[String, Numeric]?
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
176 177 178 |
# File 'lib/puppeteer/bidi/http_response.rb', line 176 def update_data(data) @data = data end |
#url ⇒ String
56 57 58 |
# File 'lib/puppeteer/bidi/http_response.rb', line 56 def url @data["url"] end |