最近的项目从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
往期文章回顾: