Rethinking http3xx redirection mechanism


Problem introduction

When collecting data some time ago, we need to download the CDN log for analysis. The interface of log download is very complex, and there is no corresponding SDK. It only provides a shell script. Although it is convenient to use on Ubuntu, the redirection analysis in the script is very complex. So I want to have a deeper understanding of redirection.

Script for querying the list of network log


Usage() {
	echo " [query_url] [user] [passwd] [start_time] [end_time] [channels]"
	return 0
#check input parameters
if [ $# -eq 1 ]; then
	if [ "$1" = "-h" ]; then
		exit 0
		exit -1
elif [ $# -ne 6 ]; then
	exit -1
#params set
passwd=`echo $3 | sed 's/&/%26/g' `
#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
#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:/'`
curl -s -k -D $TMP_FILE $TMP_URL
cat $TMP_FILE | grep "HTTP/" | grep "302" > /dev/null
if [ $? -ne 0 ]; then
	exit -3
#redirect to query url with start_time, end_time and channels
TMP_URL=`cat $TMP_FILE | grep "Location: "|sed 's/\r//' | awk '{print $2}'`
curl -s -D $TMP_FILE $TMP_URL
#check query result
cat $TMP_FILE | grep "HTTP/" | grep "200" > /dev/null
if [ $? -ne 0 ]; then
		cat $TMP_FILE | grep "HTTP/" | grep "404" > /dev/null
		exit -404
		exit -4
exit 0

Script call command and result (user name, password, domain and wskey have been processed, call result is only for reference)

[email protected]:/tmp# sh /root/ "" user1 passwd1 2017-08-30-0000 2017-08-30-2359 ""
{"logs": [{"domain": "", "files": [{"size": 4320, "end_time": "2017-08-30-1159", "start_time": "2017-08-30-0000", "url": ""}, {"size": 8006, "end_time": "2017-08-30-2359", "start_time": "2017-08-30-1200", "url": ""}]}]}

The calling process of the script is 302 redirection step by step according to the parameters. The redirection depends on the parameters. The parameters that each redirection depends on are not the same, not just URL jump. If you directly use the following HTTP link, you can’t jump to it. Therefore, you need to parse layer by layer according to the shell script.

The principle of HTTP redirection

The client initiates an HTTP request. If the server returns an HTTP redirection response, the client will request a new URL. This is the process of redirection. This process is redirection. It is automatically completed between the client and the server, and is invisible to the user.

Different types of redirection mapping can be divided into three categories: permanent redirection, temporary redirection and special redirection.

If you want to permanently change your website to a new domain name, you should use 301 permanent redirection. When the search engine robot encounters the status code, it will trigger an update operation and modify the URL related to the resource in its index library.

The use of HTTP redirection

This paper mainly introduces the use of HTTP redirection in Python and shell.


The HTTP libraries urllib, urllib2 and requests commonly used in Python support HTTP redirection. Take the requests library as an example.

import requests

def get_final_link(url):
        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'])
            return r.url
        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='')
print get_final_link(url='')
print get_final_link(url='')
print get_ final_ link1(url=' ') will occur

If it is determined that all HTTP (s) requests are in the process of redirection, then allow_ Set the redirects parameter to true to get the final HTTP link. If not, you need to do recursive parsing yourself.

If you want to download files simply, you can use theurllib.urlretrieveEasily competent, even if the final link is FTP.


Simulate with curl command

-L parameter, when the page has a jump, the output jump to the page

-When there is a jump in the I parameter header information, the new URL address to jump to can be determined by curl – L – I URL | grep location

[email protected]:~# curl -L -I ""
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=0F11668F6EBF4DC16B43E322CCF16C85; Path=/DCC
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:0687B16F2F5D0A2637FACDB23F[email protected]
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

Finally, jump to the required FTP link.

HTTP redirection packet capture verification

The results of packet capture using Wireshark are as follows:

The first jump process is as follows

The second jump process is as follows

So you can clearly see the process of 302 jump through the packet capture

reference resources:

  1. Common parameter usage of CSDN curl command
  2. Mozilla HTTP redirection

Remember to praise me!

Carefully sorted out the video courses and e-books in all directions of the computer, including introduction, advanced and actual combat. According to the reasonable classification according to the directory, you can always find the learning materials you need. What are you waiting for? Pay attention to download it!!!


If you don’t forget, there will be an echo. Please give me a compliment. Thank you very much.

I am a bright brother in the workplace, YY Senior Software Engineer, with four years of working experience. I refuse to be a leading slash programmer.

Listen to me, more progress, a shuttle of procedural life

If you are lucky enough to help you, please give me a “like” to pay attention to it. If you can comment on it and give me encouragement, I will be very grateful.

List of articles of workplace bright brother:More articles


All my articles and answers have cooperation with the copyright protection platform. The copyright belongs to brother Liang in the workplace. Without authorization, reprint must be investigated!