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

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

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

问题

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

 func GetLoginLinks(w *http.ResponseWriter, r *http.Request) (string, error) {
 	c := appengine.NewContext(r)
 	u := user.Current(c)
 	if u == nil {
 		url, err := user.LoginURL(c, r.URL.String())
 		if err != nil {
 			http.Error(*w, err.Error(), http.StatusInternalServerError)
 			return "", err
 		}
 
 		c.Debugf("Return url: " + r.URL.String())
 		c.Debugf("login url: " + url)
 		c.Debugf("url type: %T", url)
 		v := LoginItem(url, "Login")
 		return v, nil
 	}
 }

它给出以下输出:

2013/06/17 21:48:28 DEBUG: Return url: /
2013/06/17 21:48:28 DEBUG: login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
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:

 func GetLoginLinks(w *http.ResponseWriter, r *http.Request) (string, error) {
 	c := appengine.NewContext(r)
 	u := user.Current(c)
 	if u == nil {
 		url, err := user.LoginURL(c, r.URL.String())
 		if err != nil {
 			http.Error(*w, err.Error(), http.StatusInternalServerError)
 			return "", err
 		}
 
 		c.Debugf("Return url: " + r.URL.String())
 		c.Debugf("login url: " + url)
 		c.Debugf("url type: %T", url)
 		v := LoginItem(url, "Login")
 		return v, nil
 	}
 }

It gives the following output:

2013/06/17 21:48:28 DEBUG: Return url: /
2013/06/17 21:48:28 DEBUG: login url: /_ah/login?continue=http%A(MISSING)//localhost%A(MISSING)8080/
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”动词的格式参数。为了安全起见,永远不要使用任意字符串作为格式字符串。例如,

package main

import "fmt"

func main() {
    url := "/_ah/login?continue=http%3A//localhost%3A8080/"
    fmt.Printf("login url: " + url)
    fmt.Println()
    fmt.Printf("login url: %s", url)
    fmt.Println()
}

输出:

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

修改为:

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,

package main

import "fmt"

func main() {
	url := "/_ah/login?continue=http%3A//localhost%3A8080/"
	fmt.Printf("login url: " + url)
	fmt.Println()
	fmt.Printf("login url: %s", url)
	fmt.Println()
}

Output:

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

Write:

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:

确定