File indexing completed on 2024-06-23 05:55:43

0001 <?php
0002 
0003 /**
0004  * Copyright (c) 2007-2011, Servigistics, Inc.
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions are met:
0009  *
0010  *  - Redistributions of source code must retain the above copyright notice,
0011  *    this list of conditions and the following disclaimer.
0012  *  - Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *  - Neither the name of Servigistics, Inc. nor the names of
0016  *    its contributors may be used to endorse or promote products derived from
0017  *    this software without specific prior written permission.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  *
0031  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
0032  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
0033  * @version $Id: $
0034  *
0035  * @package Apache
0036  * @subpackage Solr
0037  * @author Timo Schmidt <timo.schmidt@aoemedia.de>, Donovan Jimenez <djimenez@conduit-it.com>
0038  */
0039 require_once 'Zend/Service/Solr/HttpTransport/Abstract.php';
0040 
0041 /**
0042  * An alternative Curl HTTP transport that opens and closes a curl session for
0043  * every request. This isn't the recommended way to use curl, but some version of
0044  * PHP have memory issues.
0045  */
0046 class Zend_Service_Solr_HttpTransport_CurlNoReuse extends Zend_Service_Solr_HttpTransport_Abstract {
0047     /**
0048      * SVN Revision meta data for this class
0049      */
0050     const SVN_REVISION = '$Revision:$';
0051 
0052     /**
0053      * SVN ID meta data for this class
0054      */
0055     const SVN_ID = '$Id:$';
0056 
0057     public function performGetRequest($url, $timeout = false) {
0058         // check the timeout value
0059         if ($timeout === false || $timeout <= 0.0) {
0060             // use the default timeout
0061             $timeout = $this->getDefaultTimeout();
0062         }
0063 
0064         $curl = curl_init();
0065 
0066         // set curl GET options
0067         curl_setopt_array($curl, array(
0068             // return the response body from curl_exec
0069             CURLOPT_RETURNTRANSFER => true,
0070             // get the output as binary data
0071             CURLOPT_BINARYTRANSFER => true,
0072             // we do not need the headers in the output, we get everything we need from curl_getinfo
0073             CURLOPT_HEADER => false,
0074             // set the URL
0075             CURLOPT_URL => $url,
0076             // set the timeout
0077             CURLOPT_TIMEOUT => $timeout
0078         ));
0079 
0080         // make the request
0081         $responseBody = curl_exec($curl);
0082 
0083         // get info from the transfer
0084         $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
0085         $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
0086 
0087         // close our curl session - we're done with it
0088         curl_close($curl);
0089 
0090         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0091     }
0092 
0093     public function performHeadRequest($url, $timeout = false) {
0094         // check the timeout value
0095         if ($timeout === false || $timeout <= 0.0) {
0096             // use the default timeout
0097             $timeout = $this->getDefaultTimeout();
0098         }
0099 
0100         $curl = curl_init();
0101 
0102         // set curl HEAD options
0103         curl_setopt_array($curl, array(
0104             // return the response body from curl_exec
0105             CURLOPT_RETURNTRANSFER => true,
0106             // get the output as binary data
0107             CURLOPT_BINARYTRANSFER => true,
0108             // we do not need the headers in the output, we get everything we need from curl_getinfo
0109             CURLOPT_HEADER => false,
0110             // this both sets the method to HEAD and says not to return a body
0111             CURLOPT_NOBODY => true,
0112             // set the URL
0113             CURLOPT_URL => $url,
0114             // set the timeout
0115             CURLOPT_TIMEOUT => $timeout
0116         ));
0117 
0118         // make the request
0119         $responseBody = curl_exec($curl);
0120 
0121         // get info from the transfer
0122         $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
0123         $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
0124 
0125         // close our curl session - we're done with it
0126         curl_close($curl);
0127 
0128         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0129     }
0130 
0131     public function performPostRequest($url, $postData, $contentType, $timeout = false) {
0132         // check the timeout value
0133         if ($timeout === false || $timeout <= 0.0) {
0134             // use the default timeout
0135             $timeout = $this->getDefaultTimeout();
0136         }
0137 
0138         $curl = curl_init();
0139 
0140         // set curl POST options
0141         curl_setopt_array($curl, array(
0142             // return the response body from curl_exec
0143             CURLOPT_RETURNTRANSFER => true,
0144             // get the output as binary data
0145             CURLOPT_BINARYTRANSFER => true,
0146             // we do not need the headers in the output, we get everything we need from curl_getinfo
0147             CURLOPT_HEADER => false,
0148             // make sure we're POST
0149             CURLOPT_POST => true,
0150             // set the URL
0151             CURLOPT_URL => $url,
0152             // set the post data
0153             CURLOPT_POSTFIELDS => $postData,
0154             // set the content type
0155             CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
0156             // set the timeout
0157             CURLOPT_TIMEOUT => $timeout
0158         ));
0159 
0160         // make the request
0161         $responseBody = curl_exec($curl);
0162 
0163         // get info from the transfer
0164         $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
0165         $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
0166 
0167         // close our curl session - we're done with it
0168         curl_close($curl);
0169 
0170         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0171     }
0172 
0173 }