Skip to content

Commit e0fadb9

Browse files
authored
Merge pull request #3 from toban/main
Add host headers to request methods in
2 parents 65cd6df + 7a91d8d commit e0fadb9

4 files changed

Lines changed: 29 additions & 23 deletions

File tree

classes/Common.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?PHP
22

33
namespace Toolforge ;
4+
use WbstackMagnusOauth;
45

56
# declare(strict_types=1); # PHP7
67

@@ -359,7 +360,7 @@ public function getMultipleURLsInParallel ( $urls , $batch_size = 50 ) {
359360
if ( count($batches[count($batches)-1]) >= $batch_size ) $batches[] = [] ;
360361
$batches[count($batches)-1][$k] = $v ;
361362
}
362-
363+
363364
foreach ( $batches AS $batch_urls ) {
364365

365366
$mh = curl_multi_init();
@@ -374,6 +375,9 @@ public function getMultipleURLsInParallel ( $urls , $batch_size = 50 ) {
374375
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
375376
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
376377
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);
378+
379+
WbstackMagnusOauth::setCurlHttpHeaders( $ch[$key] );
380+
377381
curl_multi_add_handle($mh,$ch[$key]);
378382
}
379383

@@ -389,7 +393,7 @@ public function getMultipleURLsInParallel ( $urls , $batch_size = 50 ) {
389393

390394
curl_multi_close($mh);
391395
}
392-
396+
393397
return $ret ;
394398
}
395399

public_html/php/WbstackMagnusOauth.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,21 @@ public static function getSite(
223223
return $site;
224224
}
225225

226+
/**
227+
* Set the HTTP Headers for the curl handle
228+
*
229+
* Sets the HOST parameter when internally talking to wbstack platform ingress
230+
*
231+
*/
232+
public static function setCurlHttpHeaders( $curlHandle, $headers = [] ) {
233+
234+
if( WbstackMagnusOauth::isLocalHost() ) {
235+
$domain = $_SERVER['SERVER_NAME'];
236+
$headers[] = 'HOST: ' . $domain;
237+
}
238+
239+
if( !empty($headers) ) {
240+
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
241+
}
242+
}
226243
}

public_html/php/oauth.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function fetchAccessToken() {
151151
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
152152

153153
curl_setopt( $ch, CURLOPT_HEADER, 0 );
154-
$this->setCurlHttpHeaders( $ch );
154+
WbstackMagnusOauth::setCurlHttpHeaders( $ch );
155155

156156
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
157157
$data = curl_exec( $ch );
@@ -242,24 +242,6 @@ function sign_request( $method, $url, $params = [] ) {
242242
return base64_encode( hash_hmac( 'sha1', $toSign, $key, true ) );
243243
}
244244

245-
/**
246-
* Set the HTTP Headers for the curl handle
247-
*
248-
* Sets the HOST parameter when internally talking to wbstack platform ingress
249-
*
250-
*/
251-
function setCurlHttpHeaders( $curlHandle, $headers = [] ) {
252-
253-
if( WbstackMagnusOauth::isLocalHost() ) {
254-
$domain = $_SERVER['SERVER_NAME'];
255-
$headers[] = 'HOST: ' . $domain;
256-
}
257-
258-
if( !empty($headers) ) {
259-
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
260-
}
261-
}
262-
263245
/**
264246
* Request authorization
265247
* @return void
@@ -292,7 +274,7 @@ function doAuthorizationRedirect($callback='') {
292274
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
293275
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
294276
curl_setopt( $ch, CURLOPT_HEADER, 0 );
295-
$this->setCurlHttpHeaders( $ch );
277+
WbstackMagnusOauth::setCurlHttpHeaders( $ch );
296278
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
297279
$data = curl_exec( $ch );
298280
if ( !$data ) {
@@ -509,7 +491,7 @@ function doApiQuery( $post, &$ch = null , $mode = '' , $iterations_left = 5 , $l
509491
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
510492

511493
curl_setopt( $ch, CURLOPT_HEADER, 0 );
512-
$this->setCurlHttpHeaders( $ch, [ $header ] );
494+
WbstackMagnusOauth::setCurlHttpHeaders( $ch, [ $header ] );
513495

514496
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
515497

public_html/php/wikidata.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ protected function getMultipleURLsInParallel ( $urls ) {
180180
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
181181
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
182182
curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);
183+
184+
WbstackMagnusOauth::setCurlHttpHeaders( $ch[$key] );
185+
183186
curl_multi_add_handle($mh,$ch[$key]);
184187
}
185188

0 commit comments

Comments
 (0)