Ask questionsnet/http: readCookies unable to parse out cookies that are not well written
<!-- Please answer these questions before submitting your issue. Thanks! For questions please use one of our forums: https://github.com/golang/go/wiki/Questions -->
go version)?<pre> $ go version go version go1.13.10 darwin/amd64 </pre>
yes
go env)?<details><summary><code>go env</code> Output</summary><br><pre> $ go env GO111MODULE="auto" GOARCH="amd64" GOBIN="" GOCACHE="/Users/pangzhiqiang/Library/Caches/go-build" GOENV="/Users/pangzhiqiang/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GONOPROXY="micode.be.xiaomi.com" GONOSUMDB="micode.be.xiaomi.com" GOOS="darwin" GOPATH="/Users/pangzhiqiang/data/code/golang/myproject" GOPRIVATE="micode.be.xiaomi.com" GOROOT="/usr/local/Cellar/[email protected]/1.13.10_1/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/[email protected]/1.13.10_1/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/pangzhiqiang/data/code/golang/myproject/asap/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0h/652yzkf17qd7n446g954_1j40000gn/T/go-build435664031=/tmp/go-build -gno-record-gcc-switches -fno-common" </pre></details>
<!-- If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best. -->
package main
import (
"net/http"
_ "asap/docs"
"github.com/gin-gonic/gin"
)
func setupRouter() *gin.Engine {
r := gin.Default()
// Ping test
r.GET("/ping", ping)
return r
}
// @Summary 接口探活
// @Produce json
// @Param lang query string false "en"
// @Success 200 {string} string "ok"
// @Router /ping [get]
func ping(c *gin.Context) {
cookies := c.Request.Cookies()
cookieInfo := ""
for _, cookie := range cookies{
cookieInfo += cookie.Name + ":" + cookie.Value + "\n"
}
c.String(http.StatusOK, cookieInfo )
}
func main() {
r := setupRouter()
// Listen and Server in 0.0.0.0:8080
r.Run(":9090")
}
2.client:
<?php
$url = "http://127.0.0.1:9090/ping";
$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) ;
string(66) "xmuuid:XMGUEST-FCF117BF-4D1B-272F-829D-25E19826D4F8 type:protobuf "
string(0) ""
readCookies can't parse this formate
if splitIndex := strings.Index(line, ";"); splitIndex > 0 {
part, line = line[:splitIndex], line[splitIndex+1:]
} else {
part, line = line, ""
}
Related questions