@@ -75,66 +75,6 @@ public static string Fetch(
7575 }
7676 }
7777
78- public static void DownloadFile (
79- string url ,
80- string path ,
81- bool overwrite = true ,
82- bool http3 = false ,
83- Action < string > ? onHttpVersion = null ,
84- Action < double > ? onProgress = null ,
85- CancellationToken ct = default
86- )
87- {
88- IntPtr handle = LibCurl . curl_easy_init ( ) ;
89- if ( handle == IntPtr . Zero )
90- throw new InvalidOperationException ( "curl_easy_init failed" ) ;
91-
92- using var fs = new FileStream (
93- path ,
94- overwrite ? FileMode . Create : FileMode . CreateNew ,
95- FileAccess . Write
96- ) ;
97- var writePin = GCHandle . Alloc ( fs ) ;
98- var xferPin = InstallXferCallback ( handle , ct , onProgress ) ;
99- try
100- {
101- LibCurl . curl_easy_impersonate ( handle , Impersonation , 1 ) ;
102- LibCurl . curl_easy_setopt_str ( handle , LibCurl . CURLOPT_ACCEPT_ENCODING , "" ) ;
103- LibCurl . curl_easy_setopt_str ( handle , LibCurl . CURLOPT_URL , url ) ;
104- LibCurl . curl_easy_setopt_cb ( handle , LibCurl . CURLOPT_WRITEFUNCTION , OnWriteFile ) ;
105- LibCurl . curl_easy_setopt_ptr (
106- handle ,
107- LibCurl . CURLOPT_WRITEDATA ,
108- GCHandle . ToIntPtr ( writePin )
109- ) ;
110- LibCurl . curl_easy_setopt_long ( handle , LibCurl . CURLOPT_FOLLOWLOCATION , 1L ) ;
111- LibCurl . curl_easy_setopt_str ( handle , LibCurl . CURLOPT_CAINFO , "cacert.pem" ) ;
112- if ( http3 )
113- LibCurl . curl_easy_setopt_long (
114- handle ,
115- LibCurl . CURLOPT_HTTP_VERSION ,
116- LibCurl . CURL_HTTP_VERSION_3
117- ) ;
118-
119- int code = LibCurl . curl_easy_perform ( handle ) ;
120- if ( code == LibCurl . CURLE_ABORTED_BY_CALLBACK )
121- ct . ThrowIfCancellationRequested ( ) ;
122- if ( code != LibCurl . CURLE_OK )
123- throw new InvalidOperationException (
124- $ "curl_easy_perform returned error code { code } "
125- ) ;
126-
127- ReportHttpVersion ( handle , onHttpVersion ) ;
128- }
129- finally
130- {
131- writePin . Free ( ) ;
132- if ( xferPin . IsAllocated )
133- xferPin . Free ( ) ;
134- LibCurl . curl_easy_cleanup ( handle ) ;
135- }
136- }
137-
13878 public static Dictionary < string , string > GetHeaders (
13979 string url ,
14080 bool http3 = false ,
0 commit comments