CakePHPのHttpSocketをプロキシ対応にする

開発環境がプロキシの内側で、HttpSocketがそのまま使えずに困ったので。

素人がかなりアドホックに修正したので、かなり乱暴な修正かもしれないけどとりあえず晒しておく。

使うときは、

$socket = new HttpSocket(
    aa( 'proxy',
            aa('host', '127.0.0.1',
                  'port', 8123)
    )
);

のようにすると良いかと。

======

$ diff -u http_socket_org.php http_socket.php
--- http_socket_org.php 2008-12-19 11:16:01.000000000 +0900
+++ http_socket.php     2009-03-08 01:25:01.250000000 +0900
@@ -114,6 +114,10 @@
                'protocol'   => 'tcp',
                'port'           => 80,
                'timeout'        =>     30,
+               'proxy' => array(
+                       'host' => null,
+                       'port' => null
+                ),
                'request' => array(
                        'uri' => array(
                                'scheme' => 'http',
@@ -191,6 +195,14 @@
                if (isset($host)) {
                        $this->config['host'] = $host;
                }
+
+               // for proxy use.
+               if (!empty($this->config['proxy']['host']) && !empty($this->config['proxy']['port']))
+               {
+                       $this->config['host'] = $this->config['proxy']['host'];
+                       $this->config['port'] = $this->config['proxy']['port'];
+               }
+
                $cookies = null;

                if (is_array($this->request['header'])) {
@@ -728,7 +740,14 @@

                $request['uri'] = $this->parseUri($request['uri']);
                $request = array_merge(array('method' => 'GET'), $request);
-               $request['uri'] = $this->buildUri($request['uri'], '/%path?%query');
+
+               // if proxy use. switch buildUri() argument.
+               if (isset($this->config['proxy']['host']) && isset($this->config['proxy']['port'])) {
+                       $request['uri'] = $this->buildUri($request['uri']);
+               }
+               else {
+                       $request['uri'] = $this->buildUri($request['uri'], '/%path?%query');
+               }

                if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
                        trigger_error(sprintf(__('HttpSocket::buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.', true), join(',', $asteriskMethods)), E_USER_WARNING);
タイトルとURLをコピーしました