imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

digest: algorithm is not supported #359

Closed B190102B closed 4 weeks ago

B190102B commented 4 weeks ago

代码:

    _, err := req.DevMode().R().SetDigestAuth("username", "password").Put(url)
    if err != nil {
        panic(err)
    }

Output:

HTTP/1.1 401 Unauthorized
Date: Thu, 30 May 2024 17:28:44 GMT
Server: web
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: default-src 'self' 'unsafe-inline' 'unsafe-eval'
X-XSS-Protection: 1
Strict-Transport-Security: max-age=31536000;includeSubDomains
X-Content-Type-Options: nosniff
Content-Length: 142
Content-Type: text/html
Connection: close
WWW-Authenticate: Digest realm="DS-TCG405-E", domain="::", qop="auth", nonce="4d6b493452444d335130553659324534597a41304f47493d", opaque="", algorithm="MD5", stale="FALSE"
X-Appweb-Seq: 193970

panic: digest: algorithm is not supported

req 无法识别 algorithm="MD5", 想请问为什么 c.algorithm 不使用 strings.Trim

digest.go:

func parseChallenge(input string) (*challenge, error) {
    const ws = " \n\r\t"
    const qs = `"`
    s := strings.Trim(input, ws)
    if !strings.HasPrefix(s, "Digest ") {
        return nil, errDigestBadChallenge
    }
    s = strings.Trim(s[7:], ws)
    sl := strings.Split(s, ",")
    c := &challenge{}
    var r []string
    for i := range sl {
        r = strings.SplitN(strings.TrimSpace(sl[i]), "=", 2)
        if len(r) != 2 {
            return nil, errDigestBadChallenge
        }
        switch r[0] {
        case "realm":
            c.realm = strings.Trim(r[1], qs)
        case "domain":
            c.domain = strings.Trim(r[1], qs)
        case "nonce":
            c.nonce = strings.Trim(r[1], qs)
        case "opaque":
            c.opaque = strings.Trim(r[1], qs)
        case "stale":
            c.stale = r[1]
        case "algorithm":
            // Here
            c.algorithm = r[1]
        case "qop":
            c.qop = strings.Trim(r[1], qs)
        case "charset":
            if strings.ToUpper(strings.Trim(r[1], qs)) != "UTF-8" {
                return nil, errDigestCharset
            }
        case "userhash":
            c.userhash = strings.Trim(r[1], qs)
        default:
            return nil, errDigestBadChallenge
        }
    }
    return c, nil
}
imroc commented 4 weeks ago

应该是实现上的小失误,已修复,可升级到最新试试

B190102B commented 4 weeks ago

Problem solved, thank for your response