代码之家  ›  专栏  ›  技术社区  ›  Arseni Mourzenko

我应该如何正确地中断HTTP上传请求?

  •  1
  • Arseni Mourzenko  · 技术社区  · 7 年前

    我开发了一个服务器应用程序,其中包括处理大块数据的上传。当上传请求启动时,甚至在接收数据块之前,服务器应用程序都会执行一些检查,以便客户端在出错时中止操作,而不是在发送千兆字节数据之后才发现有问题。

    在使用服务器应用程序时 curl ,我发现了一个奇怪的行为。

    • 卷曲
    • 服务器立即用htp403响应以发出问题信号,并提供带有问题详细信息的JSON响应。
    • 卷曲

      curl:(18)传输已关闭,剩余30个字节可供读取

    $ curl -X PUT --limit-rate 2M http://127.0.0.1/blob -F files[]=@/tmp/tmp75hw30vc -v
    
    *   Trying 127.0.0.1...
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > PUT /blob HTTP/1.1
    > Host: 127.0.0.1
    > User-Agent: curl/7.58.0
    > Accept: */*
    > Content-Length: 2439352842
    > Content-Type: multipart/form-data; boundary=------------------------32c442f4cf8abe0c
    > Expect: 100-continue
    > 
    < HTTP/1.1 100 Continue
    < HTTP/1.1 403 FORBIDDEN
    < Server: nginx/1.10.3
    < Date: Sat, 29 Sep 2018 22:03:16 GMT
    < Content-Type: application/json
    < Content-Length: 30
    < Connection: keep-alive
    * HTTP error before end of send, stop sending
    < 
    * transfer closed with 30 bytes remaining to read
    * stopped the pause stream!
    * Closing connection 0
    curl: (18) transfer closed with 30 bytes remaining to read
    

    在服务器端,代码(使用Flask)如下:

    def receive_blob():
        if _can_upload():
            return flask.jsonify({"error": "already-uploading"}), 403
    
        ...
    

    卷曲 我正在使用的选项。

    我该怎么做才能避免这种情况,即 卷曲 显示服务器返回的JSON错误消息?


    How to handle "100 continue" HTTP message? 因为我的明确要求 卷曲 显示JSON错误消息。链接的问题请添加 --fail

    0 回复  |  直到 7 年前