dongshen2903 2017-05-04 08:17
浏览 372

转到http.MaxBytesReader重置连接

I am trying to implement a restriction on file size when uploading files. The following code correctly detects whenever the file (or rather the request body, but close enough for my purposes) is larger than 1 MB. If this is the case however, the page returned is a "The connection was reset" page, instead of a page with my custom error message. If the file is under 1 MB I correctly get a page saying "File upload OK".

I would love some pointers on why the connection to the server is reset instead of serving what I specify in the code below.

func baseHandler(writer http.ResponseWriter, request *http.Request) {
    request.Body = http.MaxBytesReader(writer, request.Body, 1024 * 1024)
    _, _, err := request.FormFile("uploadfile")

    if err != nil {
        fmt.Fprintf(writer, "ERROR: %v", err) // Should be displayed in browser, is not
        fmt.Printf("ERROR: %v", err) // Gets written to terminal, so any errors are correctly identified

        return
    }

    fmt.Fprintf(writer, "File upload OK")
}


func main() {
    http.HandleFunc("/", baseHandler)
    http.ListenAndServe(":8080", nil)
}
  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2017-05-04 14:07
    关注

    The server does write the response. The issue is that the client does not read the response.

    When the MaxBytesReader limit is breached, the server stops reading data from the client. Also, the server fully closes the connection a half second after writing the response to the client.

    Many HTTP clients write the complete request body before reading the response and stop on any error writing the request body. These clients report "connection reset" errors and the like when the request body is sufficiently large and ignore the response written by the server.

    Server code pointers: When the MaxBytesReader limit is breached, the response's requestBodyLimitHit field is set to true. All relevant code is near uses of this field.

    评论

报告相同问题?

悬赏问题

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