为什么上传文件~2,5MiB或大引起一连接的重置?

0

的问题

我们正在努力实施的图像上传通过后请求。 我们还希望限制的图像到-1.0的信息库。 它的工作现在较小的图像,但任何东西~2,5MiB或大会使连接到重置。 它似乎也发送的多次请求后的第一个到相同的处理程序。

主。去:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", uploadHandler)
    http.ListenAndServe("localhost:8080", nil)
}

func uploadHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method == "POST" {
        postHandler(w, r)
        return
    } else {
        http.ServeFile(w, r, "index.html")
    }
}

func postHandler(w http.ResponseWriter, r *http.Request) {
    // Send an error if the request is larger than 1 MiB
    if r.ContentLength > 1<<20 {
        // if larger than ~2,5 MiB, this will print 2 or more times
        log.Println("File too large")
        // And this error will never arrive, instead a Connection reset
        http.Error(w, "response too large", http.StatusRequestEntityTooLarge)
        return
    }
    return
}

index.html:

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form method="POST" enctype="multipart/form-data">
      <input type="file" accept="image/*" name="profile-picture"><br>
      <button type="submit" >Upload</button>
  </form>
  </body>
</html>

输出当载a-2,4MiB文件

$ go run main.go
2021/11/23 22:00:14 File too large

它还表示"要求太大"的浏览器

输出当上传~2,5MiB文件

$ go run main.go
2021/11/23 22:03:25 File too large
2021/11/23 22:03:25 File too large

浏览器现在显示的连接是重置

go http
2021-11-23 20:06:27
1

最好的答案

3

客户是试图发送数据服务器。 服务器是不是读取的数据,这只是看标题和关闭连接。 客户解释为"连接的重置"。 这是您的控制。

而不是检查的标题,标题可以谎言,使用 http.MaxBytesReader 阅读的实际内容,但是错误的,如果它是太大。

const MAX_UPLOAD_SIZE = 1<<20

func postHandler(w http.ResponseWriter, r *http.Request) {
    // Wrap the body in a reader that will error at MAX_UPLOAD_SIZE
    r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)

    // Read the body as normal. Check for an error.
    if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
        // We're assuming it errored because the body is too large.
        // There are other reasons it could error, you'll have to
        // look at err to figure that out.
        log.Println("File too large")
        http.Error(w, "Your file is too powerful", http.StatusRequestEntityTooLarge)
        return
    }

    fmt.Fprintf(w, "Upload successful")
}

看到 如何处理文件上传中去 对于更详细的说明。

2021-11-23 20:55:11

这似乎是工作的一个延伸的和实际适合我们的需要。 然而,它仍然引起连接的重置有一个足够大的图像喜欢 这个,因此,例如,如果我想提升到MAX_UPLOAD_SIZE 20 << 20 无论出于何种原因我不能上任何东西的大小。
urist

我还以为 r.ContentLength 可以用作一个快速检查之前的任何文件甚至还上载在所有的,即使我知道也可能是欺骗。 我猜你可以实现这在客户方面的事情
urist

我想我想通了,为什么连接的重从我的上述意见,你需要真正开始使用该数据(例如与 r.FormFile),否则它只是将停止和返回,关闭连接,一旦它开始变得烦恼的恒定的数据被发送的客户。
urist

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................