Bash 如何使用Curl检索真正的重定向位置头?不使用{redirect_url}

Bash 如何使用Curl检索真正的重定向位置头?不使用{redirect_url},bash,redirect,curl,Bash,Redirect,Curl,我意识到Curl{redirect_url}并不总是显示相同的重定向url。例如,如果URL头是Location:https://\example.com,这将重定向到https://\example.com,但是curl{redirect\u URL}显示重定向\u URL:https://host-domain.com/https:/\example.com,它不会显示响应真实位置标题。(我喜欢看到真实的位置:结果。) 这是我正在参与的聚会: #!/bin/bash # Usage: url

我意识到Curl{redirect_url}并不总是显示相同的重定向url。例如,如果URL头是
Location:https://\example.com
,这将重定向到
https://\example.com
,但是curl{redirect\u URL}显示
重定向\u URL:https://host-domain.com/https:/\example.com
,它不会显示响应真实位置标题。(我喜欢看到真实的
位置:
结果。)

这是我正在参与的聚会:

#!/bin/bash
# Usage: urls-checker.sh domains.txt
FILE="$1"
while read -r LINE; do
     # read the response to a variable
     response=$(curl -H 'Cache-Control: no-cache' -s -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$LINE")
     # get the title
     title=$(sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'<<<"$response")
     # read the write-out from the last line
     read -r http_code size_header redirect_url < <(tail -n 1 <<<"$response")
     printf "***Url: %s\n\n" "$LINE"
     printf "Status: %s\n\n" "$http_code"
     printf "Size: %s\n\n" "$size_header"
     printf "Redirect-url: %s\n\n" "$redirect_url"
     printf "Title: %s\n\n" "$title"
     # -c 20 only shows the 20 first chars from response
     printf "Body: %s\n\n" "$(head -c 100 <<<"$response")"
done < "${FILE}"
#/bin/bash
#用法:url-checker.sh domains.txt
FILE=“$1”
而read-r行;做
#读取对变量的响应
响应=$(curl-H'Cache Control:no Cache'-s-k--最长时间2--写出“{http\u code}%{size\u header}%{redirect\u url}'”$LINE)
#得名

title=$(sed-n's/*\(.*\)./\1/ip;T;q'
https:/\example.com
不是合法的URL(*)。事实上,这在浏览器中是一种令人厌恶的(我一直反对的)方式,而curl则不是。
%{redirect\u URL}
准确地显示了URL curl将重定向到

URL应该用于向前斜杠,因此上面的内容应该类似于
http://example.com


(*)=我拒绝接受WHATWG“定义”。

要读取服务器返回的确切
位置
标题字段值,可以将该选项与
grep
结合使用

例如:

$ curl 'http://httpbin.org/redirect-to?url=http:/\example.com' -si | grep -oP 'Location: \K.*'
http:/\example.com
或者,如果您想读取所有标题内容
——写出
变量
行(根据脚本):

response=$(curl-H'Cache Control:no Cache'-s-i-k--最长时间2--写出“{http\u code}%{size\u header}%{redirect\u url}'”$url)
#把反应分成几部分

headers=$(sed-n'1,/^\r$/p'根据@randomir answer,因为我只需要原始重定向URL,所以我在批处理中使用此命令

 curl  -w "%{redirect_url}" -o /dev/null -s "https://stackoverflow.com/q/46507336/3019002"

我同意URL
https://\example.com
不是有效的URL,但这只是一个示例。我需要实际位置头进行调试。我只想得到实际结果。Curl请求确实显示
location:https://\example.com
,我想在我的结果上看到这是
重定向URL:https://\example.com
,我明白这一点。我不想被重定向。你可以用这个
curl-v-w“redirect\u url:%{redirect\u url}\n”-o/dev/null-s测试https://app-1495017961.000webhostapp.com/redirect.php?url=https:/\example.com“
由于重定向使用的是断开的URL,因此无法使用比浏览器更严格的URL解析器。很酷!是的,这很完美。我如何将其集成到我的代码中以显示
重定向URL:http://\example.com
?如果您能帮助我,请感谢是的,它看起来不错,但我添加了它吗特别是代码?看起来不错,只要确保使用新变量
location
,而不是(或除了)
redirect\u url
。另外,对于body,您可以使用
content
。您能帮我更新代码吗?如果有很大帮助的话。太棒了。非常感谢@randomir!
#!/bin/bash
# Usage: urls-checker.sh domains.txt
file="$1"
while read -r url; do
    # read the response to a variable
    response=$(curl -H 'Cache-Control: no-cache' -s -i -k --max-time 2 --write-out '%{http_code} %{size_header} %{redirect_url} ' "$url")

    # break the response in parts
    headers=$(sed -n '1,/^\r$/p' <<<"$response")
    content=$(sed -e '1,/^\r$/d' -e '$d' <<<"$response")
    read -r http_code size_header redirect_url < <(tail -n1 <<<"$response")

    # get the real Location
    location=$(grep -oP 'Location: \K.*' <<<"$headers")

    # get the title
    title=$(sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'<<<"$content")

    printf "***Url: %s\n\n" "$url"
    printf "Status: %s\n\n" "$http_code"
    printf "Size: %s\n\n" "$size_header"
    printf "Redirect-url: %s\n\n" "$location"
    printf "Title: %s\n\n" "$title"
    printf "Body: %s\n\n" "$(head -c 100 <<<"$content")"
done < "$file"
 curl  -w "%{redirect_url}" -o /dev/null -s "https://stackoverflow.com/q/46507336/3019002"