[Linux] 使用 wget keep-alive 時,無法通過 HTTP authentication 認證?

[Linux] 使用 wget keep-alive 時,無法通過 HTTP authentication 認證?

今天遇到一個奇怪的問題…

我想用 wget 抓一個 http server 上的檔案,

一開始沒有打帳號密碼,所以抓不下來…

但是後來用 –user 和 –password 給正確的帳號密碼,也還是失敗,

只會抓下一個很小的檔案:

testuser@localhost ~ $ wget --user=myuser --password=mypass http://172.22.1.1/Win7.ova
--2016-02-03 14:50:28--  http://172.22.1.1/Win7.ova
Connecting to 172.22.1.1:80... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Reusing existing connection to 172.22.1.1:80.
HTTP request sent, awaiting response... 200 No headers, assuming HTTP/0.9
Length: unspecified
Saving to: Win7.ova’
[ <=>                                                                                                                                                 ] 2,435       --.-K/s   in 0s
2016-02-03 14:50:28 (265 MB/s) - Win7.ova’ saved [2435]

 

用 Wireshark 抓了封包來看,一開始 wget 送出了 GET 請求,

http server 傳回了 401 Unauthorized,表示需要認證 (注意此時有用 Keep-Alive header):

GET /Win7.ova HTTP/1.1
User-Agent: Wget/1.14 (linux-gnu)
Accept: */*
Host: 172.22.1.1
Connection: Keep-Alive
HTTP/1.1 401 Unauthorized
Content-Type: text/html
WWW-Authenticate: Basic realm=""
Accept-Ranges: bytes
Server: HFS 2.3g
Set-Cookie: HFS_SID_=0.639220340875909; path=/; HttpOnly

 

於是 wget 接著送出了 Authorization header,

但 http server 此時還是回應 Unauthorized:

GET /Win7.ova HTTP/1.1
User-Agent: Wget/1.14 (linux-gnu)
Accept: */*
Host: 172.22.1.1
Connection: Keep-Alive
Authorization: Basic dXNieDp1c2J4
Cookie: HFS_SID_=0.639220340875909
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Unauthorized</h1>
Either your user name and password do not match, or you are not permitted to access this resource.
</div>
</body>
</html>

 

感覺上 wget 的運作應該沒有問題,但為什麼 web server 不接受這個認證的回應呢?

後來發現,如果加上 –no-http-keep-alive 選項,這個問題就消失了!

HTTP keep-alive 是要讓多個 HTTP 請求透過同一個 TCP 連線傳送,

當使用 –no-http-keep-alive 選項關閉時,每個 HTTP 請求都是一個新的 TCP 連線~

那為什麼這樣子就可以解決問題呢?

猜測是 web server 的實作問題,當第一次連線認證失敗後,

下一次連線如果還是走同一個 TCP 連線,web server 可能認為同一連線都是認證失敗的,

因此就直接拒絕囉~~

(本頁面已被瀏覽過 1,200 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料