重新認(rèn)識HTTP3xx重定向機(jī)制

問題引入

前段時(shí)間做數(shù)據(jù)收集時(shí)需要下載網(wǎng)宿cdn的日志進(jìn)行分析。而網(wǎng)宿對日志下載的接口搞得很復(fù)雜,又沒有提供相應(yīng)的sdk,只是提供了一個(gè)shell腳本,雖然在ubuntu上使用很方便,但是腳本里面的各種重定向分析非常復(fù)雜。故此想對重定向在深入了解一點(diǎn)。

查詢網(wǎng)宿日志列表的腳本

#!/bin/sh

TMP_FILE="/tmp/wslog_query_client.log"
#Usage
Usage() {
    echo "wslog_query_client.sh [query_url] [user] [passwd] [start_time] [end_time] [channels]"
    return 0
}
#check input parameters
if [ $# -eq 1 ]; then
    if [ "$1" = "-h" ]; then
        Usage
        exit 0
    else
        Usage
        exit -1
    fi
elif [ $# -ne 6 ]; then
    Usage
    exit -1
fi
#params set
url=$1
user=$2
passwd=`echo $3 | sed 's/&/%26/g' `
start_time=$4
end_time=$5
channels=$6
#access logQuery access API
curl -s -D $TMP_FILE $1
cat $TMP_FILE | grep "HTTP/" | grep "302" > /dev/null
if [ $? -ne 0 ]; then
    exit -2
fi
#redirect to verify url with user and passwd
TMP_URL=`cat $TMP_FILE | grep "Location: "|sed 's/\r//' | awk '{print $2}' | sed 's/http:/https:/'`
TMP_URL="${TMP_URL}?u=$user&p=$passwd&channel=$channels"
curl -s -k -D $TMP_FILE $TMP_URL
cat $TMP_FILE | grep "HTTP/" | grep "302" > /dev/null
if [ $? -ne 0 ]; then
    exit -3
fi
#redirect to query url with start_time, end_time and channels
TMP_URL=`cat $TMP_FILE | grep "Location: "|sed 's/\r//' | awk '{print $2}'`
TMP_URL="${TMP_URL}&start_time=$start_time&end_time=$end_time&channels=$channels"
curl -s -D $TMP_FILE $TMP_URL
#check query result
cat $TMP_FILE | grep "HTTP/" | grep "200" > /dev/null
if [ $? -ne 0 ]; then
    if 
        cat $TMP_FILE | grep "HTTP/" | grep "404" > /dev/null
    then
        exit -404
    else
        exit -4
    fi
fi
exit 0

腳本調(diào)用命令和結(jié)果(用戶名,密碼,domain,wskey均已處理,調(diào)用結(jié)果只有參考作用)

root@sz3:/tmp# sh /root/wslog_query_client.sh "http://dx.wslog.chinanetcenter.com/logQuery/access" user1 passwd1 2017-08-30-0000 2017-08-30-2359 "rtmp-wsz.enterprise.com"
{"logs": [{"domain": "rtmp-wsz.enterprise.com", "files": [{"size": 4320, "end_time": "2017-08-30-1159", "start_time": "2017-08-30-0000", "url": "http://dx.wslog.chinanetcenter.com/log/qukan/rtmp-wsz.enterprise.com/2017-08-30-0000-1130_rtmp-wsz.enterprise.com.cn.log.gz?wskey=e4030060bdfe9d5600a77726c5900d07aa3adae00e8b2"}, {"size": 8006, "end_time": "2017-08-30-2359", "start_time": "2017-08-30-1200", "url": "http://dx.wslog.chinanetcenter.com/log/qukan/rtmp-wsz.enterprise.com/2017-08-30-1200-2330_rtmp-wsz.enterprise.com.cn.log.gz?wskey=3772006094880e8300a73cc2c59006bfeea33ae00d9da"}]}]}

腳本的調(diào)用過程是根據(jù)參數(shù)一步一步的進(jìn)行302重定向,重定向時(shí)會(huì)依賴于參數(shù),每次重定向依賴的參數(shù)都不相同,不僅僅是url跳轉(zhuǎn),如果直接使用以下http鏈接則無法跳轉(zhuǎn)到,因此需要按照shell腳本那樣一層一層解析。

http://dx.wslog.chinanetcenter.com/logQuery/access?user=user1&passwd=passwd1&channels=rtmp-wsz.enterprise.com&start_time=2017-08-30-0000&end_time=2017-08-30-2359

HTTP重定向的原理

客戶端發(fā)起http請求,如果服務(wù)端返回http重定向響應(yīng),那么客戶端會(huì)請求返回的新url,這就是重定向的過程,這個(gè)過程就是重定向。在客戶端和服務(wù)端之間自動(dòng)完成,用戶不可見。

不同類型的重定向映射可以劃分為三個(gè)類別:永久重定向、臨時(shí)重定向和特殊重定向。

如果你想把自己的網(wǎng)站永久更改為一個(gè)新的域名,則應(yīng)該使用301永久重定向,搜索引擎機(jī)器人會(huì)在遇到該狀態(tài)碼時(shí)觸發(fā)更新操作,在其索引庫中修改與該資源相關(guān)的 URL 。

HTTP重定向的使用

主要以Python和shell兩種語言來介紹http重定向的使用。

Python

Python常用的http庫urllib,urllib2,requests都支持http重定向。以requests庫為例做介紹。

import requests


def get_final_link(url):
    try:
        r = requests.get(url=url, allow_redirects=False)
        if r.status_code == 302 or r.status_code == 301:
            return get_final_link(r.headers['Location'])
        else:
            return r.url
    except:
        return url


def get_final_link1(url):
    r = requests.get(url=url, allow_redirects=True)
    for rsp in r.history:
        print rsp.url
    return r.url

print get_final_link(url='http://runreport.dnion.com/DCC/logDownLoad.do?user=user1&password=password1&domain=rtmpdist-d.quklive.com&date=20171026&hour=10')
print get_final_link(url='https://github.com')
print get_final_link(url='http://github.com')
print get_final_link1(url='http://github.com')  # 會(huì)發(fā)生301重定向

如果確定重定向的過程中全部都是http(s)請求,則allow_redirects參數(shù)設(shè)置成True即可得到最終的http鏈接。如果不是則需要自己進(jìn)行遞歸解析。

如果是要簡單的下載文件,則可以使用urllib.urlretrieve輕松勝任,即使最終鏈接是ftp。

Shell

使用curl命令模擬

-L參數(shù),當(dāng)頁面有跳轉(zhuǎn)的時(shí)候,輸出跳轉(zhuǎn)到的頁面

-I參數(shù) header信息 當(dāng)有跳轉(zhuǎn)時(shí),可以通過 curl -L -I URL|grep Location 來確定跳轉(zhuǎn)到的新url地址

root@sz3:~# curl -L -I "http://runreport.dnion.com/DCC/logDownLoad.do?user=user1&password=password1&domain=rtmpdist-d.quklive.com&date=20171026&hour=10"
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=0F11668F6EBF4DC16B43E322CCF16C85; Path=/DCC
Location: http://runreport.dnion.com/logDownLoad.do?user=qukan&password=0cddcbf6d292fab5de0aas931bf19c&domain=rtmpdist-d.quklive.com&date=20171026&hour=10
Content-Type: text/html;charset=GBK
Content-Length: 0
Date: Mon, 06 Nov 2017 09:46:44 GMT

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: ftp://ABA606843D412DAE34F28CDB23F7A31E:0687B16F2F5D0A2637FACDB23FAC982179411FA7466F10B2E7D0F4AA2D7F6AD42536F122549D0A6E40337E896@125.39.237.48:55621/rtmpdist-d.quklive.com_20171026_10_11.gz
Content-Type: text/html;charset=GBK
Content-Length: 0
Date: Mon, 06 Nov 2017 09:46:44 GMT

Last-Modified: Thu, 26 Oct 2017 02:30:13 GMT
Content-Length: 1932
Accept-ranges: bytes

最后跳轉(zhuǎn)到需要的ftp鏈接。

HTTP重定向抓包驗(yàn)證

使用wireshark抓包結(jié)果如下:

第一次跳轉(zhuǎn)過程如下圖

第二次跳轉(zhuǎn)過程如下圖

所以通過抓包可以清晰的看到302跳轉(zhuǎn)的過程


參考:

  1. csdn-curl命令的常見參數(shù)使用
  2. mozilla-HTTP 的重定向

念念不忘,必有回響,小伙伴們幫我點(diǎn)個(gè)贊吧,非常感謝。

我是職場亮哥,YY高級軟件工程師、四年工作經(jīng)驗(yàn),拒絕咸魚爭當(dāng)龍頭的斜杠程序員。

聽我說,進(jìn)步多,程序人生一把梭

如果有幸能幫到你,請幫我點(diǎn)個(gè)【贊】,給個(gè)關(guān)注,如果能順帶評論給個(gè)鼓勵(lì),將不勝感激。

職場亮哥文章列表:更多文章

本人所有文章、回答都與版權(quán)保護(hù)平臺(tái)有合作,著作權(quán)歸職場亮哥所有,未經(jīng)授權(quán),轉(zhuǎn)載必究!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容