如何处理来自代理的额外HTTP头? [英] What to do with extra HTTP header from proxy?

查看:175
本文介绍了如何处理来自代理的额外HTTP头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的环境要求对场外服务使用出站代理。通常这不是一个问题。在这种情况下,使用Twilio,返回的额外标头会打断客户端。

Our environment requires the use of an outbound proxy for offsite services. Normally this isn't a problem. In this case with Twilio, the extra header returned breaks the client.

传出标头:

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头。

I can only assume the proxy is adding the extra HTTP header.

Twilio客户端检查:

The Twilio client does check for:

list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue') 

根据我的理解,有一些时间或版本的curl会自动在请求中添加一个Expect头,HTTP 100将在响应中返回,但在这种情况下它不是,并且响应为200 Connection建立。对于什么是值得添加一个空的期望:或期望:培根没有改变结果。

As I understand it, there are times or versions of curl that will automatically add an Expect header in the request, and the HTTP 100 would be returned in the response, but in this case it is not, and the response is 200 Connection established. For what it's worth adding an empty Expect: or an Expect:bacon didn't change the results.

我真的不想在Twilio客户端上太多这里,我特别想避免只是添加一个|| $ parts [0] =='HTTP / 1.0 200 Connection established',因为它看起来很乱。

I'd really prefer not to hack on the Twilio client too much here, and I especially would like to avoid just adding a || $parts[0] == 'HTTP/1.0 200 Connection established' as it seems like that's messy.

可以发送一个请求头, /隐藏额外的标题?或者,curl选项我不看到忽略它?

Is it possible to send a request header in the that will suppress/hide the extra header? Or, a curl option I'm not seeing to ignore it?

出站代理是Linux / Squid

The outbound proxy is Linux/Squid

推荐答案

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

The proxy issue is something a lot of scripts are facing. The preferred solution I can find on the internet is to simply add the following lines of code.

<?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客户端会有点凌乱。幸运的是,你可以使用命名空间来重建本地函数。请参阅以下示例。

Now to add this to the twilio client would be a bit messy indeed. Luckily you can use namespaces to recreate native functions. See the following example.

<?php
namespace FakeCurl;

//create curl_exec function with same name, but its created in the FakeCurl namespace now.
function curl_exec($ch) {
  //execute the actual curl_exec function in the main namespace
  $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 "ADDED TO RESPONSE\r\n\r\n".$response;
}

//make a regular curl request, no alterations.

$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_HEADER => true,
    CURLOPT_NOBODY => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'http://stackoverflow.com' ) );
$response = curl_exec( $curl );
curl_close( $curl );

echo '<pre>'.$response.'</pre>';

?>

输出

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

因此,要与twilio客户端一起使用,您需要在脚本的最顶端放置以下内容:

So to use with the twilio client, you need to put at the very top of your script the following:

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

如果命名空间选项由于某种原因失败,我将在twilio客户端之外添加一个简单的函数。

Should the namespace option fail for some reason, I would add a simple function outside the twilio client like.

<?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;
}

然后改变twilio脚本 TinyHttp.php 并更改以下行(〜linenr 63)

And then alter the twilio script TinyHttp.php and change only the following line (~linenr 63)

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

这篇关于如何处理来自代理的额外HTTP头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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