Redirects: relative vs absolute

2003-02-24 Thread Grant McLean

I've seen a number of code examples for redirects which output a
root-relative URI in the Location header. Eg:

  Location: /images/item1.gif


Although browsers seem to accept this and do 'the right thing', the 
HTTP RFC seems to be pretty clear that the Location header must be 
an absolute URI.  Am I reading it wrong?

Is there an easy way to get the absolute URI of the current request 
to use with Apache::URI-parse to translate a relative URI to 
absolute? 

I have managed to get the hostname, port number and path components
individually from the request object and paste them together but 
it's all a bit messy.

Regards
Grant


Re: Redirects: relative vs absolute

2003-02-24 Thread Randal L. Schwartz
 Grant == Grant McLean [EMAIL PROTECTED] writes:

Grant I've seen a number of code examples for redirects which output a
Grant root-relative URI in the Location header. Eg:

Grant   Location: /images/item1.gif

If that's coming out from a CGI or Apache::Registry script, the
browser never sees it, because it's handled as an internal redirect.
That can be messy if it's an HTML page with relative links, as the
browser will fetch the links relative to the *old* URL, not the new
URL.  It's also messy if you meant to keep that URL private, but
something breaks as a result, and the URL gets publicized in an error
message.

Now, you may be talking about a response header that's not related to
CGI or Apache::Registry, in which case this doesn't apply.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
[EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


Re: Redirects: relative vs absolute

2003-02-24 Thread Mithun Bhattacharya

--- Grant McLean [EMAIL PROTECTED] wrote:

 Is there an easy way to get the absolute URI of the current request 
 to use with Apache::URI-parse to translate a relative URI to 
 absolute? 

From perldoc URI

-
$uri = URI#8722;new_abs( $str, $base_uri )
 This constructs a new absolute URI object.  The $str argument can
 denote a relative or absolute URI.  If relative, then it will be
 absolutized using $base_uri as base. The $base_uri must be an
abso#8208;
 lute URI.
-



Regards
Mithun

__
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/


Re: Redirects: relative vs absolute

2003-02-24 Thread Geoffrey Young
Grant McLean wrote:
I've seen a number of code examples for redirects which output a
root-relative URI in the Location header. Eg:
  Location: /images/item1.gif

Although browsers seem to accept this and do 'the right thing', the 
HTTP RFC seems to be pretty clear that the Location header must be 
an absolute URI.  Am I reading it wrong?
no.

Is there an easy way to get the absolute URI of the current request 
to use with Apache::URI-parse to translate a relative URI to 
absolute? 

I have managed to get the hostname, port number and path components
individually from the request object and paste them together but 
it's all a bit messy.
to generate a URI that points to the same server as the current request it's 
best to use Apache::URI methods, which are pretty easy.

  my $uri = Apache::URI-parse($r);
  $uri-path('/some/new/document.html');
  my $absolute_uri = $uri-unparse;
note that calling path() (or any other method) is optional - you don't need 
to change anything in order to unparse() the URI and get an absolute URI back.

Apache::URI::parse() can take an optional second argument, which it will 
then parse and use as the base for the URI (in place of the current request).

HTH

--Geoff



RE: Redirects: relative vs absolute

2003-02-24 Thread Grant McLean
Geoffrey Young wrote:
 Grant McLean wrote:
  Is there an easy way to get the absolute URI of the current request 
  to use with Apache::URI-parse to translate a relative URI to 
  absolute? 
 to generate a URI that points to the same server as the 
 current request it's best to use Apache::URI methods, which are
 pretty easy.
 
my $uri = Apache::URI-parse($r);
$uri-path('/some/new/document.html');
my $absolute_uri = $uri-unparse;
 
 note that calling path() (or any other method) is optional - 
 you don't need  to change anything in order to unparse() the 
 URI and get an absolute URI back.

Excellent, I knew there must be an easier way than what I was doing.

Regards
Grant


Re: Redirects: relative vs absolute

2003-02-24 Thread Stas Bekman
Geoffrey Young wrote:

to generate a URI that points to the same server as the current request 
it's best to use Apache::URI methods, which are pretty easy.

  my $uri = Apache::URI-parse($r);
  $uri-path('/some/new/document.html');
  my $absolute_uri = $uri-unparse;
note that calling path() (or any other method) is optional - you don't 
need to change anything in order to unparse() the URI and get an 
absolute URI back.
So basically it's:

my $absolute_uri = Apache::URI-parse($r)-unparse;

So just a heads up for mp2, there is a new method to do that:

use Apache::URI ();
my $absolute_uri = $r-construct_url();
__
Stas BekmanJAm_pH -- Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide --- http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com