Forum Moderators: coopster & phranque

Message Too Old, No Replies

Redirection syntax in Perl

It works but I don't know why.

         

ThomasAJ

9:46 am on Sep 6, 2002 (gmt 0)

10+ Year Member



I'm a newbie at Perl and CGI!

I looked around and found this code to redirect on a 'how to' website:

print "Location: $URL ";

It would not work for me at all. Got 'Internal Server error..."

I looked at another web site and found:

print "Location: $URL\n\n";

I added the \n\n and it worked!

I looked at the print function documentation at perl.com and there was nothing about the \n.

Perhaps it's about Perl in the context of CGI documentation that I need to find?

Any pointers as to what I don't understand.

Thanks

Tom

andreasfriedrich

12:24 pm on Sep 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any HTTP header must end with two newline characters. Thatīs what the two \n do.

andreasfriedrich

12:37 pm on Sep 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both types (requests, responses) of message consist of a start-line, zero 
or more header fields (also known as "headers"), an empty line (i.e.,
a line with nothing preceding the CRLF) indicating the end of the
header fields, and possibly a message-body.

generic-message = start-line
*(message-header CRLF)
CRLF
[ message-body ]
start-line = Request-Line Ķ Status-Line

HTTP/1.1 - 4.1 Message Types [ietf.org]

See for an explanation of quoting operators and escape sequences [perldoc.com].

Itīs not in the documentation for print [perldoc.com] since it would be wrong there. All print does is output a string or list of strings to the specified filehandle or to STDOUT. You would need to look up the docs for strings to find out about escape sequences supported in strings. But since this depends on the quoting operator used, it is documented there [perldoc.com].

Robber

1:00 pm on Sep 6, 2002 (gmt 0)

10+ Year Member



Thats it really, nothing else to add really, except to say you might have come across this with a different header type more frequently. When your script outputs html to the browser you have

print "Content-type: text/html\n\n";

The redirect is just a different header type but t still needs the 2 line breaks (\n).

ThomasAJ

11:53 pm on Sep 6, 2002 (gmt 0)

10+ Year Member



Thanks for that.

I wonder why the original 'how to' site didn't test the code?
Rhetorical!

Tom

andreasfriedrich

12:20 am on Sep 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



laziness, impatience, and hubris

sometimes people get to lazy.

I know I shouldnīt have answered your rethorical question. Just my 2 cents

ThomasAJ

2:23 am on Sep 7, 2002 (gmt 0)

10+ Year Member



andreasfriedrich

Thanks, but I could not figure out what a start-line is, nor was it used in the how-tos. At least not that I noticed.

Another quick and last (hopefully) question.

The not so good how-to site had the exit() function as the last executable in the redirect script.

Is this necessary to close the script and/or clean-up the 'environment'.

Tom

Robber

8:43 am on Sep 7, 2002 (gmt 0)

10+ Year Member



If there is nothing else in the script I wouldnt have though it necessary, otherwise it might be a good idea to have it in there. However, I'd be interested to hear others opinions here as well.

andreasfriedrich

12:46 pm on Sep 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I could not figure out what a start-line is

Itīs all in the little quote from the HTTP spec.

Disregard the underscore in the following text, itīs just used for alignment.

generic-message = start-line 
__________________*(message-header CRLF)
__________________CRLF
__________________[ message-body ]

start-line _____= Request-Line Ķ Status-Line

Perhaps it is clearer now that the alignment is ok.

A generic message in http (such as the request by the browser and the reply by the server) consists of a start-line, zero or more header fields (also known as "headers"), an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.

Start-line is defined as either Request-Line or Status-Line.

A Request-Line is something like

GET / HTTP/1.1
or
HEAD / HTTP/1.1

A Status-Line is the first line of a response header and looks like

HTTP/1.1 200 OK
or
HTTP/1.1 400 Bad Request

As for the question about the exit have a look at the following quote:

The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

From that follows that your script should output some code like


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://server/">here</A>.<P>
<HR>
<ADDRESS>Apache/1.3.26 Server at server Port 80</ADDRESS>
</BODY></HTML>

and then it should stop. So if your script might output anything else or would normally run for some time it is advisable to exit it explicitly.