Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active February 1, 2024 23:56
Show Gist options
  • Save scivision/1951564ecd0fab696a0e0af01d7dba84 to your computer and use it in GitHub Desktop.
Save scivision/1951564ecd0fab696a0e0af01d7dba84 to your computer and use it in GitHub Desktop.
Get and set CMake HTTP user agent
cmake_minimum_required(VERSION 3.19)
option(CMAKE_TLS_VERIFY "Check cert" ON)
function(user_agent url fn)
file(DOWNLOAD ${url} ${fn}
STATUS stat
)
if(NOT stat EQUAL 0)
message(FATAL_ERROR "${url}: ${stat}")
endif()
endfunction()
function(fake_user_agent url fn)
# Date isn't needed, it's just an example of setting multiple HTTPHEADER
string(TIMESTAMP now "%a, %d %b %Y %H:%M:%S +0000" UTC)
file(DOWNLOAD ${url} ${fn}
HTTPHEADER "Date: ${now}"
HTTPHEADER "Mozilla/5.0"
STATUS stat
)
if(NOT stat EQUAL 0)
message(FATAL_ERROR "${url}: ${stat}")
endif()
endfunction()
function(parse_json json_file)
file(READ ${json_file} meta)
string(JSON ua GET ${meta} 0 ua rawUa)
message(STATUS "CMake ${CMAKE_VERSION} user agent: ${ua}")
message(VERBOSE "${meta}")
endfunction()
set(url https://www.whatsmyua.info/api/v1/ua)
set(fn ua.json)
user_agent(${url} ${fn})
parse_json(${fn})
fake_user_agent(${url} ${fn})
parse_json(${fn})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment