Php 如何处理来自代理的额外HTTP头?

Php 如何处理来自代理的额外HTTP头?,php,curl,proxy,http-headers,twilio,Php,Curl,Proxy,Http Headers,Twilio,我们的环境要求对非现场服务使用出站代理。通常这不是问题。在本例中,对于Twilio,返回的额外头会中断客户端 传出标题: POST /2010-04-01/Accounts/FOO/SMS/Messages.json HTTP/1.1 Authorization: Basic FOO== User-Agent: twilio-php/3.10.0 Host: api.twilio.com Accept: */* Accept-Charset: utf-8 Content-Type: applic

我们的环境要求对非现场服务使用出站代理。通常这不是问题。在本例中,对于Twilio,返回的额外头会中断客户端

传出标题:

POST /2010-04-01/Accounts/FOO/SMS/Messages.json HTTP/1.1
Authorization: Basic FOO==
User-Agent: twilio-php/3.10.0
Host: api.twilio.com
Accept: */*
Accept-Charset: utf-8
Content-Type: application/x-www-form-urlencoded
Content-Length: 108
响应标题:

HTTP/1.0 200 Connection established

HTTP/1.1 201 Created
Server: nginx
Date: Thu, 06 Jun 2013 14:39:24 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 551
Connection: close
X-Powered-By: PHP/5.3.11
我只能假设代理正在添加额外的HTTP头

Twilio客户端会检查:

list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue') 
据我所知,curl的某些时间或版本会自动在请求中添加Expect头,HTTP 100会在响应中返回,但在本例中不是这样,响应是200连接建立的。值得一提的是,添加一个空的Expect:或Expect:bacon并没有改变结果

我真的不想在这里对Twilio客户端进行太多的攻击,我特别希望避免仅仅添加一个| |$parts[0]==“HTTP/1.0 200连接已建立”,因为这看起来很混乱

是否可以在中发送一个请求标头,以抑制/隐藏额外标头?或者,一个卷曲选项,我看不到忽略它


出站代理是Linux/Squid

代理问题是许多脚本都面临的问题。我在互联网上找到的首选解决方案是简单地添加以下代码行

<?php
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string
if (false !== stripos($response, "HTTP/1.0 200 Connection established\r\n\r\n")) {
  $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $response);
}
?>
因此,要与twilio客户端一起使用,您需要在脚本的最顶端添加以下内容:

<?php
function fixProxyResponse($response) {
  // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string
  if (false !== stripos($response, "HTTP/1.0 200 Connection established\r\n\r\n")) {
    $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $response);
  } 

  return $response;
}


一些非常晚的澄清。当您通过代理连接到SSL/TLS服务器时,代理将使用HTTP connect建立一个隧道。这包含在和过期的RFC2616中,而不是RFC2616

最初的隧道规范要求代理在成功连接到服务器后返回“200连接已建立”到客户端,这就是您看到的。在连接变得透明并从服务器获得实际响应之前,后面可能会有更多的头,然后是一个空行。因此,您将得到两组标题。RFC2817放松了这一点,并允许对连接请求做出任何2xx响应

这意味着,简而言之,您不能依赖于使用上面的php代码检测和剥离单个标题行。可能有多行,第一行可能没有200代码,并且可能不包括“已建立连接”字符串。您必须准备好检测两个完整的标题集


cURL在2004年7.11.1之前一直剥离最初的connect响应,但现在将所有内容发送回客户端。有关详细信息,请参阅。

必须等待一个小时才能获奖,但谢谢。你已经证实了我的想法,我无法摆脱修改Twilio的TinyHttp代码,我会继续这样做。等待赏金结束总是好的。有时候,当名称空间位于列表的顶部时,人们会想出很好的解决方案。顺便问一下,名称空间选项不起作用吗?因为你根本不需要改变它。不幸的是,这是对As站点的更新,它在centos上仍然是5.x版本。我们很快就要迁移它了,但是现在,对于这个补丁,我将不得不放弃名称空间。糟透了。这真的让我大吃一惊。@Theurrican那是什么?根据规范,http响应中只有一个“状态行”,后面是标题定义。也就是说,如果我读对了。。。201状态的措辞也很有趣
ADDED TO RESPONSE

HTTP/1.1 200 OK
Cache-Control: public, max-age=11
Content-Length: 191951
Content-Type: text/html; charset=utf-8
Expires: Wed, 12 Jun 2013 07:09:02 GMT
Last-Modified: Wed, 12 Jun 2013 07:08:02 GMT
Vary: *
X-Frame-Options: SAMEORIGIN
Date: Wed, 12 Jun 2013 07:08:49 GMT
<?php
namespace FakeCurl;
function curl_exec($ch) {
  $response =  \curl_exec($ch);

  // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string
  if (false !== stripos($response, "HTTP/1.0 200 Connection established\r\n\r\n")) {
    $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $response);
  } 

  return $response;
}

include("twilio.php");
?>
<?php
function fixProxyResponse($response) {
  // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string
  if (false !== stripos($response, "HTTP/1.0 200 Connection established\r\n\r\n")) {
    $response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $response);
  } 

  return $response;
}
if ($response = curl_exec($curl)) {
  $parts = explode("\r\n\r\n", $response, 3);
  list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
if ($response = curl_exec($curl)) {
  $parts = explode("\r\n\r\n", fixProxyResponse($response), 3);
  list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')