How does http.FileServer handle index.html?

polaris · · 507 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>This feels like it ought to be a really stupid question, but looking at the code I just don&#39;t see how http.FileServer can ever actually return the contents of an index.html file. When it gets a request for /index.html, it redirects it to /, which makes sense, but when it gets a request for /, it has no special-case handling for / to open index.html, and so I get a 404.</p> <p>This flow doesn&#39;t make sense to me. What am I missing? (I&#39;m a rank n00b, in case it&#39;s not obvious, and of course I can hack around this, but I want to understand why it&#39;s being done this way).</p> <hr/>**评论:**<br/><br/>abhayakara: <pre><p>Thanks to <a href="/u/Redundancy_" rel="nofollow">/u/Redundancy_</a> and <a href="/u/sh41" rel="nofollow">/u/sh41</a> for their help. What was actually going on, embarrassingly, was that I was in my python mode of thinking and hadn&#39;t typed &#34;go install&#34; after my most recent update, and so the 404 error was a result of the code looking in the wrong place. Once I fixed this, the problem went away.</p> <p>If you are curious, the mental block that produced this was that I assumed io.Open wouldn&#39;t work on a directory, and didn&#39;t read past that, and for some reason <a href="/u/Redundancy_" rel="nofollow">/u/Redundancy_</a>&#39;s anchored link showed me the bit of code I&#39;d already been looking at, and not the bit of code I needed to look at.</p> <p>Thanks for your kind assistance, both of you!</p></pre>Redundancy_: <pre><p>I messed up the initial link (wasn&#39;t going to the correct line) but fixed it a minute or two later.</p> <p>Glad you got to the bottom of your problem.</p></pre>sh41: <pre><blockquote> <p>and for some reason <a href="/u/Redundancy_" rel="nofollow">/u/Redundancy_</a>&#39;s anchored link showed me the bit of code I&#39;d already been looking at, and not the bit of code I needed to look at.</p> </blockquote> <p>That&#39;s why it&#39;s nice to highlight selected lines when displaying source code. golang.org&#39;s source code view doesn&#39;t do this, but gotools.org does:</p> <p><a href="https://gotools.org/net/http#fs.go-L405" rel="nofollow">https://gotools.org/net/http#fs.go-L405</a></p></pre>Akkifokkusu: <pre><p>golang.org can do it, but it uses character position instead of line position for highlighting: <a href="https://golang.org/src/net/http/fs.go?s=12366:12422#L405" rel="nofollow">https://golang.org/src/net/http/fs.go?s=12366:12422#L405</a></p></pre>sh41: <pre><p>How did you create that link?</p></pre>Redundancy_: <pre><p><a href="https://golang.org/src/net/http/fs.go#L405" rel="nofollow">https://golang.org/src/net/http/fs.go#L405</a> ?</p></pre>abhayakara: <pre><p>Yes, I read the source code, and saw that comment. But there is no http.ContentServer, just http.FileServer. I&#39;m really trying to understand what the intended flow is here, not just get a workaround answer. As I said, it&#39;s quite obvious how I could hack my way around this.</p></pre>Redundancy_: <pre><p>You said that there was no code in http.FileServer that handles a dir-&gt;index.html, but that code is reached through FileServer(...) -&gt; *fileHandler --&gt; fileHandler.ServeHTTP -&gt; serveFile If it is a directory, it tries to open index.html, and if there are no errors doing that, it passes it on to serveContent. Unless I&#39;m missing something, this is the special case handling that you were asking for?</p> <p>Why you get a 404 is a different question I can&#39;t answer... FileServer should return a directory listing if it doesn&#39;t find an index.html, which can be quite annoying behaviour not to be able to turn off.</p> <p>Is it possible that it&#39;s a mux issue and that it&#39;s actually hitting another handler?</p></pre>sh41: <pre><p>Read that comment more carefully, and look at the code that follows it.</p> <p>Here&#39;s a sample execution with values on the right.</p> <pre><code>// use contents of index.html for directory, if present | if d.IsDir() { | true index := strings.TrimSuffix(name, &#34;/&#34;) + indexPage | &#34;/index.html&#34; ff, err := fs.Open(index) | {File index.html}, nil if err == nil { | true defer ff.Close() | dd, err := ff.Stat() | {FileInfo index.html}, nil if err == nil { | true name = index | name = &#34;/index.html&#34; d = dd | {FileInfo index.html} f = ff | {File index.html} } } } </code></pre></pre>abhayakara: <pre><p>Thanks, sorry, for some reason when I clicked on the link the first time it didn&#39;t lead to the comment that <a href="/u/Redundancy_" rel="nofollow">/u/Redundancy_</a> intended me to see, so I missed the point. I am at this point just trying to figure out why the code is not working as intended: the behavior I see is that it fails to return index.html.</p> <p>How did you get that trace?</p></pre>

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

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