Merge topic 'ExternalData-add-httpheader'

5106f34eed ExternalData: Add option to specify HTTP headers

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !10275
diff --git a/Help/release/dev/ExternalData-add-httpheader.rst b/Help/release/dev/ExternalData-add-httpheader.rst
new file mode 100644
index 0000000..bd950d8
--- /dev/null
+++ b/Help/release/dev/ExternalData-add-httpheader.rst
@@ -0,0 +1,5 @@
+ExternalData-add-httpheader
+---------------------------
+
+* The :module:`ExternalData` module gained a
+  :variable:`ExternalData_HTTPHEADERS` variable to specify HTTP headers.
diff --git a/Modules/ExternalData.cmake b/Modules/ExternalData.cmake
index 8c75355..70b547b 100644
--- a/Modules/ExternalData.cmake
+++ b/Modules/ExternalData.cmake
@@ -133,6 +133,14 @@
   ``<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 @@
     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 @@
   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 @@
     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)