1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
82
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Response protocol object
//
// Represents an HTTP response from navigation operations.
// Response objects are created by the server when Frame.goto() or similar navigation
// methods complete successfully.
use crate::error::Result;
use crate::server::channel_owner::{ChannelOwner, ChannelOwnerImpl, ParentOrConnection};
use serde_json::Value;
use std::any::Any;
use std::sync::Arc;
/// TLS/SSL security details for an HTTPS response.
///
/// All fields are optional — the server provides what's available.
///
/// See: <https://playwright.dev/docs/api/class-response#response-security-details>
#[derive(Debug, Clone)]
pub struct SecurityDetails {
/// Certificate issuer name.
pub issuer: Option<String>,
/// TLS protocol version (e.g., "TLS 1.3").
pub protocol: Option<String>,
/// Certificate subject name.
pub subject_name: Option<String>,
/// Unix timestamp (seconds) when the certificate becomes valid.
pub valid_from: Option<f64>,
/// Unix timestamp (seconds) when the certificate expires.
pub valid_to: Option<f64>,
}
/// Remote server address (IP and port).
///
/// See: <https://playwright.dev/docs/api/class-response#response-server-addr>
#[derive(Debug, Clone)]
pub struct RemoteAddr {
/// Server IP address.
pub ip_address: String,
/// Server port.
pub port: u16,
}
/// Resource size information for a request/response pair.
///
/// See: <https://playwright.dev/docs/api/class-request#request-sizes>
#[derive(Debug, Clone)]
pub struct RequestSizes {
/// Size of the request body in bytes. Set to 0 if there was no body.
pub request_body_size: i64,
/// Total number of bytes from the start of the HTTP request message
/// until (and including) the double CRLF before the body.
pub request_headers_size: i64,
/// Size of the received response body in bytes.
pub response_body_size: i64,
/// Total number of bytes from the start of the HTTP response message
/// until (and including) the double CRLF before the body.
pub response_headers_size: i64,
}
/// A single HTTP header entry with a name and value.
///
/// Used by `Response::headers_array()` to return all headers preserving duplicates.
///
/// See: <https://playwright.dev/docs/api/class-response#response-headers-array>
#[derive(Debug, Clone)]
pub struct HeaderEntry {
/// Header name (lowercase)
pub name: String,
/// Header value
pub value: String,
}
/// Response represents an HTTP response from a navigation operation.
///
/// Response objects are not created directly - they are returned from
/// navigation methods like page.goto() or page.reload().
///
/// See: <https://playwright.dev/docs/api/class-response>
#[derive(Clone)]
pub struct ResponseObject {
base: ChannelOwnerImpl,
}
impl ResponseObject {
/// Creates a new Response from protocol initialization
///
/// This is called by the object factory when the server sends a `__create__` message
/// for a Response object.
pub fn new(
parent: Arc<dyn ChannelOwner>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self> {
let base = ChannelOwnerImpl::new(
ParentOrConnection::Parent(parent),
type_name,
guid,
initializer,
);
Ok(Self { base })
}
/// Returns the status code of the response (e.g., 200 for a success).
///
/// See: <https://playwright.dev/docs/api/class-response#response-status>
pub fn status(&self) -> u16 {
self.initializer()
.get("status")
.and_then(|v| v.as_u64())
.unwrap_or(0) as u16
}
/// Returns the status text of the response (e.g. usually an "OK" for a success).
///
/// See: <https://playwright.dev/docs/api/class-response#response-status-text>
pub fn status_text(&self) -> &str {
self.initializer()
.get("statusText")
.and_then(|v| v.as_str())
.unwrap_or("")
}
/// Returns the URL of the response.
///
/// See: <https://playwright.dev/docs/api/class-response#response-url>
pub fn url(&self) -> &str {
self.initializer()
.get("url")
.and_then(|v| v.as_str())
.unwrap_or("")
}
/// Returns the response body as bytes.
///
/// Sends a `"body"` RPC call to the Playwright server, which returns the body
/// as a base64-encoded binary string.
///
/// See: <https://playwright.dev/docs/api/class-response#response-body>
pub async fn body(&self) -> Result<Vec<u8>> {
use serde::Deserialize;
#[derive(Deserialize)]
struct BodyResponse {
binary: String,
}
let result: BodyResponse = self.channel().send("body", serde_json::json!({})).await?;
use base64::Engine;
let bytes = base64::engine::general_purpose::STANDARD
.decode(&result.binary)
.map_err(|e| {
crate::error::Error::ProtocolError(format!(
"Failed to decode response body from base64: {}",
e
))
})?;
Ok(bytes)
}
/// Returns TLS/SSL security details for HTTPS connections, or `None` for HTTP.
///
/// See: <https://playwright.dev/docs/api/class-response#response-security-details>
pub async fn security_details(&self) -> Result<Option<SecurityDetails>> {
let result: serde_json::Value = self
.channel()
.send("securityDetails", serde_json::json!({}))
.await?;
let value = result.get("value");
match value {
Some(v) if v.is_object() && !v.as_object().unwrap().is_empty() => {
Ok(Some(SecurityDetails {
issuer: v.get("issuer").and_then(|v| v.as_str()).map(String::from),
protocol: v.get("protocol").and_then(|v| v.as_str()).map(String::from),
subject_name: v
.get("subjectName")
.and_then(|v| v.as_str())
.map(String::from),
valid_from: v.get("validFrom").and_then(|v| v.as_f64()),
valid_to: v.get("validTo").and_then(|v| v.as_f64()),
}))
}
_ => Ok(None),
}
}
/// Returns the server's IP address and port for this response, or `None`.
///
/// See: <https://playwright.dev/docs/api/class-response#response-server-addr>
pub async fn server_addr(&self) -> Result<Option<RemoteAddr>> {
let result: serde_json::Value = self
.channel()
.send("serverAddr", serde_json::json!({}))
.await?;
let value = result.get("value");
match value {
Some(v) if !v.is_null() => {
let ip_address = v
.get("ipAddress")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
let port = v.get("port").and_then(|v| v.as_u64()).unwrap_or(0) as u16;
Ok(Some(RemoteAddr { ip_address, port }))
}
_ => Ok(None),
}
}
/// Returns resource size information for this response.
///
/// See: <https://playwright.dev/docs/api/class-request#request-sizes>
pub async fn sizes(&self) -> Result<RequestSizes> {
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SizesRaw {
request_body_size: i64,
request_headers_size: i64,
response_body_size: i64,
response_headers_size: i64,
}
#[derive(Deserialize)]
struct RpcResult {
sizes: SizesRaw,
}
let result: RpcResult = self.channel().send("sizes", serde_json::json!({})).await?;
Ok(RequestSizes {
request_body_size: result.sizes.request_body_size,
request_headers_size: result.sizes.request_headers_size,
response_body_size: result.sizes.response_body_size,
response_headers_size: result.sizes.response_headers_size,
})
}
/// Returns the raw response headers as name-value pairs (preserving duplicates).
///
/// Sends a `"rawResponseHeaders"` RPC call to the Playwright server.
///
/// See: <https://playwright.dev/docs/api/class-response#response-headers-array>
pub async fn raw_headers(&self) -> Result<Vec<HeaderEntry>> {
use serde::Deserialize;
#[derive(Deserialize)]
struct RawHeadersResponse {
headers: Vec<HeaderEntryRaw>,
}
#[derive(Deserialize)]
struct HeaderEntryRaw {
name: String,
value: String,
}
let result: RawHeadersResponse = self
.channel()
.send("rawResponseHeaders", serde_json::json!({}))
.await?;
Ok(result
.headers
.into_iter()
.map(|h| HeaderEntry {
name: h.name,
value: h.value,
})
.collect())
}
}
impl ChannelOwner for ResponseObject {
fn guid(&self) -> &str {
self.base.guid()
}
fn type_name(&self) -> &str {
self.base.type_name()
}
fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
self.base.parent()
}
fn connection(&self) -> Arc<dyn crate::server::connection::ConnectionLike> {
self.base.connection()
}
fn initializer(&self) -> &Value {
self.base.initializer()
}
fn channel(&self) -> &crate::server::channel::Channel {
self.base.channel()
}
fn dispose(&self, reason: crate::server::channel_owner::DisposeReason) {
self.base.dispose(reason)
}
fn adopt(&self, child: Arc<dyn ChannelOwner>) {
self.base.adopt(child)
}
fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
self.base.add_child(guid, child)
}
fn remove_child(&self, guid: &str) {
self.base.remove_child(guid)
}
fn on_event(&self, _method: &str, _params: Value) {
// Response objects don't have events
}
fn was_collected(&self) -> bool {
self.base.was_collected()
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl std::fmt::Debug for ResponseObject {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ResponseObject")
.field("guid", &self.guid())
.finish()
}
}