1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | * |
---|
31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
33 | * @link http://www.solarium-project.org/ |
---|
34 | * |
---|
35 | * @package Solarium |
---|
36 | * @subpackage Client |
---|
37 | */ |
---|
38 | |
---|
39 | /** |
---|
40 | * Basic HTTP adapter using a stream |
---|
41 | * |
---|
42 | * @package Solarium |
---|
43 | * @subpackage Client |
---|
44 | */ |
---|
45 | class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter |
---|
46 | { |
---|
47 | |
---|
48 | /** |
---|
49 | * Handle Solr communication |
---|
50 | * |
---|
51 | * @throws Solarium_Exception |
---|
52 | * @param Solarium_Client_Request $request |
---|
53 | * @return Solarium_Client_Response |
---|
54 | */ |
---|
55 | public function execute($request) |
---|
56 | { |
---|
57 | $context = $this->createContext($request); |
---|
58 | $uri = $this->getBaseUri() . $request->getUri(); |
---|
59 | |
---|
60 | list($data, $headers) = $this->_getData($uri, $context); |
---|
61 | |
---|
62 | $this->check($data, $headers); |
---|
63 | |
---|
64 | return new Solarium_Client_Response($data, $headers); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Check result of a request |
---|
69 | * |
---|
70 | * @throws Solarium_Client_HttpException |
---|
71 | * @param string $data |
---|
72 | * @param array $headers |
---|
73 | * @return void |
---|
74 | */ |
---|
75 | public function check($data, $headers) |
---|
76 | { |
---|
77 | // if there is no data and there are no headers it's a total failure, |
---|
78 | // a connection to the host was impossible. |
---|
79 | if (false === $data && count($headers) == 0) { |
---|
80 | throw new Solarium_Client_HttpException("HTTP request failed"); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Create a stream context for a request |
---|
86 | * |
---|
87 | * @param Solarium_Client_Request $request |
---|
88 | * @return resource |
---|
89 | */ |
---|
90 | public function createContext($request) |
---|
91 | { |
---|
92 | $method = $request->getMethod(); |
---|
93 | $context = stream_context_create( |
---|
94 | array('http' => array( |
---|
95 | 'method' => $method, |
---|
96 | 'timeout' => $this->getTimeout() |
---|
97 | )) |
---|
98 | ); |
---|
99 | |
---|
100 | if ($method == Solarium_Client_Request::METHOD_POST) { |
---|
101 | $data = $request->getRawData(); |
---|
102 | if (null !== $data) { |
---|
103 | stream_context_set_option( |
---|
104 | $context, |
---|
105 | 'http', |
---|
106 | 'content', |
---|
107 | $data |
---|
108 | ); |
---|
109 | |
---|
110 | $request->addHeader('Content-Type: text/xml; charset=UTF-8'); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | $headers = $request->getHeaders(); |
---|
115 | if (count($headers) > 0) { |
---|
116 | stream_context_set_option( |
---|
117 | $context, |
---|
118 | 'http', |
---|
119 | 'header', |
---|
120 | implode("\r\n", $headers) |
---|
121 | ); |
---|
122 | } |
---|
123 | |
---|
124 | return $context; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Execute request |
---|
129 | * |
---|
130 | * @param string $uri |
---|
131 | * @param resource $context |
---|
132 | * @return array |
---|
133 | */ |
---|
134 | protected function _getData($uri, $context) |
---|
135 | { |
---|
136 | // @codeCoverageIgnoreStart |
---|
137 | $data = @file_get_contents($uri, false, $context); |
---|
138 | |
---|
139 | if (isset($http_response_header)) { |
---|
140 | $headers = $http_response_header; |
---|
141 | } else { |
---|
142 | $headers = array(); |
---|
143 | } |
---|
144 | |
---|
145 | return array($data, $headers); |
---|
146 | // @codeCoverageIgnoreEnd |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|