ExternalData: Add option to specify HTTP headers

Add a `ExternalData_HTTPHEADERS` variable to specify them.

Fixes: #26638
This commit is contained in:
Jakob Ronestjärna
2025-02-01 23:47:59 +01:00
committed by Brad King
parent 4a11fd8dde
commit 5106f34eed
2 changed files with 33 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
ExternalData-add-httpheader
---------------------------
* The :module:`ExternalData` module gained a
:variable:`ExternalData_HTTPHEADERS` variable to specify HTTP headers.

View File

@@ -133,6 +133,14 @@ calling any of the functions provided by this module.
``<key>`` in entries of the ``ExternalData_URL_TEMPLATES`` list.
See `Custom Fetch Scripts`_.
.. variable:: ExternalData_HTTPHEADERS
.. versionadded:: 4.0
The ``ExternalData_HTTPHEADERS`` variable may be used to supply a list of
headers, each element containing one header with the form ``Key: Value``.
See the :command:`file(DOWNLOAD)` command's ``HTTPHEADER`` option.
.. variable:: ExternalData_LINK_CONTENT
The ``ExternalData_LINK_CONTENT`` variable may be set to the name of a
@@ -462,6 +470,19 @@ function(ExternalData_add_target target)
endif()
endforeach()
# Store http headers.
if(ExternalData_HTTPHEADERS)
message(STATUS "${CMAKE_CURRENT_BINARY_DIR}/${target}_config.cmake")
string(CONCAT _ExternalData_CONFIG_CODE "${_ExternalData_CONFIG_CODE}\n"
"set(ExternalData_HTTPHEADERS)")
foreach(h IN LISTS ExternalData_HTTPHEADERS)
string(REPLACE "\\" "\\\\" tmp "${h}")
string(REPLACE "\"" "\\\"" h "${tmp}")
string(CONCAT _ExternalData_CONFIG_CODE "${_ExternalData_CONFIG_CODE}\n"
"list(APPEND ExternalData_HTTPHEADERS \"${h}\")")
endforeach()
endif()
# Store configuration for use by build-time script.
set(config ${CMAKE_CURRENT_BINARY_DIR}/${target}_config.cmake)
configure_file(${_ExternalData_SELF_DIR}/ExternalData_config.cmake.in ${config} @ONLY)
@@ -982,6 +1003,12 @@ function(_ExternalData_download_file url file err_var msg_var)
set(retry 3)
while(retry)
math(EXPR retry "${retry} - 1")
set(httpheader_args)
if (ExternalData_HTTPHEADERS)
foreach(h IN LISTS ExternalData_HTTPHEADERS)
list(APPEND httpheader_args HTTPHEADER "${h}")
endforeach()
endif()
if(ExternalData_TIMEOUT_INACTIVITY)
set(inactivity_timeout INACTIVITY_TIMEOUT ${ExternalData_TIMEOUT_INACTIVITY})
elseif(NOT "${ExternalData_TIMEOUT_INACTIVITY}" EQUAL 0)
@@ -1000,7 +1027,7 @@ function(_ExternalData_download_file url file err_var msg_var)
if (ExternalData_SHOW_PROGRESS)
list(APPEND show_progress_args SHOW_PROGRESS)
endif ()
file(DOWNLOAD "${url}" "${file}" STATUS status LOG log ${inactivity_timeout} ${absolute_timeout} ${show_progress_args})
file(DOWNLOAD "${url}" "${file}" STATUS status LOG log ${httpheader_args} ${inactivity_timeout} ${absolute_timeout} ${show_progress_args})
list(GET status 0 err)
list(GET status 1 msg)
if(err)