Skip to content

Commit 7c3d539

Browse files
committed
fix(requests): update Requests class and certificate path
1 parent 371ffb7 commit 7c3d539

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

php/EE/Bootstrap/IncludeFrameworkAutoloader.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function process( BootstrapState $state ) {
3434
$mappings = [
3535
'EE' => EE_ROOT . '/php/EE',
3636
'cli' => EE_VENDOR_DIR . '/wp-cli/php-cli-tools/lib/cli',
37-
'Requests' => EE_VENDOR_DIR . '/rmccue/requests/library/Requests',
37+
'WpOrg\\Requests' => EE_VENDOR_DIR . '/rmccue/requests/src', // New PSR-4 mapping
3838
'Symfony\Component\Finder' => EE_VENDOR_DIR . '/symfony/finder/',
3939
'Psr\Log' => EE_VENDOR_DIR . '/psr/log/Psr/Log/',
4040
'Monolog' => EE_VENDOR_DIR . '/monolog/monolog/src/Monolog',
@@ -47,7 +47,6 @@ public function process( BootstrapState $state ) {
4747
);
4848
}
4949

50-
include_once EE_VENDOR_DIR . '/rmccue/requests/library/Requests.php';
5150
include_once EE_VENDOR_DIR . '/wp-cli/mustangostang-spyc/Spyc.php';
5251

5352
$autoloader->register();

php/utils.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,13 @@ function replace_path_consts( $source, $path ) {
492492
* @param string $method HTTP method (GET, POST, DELETE, etc.)
493493
* @param string $url URL to make the HTTP request to.
494494
* @param array $headers Add specific headers to the request.
495+
* @param array $data
495496
* @param array $options
496497
* @return object
497498
*/
498499
function http_request( $method, $url, $data = null, $headers = array(), $options = array() ) {
499500

500-
$cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem';
501+
$cert_path = '/rmccue/requests/certificates/cacert.pem';
501502
$halt_on_error = ! isset( $options['halt_on_error'] ) || (bool) $options['halt_on_error'];
502503
if ( inside_phar() ) {
503504
// cURL can't read Phar archives
@@ -521,23 +522,31 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
521522
}
522523

523524
try {
524-
return \Requests::request( $url, $headers, $data, $method, $options );
525-
} catch ( \Requests_Exception $ex ) {
526-
// CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7.
527-
if ( 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) {
528-
$error_msg = sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() );
529-
if ( $halt_on_error ) {
530-
EE::error( $error_msg );
531-
}
532-
throw new \RuntimeException( $error_msg, null, $ex );
533-
}
525+
// Updated class name: \WpOrg\Requests\Requests
526+
$response = \WpOrg\Requests\Requests::request( $url, $headers, $data, $method, $options );
527+
return $response;
528+
} catch ( \WpOrg\Requests\Exception $ex ) { // Updated exception class name
529+
534530
// Handle SSL certificate issues gracefully
535-
\EE::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) );
536-
$options['verify'] = false;
537-
try {
538-
return \Requests::request( $url, $headers, $data, $method, $options );
539-
} catch ( \Requests_Exception $ex ) {
540-
$error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() );
531+
$curl_error = false;
532+
if ( $ex instanceof \WpOrg\Requests\Exception\Transport\Curl ) {
533+
$curl_error = true;
534+
}
535+
536+
if ( $curl_error && in_array( $ex->getCode(), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) {
537+
\EE::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) );
538+
$options['verify'] = false;
539+
try {
540+
return \WpOrg\Requests\Requests::request( $url, $headers, $data, $method, $options );
541+
} catch ( \WpOrg\Requests\Exception $ex ) {
542+
$error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() );
543+
if ( $halt_on_error ) {
544+
EE::error( $error_msg );
545+
}
546+
throw new \RuntimeException( $error_msg, null, $ex );
547+
}
548+
} else {
549+
$error_msg = sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() );
541550
if ( $halt_on_error ) {
542551
EE::error( $error_msg );
543552
}

tests/test-utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public function testHttpRequestBadCAcert() {
383383

384384
// Hack to create bad CAcert, using Utils\get_vendor_paths() preference for a path as part of a Composer-installed larger project.
385385
$vendor_dir = EE_ROOT . '/../../../vendor';
386-
$cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem';
386+
$cert_path = '/rmccue/requests/certificates/cacert.pem';
387387
$bad_cacert_path = $vendor_dir . $cert_path;
388388
if ( ! file_exists( $bad_cacert_path ) ) {
389389
// Capture any directories created so can clean up.

0 commit comments

Comments
 (0)