<p>This feels like it ought to be a really stupid question, but looking at the code I just don'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't make sense to me. What am I missing? (I'm a rank n00b, in case it's not obvious, and of course I can hack around this, but I want to understand why it'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't typed "go install" 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't work on a directory, and didn't read past that, and for some reason <a href="/u/Redundancy_" rel="nofollow">/u/Redundancy_</a>'s anchored link showed me the bit of code I'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'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>'s anchored link showed me the bit of code I'd already been looking at, and not the bit of code I needed to look at.</p>
</blockquote>
<p>That's why it's nice to highlight selected lines when displaying source code. golang.org's source code view doesn'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'm really trying to understand what the intended flow is here, not just get a workaround answer. As I said, it'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->index.html, but that code is reached through FileServer(...) -> *fileHandler --> fileHandler.ServeHTTP -> 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'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't answer... FileServer should return a directory listing if it doesn'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's a mux issue and that it'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'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, "/") + indexPage | "/index.html"
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 = "/index.html"
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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传