r53414 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53413‎ | r53414 | r53415 >
Date:19:06, 17 July 2009
Author:dale
Status:deferred
Tags:
Comment:
fixed php based request path (uses fopen instead of curl) r53282 c3232
Modified paths:
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/HttpFunctions.php
@@ -255,7 +255,9 @@
256256
257257 function __construct( $url, $opt ){
258258 global $wgSyncHTTPTimeout;
 259+ //double check its a valid url:
259260 $this->url = $url;
 261+
260262 // set the timeout to default sync timeout (unless the timeout option is provided)
261263 $this->timeout = ( isset( $opt['timeout'] ) ) ? $opt['timeout'] : $wgSyncHTTPTimeout;
262264 $this->method = ( isset( $opt['method'] ) ) ? $opt['method'] : 'GET';
@@ -272,6 +274,16 @@
273275 * 'adapter' => 'curl', 'soket'
274276 */
275277 public function doRequest() {
 278+
 279+ #make sure we have a valid url
 280+ if( !UploadFromUrl::isValidURI( $this->url ) )
 281+ return Status::newFatal('bad-url');
 282+
 283+ #check for php.ini allow_url_fopen
 284+ if( ini_get('allow_url_fopen') == 0){
 285+ return Status::newFatal('allow_url_fopen needs to be enabled for http copy to work');
 286+ }
 287+
276288 # Use curl if available
277289 if ( function_exists( 'curl_init' ) ) {
278290 return $this->doCurlReq();
@@ -318,6 +330,7 @@
319331 if( !$cwrite->status->isOK() ){
320332 wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" );
321333 $status = $cwrite->status;
 334+ return $status;
322335 }
323336 curl_setopt( $c, CURLOPT_WRITEFUNCTION, array( $cwrite, 'callbackWriteBody' ) );
324337 }
@@ -369,24 +382,58 @@
370383 }
371384
372385 public function doPhpReq(){
373 - #$use file_get_contents...
374 - # This doesn't have local fetch capabilities...
 386+ global $wgTitle, $wgHTTPProxy;
 387+ //start with good status:
 388+ $status = Status::newGood();
375389
 390+ //setup the headers
376391 $headers = array( "User-Agent: " . Http :: userAgent() );
377 - if( strcasecmp( $method, 'post' ) == 0 ) {
 392+ if ( is_object( $wgTitle ) ) {
 393+ $headers[] = "Referer: ". $wgTitle->getFullURL();
 394+ }
 395+
 396+ if( strcasecmp( $this->method, 'post' ) == 0 ) {
378397 // Required for HTTP 1.0 POSTs
379398 $headers[] = "Content-Length: 0";
380399 }
381 - $opts = array(
 400+ $fcontext = stream_context_create ( array(
382401 'http' => array(
383 - 'method' => $method,
 402+ 'method' => $this->method,
384403 'header' => implode( "\r\n", $headers ),
385 - 'timeout' => $timeout ) );
386 - $ctx = stream_context_create( $opts );
 404+ 'timeout' => $this->timeout )
 405+ )
 406+ );
387407
388 - $status = new Status;
389 - $status->value = file_get_contents( $url, false, $ctx );
390 - if( !$status->value ){
 408+ $fh = fopen( $this->url, "r", false, $fcontext);
 409+
 410+ // set the write back function (if we are writing to a file)
 411+ if( $this->target_file_path ){
 412+ $cwrite = new simpleFileWriter( $this->target_file_path, $this->upload_session_key );
 413+ if( !$cwrite->status->isOK() ){
 414+ wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" );
 415+ $status = $cwrite->status;
 416+ return $status;
 417+ }
 418+ //read $fh into the simpleFileWriter (grab in 64K chunks since its likely a media file)
 419+ while ( !feof( $fh )) {
 420+ $contents = fread($fh, 65536);
 421+ $cwrite->callbackWriteBody($fh, $contents );
 422+ }
 423+
 424+ $cwrite->close();
 425+ //check for simpleFileWriter error:
 426+ if( !$cwrite->status->isOK() ){
 427+ return $cwrite->status;
 428+ }
 429+ }else{
 430+ //read $fh into status->value
 431+ $status->value = @stream_get_contents( $fh );
 432+ }
 433+ //close the url file wrapper
 434+ fclose( $fh );
 435+
 436+ //check for "false"
 437+ if( $status->value === false ){
391438 $status->error( 'file_get_contents-failed' );
392439 }
393440 return $status;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r53282here it is ... the upload-api, script-server, js2 (javascript phase2) branch ...dale23:52, 14 July 2009

Status & tagging log