request_uri() hack for tunelled connections

anisotropic - April 2, 2005 - 00:25
Project:Drupal
Version:5.1
Component:base system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

I run a drupal instance at work on our internal network as a FAQ tool, and had problems using the site while using ssh tunnels from the outside. I tracked this down to a problem with request_uri() where the following case was not accounted for:

in $_SERVER

HTTP_HOST is something like this:
hostname:8080

because this is the port the client is using and passing along,
but SERVER_PORT is '80' and REQUEST_URI is just something like /drupal/?q=support

the following is a quick hack to fix this, intended not as a patch, but just to illutstrate an edge-case and a way to work around it. There are no doubt better ways to do this. =)

the quick hack:

<?php
function request_uri() {
 
// echo "<code>&quot;.print_r($_SERVER, 1).&quot;</code>\n"; debugging? bah!

 
if(isset($_SERVER['HTTP_HOST'])) {
   
$host = explode(':', $_SERVER['HTTP_HOST']);
  }

  if(
$host[1] != $_SERVER['SERVER_PORT']) {
   
// boom, we're tunnelled, better supply an entire url
   
$uri = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

  } else {
  if (isset(
$_SERVER['REQUEST_URI'])) {
   
$uri = $_SERVER['REQUEST_URI'];
  }
  else {
    if (isset(
$_SERVER['argv'])) {
     
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
    }
    else {
          
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
          }
      }
  }
  return
check_url($uri);
}
?>

#1

anisotropic - April 19, 2005 - 22:19
Version:<none>» 4.6.0

I forgot to mention this will only work if you also hack conf.php / settings.php:

// hack for tunneling
$arr = explode("/", $_SERVER['PHP_SELF']);
$arr = array_slice($arr, 0, -1);
$app_base = implode("/", $arr);

$base_url = "http://".$_SERVER['HTTP_HOST'] . $app_base;

#2

killes@www.drop.org - August 13, 2005 - 21:29
Category:bug report» feature request

not a bug.

#3

magico - August 15, 2006 - 17:11
Version:4.6.0» x.y.z

#4

mstevenson - February 13, 2007 - 23:01
Version:x.y.z» 5.1

Could someone please post how to get this to work for 5.1? I really need to get this working from inside an SSH tunnel. With this hack, does it matter what port you use as your local port for the tunnel?

 
 

Drupal is a registered trademark of Dries Buytaert.