如何处理“100继续"HTTP 消息? [英] How to handle "100 continue" HTTP message?

查看:36
本文介绍了如何处理“100继续"HTTP 消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的 HTTP 服务器,它将主要接受来自 cURL 的 PUT 请求作为客户端,我在处理 Expect: 100-continue 标头时遇到了一些问题.

据我所知,服务器应该读取标头,在连接上发回 HTTP/1.1 100 Continue 响应,将流读取到 Content- 上的值-长度,然后发回真正的响应代码(通常是HTTP/1.1 200 OK,但任何其他有效的 HTTP 应答都应该这样做).

嗯,这正是我的服务器所做的.问题是,显然,如果我发送 100 Continue 回答,cURL 无法报告任何后续的 HTTP 错误代码并假设上传成功.例如,如果上传由于内容的性质而被拒绝(有一个基本的数据检查发生),我希望调用客户端检测到问题并采取相应措施.

我是否遗漏了一些明显的东西?

这里是 cURL 的示例输出,带有包含错误的辅助标题:

>PUT/test1%2Epdf HTTP/1.1>授权:基本xxxx>用户代理:curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3>主机:本地主机>接受: */*>内容长度:24>期望:100-继续><HTTP/1.1 100 继续<HTTP/1.1 415 不支持的媒体类型<连接:关闭<内容类型:文本/xml<内容长度:289<

解决方案

如果您使用 libcURL 编写客户端程序,请确保将 CURLOPT_FAILONERROR 选项设置为 1.例如,在 C 中,您将执行以下操作:

curl_easy_setopt (curl_handle, CURLOPT_FAILONERROR, 1L);

根据 libcURL 文档,这个选项如果返回的 HTTP 代码等于或大于 400,则告诉库静默失败."

此外,文档明确指出默认操作是正常返回页面,忽略该代码."

如果您使用 curl 命令行工具,只需在 curl 命令中添加 -f--fail 将导致与上述类似的行为.这也在 curl 手册页中进行了描述.

请注意,这两种方法都不是故障安全的,如文档中明确所述:

<块引用>

这种方法不是万无一失的,在某些情况下,不成功的响应代码会漏掉,尤其是在涉及身份验证时(响应代码 401 和 407)."

I'm writing a simplistic HTTP server that will accept PUT requests mostly from cURL as client and I'm having a bit of an issue with handling the Expect: 100-continue header.

As I understand it, the server is supposed to read the header, send back a HTTP/1.1 100 Continue response on the connection, read the stream up to the value on Content-Length and then send back the real response code (Usually HTTP/1.1 200 OK but any other valid HTTP answer should do).

Well, that's exactly what my server does. The problem is that, apparently, if I send a 100 Continue answer, cURL fails to report any subsequent HTTP error code and assumes the upload was a success. For instance, if the upload is rejected due to the nature of the content (there is a basic data check happening), I want the calling client to detect the problem and act accordingly.

Am I missing something obvious ?

edit: here is a sample output from cURL with a secondary header containing an error:

> PUT /test1%2Epdf HTTP/1.1
> Authorization: Basic xxxx
> User-Agent: curl/7.20.0 (i386-pc-win32) libcurl/7.20.0 OpenSSL/0.9.8l zlib/1.2.3
> Host: localhost
> Accept: */*
> Content-Length: 24
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 415 Unsupported Media Type
< Connection: close
< Content-Type: text/xml
< Content-Length: 289
<

解决方案

If you are using libcURL to write your client-side program, make sure you set the CURLOPT_FAILONERROR option to 1. For example, in C, you would do something like:

curl_easy_setopt (curl_handle, CURLOPT_FAILONERROR, 1L);

According to the libcURL documentation, this option "tells the library to fail silently if the HTTP code returned is equal to or larger than 400."

Furthermore, the documentation makes it clear that "the default action would be to return the page normally, ignoring that code."

If you are using the curl command line tool, simply adding -f or --fail into your curl command will cause similar behaviour as described above. This is also described in the curl man page.

Note that both these methods are not fail-safe, as is clearly stated in the docs:

"This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407)."

这篇关于如何处理“100继续"HTTP 消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆