Please point out my mistake in a basic web wrapper

polaris · · 595 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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, &amp;value); err == nil { log.Printf(&#34;good cookie: User: %s; Group: %s&#34;, value[0], value[1]) fn(w, r) } else { http.Redirect(w, r, &#34;/login&#34;, http.StatusFound) log.Printf(&#34;old or bad cookie&#34;) } } else { http.Redirect(w, r, &#34;/login&#34;, http.StatusFound) log.Printf(&#34;no cookie&#34;) } } } </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&#39;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(&#34;skipping cookie check while in TESTMODE&#34;) fn(w, r) } else { if cookie, err := r.Cookie(cookieName); err == nil { var value []string if err = s.Decode(cookieName, cookie.Value, &amp;value); err == nil { log.Printf(&#34;good cookie: User: %s; Group: %s&#34;, value[0], value[1]) fn(w, r) } else { http.Redirect(w, r, &#34;/login&#34;, http.StatusFound) log.Printf(&#34;old or bad cookie&#34;) } } else { http.Redirect(w, r, &#34;/login&#34;, http.StatusFound) log.Printf(&#34;no cookie&#34;) } } } } </code></pre> <hr/>**评论:**<br/><br/>Dummies102: <pre><p>what happens if you remove this line <code>log.Printf(&#34;good cookie: User: %s; Group: %s&#34;, 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 &#34;/usr/local/go/src/net/http/server.go:1287 +0xb5&#34; 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&#39;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&#39;t exist in this case. Thanks!</p></pre>taoofshawn: <pre><p>The runtime error is &#34;index out of range&#34;</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&#39;t show that log line and your snippet doesn&#39;t explain where TESTMODE is set.</p></pre>taoofshawn: <pre><p>Yes, I&#39;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 &#34;TESTMODE: No cookie check cookieCheck()&#34;. The later iteration when I copied the code for this posting had &#34;skipping cookie check while in TESTMODE&#34;.<br/> So basically &#34;skipping cookie check while in TESTMODE&#34; == &#34;TESTMODE: No cookie check cookieCheck()&#34;</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

595 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传