dongsheng4679 2017-10-29 17:47
浏览 68

在Cookie名称上添加对分隔符的支持

I am working with an external API/web that has a cookie with an @ on its name. Go-lang does not support this and ignores the Set-Cookie.

How can I implement this without modifying the Go source code directly?

As implemented on Go:

http://tools.ietf.org/html/rfc6265#section-4.1.1 specifies that:
set-cookie-header = "Set-Cookie:" SP set-cookie-string
set-cookie-string = cookie-pair *( ";" SP cookie-av )
cookie-pair       = cookie-name "=" cookie-value
cookie-name       = token

Then in http://tools.ietf.org/html/rfc2616#section-2.2, token is
specified as:
token          = 1*<any CHAR except CTLs or separators>
separators     = "(" | ")" | "<" | ">" | "@"
                     | "," | ";" | ":" | "\" | <">
                     | "/" | "[" | "]" | "?" | "="
                     | "{" | "}" | SP  | HT
  • 写回答

1条回答 默认 最新

  • dongyongju9560 2017-10-29 19:43
    关注

    Cookies are just headers with key value pairs, they're not complex. So copy the code you need out from here, and use it to parse the set-cookie header instead in your own way, instead of relying on the stdlib.

    https://golang.org/src/net/http/cookie.go

    You could probably adjust this function to allow @:

    func isNotToken(r rune) bool {
        // Allow @, unlike the go stdlib
        // document reasons here
        if r == '@' {
            return false
        }
    
        return !httplex.IsTokenRune(r)
    }
    

    Alternatively, get hold of the header and just write some simpler parsing code in this particular instance, as all you need to read is something like this:

    Set-Cookie: example@example="abc";Path=/

    You could probably get away using strings.Split or a regexp if parsing a simple predictable cookie value.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序