<p>First the code that works. This is a basic wrapper that checks for a good cookie before returning the next function.</p>
<pre><code>func cookieCheck(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if cookie, err := r.Cookie(cookieName); err == nil {
var value []string
if err = s.Decode(cookieName, cookie.Value, &value); err == nil {
log.Printf("good cookie: User: %s; Group: %s", value[0], value[1])
fn(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
log.Printf("old or bad cookie")
}
} else {
http.Redirect(w, r, "/login", http.StatusFound)
log.Printf("no cookie")
}
}
}
</code></pre>
<p>I would like to add a check to see if the program is running in a test mode and if so, skip the cookie check and pass along the function. I've changed it to the following. It compiles, but gives a runtime error when TESTMODE==true. (If TESTMODE==false, then there is no error</p>
<pre><code>func cookieCheck(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if TESTMODE {
log.Printf("skipping cookie check while in TESTMODE")
fn(w, r)
} else {
if cookie, err := r.Cookie(cookieName); err == nil {
var value []string
if err = s.Decode(cookieName, cookie.Value, &value); err == nil {
log.Printf("good cookie: User: %s; Group: %s", value[0], value[1])
fn(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
log.Printf("old or bad cookie")
}
} else {
http.Redirect(w, r, "/login", http.StatusFound)
log.Printf("no cookie")
}
}
}
}
</code></pre>
<hr/>**评论:**<br/><br/>Dummies102: <pre><p>what happens if you remove this line <code>log.Printf("good cookie: User: %s; Group: %s", value[0], value[1])</code></p></pre>taoofshawn: <pre><p>No change, same runtime error</p></pre>kocekoga: <pre><p>This is probably not the part of the code that is causing the problem. Actual problem is somewhere inside handler because "/usr/local/go/src/net/http/server.go:1287 +0xb5" will execute only if there was panic wile handling request. Check code that is depending on wrapper functionality. Possibly something to do with writing to response writer.</p></pre>taoofshawn: <pre><p>I think this is it. I haven't located the exact problem yet, but there is some very suspect error handling in the handler where it is trying to read cookie data that wouldn't exist in this case.
Thanks!</p></pre>taoofshawn: <pre><p>The runtime error is "index out of range"</p>
<pre><code>2015/09/14 12:30:09 TESTMODE: No cookie check cookieCheck()
2015/09/14 12:30:09 http: panic serving 1.1.1.1:54429: runtime error: index out of range
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc8201b8d10, 0x7fa86f393e98, 0xc820091080)
/usr/local/go/src/net/http/server.go:1287 +0xb5
</code></pre></pre>nathj07: <pre><p>Are you sure it is going into the TESTMODE == true branch? I ask because your output doesn't show that log line and your snippet doesn't explain where TESTMODE is set.</p></pre>taoofshawn: <pre><p>Yes, I'm sure. The confusion is because I used slightly different log outputs when trying this multiple times. I saved the runtime error when the log entry was "TESTMODE: No cookie check cookieCheck()". The later iteration when I copied the code for this posting had "skipping cookie check while in TESTMODE".<br/>
So basically "skipping cookie check while in TESTMODE" == "TESTMODE: No cookie check cookieCheck()"</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传