Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13815 anikendra 1
<?php
2
/**
3
 * Copyright (c) 2007-2011, Servigistics, Inc.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 *  - Redistributions of source code must retain the above copyright notice,
10
 *    this list of conditions and the following disclaimer.
11
 *  - Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *  - Neither the name of Servigistics, Inc. nor the names of
15
 *    its contributors may be used to endorse or promote products derived from
16
 *    this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
31
 * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
32
 * @version $Id: $
33
 *
34
 * @package Apache
35
 * @subpackage Solr
36
 * @author Donovan Jimenez <djimenez@conduit-it.com>
37
 */
38
 
39
// Require Apache_Solr_HttpTransport_Abstract
40
require_once(dirname(__FILE__) . '/Abstract.php');
41
 
42
/**
43
 * HTTP Transport implemenation that uses the builtin http URL wrappers and file_get_contents
44
 */
45
class Apache_Solr_HttpTransport_FileGetContents extends Apache_Solr_HttpTransport_Abstract
46
{
47
	/**
48
	 * SVN Revision meta data for this class
49
	 */
50
	const SVN_REVISION = '$Revision:  $';
51
 
52
	/**
53
	 * SVN ID meta data for this class
54
	 */
55
	const SVN_ID = '$Id:  $';
56
 
57
	/**
58
	 * Reusable stream context resources for GET and POST operations
59
	 *
60
	 * @var resource
61
	 */
62
	private $_getContext, $_headContext, $_postContext;
63
 
64
	/**
65
	 * Initializes our reuseable get and post stream contexts
66
	 */
67
	public function __construct()
68
	{
69
		$this->_getContext = stream_context_create();
70
		$this->_headContext = stream_context_create();
71
		$this->_postContext = stream_context_create();
72
	}
73
 
74
	public function performGetRequest($url, $timeout = false)
75
	{
76
		// set the timeout if specified
77
		if ($timeout !== FALSE && $timeout > 0.0)
78
		{
79
			// timeouts with file_get_contents seem to need
80
			// to be halved to work as expected
81
			$timeout = (float) $timeout / 2;
82
 
83
			stream_context_set_option($this->_getContext, 'http', 'timeout', $timeout);
84
		}
85
		else
86
		{
87
			// use the default timeout pulled from default_socket_timeout otherwise
88
			stream_context_set_option($this->_getContext, 'http', 'timeout', $this->getDefaultTimeout());
89
		}
90
 
91
		// $http_response_headers will be updated by the call to file_get_contents later
92
		// see http://us.php.net/manual/en/wrappers.http.php for documentation
93
		// Unfortunately, it will still create a notice in analyzers if we don't set it here
94
		$http_response_header = null;
95
		$responseBody = @file_get_contents($url, false, $this->_getContext);
96
 
97
		return $this->_getResponseFromParts($responseBody, $http_response_header);
98
	}
99
 
100
	public function performHeadRequest($url, $timeout = false)
101
	{
102
		stream_context_set_option($this->_headContext, array(
103
				'http' => array(
104
					// set HTTP method
105
					'method' => 'HEAD',
106
 
107
					// default timeout
108
					'timeout' => $this->getDefaultTimeout()
109
				)
110
			)
111
		);
112
 
113
		// set the timeout if specified
114
		if ($timeout !== FALSE && $timeout > 0.0)
115
		{
116
			// timeouts with file_get_contents seem to need
117
			// to be halved to work as expected
118
			$timeout = (float) $timeout / 2;
119
 
120
			stream_context_set_option($this->_headContext, 'http', 'timeout', $timeout);
121
		}
122
 
123
		// $http_response_headers will be updated by the call to file_get_contents later
124
		// see http://us.php.net/manual/en/wrappers.http.php for documentation
125
		// Unfortunately, it will still create a notice in analyzers if we don't set it here
126
		$http_response_header = null;
127
		$responseBody = @file_get_contents($url, false, $this->_headContext);
128
 
129
		return $this->_getResponseFromParts($responseBody, $http_response_header);
130
	}
131
 
132
	public function performPostRequest($url, $rawPost, $contentType, $timeout = false)
133
	{
134
		stream_context_set_option($this->_postContext, array(
135
				'http' => array(
136
					// set HTTP method
137
					'method' => 'POST',
138
 
139
					// Add our posted content type
140
					'header' => "Content-Type: $contentType",
141
 
142
					// the posted content
143
					'content' => $rawPost,
144
 
145
					// default timeout
146
					'timeout' => $this->getDefaultTimeout()
147
				)
148
			)
149
		);
150
 
151
		// set the timeout if specified
152
		if ($timeout !== FALSE && $timeout > 0.0)
153
		{
154
			// timeouts with file_get_contents seem to need
155
			// to be halved to work as expected
156
			$timeout = (float) $timeout / 2;
157
 
158
			stream_context_set_option($this->_postContext, 'http', 'timeout', $timeout);
159
		}
160
 
161
		// $http_response_header will be updated by the call to file_get_contents later
162
		// see http://us.php.net/manual/en/wrappers.http.php for documentation
163
		// Unfortunately, it will still create a notice in analyzers if we don't set it here
164
		$http_response_header = null;
165
		$responseBody = @file_get_contents($url, false, $this->_postContext);
166
 
167
		// reset content of post context to reclaim memory
168
		stream_context_set_option($this->_postContext, 'http', 'content', '');
169
 
170
		return $this->_getResponseFromParts($responseBody, $http_response_header);
171
	}
172
 
173
	private function _getResponseFromParts($rawResponse, $httpHeaders)
174
	{
175
		//Assume 0, false as defaults
176
		$status = 0;
177
		$contentType = false;
178
 
179
		//iterate through headers for real status, type, and encoding
180
		if (is_array($httpHeaders) && count($httpHeaders) > 0)
181
		{
182
			//look at the first headers for the HTTP status code
183
			//and message (errors are usually returned this way)
184
			//
185
			//HTTP 100 Continue response can also be returned before
186
			//the REAL status header, so we need look until we find
187
			//the last header starting with HTTP
188
			//
189
			//the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1
190
			//
191
			//Thanks to Daniel Andersson for pointing out this oversight
192
			while (isset($httpHeaders[0]) && substr($httpHeaders[0], 0, 4) == 'HTTP')
193
			{
194
				// we can do a intval on status line without the "HTTP/1.X " to get the code
195
				$status = intval(substr($httpHeaders[0], 9));
196
 
197
				// remove this from the headers so we can check for more
198
				array_shift($httpHeaders);
199
			}
200
 
201
			//Look for the Content-Type response header and determine type
202
			//and encoding from it (if possible - such as 'Content-Type: text/plain; charset=UTF-8')
203
			foreach ($httpHeaders as $header)
204
			{
205
				// look for the header that starts appropriately
206
				if (strncasecmp($header, 'Content-Type:', 13) == 0)
207
				{
208
					$contentType = substr($header, 13);
209
					break;
210
				}
211
			}
212
		}
213
 
214
		return new Apache_Solr_HttpTransport_Response($status, $contentType, $rawResponse);
215
	}
216
}