Skip to content

Commit 300eeb4

Browse files
Pantheon Automationpwtyler
authored andcommitted
pantheon_apachesolr: handle curl config for local development
1 parent db58052 commit 300eeb4

4 files changed

Lines changed: 69 additions & 27 deletions

File tree

modules/pantheon/pantheon_apachesolr/Pantheon_Apache_Solr_Service.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ protected function _makeHttpRequest($url, $options = array()) {
453453
if (!isset($ch)) {
454454
// The parent PHPSolrClient library assumes http
455455
// $url = str_replace('http://', 'https://', $url);
456-
list($ch, $opts) = pantheon_curl_setup($url, NULL, $port, NULL);
456+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, $port);
457457

458458
// These options only need to be set once
459459
$opts = pantheon_apachesolr_curlopts($opts);
@@ -486,12 +486,19 @@ protected function _makeHttpRequest($url, $options = array()) {
486486
return NULL;
487487
}
488488

489-
// mimick the $result object from drupal_http_request()
489+
// mimic the $result object from drupal_http_request()
490490
// TODO; better error handling
491491
$result = new stdClass();
492492
list($split, $result->data) = explode("\r\n\r\n", $response, 2);
493493
$split = preg_split("/\r\n|\n|\r/", $split);
494-
list($result->protocol, $result->code, $result->status_message) = explode(' ', trim(array_shift($split)), 3);
494+
495+
$status_line = trim(array_shift($split));
496+
$status_parts = explode(' ', $status_line, 3);
497+
498+
$result->protocol = isset($status_parts[0]) ? $status_parts[0] : '';
499+
$result->code = isset($status_parts[1]) ? $status_parts[1] : 0;
500+
$result->status_message = isset($status_parts[2]) ? $status_parts[2] : 'Unknown';
501+
495502
// Parse headers.
496503
$result->headers = array();
497504
while (!empty($split) && $line = trim(array_shift($split))) {

modules/pantheon/pantheon_apachesolr/Pantheon_Search_Api_Solr_Service.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ function __construct(array $options) {
2020

2121
// pantheon_curl_setup will determine whether binding.pem needed and return
2222
// path in options if so.
23-
list($ch, $opts) = pantheon_curl_setup("", NULL, $options['port'], NULL);
24-
if ( isset($opts[CURLOPT_SSLCERT]) ) {
25-
$stream_context_ssl = array('local_cert' => $opts[CURLOPT_SSLCERT]);
23+
list($ch, $opts) = pantheon_apachesolr_curl_setup("", $options['port']);
24+
$sslContext = array();
25+
26+
// Use ssl cert if provided
27+
if (isset($opts[CURLOPT_SSLCERT])) {
28+
$sslContext['local_cert'] = $opts[CURLOPT_SSLCERT];
2629
} else {
27-
$stream_context_ssl = array(
28-
'verify_peer' => (bool)$opts[CURLOPT_SSL_VERIFYPEER],
29-
'verify_peer_name' => (bool)$opts[CURLOPT_SSL_VERIFYHOST]
30-
);
30+
// Add peer verification settings if available.
31+
if (isset($opts[CURLOPT_SSL_VERIFYPEER])) {
32+
$sslContext['verify_peer'] = (bool) $opts[CURLOPT_SSL_VERIFYPEER];
33+
}
34+
if (isset($opts[CURLOPT_SSL_VERIFYHOST])) {
35+
$sslContext['verify_peer_name'] = (bool) $opts[CURLOPT_SSL_VERIFYHOST];
36+
}
37+
}
38+
39+
if ($sslContext) {
40+
$this->setStreamContext(stream_context_create(array('ssl' => $sslContext)));
3141
}
32-
$this->setStreamContext(stream_context_create(array('ssl' => $stream_context_ssl)));
3342

3443
// Adding in general settings for Search API
3544
$options += array(

modules/pantheon/pantheon_apachesolr/pantheon_apachesolr.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = Pantheon Apache Solr
22
description = Exposes Pantheon's ApacheSolr Service
33
package = Pantheon
4-
version = "7.x-1.1"
4+
version = "7.x-1.1.1"
55
files[] = Pantheon_Apache_Solr_Service.php
66
files[] = Pantheon_Search_Api_Solr_Service.php
77
configure = admin/config/search/pantheon

modules/pantheon/pantheon_apachesolr/pantheon_apachesolr.module

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,6 @@ function pantheon_apachesolr_platform_detected() {
171171
return isset($_ENV['PANTHEON_ENVIRONMENT']);
172172
}
173173

174-
/**
175-
* Return the path to the directory containing the certificates.
176-
*/
177-
function pantheon_apachesolr_certs_dir() {
178-
if (pantheon_apachesolr_platform_detected()) {
179-
return $_ENV['HOME'] . '/certs';
180-
}
181-
return './';
182-
}
183-
184174
/**
185175
* Display an error message if not running on the Pantheon platform.
186176
*/
@@ -224,7 +214,7 @@ function pantheon_apachesolr_post_schema_exec($schema) {
224214

225215
$url = 'https://'. $host .'/'. $path;
226216

227-
list($ch, $opts) = pantheon_curl_setup($url, NULL, PANTHEON_APACHESOLR_PORT, NULL);
217+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, PANTHEON_APACHESOLR_PORT);
228218

229219
$file = fopen($schema, 'r');
230220
// set (or override) remaining headers
@@ -368,7 +358,7 @@ function pantheon_apachesolr_query_submit($form, &$form_state) {
368358
$path = 'sites/self/environments/'. variable_get('pantheon_environment', 'dev') .'/index';
369359
$url = 'https://'. $host .'/'. $path . $form_state['values']['query'];
370360

371-
list($ch, $opts) = pantheon_curl_setup($url, NULL, PANTHEON_APACHESOLR_PORT, NULL);
361+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, PANTHEON_APACHESOLR_PORT);
372362
$opts = pantheon_apachesolr_curlopts($opts);
373363
$opts[CURLOPT_HEADER] = 0;
374364

@@ -548,7 +538,7 @@ function pantheon_apachesolr_status($form, &$form_state) {
548538

549539
$url = 'https://'. $host . '/' . $path;
550540

551-
list($ch, $opts) = pantheon_curl_setup($url, NULL, PANTHEON_APACHESOLR_PORT, NULL);
541+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, PANTHEON_APACHESOLR_PORT);
552542

553543
$opts = pantheon_apachesolr_curlopts($opts);
554544
$opts[CURLOPT_CONNECTTIMEOUT] = 5;
@@ -620,7 +610,7 @@ function pantheon_apachesolr_status($form, &$form_state) {
620610
$url = 'https://'. $host . '/' . $path;
621611

622612
$opts = array();
623-
list($ch, $opts) = pantheon_curl_setup($url, NULL, PANTHEON_APACHESOLR_PORT, NULL);
613+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, PANTHEON_APACHESOLR_PORT);
624614

625615
unset($opts[CURLOPT_HTTPHEADER]);
626616
$opts[CURLOPT_CONNECTTIMEOUT] = 5;
@@ -719,7 +709,7 @@ function pantheon_apachesolr_requirements($phase) {
719709

720710
$url = 'https://'. $host . '/' . $path;
721711

722-
list($ch, $opts) = pantheon_curl_setup($url, NULL, PANTHEON_APACHESOLR_PORT, NULL);
712+
list($ch, $opts) = pantheon_apachesolr_curl_setup($url, PANTHEON_APACHESOLR_PORT);
723713
$opts = pantheon_apachesolr_curlopts($opts);
724714

725715

@@ -759,3 +749,39 @@ function pantheon_apachesolr_requirements($phase) {
759749
}
760750
return $requirements;
761751
}
752+
753+
/**
754+
* Initializes a cURL handle with default options, or delegates to Pantheon's
755+
* `pantheon_curl_setup()` if available.
756+
*
757+
* When running on the Pantheon platform, this function uses the pre-configured
758+
* `pantheon_curl_setup()` logic. Otherwise, it sets up a basic cURL handle with
759+
* common defaults for communicating with Apache Solr.
760+
*
761+
* @param string $url The URL to set via CURLOPT_URL.
762+
* @param int $port The port to set via CURLOPT_PORT.
763+
*
764+
* @return array{0: resource, 1: array} An array containing the initialized cURL handle
765+
* and an array of the options applied.
766+
*/
767+
function pantheon_apachesolr_curl_setup($url, $port) {
768+
// If on pantheon, handled in prepend...
769+
if (function_exists("pantheon_curl_setup")) {
770+
return pantheon_curl_setup($url, NULL, $port, NULL);
771+
}
772+
773+
// create a new cURL resource
774+
$ch = curl_init();
775+
776+
// default options for most cases
777+
$opts = array(
778+
CURLOPT_URL => $url,
779+
CURLOPT_HEADER => 1,
780+
CURLOPT_PORT => $port,
781+
CURLOPT_RETURNTRANSFER => 1,
782+
CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'X-Ignore-Agent: 1'),
783+
);
784+
785+
curl_setopt_array($ch, $opts);
786+
return array($ch, $opts);
787+
}

0 commit comments

Comments
 (0)