-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathNetworkManager.py
More file actions
620 lines (554 loc) · 26.6 KB
/
NetworkManager.py
File metadata and controls
620 lines (554 loc) · 26.6 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 FreeCAD Project Association
# SPDX-FileNotice: Part of the AddonManager.
################################################################################
# #
# This addon is free software: you can redistribute it and/or modify #
# it under the terms of the GNU Lesser General Public License as #
# published by the Free Software Foundation, either version 2.1 #
# of the License, or (at your option) any later version. #
# #
# This addon is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty #
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# See the GNU Lesser General Public License for more details. #
# #
# You should have received a copy of the GNU Lesser General Public #
# License along with this addon. If not, see https://www.gnu.org/licenses #
# #
################################################################################
"""
#############################################################################
#
# ABOUT NETWORK MANAGER
#
# A wrapper around QNetworkAccessManager providing proxy-handling
# capabilities, and simplified access to submitting requests from any
# application thread.
#
#
# USAGE
#
# Once imported, this file provides access to a global object called
# AM_NETWORK_MANAGER. This is a QObject running on the main thread, but
# designed to be interacted with from any other application thread. It
# provides two principal methods: submit_unmonitored_get() and
# submit_monitored_get(). Use the unmonitored version for small amounts of
# data (suitable for caching in RAM, and without a need to show a progress
# bar during download), and the monitored version for larger amounts of data.
# Both functions take a URL, and return an integer index. That index allows
# tracking of the completed request by attaching to the signals completed(),
# progress_made(), and progress_complete(). All three provide, as the first
# argument to the signal, the index of the request the signal refers to.
# Code attached to those signals should filter them to look for the indices
# of the requests they care about. Requests may complete in any order.
#
# A secondary blocking interface is also provided, for very short network
# accesses: the blocking_get() function blocks until the network transmission
# is complete, directly returning a QByteArray object with the received data.
# Do not run on the main GUI thread!
"""
import threading
import os
import queue
import itertools
import tempfile
import time
from typing import Dict, List, Optional
import addonmanager_freecad_interface as fci
from addonmanager_preferences_migrations import migrate_proxy_settings_2025
from PySideWrapper import QtCore, QtNetwork, QtWidgets
translate = fci.translate
# This is the global instance of the NetworkManager that outside code
# should access
AM_NETWORK_MANAGER = None
# Added in Qt 5.15
if hasattr(QtNetwork.QNetworkRequest, "DefaultTransferTimeoutConstant"):
timeoutConstant = QtNetwork.QNetworkRequest.DefaultTransferTimeoutConstant
if hasattr(timeoutConstant, "value"):
# Qt 6 changed the timeout constant to have a 'value' attribute.
# The function setTransferTimeout does not accept
# DefaultTransferTimeoutConstant of type
# QtNetwork.QNetworkRequest.TransferTimeoutConstant any
# longer but only an int.
default_timeout = timeoutConstant.value
else:
# In Qt 5.15 we can use the timeoutConstant as is.
default_timeout = timeoutConstant
else:
default_timeout = 30000
class QueueItem:
"""A container for information about an item in the network queue."""
def __init__(
self,
index: int,
request: QtNetwork.QNetworkRequest,
track_progress: bool,
operation: QtNetwork.QNetworkAccessManager.Operation = QtNetwork.QNetworkAccessManager.GetOperation,
):
self.index = index
self.request = request
self.original_url = request.url()
self.track_progress = track_progress
self.operation = operation
class NetworkManager(QtCore.QObject):
"""A single global instance of NetworkManager is instantiated and stored as
AM_NETWORK_MANAGER. Outside threads should send GET requests to this class by
calling the submit_unmonitored_request() or submit_monitored_request() function,
as needed. See the documentation of those functions for details."""
# Connect to complete for requests with no progress monitoring (e.g. small amounts of data)
completed = QtCore.Signal(
int, int, QtCore.QByteArray
) # Index, http response code, received data (if any)
content_length = QtCore.Signal(int, int, int)
# Connect to progress_made and progress_complete for large amounts of data, which get buffered into a temp file
# That temp file should be deleted when your code is done with it
progress_made = QtCore.Signal(int, int, int) # Index, bytes read, total bytes (may be None)
progress_complete = QtCore.Signal(int, int, os.PathLike) # Index, http response code, filename
__request_queued = QtCore.Signal()
def __init__(self):
super().__init__()
self.counting_iterator = itertools.count()
self.queue = queue.Queue()
self.__last_started_index = 0
self.__abort_when_found: List[int] = []
self.replies: Dict[int, QtNetwork.QNetworkReply] = {}
self.file_buffers = {}
# We support an arbitrary number of threads using synchronous GET calls:
self.synchronous_lock = threading.Lock()
self.synchronous_complete: Dict[int, bool] = {}
self.synchronous_result_data: Dict[int, QtCore.QByteArray] = {}
# Only one size request at a time:
self.download_size_lock = threading.Lock()
# Make sure we exit nicely on quit
if QtCore.QCoreApplication.instance() is not None:
QtCore.QCoreApplication.instance().aboutToQuit.connect(self.__aboutToQuit)
# Create the QNAM on this thread:
self.QNAM = QtNetwork.QNetworkAccessManager()
self.QNAM.proxyAuthenticationRequired.connect(self.__authenticate_proxy)
self.QNAM.authenticationRequired.connect(self.__authenticate_resource)
self.QNAM.setRedirectPolicy(QtNetwork.QNetworkRequest.ManualRedirectPolicy)
qnam_cache = QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.CacheLocation)
os.makedirs(qnam_cache, exist_ok=True)
self.diskCache = QtNetwork.QNetworkDiskCache()
self.diskCache.setCacheDirectory(qnam_cache)
self.QNAM.setCache(self.diskCache)
self.monitored_connections: List[int] = []
self._setup_proxy()
# A helper connection for our blocking interface
self.completed.connect(self.__synchronous_process_completion)
# Set up our worker connection
self.__request_queued.connect(self.__setup_network_request)
def _setup_proxy(self):
"""Set up the proxy based on user preferences or prompts on command line"""
# Set up the proxy, if necessary:
if fci.FreeCAD:
migrate_proxy_settings_2025()
proxy_type = fci.Preferences().get("proxy_type")
proxy_host = fci.Preferences().get("proxy_host")
proxy_port = fci.Preferences().get("proxy_port")
else:
proxy_type, proxy_host, proxy_port = self._setup_proxy_standalone()
if proxy_type == "none":
pass
elif proxy_type == "system":
query = QtNetwork.QNetworkProxyQuery(QtCore.QUrl("https://github.com/FreeCAD/FreeCAD"))
proxy = QtNetwork.QNetworkProxyFactory.systemProxyForQuery(query)
if proxy and proxy[0]:
self.QNAM.setProxy(proxy[0]) # This may still be QNetworkProxy.NoProxy
elif proxy_type == "custom":
try:
fci.Console.PrintMessage(f"Using proxy {proxy_host}:{proxy_port} \n")
proxy = QtNetwork.QNetworkProxy(
QtNetwork.QNetworkProxy.HttpProxy, proxy_host, proxy_port
)
self.QNAM.setProxy(proxy)
except Exception as e:
fci.Console.PrintError(f"Error setting up proxy: {e}\n")
def _setup_proxy_standalone(self):
"""If we are NOT running inside FreeCAD, prompt the user for proxy information"""
print("Please select a proxy type:")
print("1) No proxy")
print("2) Use system proxy settings")
print("3) Custom proxy settings")
result = input("Choice: ")
if result == "1":
return "none", "", ""
elif result == "2":
return "system", "", ""
elif result == "3":
host = input("Enter proxy host: ")
port = int(input("Enter proxy port: "))
else:
print("Defaulting to system proxy settings")
return "system", "", ""
return "custom", host, port
def __aboutToQuit(self):
"""Called when the application is about to quit. Not currently used."""
def __setup_network_request(self):
"""Get the next request off the queue and launch it."""
try:
item = self.queue.get_nowait()
if item:
if item.index in self.__abort_when_found:
self.__abort_when_found.remove(item.index)
return # Do not do anything with this item, it's been aborted...
if item.track_progress:
self.monitored_connections.append(item.index)
self.__launch_request(item.index, item.request, item.operation)
except queue.Empty:
# Once the queue is empty, there's nothing left to do for now
pass
def __launch_request(
self,
index: int,
request: QtNetwork.QNetworkRequest,
operation: QtNetwork.QNetworkAccessManager.Operation,
) -> None:
"""Given a network request, ask the QNetworkAccessManager to begin processing it."""
if operation == QtNetwork.QNetworkAccessManager.GetOperation:
reply = self.QNAM.get(request)
elif operation == QtNetwork.QNetworkAccessManager.HeadOperation:
reply = self.QNAM.head(request)
else:
raise NotImplementedError(f"Unknown operation {operation.name}")
self.replies[index] = reply
self.__last_started_index = index
reply.finished.connect(self.__reply_finished)
reply.sslErrors.connect(self.__on_ssl_error)
if index in self.monitored_connections:
reply.readyRead.connect(self.__ready_to_read)
reply.downloadProgress.connect(self.__download_progress)
def query_download_size(self, url: str, timeout_ms: int = default_timeout):
"""A query to get the download size in the 'Content-Length' header, or zero if the server
doesn't support it. Connect to the download_size signal to get the result when it arrives.
"""
with self.download_size_lock:
current_index = next(self.counting_iterator) # A thread-safe counter
item = QueueItem(
current_index,
self.__create_get_request(url, timeout_ms, disable_cache=True),
track_progress=False,
operation=QtNetwork.QNetworkAccessManager.HeadOperation,
)
self.queue.put(item)
self.__request_queued.emit()
return current_index
def submit_unmonitored_get(
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
) -> int:
"""Adds this request to the queue, and returns an index that can be used by calling code
in conjunction with the completed() signal to handle the results of the call. All data is
kept in memory, and the completed() call includes a direct handle to the bytes returned. It
is not called until the data transfer has finished and the connection is closed."""
current_index = next(self.counting_iterator) # A thread-safe counter
# Use a queue because we can only put things on the QNAM from the main event loop thread
self.queue.put(
QueueItem(
current_index,
self.__create_get_request(url, timeout_ms, disable_cache),
track_progress=False,
)
)
self.__request_queued.emit()
return current_index
def submit_monitored_get(
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
) -> int:
"""Adds this request to the queue, and returns an index that can be used by calling code
in conjunction with the progress_made() and progress_completed() signals to handle the
results of the call. All data is cached to disk, and progress is reported periodically
as the underlying QNetworkReply reports its progress. The progress_completed() signal
contains a path to a temporary file with the stored data. Calling code should delete this
file when done with it (or move it into its final place, etc.)."""
current_index = next(self.counting_iterator) # A thread-safe counter
# Use a queue because we can only put things on the QNAM from the main event loop thread
self.queue.put(
QueueItem(
current_index,
self.__create_get_request(url, timeout_ms, disable_cache),
track_progress=True,
)
)
self.__request_queued.emit()
return current_index
def blocking_get_with_retries(
self,
url: str,
timeout_ms: int = default_timeout,
max_attempts: int = 3,
delay_ms: int = 1000,
disable_cache: bool = False,
):
"""Submits a GET request to the QNetworkAccessManager and blocks until it is complete. Do
not use on the main GUI thread, it will prevent any event processing while it blocks.
:url: The URL to fetch
:timeout_ms: The timeout in milliseconds
:max_attempts: The number of attempts to make if the request fails
:delay_ms: The delay between attempts in milliseconds
:returns: The response data, or None if the request failed after max_attempts attempts.
"""
if max_attempts < 1:
raise ValueError("max_attempts must be greater than 0")
if timeout_ms < 1:
raise ValueError("timeout_ms must be greater than 0")
attempt = 0
while True:
attempt += 1
p = self.blocking_get(url, timeout_ms)
if p is not None or attempt >= max_attempts:
return p
if QtCore.QThread.currentThread().isInterruptionRequested():
return None
fci.Console.PrintWarning(
f"Failed to get {url}, retrying in {delay_ms}ms... (attempt {attempt} of {max_attempts})\n"
)
time.sleep(delay_ms / 1000)
def blocking_get(
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
) -> Optional[QtCore.QByteArray]:
"""Submits a GET request to the QNetworkAccessManager and blocks until it is complete. Do
not use on the main GUI thread, it will prevent any event processing while it blocks.
:url: The URL to fetch
:timeout_ms: The timeout in milliseconds
:returns: The response data, or None if the request failed after max_attempts attempts.
"""
current_index = next(self.counting_iterator) # A thread-safe counter
with self.synchronous_lock:
self.synchronous_complete[current_index] = False
self.queue.put(
QueueItem(
current_index,
self.__create_get_request(url, timeout_ms, disable_cache),
track_progress=False,
)
)
self.__request_queued.emit()
while True:
if QtCore.QThread.currentThread().isInterruptionRequested():
return None
QtCore.QCoreApplication.processEvents()
with self.synchronous_lock:
if self.synchronous_complete[current_index]:
break
with self.synchronous_lock:
self.synchronous_complete.pop(current_index)
if current_index in self.synchronous_result_data:
return self.synchronous_result_data.pop(current_index)
return None
def __synchronous_process_completion(
self, index: int, code: int, data: QtCore.QByteArray
) -> None:
"""Check the return status of a completed process, and handle its returned data (if
any)."""
with self.synchronous_lock:
if index in self.synchronous_complete:
if code == 200:
self.synchronous_result_data[index] = data
else:
fci.Console.PrintWarning(
translate(
"AddonsInstaller",
"Addon Manager: Unexpected {} response from server",
).format(code)
+ "\n"
)
self.synchronous_complete[index] = True
@staticmethod
def __create_get_request(
url: str, timeout_ms: int, disable_cache: bool
) -> QtNetwork.QNetworkRequest:
"""Construct a network request to a given URL"""
request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
request.setAttribute(
QtNetwork.QNetworkRequest.RedirectPolicyAttribute,
QtNetwork.QNetworkRequest.ManualRedirectPolicy,
)
if disable_cache:
request.setAttribute(QtNetwork.QNetworkRequest.CacheSaveControlAttribute, False)
request.setAttribute(
QtNetwork.QNetworkRequest.CacheLoadControlAttribute,
QtNetwork.QNetworkRequest.AlwaysNetwork,
)
else:
request.setAttribute(QtNetwork.QNetworkRequest.CacheSaveControlAttribute, True)
request.setAttribute(
QtNetwork.QNetworkRequest.CacheLoadControlAttribute,
QtNetwork.QNetworkRequest.PreferNetwork,
)
if hasattr(request, "setTransferTimeout"):
# Added in Qt 5.15
# In Qt 5, the function setTransferTimeout seems to accept
# DefaultTransferTimeoutConstant of type
# PySide2.QtNetwork.QNetworkRequest.TransferTimeoutConstant,
# whereas in Qt 6, the function seems to only accept an
# integer.
request.setTransferTimeout(timeout_ms)
request.setRawHeader(b"User-Agent", b"FreeCAD AddonManager/1.0")
return request
def abort_all(self):
"""Abort ALL network calls in progress, including clearing the queue"""
for reply in self.replies.values():
if reply.isRunning():
reply.abort()
while True:
try:
self.queue.get()
self.queue.task_done()
except queue.Empty:
break
def abort(self, index: int):
"""Abort a specific request"""
if index in self.replies and self.replies[index].isRunning():
self.replies[index].abort()
elif index < self.__last_started_index:
# It's still in the queue. Mark it for later destruction.
self.__abort_when_found.append(index)
def __authenticate_proxy(
self,
reply: QtNetwork.QNetworkProxy,
authenticator: QtNetwork.QAuthenticator,
):
"""If proxy authentication is required, attempt to authenticate. If the GUI is running this displays
a window asking for credentials. If the GUI is not running, it prompts on the command line.
"""
if fci.FreeCAD and fci.GuiUp:
proxy_authentication = fci.loadUi(
os.path.join(os.path.dirname(__file__), "proxy_authentication.ui")
)
proxy_authentication.setObjectName("AddonManager_ProxyAuthenticationDialog")
# Show the right labels, etc.
proxy_authentication.labelProxyAddress.setText(f"{reply.hostName()}:{reply.port()}")
if authenticator.realm():
proxy_authentication.labelProxyRealm.setText(authenticator.realm())
else:
proxy_authentication.labelProxyRealm.hide()
proxy_authentication.labelRealmCaption.hide()
result = proxy_authentication.exec()
if result == QtWidgets.QDialogButtonBox.Ok:
authenticator.setUser(proxy_authentication.lineEditUsername.text())
authenticator.setPassword(proxy_authentication.lineEditPassword.text())
else:
username = input("Proxy username: ")
import getpass
password = getpass.getpass()
authenticator.setUser(username)
authenticator.setPassword(password)
def __authenticate_resource(
self,
_reply: QtNetwork.QNetworkReply,
_authenticator: QtNetwork.QAuthenticator,
):
"""Unused."""
def __on_ssl_error(self, reply: str, errors: List[str] = None):
"""Called when an SSL error occurs: prints the error information."""
fci.Console.PrintWarning(
translate("AddonsInstaller", "Error with encrypted connection") + "\n:"
)
fci.Console.PrintWarning(reply)
if errors is not None:
for error in errors:
fci.Console.PrintWarning(error)
def __download_progress(self, bytesReceived: int, bytesTotal: int) -> None:
"""Monitors download progress and emits a progress_made signal"""
sender = self.sender()
if not sender:
return
for index, reply in self.replies.items():
if reply == sender:
self.progress_made.emit(index, bytesReceived, bytesTotal)
return
def __ready_to_read(self) -> None:
"""Called when data is available, this reads that data."""
sender = self.sender()
if not sender:
return
for index, reply in self.replies.items():
if reply == sender:
self.__data_incoming(index, reply)
return
def __data_incoming(self, index: int, reply: QtNetwork.QNetworkReply) -> None:
"""Read incoming data and attach it to a data object"""
if not index in self.replies:
# We already finished this reply, this is a vestigial signal
return
buffer = reply.readAll()
if not index in self.file_buffers:
f = tempfile.NamedTemporaryFile("wb", delete=False)
self.file_buffers[index] = f
else:
f = self.file_buffers[index]
try:
f.write(buffer.data())
except OSError as e:
fci.Console.PrintError(f"Network Manager internal error: {str(e)}")
def __reply_finished(self) -> None:
"""Called when a reply has been completed: this makes sure the data has been read and
any notifications have been called."""
reply: QtNetwork.QNetworkReply = self.sender()
if not reply:
# This can happen during a cancellation operation: silently do nothing
return
index = None
for key, value in self.replies.items():
if reply == value:
index = key
break
if index is None:
return
response_code = reply.attribute(QtNetwork.QNetworkRequest.HttpStatusCodeAttribute)
redirect_codes = [301, 302, 303, 305, 307, 308]
if response_code in redirect_codes: # This is a redirect
timeout_ms = default_timeout
if hasattr(reply, "request"):
request = reply.request()
if hasattr(request, "transferTimeout"):
timeout_ms = request.transferTimeout()
new_url = reply.attribute(QtNetwork.QNetworkRequest.RedirectionTargetAttribute)
disable_cache = not reply.request().attribute(
QtNetwork.QNetworkRequest.CacheSaveControlAttribute
)
self.__launch_request(
index,
self.__create_get_request(new_url, timeout_ms, disable_cache=disable_cache),
reply.operation(),
)
return # The task is not done, so get out of this method now
if reply.error() != QtNetwork.QNetworkReply.NetworkError.OperationCanceledError:
# It this was not a timeout, make sure we mark the queue task done
self.queue.task_done()
if reply.error() == QtNetwork.QNetworkReply.NetworkError.NoError:
if index in self.monitored_connections:
# Make sure to read any remaining data
self.__data_incoming(index, reply)
self.monitored_connections.remove(index)
f = self.file_buffers[index]
f.close()
self.progress_complete.emit(index, response_code, f.name)
elif reply.operation() == QtNetwork.QNetworkAccessManager.HeadOperation:
# The data we were trying to read was the ContentLengthHeader, so make sure that's what we emit
data = reply.header(QtNetwork.QNetworkRequest.ContentLengthHeader)
if data is None:
data = 0
self.content_length.emit(index, response_code, data)
else:
data = reply.readAll()
self.completed.emit(index, response_code, data)
else:
if index in self.monitored_connections:
self.progress_complete.emit(index, response_code, "")
elif reply.operation() == QtNetwork.QNetworkAccessManager.HeadOperation:
self.content_length.emit(index, response_code, 0)
else:
self.completed.emit(index, response_code, None)
self.replies.pop(index)
def InitializeNetworkManager():
"""Called once at the beginning of program execution to create the appropriate manager object"""
global AM_NETWORK_MANAGER
if AM_NETWORK_MANAGER is None:
AM_NETWORK_MANAGER = NetworkManager()
def ForceReinitializeNetworkManager():
"""Called when the user changes the network settings, to force a re-initialization of the
network manager."""
global AM_NETWORK_MANAGER
AM_NETWORK_MANAGER = NetworkManager()