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 Timo Schmidt <timo.schmidt@aoemedia.de>, Donovan Jimenez <djimenez@conduit-it.com>
37
 */
38
 
39
// Require Apache_Solr_HttpTransport_Abstract
40
require_once(dirname(__FILE__) . '/Abstract.php');
41
 
42
/**
43
 * An alternative Curl HTTP transport that opens and closes a curl session for
44
 * every request. This isn't the recommended way to use curl, but some version of
45
 * PHP have memory issues.
46
 */
47
class Apache_Solr_HttpTransport_CurlNoReuse extends Apache_Solr_HttpTransport_Abstract
48
{
49
	/**
50
	 * SVN Revision meta data for this class
51
	 */
52
	const SVN_REVISION = '$Revision:$';
53
 
54
	/**
55
	 * SVN ID meta data for this class
56
	 */
57
	const SVN_ID = '$Id:$';
58
 
59
	public function performGetRequest($url, $timeout = false)
60
	{
61
		// check the timeout value
62
		if ($timeout === false || $timeout <= 0.0)
63
		{
64
			// use the default timeout
65
			$timeout = $this->getDefaultTimeout();
66
		}
67
 
68
		$curl = curl_init();
69
 
70
		// set curl GET options
71
		curl_setopt_array($curl, array(
72
			// return the response body from curl_exec
73
			CURLOPT_RETURNTRANSFER => true,
74
 
75
			// get the output as binary data
76
			CURLOPT_BINARYTRANSFER => true,
77
 
78
			// we do not need the headers in the output, we get everything we need from curl_getinfo
79
			CURLOPT_HEADER => false,
80
 
81
			// set the URL
82
			CURLOPT_URL => $url,
83
 
84
			// set the timeout
85
			CURLOPT_TIMEOUT => $timeout
86
		));
87
 
88
		// make the request
89
		$responseBody = curl_exec($curl);
90
 
91
		// get info from the transfer
92
		$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
93
		$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
94
 
95
		// close our curl session - we're done with it
96
		curl_close($curl);
97
 
98
		return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
99
	}
100
 
101
	public function performHeadRequest($url, $timeout = false)
102
	{
103
		// check the timeout value
104
		if ($timeout === false || $timeout <= 0.0)
105
		{
106
			// use the default timeout
107
			$timeout = $this->getDefaultTimeout();
108
		}
109
 
110
		$curl = curl_init();
111
 
112
		// set curl HEAD options
113
		curl_setopt_array($curl, array(
114
			// return the response body from curl_exec
115
			CURLOPT_RETURNTRANSFER => true,
116
 
117
			// get the output as binary data
118
			CURLOPT_BINARYTRANSFER => true,
119
 
120
			// we do not need the headers in the output, we get everything we need from curl_getinfo
121
			CURLOPT_HEADER => false,
122
 
123
			// this both sets the method to HEAD and says not to return a body
124
			CURLOPT_NOBODY => true,
125
 
126
			// set the URL
127
			CURLOPT_URL => $url,
128
 
129
			// set the timeout
130
			CURLOPT_TIMEOUT => $timeout
131
		));
132
 
133
		// make the request
134
		$responseBody = curl_exec($curl);
135
 
136
		// get info from the transfer
137
		$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
138
		$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
139
 
140
		// close our curl session - we're done with it
141
		curl_close($curl);
142
 
143
		return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
144
	}
145
 
146
	public function performPostRequest($url, $postData, $contentType, $timeout = false)
147
	{
148
		// check the timeout value
149
		if ($timeout === false || $timeout <= 0.0)
150
		{
151
			// use the default timeout
152
			$timeout = $this->getDefaultTimeout();
153
		}
154
 
155
		$curl = curl_init();
156
 
157
		// set curl POST options
158
		curl_setopt_array($curl, array(
159
			// return the response body from curl_exec
160
			CURLOPT_RETURNTRANSFER => true,
161
 
162
			// get the output as binary data
163
			CURLOPT_BINARYTRANSFER => true,
164
 
165
			// we do not need the headers in the output, we get everything we need from curl_getinfo
166
			CURLOPT_HEADER => false,
167
 
168
			// make sure we're POST
169
			CURLOPT_POST => true,
170
 
171
			// set the URL
172
			CURLOPT_URL => $url,
173
 
174
			// set the post data
175
			CURLOPT_POSTFIELDS => $postData,
176
 
177
			// set the content type
178
			CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
179
 
180
			// set the timeout
181
			CURLOPT_TIMEOUT => $timeout
182
		));
183
 
184
		// make the request
185
		$responseBody = curl_exec($curl);
186
 
187
		// get info from the transfer
188
		$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
189
		$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
190
 
191
		// close our curl session - we're done with it
192
		curl_close($curl);
193
 
194
		return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
195
	}
196
}