支持 HTTP 100 继续使用 PHP

发布于 2024-08-21 12:15:00 字数 758 浏览 7 评论 0原文

我正在开发一个 PHP Web 应用程序,它接受来自特定客户端的大型 POSTed 文件上传,并希望在使用 HTTP/1.1 100 上传文件之前接受或拒绝这些上传(基于各种标头和其他因素,而不仅仅是大小)继续。

HTTP/1.1 规范 8.2.3 的一些快速背景知识:

100(继续)状态(请参阅第 10.1.1 节)的目的是允许发送带有请求正文的请求消息的客户端确定源服务器是否愿意接受该请求(基于请求标头)在客户端发送请求正文之前。在某些情况下,如果服务器在不查看正文的情况下拒绝消息,则客户端发送正文可能不合适或效率极低。

问题是 Apache 从客户端看到 Expect: 100-Continue,返回 100 Continue 并在 PHP 开始处理之前接受所有文件上传...但是我需要 PHP 在 Expect: 100-Continue 之后立即开始处理。我不确定这是否可行,所以我有两个问题:

  1. 是否可以使 PHP 在 Expect: 100-continue 之后立即开始处理?
  2. 如果没有,什么是好的替代方案?

我目前正在考虑通过指定客户端首先发送一个与 POST 具有相同标头的 HEAD 请求来模拟 100 continue。然后,Web 应用程序可以返回继续 POST 的响应或错误代码。欢迎其他建议!

I'm working on a PHP webapp that accepts large POSTed file uploads from specific clients and would like to accept or reject these uploads (based on various headers and other factors, not just size) before the file is uploaded by using HTTP/1.1 100 Continue.

Some quick background from HTTP/1.1 spec 8.2.3:

The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.

The problem is that Apache sees the Expect: 100-continue from the client, returns a 100 Continue and accepts the file upload all before PHP begins processing... However I need PHP to begin processing immediately after the Expect: 100-continue. I'm not sure if this is possible so I have two questions:

  1. Is it possible to make PHP begin processing immediately after the Expect: 100-continue?
  2. If not, what is a good alternative?

I'm currently thinking of emulating 100 continue by specifying the client first send a HEAD request with the same headers as the POST. The webapp can then return a response to continue with the POST or an error code. Other suggestions are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡渣熟男 2024-08-28 12:15:00

不幸的是我认为这是不可能的。如果这是一个真正的要求,我认为最好只是看看其他语言。我认为今天的异构环境比编写这个问题时更常见,所以为什么不创建一个用其他语言编写的仅处理上传的小型服务。

但是,PHP 的工作方式是,脚本仅在客户端发送整个请求时启动。

Unfortunately I don't think this is possible. If this is a real requirement, I think it's best to simply look at other languages. I think today heterogeneous environments are more common than when this question was written, so why not create a small service written in some other language that just deals with the upload.

But yea, the way PHP works is that the script only starts when the entire request is sent by the client.

╭ゆ眷念 2024-08-28 12:15:00

尝试在 HTTP 级别上执行此操作似乎太困难了。作为开发人员,重要的是不要沉迷于特定的解决方案。问题是您需要在处理上传之前进行一系列检查。您所需要做的就是在上传之前放置一个合格的页面。如果他们通过了一系列检查并符合资格,此资格页面只会向他们显示上传表格。这正是您想要做的,只有您可以在 PHP 代码中做到这一点。如果可能的话,HTTP 100 总是需要大量额外的配置,从而给以后的支持带来麻烦。如果您在代码中执行此操作,那么您后面的人(或几年后的您自己)将能够清楚地了解应用程序正在做什么。

Trying to do this on the HTTP level seems too difficult. It is important as a developer to not get hung up on a specific solution. The problem is you want to do a series of checks before you handle the upload. All you need to do is put a qualifying page before the upload. This qualifying page will only show them the upload form if they pass the series of checks and qualify. That is exactly what you are trying to do only you can do it in code PHP. If it is possible, the HTTP 100 thing will always require lots of additional configuration, thus creating a headache for support later on. If you do it in the code those that come behind you (or yourself in a couple of years) will be able to clearly understand what the app is doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文