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

时间:2017-09-30 21:49:53

标签: bash redirect curl

我意识到Curl {redirect_url}并不总是显示相同的重定向网址。例如,如果网址标题为Location: https:/\example.com,则会重定向到https:/\example.com,但curl {redirect_url}会显示redirect_url: https://host-domain.com/https:/\example.com,并且不会显示响应实际位置标头。 (我希望看到真正的location:结果。)

这是我正在使用的BASH:

#!/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}"

如何printf "Redirect-url:原始请求location: header而不必使用redirect_url

3 个答案:

答案 0 :(得分:3)

要阅读服务器返回的确切Location标头字段值,您可以使用-i/--include选项与grep结合使用。

例如:

$ curl 'http://httpbin.org/redirect-to?url=http:/\example.com' -si | grep -oP 'Location: \K.*'
http:/\example.com

或者,如果您想阅读所有标题内容 --write-out变量行(根据您的脚本) :

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")

完全集成在您的脚本中,如下所示:

#!/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"

答案 1 :(得分:0)

https:/\example.com不是合法网址(*)。事实上,这在浏览器中令人憎恶(我曾经反对)和卷曲不起作用。 %{redirect_url}显示网址curl将重定向到...

URL应该用于转发斜杠,因此上面应该看起来像http://example.com

(*)=我拒绝接受WHATWG&#34;定义&#34;。

答案 2 :(得分:0)

根据@randomir的答案,由于我只需要原始重定向网址,我在我的批处理中使用此命令

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