forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_network.py
365 lines (307 loc) · 12.1 KB
/
_network.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import json
import mimetypes
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast
from urllib import parse
from playwright._impl._api_structures import ResourceTiming
from playwright._impl._api_types import Error
from playwright._impl._connection import (
ChannelOwner,
from_channel,
from_nullable_channel,
)
from playwright._impl._event_context_manager import EventContextManagerImpl
from playwright._impl._helper import ContinueParameters, Header, locals_to_params
from playwright._impl._wait_helper import WaitHelper
if TYPE_CHECKING: # pragma: no cover
from playwright._impl._frame import Frame
class Request(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._redirected_from: Optional["Request"] = from_nullable_channel(
initializer.get("redirectedFrom")
)
self._redirected_to: Optional["Request"] = None
if self._redirected_from:
self._redirected_from._redirected_to = self
self._failure_text: Optional[str] = None
self._timing: ResourceTiming = {
"startTime": 0,
"domainLookupStart": -1,
"domainLookupEnd": -1,
"connectStart": -1,
"secureConnectionStart": -1,
"connectEnd": -1,
"requestStart": -1,
"responseStart": -1,
"responseEnd": -1,
}
self._headers: Dict[str, str] = parse_headers(self._initializer["headers"])
def __repr__(self) -> str:
return f"<Request url={self.url!r} method={self.method!r}>"
@property
def url(self) -> str:
return self._initializer["url"]
@property
def resource_type(self) -> str:
return self._initializer["resourceType"]
@property
def method(self) -> str:
return self._initializer["method"]
@property
def post_data(self) -> Optional[str]:
data = self.post_data_buffer
if not data:
return None
return data.decode()
@property
def post_data_json(self) -> Optional[Any]:
post_data = self.post_data
if not post_data:
return None
content_type = self.headers["content-type"]
if content_type == "application/x-www-form-urlencoded":
return dict(parse.parse_qsl(post_data))
try:
return json.loads(post_data)
except Exception:
raise Error(f"POST data is not a valid JSON object: {post_data}")
@property
def post_data_buffer(self) -> Optional[bytes]:
b64_content = self._initializer.get("postData")
if not b64_content:
return None
return base64.b64decode(b64_content)
@property
def headers(self) -> Dict[str, str]:
return self._headers
async def response(self) -> Optional["Response"]:
return from_nullable_channel(await self._channel.send("response"))
@property
def frame(self) -> "Frame":
return from_channel(self._initializer["frame"])
def is_navigation_request(self) -> bool:
return self._initializer["isNavigationRequest"]
@property
def redirected_from(self) -> Optional["Request"]:
return self._redirected_from
@property
def redirected_to(self) -> Optional["Request"]:
return self._redirected_to
@property
def failure(self) -> Optional[str]:
return self._failure_text
@property
def timing(self) -> ResourceTiming:
return self._timing
class Route(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
def __repr__(self) -> str:
return f"<Route request={self.request}>"
@property
def request(self) -> Request:
return from_channel(self._initializer["request"])
async def abort(self, errorCode: str = None) -> None:
await self._channel.send("abort", locals_to_params(locals()))
async def fulfill(
self,
status: int = None,
headers: Dict[str, str] = None,
body: Union[str, bytes] = None,
path: Union[str, Path] = None,
contentType: str = None,
) -> None:
params = locals_to_params(locals())
length = 0
if isinstance(body, str):
params["body"] = body
params["isBase64"] = False
length = len(body.encode())
elif isinstance(body, bytes):
params["body"] = base64.b64encode(body).decode()
params["isBase64"] = True
length = len(body)
elif path:
del params["path"]
file_content = Path(path).read_bytes()
params["body"] = base64.b64encode(file_content).decode()
params["isBase64"] = True
length = len(file_content)
headers = {k.lower(): str(v) for k, v in params.get("headers", {}).items()}
if params.get("contentType"):
headers["content-type"] = params["contentType"]
elif path:
headers["content-type"] = (
mimetypes.guess_type(str(Path(path)))[0] or "application/octet-stream"
)
if length and "content-length" not in headers:
headers["content-length"] = str(length)
params["headers"] = serialize_headers(headers)
await self._channel.send("fulfill", params)
async def continue_(
self,
url: str = None,
method: str = None,
headers: Dict[str, str] = None,
postData: Union[str, bytes] = None,
) -> None:
overrides: ContinueParameters = {}
if url:
overrides["url"] = url
if method:
overrides["method"] = method
if headers:
overrides["headers"] = serialize_headers(headers)
if isinstance(postData, str):
overrides["postData"] = base64.b64encode(postData.encode()).decode()
elif isinstance(postData, bytes):
overrides["postData"] = base64.b64encode(postData).decode()
await self._channel.send("continue", cast(Any, overrides))
class Response(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._request: Request = from_channel(self._initializer["request"])
timing = self._initializer["timing"]
self._request._timing["startTime"] = timing["startTime"]
self._request._timing["domainLookupStart"] = timing["domainLookupStart"]
self._request._timing["domainLookupEnd"] = timing["domainLookupEnd"]
self._request._timing["connectStart"] = timing["connectStart"]
self._request._timing["secureConnectionStart"] = timing["secureConnectionStart"]
self._request._timing["connectEnd"] = timing["connectEnd"]
self._request._timing["requestStart"] = timing["requestStart"]
self._request._timing["responseStart"] = timing["responseStart"]
self._request._headers = parse_headers(self._initializer["requestHeaders"])
def __repr__(self) -> str:
return f"<Response url={self.url!r} request={self.request}>"
@property
def url(self) -> str:
return self._initializer["url"]
@property
def ok(self) -> bool:
return self._initializer["status"] == 0 or (
self._initializer["status"] >= 200 and self._initializer["status"] <= 299
)
@property
def status(self) -> int:
return self._initializer["status"]
@property
def status_text(self) -> str:
return self._initializer["statusText"]
@property
def headers(self) -> Dict[str, str]:
return parse_headers(self._initializer["headers"])
async def finished(self) -> Optional[str]:
return await self._channel.send("finished")
async def body(self) -> bytes:
binary = await self._channel.send("body")
return base64.b64decode(binary)
async def text(self) -> str:
content = await self.body()
return content.decode()
async def json(self) -> Union[Any]:
return json.loads(await self.text())
@property
def request(self) -> Request:
return self._request
@property
def frame(self) -> "Frame":
return self._request.frame
class WebSocket(ChannelOwner):
Events = SimpleNamespace(
Close="close",
FrameReceived="framereceived",
FrameSent="framesent",
Error="socketerror",
)
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._is_closed = False
self._channel.on(
"frameSent",
lambda params: self._on_frame_sent(params["opcode"], params["data"]),
)
self._channel.on(
"frameReceived",
lambda params: self._on_frame_received(params["opcode"], params["data"]),
)
self._channel.on(
"error", lambda params: self.emit(WebSocket.Events.Error, params["error"])
)
self._channel.on("close", lambda params: self._on_close())
def __repr__(self) -> str:
return f"<WebSocket url={self.url!r}>"
@property
def url(self) -> str:
return self._initializer["url"]
def expect_event(
self,
event: str,
predicate: Callable = None,
timeout: float = None,
) -> EventContextManagerImpl:
if timeout is None:
timeout = cast(Any, self._parent)._timeout_settings.timeout()
wait_helper = WaitHelper(self, f"web_socket.expect_event({event})")
wait_helper.reject_on_timeout(
timeout, f'Timeout while waiting for event "{event}"'
)
if event != WebSocket.Events.Close:
wait_helper.reject_on_event(
self, WebSocket.Events.Close, Error("Socket closed")
)
if event != WebSocket.Events.Error:
wait_helper.reject_on_event(
self, WebSocket.Events.Error, Error("Socket error")
)
wait_helper.reject_on_event(self._parent, "close", Error("Page closed"))
wait_helper.wait_for_event(self, event, predicate)
return EventContextManagerImpl(wait_helper.result())
async def wait_for_event(
self, event: str, predicate: Callable = None, timeout: float = None
) -> Any:
async with self.expect_event(event, predicate, timeout) as event_info:
pass
return await event_info
def _on_frame_sent(self, opcode: int, data: str) -> None:
if opcode == 2:
self.emit(WebSocket.Events.FrameSent, base64.b64decode(data))
else:
self.emit(WebSocket.Events.FrameSent, data)
def _on_frame_received(self, opcode: int, data: str) -> None:
if opcode == 2:
self.emit(WebSocket.Events.FrameReceived, base64.b64decode(data))
else:
self.emit(WebSocket.Events.FrameReceived, data)
def is_closed(self) -> bool:
return self._is_closed
def _on_close(self) -> None:
self._is_closed = True
self.emit(WebSocket.Events.Close)
def serialize_headers(headers: Dict[str, str]) -> List[Header]:
return [{"name": name, "value": value} for name, value in headers.items()]
def parse_headers(headers: List[Header]) -> Dict[str, str]:
return {header["name"].lower(): header["value"] for header in headers}