dongshan0202405 2013-12-20 11:05
浏览 72
已采纳

编辑后http.FileServer缓存文件并提供旧版本

Having issues with the http package in the core of go. It appears that the file contents is cached although the Content-Length in the response body is correct. To demonstrate here is a simplified version of the application I am writing.

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./www/")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println(err)
    }
}

Now suppose we have a very simple html page:

<!doctype html>
<html>
<body>
    <p>Hello there</p>
</body>
</html>

I execute the go program and access http://localhost:8080 in the browser to be presented with:

Hello there

Checking the response headers I can see the following:

Status Code:200 OK
Accept-Ranges:bytes
Content-Length:68
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Dec 2013 10:04:03 GMT
Last-Modified:Fri, 20 Dec 2013 10:03:32 GMT

Now I edit the html file so the <p> tag contains Hello there everyone and reload the page. I get the following:

Hello there

Again looking at the response headers I get

Status Code:200 OK
Accept-Ranges:bytes
Content-Length:77
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Dec 2013 10:04:34 GMT
Last-Modified:Fri, 20 Dec 2013 10:04:14 GMT

So the Content-Length has changed as well as last modified but not the actual file content delivered by the http.FileServer handler. This issue happens even after closing the program down and doing go run src/.../main.go. The only way I have found so far to clear the apparently cached version of the file is to reboot the machine the program is running on.

I have tried the following:

  • Executing program on win / ubuntu / osx 10.8.5
  • Going through the chain of functions / interfaces on golang.org/src to see if the served file is cached on disk anywhere

Any help with this would be very much appreciated.

  • 写回答

4条回答 默认 最新

  • dotws86260 2014-01-11 14:31
    关注

    Ok, so after a few weeks of ignoring the problem and moving on I have finally worked out what the issue is.

    In order to leave my main computer fairly uncustomised I use Vagrant to develop applications using golang, nodejs and php. It appears that running a go application on a Virtual Box share with all the html files stored on that share causes this issue.

    To prove this I span up a Vagrant box and copied the files from the /vagrant shared directory to /home/vagrant/testing/, then replicated all the previously listed actions. This made the problem disappear.

    So in other words, don't use a Virtual Box shared folder to host files intended for use in a http.FileServer method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。