最近的項目從golang0.9升級到golang1.13後,項目中出現了很特殊的現象,在APP裏,用戶登陸後訪問頁面正常,用戶不登陸,報錯。php
Charles抓包發現,登陸的狀況下,服務返回的是protobuf的數據,未登陸狀況下返回的是json結構。服務是根據cookie中傳入的數據來返回對應的數據類型。初步判定未登陸狀況下沒法獲取到cookie程序員
檢查登陸和未登陸狀況下cookie的區別。golang
serviceToken是登陸後的驗證信息,用戶若是未登陸,數據不存在,可是分號卻存在。初步懷疑是golang版本引發json
在代碼不變的狀況下,使用不一樣golang版本生成服務,使用腳本進行測試緩存
<?php
$url = "http://10.220.130.8:8081/in/app/sync";
//$url = "http://in-go.buy.mi.com/in/app/sync";
$cookie = "; xmuuid=XMGUEST-FCF117BF-4D1B-272F-829D-25E19826D4F8;type=protobuf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output,11) ;
複製代碼
肯定是golang版本問題cookie
查看源碼,在net/http/cookie.go中,能夠看到session
golang1.12app
// readCookies parses all "Cookie" values from the header h and
// returns the successfully parsed Cookies.
//
// if filter isn't empty, only cookies of that name are returned
func readCookies(h Header, filter string) []*Cookie {
lines, ok := h["Cookie"]
if !ok {
return []*Cookie{}
}
cookies := []*Cookie{}
for _, line := range lines {
parts := strings.Split(strings.TrimSpace(line), ";")
if len(parts) == 1 && parts[0] == "" {
continue
}
// Per-line attributes
for i := 0; i < len(parts); i++ {
parts[i] = strings.TrimSpace(parts[i])
if len(parts[i]) == 0 {
continue
}
name, val := parts[i], ""
if j := strings.Index(name, "="); j >= 0 {
name, val = name[:j], name[j+1:]
}
if !isCookieNameValid(name) {
continue
}
if filter != "" && filter != name {
continue
}
val, ok := parseCookieValue(val, true)
if !ok {
continue
}
cookies = append(cookies, &Cookie{Name: name, Value: val})
}
}
return cookies
}
複製代碼
golang1.13框架
// readCookies parses all "Cookie" values from the header h and
// returns the successfully parsed Cookies.
//
// if filter isn't empty, only cookies of that name are returned
func readCookies(h Header, filter string) []*Cookie {
lines := h["Cookie"]
if len(lines) == 0 {
return []*Cookie{}
}
cookies := make([]*Cookie, 0, len(lines)+strings.Count(lines[0], ";"))
for _, line := range lines {
line = strings.TrimSpace(line)
var part string
for len(line) > 0 { // continue since we have rest
if splitIndex := strings.Index(line, ";"); splitIndex > 0 {
part, line = line[:splitIndex], line[splitIndex+1:]
} else {
part, line = line, ""
}
part = strings.TrimSpace(part)
if len(part) == 0 {
continue
}
name, val := part, ""
if j := strings.Index(part, "="); j >= 0 {
name, val = name[:j], name[j+1:]
}
if !isCookieNameValid(name) {
continue
}
if filter != "" && filter != name {
continue
}
val, ok := parseCookieValue(val, true)
if !ok {
continue
}
cookies = append(cookies, &Cookie{Name: name, Value: val})
}
}
return cookies
}
複製代碼
你們若是喜歡個人文章,能夠關注個人公衆號(程序員麻辣燙)curl
往期文章回顧: