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

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

问题描述

我写一个简单的HTTP服务器,它将接受大部分来自cURL作为客户端的PUT请求,我有一个问题处理期望:100-continue header。

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.

根据我的理解,服务器应该读取头,发送一个 HTTP / 1.1 100 Continue 响应,读取流到 Content-Length 上的值,然后发回真正的响应代码(通常 HTTP /1.1 200 OK ,但任何其他有效的HTTP答案应该这样做。)

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).

好吧,这正是我的服务器。问题是,显然,如果我发送 100继续回答,cURL没有报告任何后续的HTTP错误代码,并假设上传是成功的。例如,如果由于内容的性质(发生了基本数据检查)而拒绝上传,则我希望呼叫客户端检测到问题并相应地采取行动。

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 ?

编辑:这里是cURL的一个示例输出,包含一个错误的第二个标题:

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
<


推荐答案

如果您使用libcURL编写客户端程序,请确保将 CURLOPT_FAILONERROR 选项设置为 1 。例如,在C中,你可以这样做:

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);

根据libcURL 文档,此选项
如果返回的HTTP代码等于或大于400,则告诉库以静默方式失败。

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."

如果使用curl命令行工具,只需将 -f - fail 添加到curl命令将引起如上所述的类似行为。这也在curl 手册页中进行了说明。

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:


不是故障安全的,并且存在不成功的响应代码将滑过的情况,特别是当涉及认证时(响应代码401和407)。

"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天全站免登陆