| 1 | // curl ABI access only; transfer scheduling and filesystem decisions stay in V. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | import sync |
| 7 | |
| 8 | #flag -lcurl |
| 9 | #include "@DIR/native_curl.h" |
| 10 | |
| 11 | struct C.CURLMsg { |
| 12 | msg i32 |
| 13 | easy_handle voidptr |
| 14 | } |
| 15 | |
| 16 | fn C.curl_global_init(int) i32 |
| 17 | fn C.curl_easy_init() voidptr |
| 18 | fn C.curl_easy_cleanup(voidptr) |
| 19 | fn C.curl_easy_strerror(i32) &char |
| 20 | fn C.curl_multi_init() voidptr |
| 21 | fn C.curl_multi_cleanup(voidptr) i32 |
| 22 | fn C.curl_multi_add_handle(voidptr, voidptr) i32 |
| 23 | fn C.curl_multi_remove_handle(voidptr, voidptr) i32 |
| 24 | fn C.curl_multi_perform(voidptr, &i32) i32 |
| 25 | fn C.curl_multi_poll(voidptr, voidptr, u32, i32, &i32) i32 |
| 26 | fn C.curl_multi_info_read(voidptr, &i32) &C.CURLMsg |
| 27 | fn C.curl_multi_strerror(i32) &char |
| 28 | fn C.v_alpm_curl_long(voidptr, i32, i64) i32 |
| 29 | fn C.v_alpm_curl_offset(voidptr, i32, i64) i32 |
| 30 | fn C.v_alpm_curl_pointer(voidptr, i32, voidptr) i32 |
| 31 | fn C.v_alpm_curl_write(voidptr, fn (voidptr, uint, uint, voidptr) uint) i32 |
| 32 | fn C.v_alpm_curl_header(voidptr, fn (voidptr, uint, uint, voidptr) uint) i32 |
| 33 | fn C.v_alpm_curl_progress(voidptr, fn (voidptr, i64, i64, i64, i64) i32) i32 |
| 34 | fn C.v_alpm_curl_info_long(voidptr, i32, &i64) i32 |
| 35 | fn C.v_alpm_curl_info_offset(voidptr, i32, &i64) i32 |
| 36 | fn C.v_alpm_curl_info_string(voidptr, i32, &&char) i32 |
| 37 | fn C.v_alpm_curl_message_result(&C.CURLMsg) i32 |
| 38 | fn C.v_alpm_curl_payload(voidptr, &voidptr) i32 |
| 39 | |
| 40 | struct CurlRuntime { |
| 41 | mut: |
| 42 | mutex &sync.Mutex = sync.new_mutex() |
| 43 | ready bool |
| 44 | } |
| 45 | |
| 46 | const curl_runtime = &CurlRuntime{} |
| 47 | |
| 48 | fn curl_multi_error(status i32) string { |
| 49 | return unsafe { cstring_to_vstring(C.curl_multi_strerror(status)) } |
| 50 | } |
| 51 | |
| 52 | fn check_curl(status i32) ! { |
| 53 | if status != 0 { |
| 54 | return AlpmError{ kind: .libcurl, detail: unsafe { cstring_to_vstring(C.curl_easy_strerror(status)) } } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn check_curl_multi(status i32) ! { |
| 59 | if status != 0 { return AlpmError{ kind: .libcurl, detail: curl_multi_error(status) } } |
| 60 | } |
| 61 | |
| 62 | fn (mut handle Handle) initialize_downloads() ! { |
| 63 | if handle.curl_multi != unsafe { nil } { return } |
| 64 | mut runtime := curl_runtime |
| 65 | runtime.mutex.@lock() |
| 66 | defer { runtime.mutex.unlock() } |
| 67 | if !runtime.ready { |
| 68 | check_curl(C.curl_global_init(C.CURL_GLOBAL_DEFAULT))! |
| 69 | runtime.ready = true |
| 70 | } |
| 71 | if handle.curl_multi == unsafe { nil } { |
| 72 | handle.curl_multi = C.curl_multi_init() |
| 73 | if handle.curl_multi == unsafe { nil } { |
| 74 | return AlpmError{ kind: .libcurl, detail: 'cannot initialize curl multi handle' } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn download_header_callback(_ voidptr, size uint, count uint, context voidptr) uint { |
| 80 | if count != 0 && size > ~uint(0) / count { return 0 } |
| 81 | mut payload := unsafe { &DownloadPayload(context) } |
| 82 | if C.v_alpm_curl_info_long(payload.curl, C.CURLINFO_RESPONSE_CODE, &payload.response) != 0 { |
| 83 | return 0 |
| 84 | } |
| 85 | return size * count |
| 86 | } |
| 87 | |
| 88 | fn download_write_callback(data voidptr, size uint, count uint, context voidptr) uint { |
| 89 | if count != 0 && size > ~uint(0) / count { return 0 } |
| 90 | length := size * count |
| 91 | mut payload := unsafe { &DownloadPayload(context) } |
| 92 | if payload.handle.download_control.interrupted != 0 { return 0 } |
| 93 | if payload.response >= 300 { return length } |
| 94 | return payload.write_bytes(data, length) |
| 95 | } |
| 96 | |
| 97 | fn download_progress_callback(context voidptr, total i64, downloaded i64, _ i64, _ i64) i32 { |
| 98 | mut payload := unsafe { &DownloadPayload(context) } |
| 99 | if payload.handle.download_control.interrupted != 0 { return 1 } |
| 100 | if payload.response >= 300 || downloaded < 0 || total <= 0 || downloaded > total { return 0 } |
| 101 | if payload.previous_progress == downloaded { return 0 } |
| 102 | payload.emit(.progress, downloaded, total, 0) |
| 103 | payload.previous_progress = downloaded |
| 104 | return 0 |
| 105 | } |
| 106 | |
| 107 | fn (mut payload DownloadPayload) configure_curl() ! { |
| 108 | curl := payload.curl |
| 109 | for status in [ |
| 110 | C.v_alpm_curl_pointer(curl, C.CURLOPT_URL, payload.url.str), |
| 111 | C.v_alpm_curl_pointer(curl, C.CURLOPT_ERRORBUFFER, payload.error_buffer.data), |
| 112 | C.v_alpm_curl_pointer(curl, C.CURLOPT_WRITEDATA, payload), |
| 113 | C.v_alpm_curl_pointer(curl, C.CURLOPT_HEADERDATA, payload), |
| 114 | C.v_alpm_curl_pointer(curl, C.CURLOPT_XFERINFODATA, payload), |
| 115 | C.v_alpm_curl_pointer(curl, C.CURLOPT_PRIVATE, payload), |
| 116 | C.v_alpm_curl_write(curl, download_write_callback), |
| 117 | C.v_alpm_curl_header(curl, download_header_callback), |
| 118 | C.v_alpm_curl_progress(curl, download_progress_callback), |
| 119 | C.v_alpm_curl_long(curl, C.CURLOPT_CONNECTTIMEOUT, 10), |
| 120 | C.v_alpm_curl_long(curl, C.CURLOPT_MAXREDIRS, 10), |
| 121 | C.v_alpm_curl_long(curl, C.CURLOPT_FILETIME, 1), |
| 122 | C.v_alpm_curl_long(curl, C.CURLOPT_NOPROGRESS, 0), |
| 123 | C.v_alpm_curl_long(curl, C.CURLOPT_FOLLOWLOCATION, 1), |
| 124 | C.v_alpm_curl_long(curl, C.CURLOPT_NOSIGNAL, 1), |
| 125 | C.v_alpm_curl_long(curl, C.CURLOPT_NETRC, C.CURL_NETRC_OPTIONAL), |
| 126 | C.v_alpm_curl_long(curl, C.CURLOPT_TCP_KEEPALIVE, 1), |
| 127 | C.v_alpm_curl_long(curl, C.CURLOPT_TCP_KEEPIDLE, 60), |
| 128 | C.v_alpm_curl_long(curl, C.CURLOPT_TCP_KEEPINTVL, 60), |
| 129 | C.v_alpm_curl_long(curl, C.CURLOPT_HTTPAUTH, i64(C.CURLAUTH_ANY)), |
| 130 | ] { |
| 131 | check_curl(status)! |
| 132 | } |
| 133 | if payload.handle.opts.integers[.disable_download_timeout] == 0 { |
| 134 | check_curl(C.v_alpm_curl_long(curl, C.CURLOPT_LOW_SPEED_LIMIT, 1))! |
| 135 | check_curl(C.v_alpm_curl_long(curl, C.CURLOPT_LOW_SPEED_TIME, 10))! |
| 136 | } |
| 137 | if payload.request.max_size > 0 { |
| 138 | check_curl(C.v_alpm_curl_offset(curl, C.CURLOPT_MAXFILESIZE_LARGE, payload.request.max_size))! |
| 139 | } |
| 140 | if useragent := os.getenv_opt('HTTP_USER_AGENT') { |
| 141 | check_curl(C.v_alpm_curl_pointer(curl, C.CURLOPT_USERAGENT, useragent.str))! |
| 142 | } |
| 143 | if payload.initial_size > 0 { |
| 144 | check_curl(C.v_alpm_curl_offset(curl, C.CURLOPT_RESUME_FROM_LARGE, payload.initial_size))! |
| 145 | } else if !payload.request.force && payload.existing_mtime != 0 { |
| 146 | check_curl(C.v_alpm_curl_long(curl, C.CURLOPT_TIMECONDITION, C.CURL_TIMECOND_IFMODSINCE))! |
| 147 | check_curl(C.v_alpm_curl_offset(curl, C.CURLOPT_TIMEVALUE_LARGE, payload.existing_mtime))! |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | fn (mut payload DownloadPayload) detach_curl() { |
| 152 | if payload.curl == unsafe { nil } { return } |
| 153 | // ftp teardown can still touch progress/error pointers after a transfer finishes. |
| 154 | for status in [ |
| 155 | C.v_alpm_curl_long(payload.curl, C.CURLOPT_NOPROGRESS, 1), |
| 156 | C.v_alpm_curl_pointer(payload.curl, C.CURLOPT_ERRORBUFFER, unsafe { nil }), |
| 157 | ] { |
| 158 | check_curl(status) or { payload.handle.diagnostics << err.msg() } |
| 159 | } |
| 160 | if payload.attached { |
| 161 | status := C.curl_multi_remove_handle(payload.handle.curl_multi, payload.curl) |
| 162 | if status != 0 { payload.handle.diagnostics << curl_multi_error(status) } |
| 163 | payload.attached = false |
| 164 | } |
| 165 | C.curl_easy_cleanup(payload.curl) |
| 166 | payload.curl = unsafe { nil } |
| 167 | } |
| 168 | |
| 169 | struct DownloadTransport { |
| 170 | multi voidptr |
| 171 | } |
| 172 | |
| 173 | fn (mut handle Handle) new_download_transport() !&DownloadTransport { |
| 174 | handle.initialize_downloads()! |
| 175 | return &DownloadTransport{ multi: handle.curl_multi } |
| 176 | } |
| 177 | |
| 178 | fn (transport &DownloadTransport) submit(mut payload DownloadPayload) ! { |
| 179 | payload.curl = C.curl_easy_init() |
| 180 | if payload.curl == unsafe { nil } { |
| 181 | return AlpmError{ kind: .libcurl, detail: 'cannot initialize curl easy handle' } |
| 182 | } |
| 183 | payload.error_buffer = []u8{len: int(C.CURL_ERROR_SIZE)} |
| 184 | payload.configure_curl()! |
| 185 | check_curl_multi(C.curl_multi_add_handle(transport.multi, payload.curl))! |
| 186 | payload.attached = true |
| 187 | } |
| 188 | |
| 189 | fn (transport &DownloadTransport) next() !(&DownloadPayload, TransferResult) { |
| 190 | for { |
| 191 | mut running := i32(0) |
| 192 | check_curl_multi(C.curl_multi_perform(transport.multi, &running))! |
| 193 | for { |
| 194 | mut messages := i32(0) |
| 195 | message := C.curl_multi_info_read(transport.multi, &messages) |
| 196 | if message == unsafe { nil } { break } |
| 197 | if message.msg != C.CURLMSG_DONE { continue } |
| 198 | mut pointer := voidptr(unsafe { nil }) |
| 199 | check_curl(C.v_alpm_curl_payload(message.easy_handle, &pointer))! |
| 200 | if pointer == unsafe { nil } { return error('download payload missing') } |
| 201 | mut payload := unsafe { &DownloadPayload(pointer) } |
| 202 | result := payload.curl_result(C.v_alpm_curl_message_result(message))! |
| 203 | return payload, result |
| 204 | } |
| 205 | check_curl_multi(C.curl_multi_poll(transport.multi, unsafe { nil }, 0, 1000, unsafe { nil }))! |
| 206 | } |
| 207 | return error('no active download') |
| 208 | } |
| 209 | |
| 210 | fn (mut payload DownloadPayload) curl_result(code i32) !TransferResult { |
| 211 | mut bytes := i64(0) |
| 212 | mut remaining := i64(-1) |
| 213 | mut remote_time := i64(-1) |
| 214 | mut condition := i64(0) |
| 215 | mut effective := &char(unsafe { nil }) |
| 216 | for result in [ |
| 217 | C.v_alpm_curl_info_long(payload.curl, C.CURLINFO_RESPONSE_CODE, &payload.response), |
| 218 | C.v_alpm_curl_info_offset(payload.curl, C.CURLINFO_SIZE_DOWNLOAD_T, &bytes), |
| 219 | C.v_alpm_curl_info_offset(payload.curl, C.CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &remaining), |
| 220 | C.v_alpm_curl_info_offset(payload.curl, C.CURLINFO_FILETIME_T, &remote_time), |
| 221 | C.v_alpm_curl_info_long(payload.curl, C.CURLINFO_CONDITION_UNMET, &condition), |
| 222 | C.v_alpm_curl_info_string(payload.curl, C.CURLINFO_EFFECTIVE_URL, &effective), |
| 223 | ] { |
| 224 | check_curl(result)! |
| 225 | } |
| 226 | |
| 227 | return TransferResult{ |
| 228 | status: match code { |
| 229 | 0 { TransferStatus.ok } |
| 230 | C.CURLE_RANGE_ERROR, C.CURLE_BAD_DOWNLOAD_RESUME { .range_error } |
| 231 | C.CURLE_COULDNT_RESOLVE_HOST { .resolve_error } |
| 232 | C.CURLE_FILESIZE_EXCEEDED { .too_large } |
| 233 | else { .failed } |
| 234 | } |
| 235 | bytes: bytes |
| 236 | expected: remaining |
| 237 | remote_time: remote_time |
| 238 | unmodified: condition == 1 |
| 239 | effective_url: gpg_string(effective) |
| 240 | detail: if payload.error_buffer[0] != 0 { |
| 241 | c_string_prefix(payload.error_buffer.bytestr()) |
| 242 | } else { |
| 243 | unsafe { cstring_to_vstring(C.curl_easy_strerror(code)) } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |