r82451 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82450‎ | r82451 | r82452 >
Date:13:09, 19 February 2011
Author:ialex
Status:resolved
Tags:
Comment:
Merged retrieval of request URL and header from WebRequest and Setup.php to avoid duplicate code and reduced the number of wfDebug() calls in Setup.php.
The only side effect is that headers name are in uppercase in the debug log.
Modified paths:
  • /trunk/phase3/includes/Setup.php (modified) (history)
  • /trunk/phase3/includes/WebRequest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Setup.php
@@ -308,37 +308,16 @@
309309 if ( $wgCommandLineMode ) {
310310 wfDebug( "\n\nStart command line script $self\n" );
311311 } else {
312 - wfDebug( "Start request\n\n" );
313 - # Output the REQUEST_URI. This is not supported by IIS in rewrite mode,
314 - # so use an alternative
315 - if ( isset( $_SERVER['REQUEST_URI'] ) ) {
316 - $requestUri = $_SERVER['REQUEST_URI'];
317 - } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
318 - $requestUri = $_SERVER['HTTP_X_ORIGINAL_URL'];
319 - } else {
320 - $requestUri = $_SERVER['PHP_SELF'];
321 - }
 312+ $debug = "Start request\n\n{$_SERVER['REQUEST_METHOD']} {$wgRequest->getRequestURL()}";
322313
323 - wfDebug( "{$_SERVER['REQUEST_METHOD']} {$requestUri}\n" );
324 -
325314 if ( $wgDebugPrintHttpHeaders ) {
326 - $headerOut = "HTTP HEADERS:\n";
 315+ $debug .= "\nHTTP HEADERS:\n";
327316
328 - if ( function_exists( 'getallheaders' ) ) {
329 - $headers = getallheaders();
330 - foreach ( $headers as $name => $value ) {
331 - $headerOut .= "$name: $value\n";
332 - }
333 - } else {
334 - $headers = $_SERVER;
335 - foreach ( $headers as $name => $value ) {
336 - if ( substr( $name, 0, 5 ) !== 'HTTP_' ) continue;
337 - $name = substr( $name, 5 );
338 - $headerOut .= "$name: $value\n";
339 - }
 317+ foreach ( $wgRequest->getAllHeaders() as $name => $value ) {
 318+ $debug .= "$name: $value\n";
340319 }
341 - wfDebug( "$headerOut\n" );
342320 }
 321+ wfDebug( "$debug\n" );
343322 }
344323
345324 wfProfileOut( $fname . '-misc1' );
Index: trunk/phase3/includes/WebRequest.php
@@ -492,10 +492,12 @@
493493 * @return String
494494 */
495495 public function getRequestURL() {
496 - if( isset( $_SERVER['REQUEST_URI']) && strlen($_SERVER['REQUEST_URI']) ) {
 496+ if( isset( $_SERVER['REQUEST_URI'] ) && strlen( $_SERVER['REQUEST_URI'] ) ) {
497497 $base = $_SERVER['REQUEST_URI'];
 498+ } elseif ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) && strlen( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
 499+ // Probably IIS; doesn't set REQUEST_URI
 500+ $base = $_SERVER['HTTP_X_ORIGINAL_URL'];
498501 } elseif( isset( $_SERVER['SCRIPT_NAME'] ) ) {
499 - // Probably IIS; doesn't set REQUEST_URI
500502 $base = $_SERVER['SCRIPT_NAME'];
501503 if( isset( $_SERVER['QUERY_STRING'] ) && $_SERVER['QUERY_STRING'] != '' ) {
502504 $base .= '?' . $_SERVER['QUERY_STRING'];
@@ -503,8 +505,8 @@
504506 } else {
505507 // This shouldn't happen!
506508 throw new MWException( "Web server doesn't provide either " .
507 - "REQUEST_URI or SCRIPT_NAME. Report details of your " .
508 - "web server configuration to http://bugzilla.wikimedia.org/" );
 509+ "REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
 510+ "of your web server configuration to http://bugzilla.wikimedia.org/" );
509511 }
510512 // User-agents should not send a fragment with the URI, but
511513 // if they do, and the web server passes it on to us, we
@@ -701,32 +703,50 @@
702704 }
703705
704706 /**
 707+ * Initialise the header list
 708+ */
 709+ private function initHeaders() {
 710+ if ( count( $this->headers ) ) {
 711+ return;
 712+ }
 713+
 714+ if ( function_exists( 'apache_request_headers' ) ) {
 715+ foreach ( apache_request_headers() as $tempName => $tempValue ) {
 716+ $this->headers[ strtoupper( $tempName ) ] = $tempValue;
 717+ }
 718+ } else {
 719+ $headers = $_SERVER;
 720+ foreach ( $_SERVER as $name => $value ) {
 721+ if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
 722+ $name = str_replace( '_', '-', substr( $name, 5 ) );
 723+ $this->headers[$name] = $value;
 724+ } elseif ( $name === 'CONTENT_LENGTH' ) {
 725+ $this->headers['CONTENT-LENGTH'] = $value;
 726+ }
 727+ }
 728+ }
 729+ }
 730+
 731+ /**
 732+ * Get an array containing all request headers
 733+ *
 734+ * @return Array mapping header name to its value
 735+ */
 736+ public function getAllHeaders() {
 737+ $this->initHeaders();
 738+ return $this->headers;
 739+ }
 740+
 741+ /**
705742 * Get a request header, or false if it isn't set
706743 * @param $name String: case-insensitive header name
707744 */
708745 public function getHeader( $name ) {
709746 $name = strtoupper( $name );
710 - if ( function_exists( 'apache_request_headers' ) ) {
711 - if ( !$this->headers ) {
712 - foreach ( apache_request_headers() as $tempName => $tempValue ) {
713 - $this->headers[ strtoupper( $tempName ) ] = $tempValue;
714 - }
715 - }
716 - if ( isset( $this->headers[$name] ) ) {
717 - return $this->headers[$name];
718 - } else {
719 - return false;
720 - }
 747+ if ( isset( $this->headers[$name] ) ) {
 748+ return $this->headers[$name];
721749 } else {
722 - $name = 'HTTP_' . str_replace( '-', '_', $name );
723 - if ( $name === 'HTTP_CONTENT_LENGTH' && !isset( $_SERVER[$name] ) ) {
724 - $name = 'CONTENT_LENGTH';
725 - }
726 - if ( isset( $_SERVER[$name] ) ) {
727 - return $_SERVER[$name];
728 - } else {
729 - return false;
730 - }
 750+ return false;
731751 }
732752 }
733753

Follow-up revisions

RevisionCommit summaryAuthorDate
r86043Fix regression introduced in r82451. Only happens when getAllHeaders was not ...nikerabbit11:58, 14 April 2011

Status & tagging log