GAE Go用户服务登录网址包含%A(MISSING)

huangapple go评论124阅读模式
英文:

GAE Go user service login url contains %A(MISSING)

问题

这个问题困扰了我一段时间!这是控制台中的一些代码:

  1. func GetLoginLinks(w *http.ResponseWriter, r *http.Request) (string, error) {
  2. c := appengine.NewContext(r)
  3. u := user.Current(c)
  4. if u == nil {
  5. url, err := user.LoginURL(c, r.URL.String())
  6. if err != nil {
  7. http.Error(*w, err.Error(), http.StatusInternalServerError)
  8. return "", err
  9. }
  10. c.Debugf("Return url: " + r.URL.String())
  11. c.Debugf("login url: " + url)
  12. c.Debugf("url type: %T", url)
  13. v := LoginItem(url, "Login")
  14. return v, nil
  15. }
  16. }

它给出以下输出:

  1. 2013/06/17 21:48:28 DEBUG: Return url: /
  2. 2013/06/17 21:48:28 DEBUG: login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
  3. 2013/06/17 21:48:28 DEBUG: url type: string

当上传到应用引擎本身时,也会失败。

我困惑的是函数的前4行直接来自开发者指南。

英文:

So this one's been puzzling me for a little while!

Here's some code in the console:

  1. func GetLoginLinks(w *http.ResponseWriter, r *http.Request) (string, error) {
  2. c := appengine.NewContext(r)
  3. u := user.Current(c)
  4. if u == nil {
  5. url, err := user.LoginURL(c, r.URL.String())
  6. if err != nil {
  7. http.Error(*w, err.Error(), http.StatusInternalServerError)
  8. return "", err
  9. }
  10. c.Debugf("Return url: " + r.URL.String())
  11. c.Debugf("login url: " + url)
  12. c.Debugf("url type: %T", url)
  13. v := LoginItem(url, "Login")
  14. return v, nil
  15. }
  16. }

It gives the following output:

  1. 2013/06/17 21:48:28 DEBUG: Return url: /
  2. 2013/06/17 21:48:28 DEBUG: login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
  3. 2013/06/17 21:48:28 DEBUG: url type: string

This also fails when uploaded to the app engine itself.

What I'm struggling with is the first 4 lines of the function comes directly from the developer guide.

答案1

得分: 4

你有一个无效的格式字符串。你在格式字符串中有一个转义的URL查询字符串:“:”被转义为“%3A”。缺少“%3A”动词的格式参数。为了安全起见,永远不要使用任意字符串作为格式字符串。例如,

  1. package main
  2. import "fmt"
  3. func main() {
  4. url := "/_ah/login?continue=http%3A//localhost%3A8080/"
  5. fmt.Printf("login url: " + url)
  6. fmt.Println()
  7. fmt.Printf("login url: %s", url)
  8. fmt.Println()
  9. }

输出:

  1. login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
  2. login url: /_ah/login?continue=http%3A//localhost%3A8080/

修改为:

  1. c.Debugf("login url: %s", url)
英文:

> package fmt
>
> Format errors:
>
> If an invalid argument is given for a verb, such as providing a string
> to %d, the generated string will contain a description of the problem,
> as in these examples:
>
> Wrong type or unknown verb: %!verb(type=value)
> Printf("%d", hi): %!d(string=hi)
> Too many arguments: %!(EXTRA type=value)
> Printf("hi", "guys"): hi%!(EXTRA string=guys)
> Too few arguments: %!verb(MISSING)
> Printf("hi%d"): hi %!d(MISSING)
> Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
> Printf("%*s", 4.5, "hi"): %!(BADWIDTH)hi
> Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
>
> All errors begin with the string "%!" followed sometimes by a single
> character (the verb) and end with a parenthesized description.
>
> If an Error or String method triggers a panic when called by a
> print routine, the fmt package reformats the error message from the
> panic, decorating it with an indication that it came through the fmt
> package. For example, if a String method calls panic("bad"), the
> resulting formatted message will look like
>
> %s(PANIC=bad)
>
> The %s just shows the print verb in use when the failure occurred.

You have an invalid format string. You have an escaped URL query string in a format string: ":" is escaped to "%3A". The format arguments for the "%3A" verbs are missing. For safety, never use an arbitrary string as a format string. For example,

  1. package main
  2. import "fmt"
  3. func main() {
  4. url := "/_ah/login?continue=http%3A//localhost%3A8080/"
  5. fmt.Printf("login url: " + url)
  6. fmt.Println()
  7. fmt.Printf("login url: %s", url)
  8. fmt.Println()
  9. }

Output:

  1. login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
  2. login url: /_ah/login?continue=http%3A//localhost%3A8080/

Write:

  1. c.Debugf("login url: %s", url)

huangapple
  • 本文由 发表于 2013年6月18日 06:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/17157836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定