Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

Redirection

I've researched the archive and the common concensus for a CGI to perform redirection is:

printf("Location: <url>\r\n\r\n");

However, I have not been able to get this to work with either Netscape 4.6 or Internet Explorer 4.0.  Netscape returns a dialogbox with the message "The document contained no data".  IE returns a dialogbox with the message "The server returned an invalid or unrecognized response".

I'm trying to develop a general purpose CGI in C++ that uses an argument from the input string to determine the destination CGI. The overriding reason is to "disable" the browser Back button.  However, I'm first trying to get something to work with a fixed URL.  The code (redirect.exe) for that is as follows:

void
main(int argc, char** argv)
{
  cout << "Location: http://www.halcyon.com/msbinder/links/\r\n\r\n";
}

The URL is my Home Page so I know it exists.

I'm calling the CGI from a start up page with the following URL:

<a href="http://boss7_man/cgi-bin/redirect.exe/add?REDIRECT=add_main%2Eexe"><font size="4"><strong>Start Up (Redirect CGI)</strong></font></a>

'boss7_man' is a Personal Web Server from MS Visual Studio; my development environment.

I've already tested a static page redirection (<meta http-equiv ...>) and that works with the browsers I'm using.
0
msbinder
Asked:
msbinder
1 Solution
 
mattropeCommented:
I think in order for the Location directive to work, you'll still have to generate a Content-type header, even though you don't intend to display anything.  For example:

cout << "Location: www.here.com\n";
cout << "Content-type: text/html\n\n";
return(0);

You're getting the "No Data" error because the web browser doesn't see you start a page, so it's probably ignoring the Location directive.  Hope that helps.
0
 
msbinderAuthor Commented:
I tried several permutations and none of them worked. i.e. as follows:

1)
Location: <url>\r\n
Content-type: text\html\r\n\r\n

Result: same error as before.

2)
Content-type: text\html\r\n\r\n
Location: <url>\r\n

Result: Screen shows "Location:<url>" as expected.

3)
Content-type: text\html\r\n
Location: <url>\r\n

Result: Screen shows "The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
Content-type: text/html
Location: http://www.halcyon.com/msbinder/links/"  as expected (need 2 [cr][lf] after the last directive.

4)
Content-type: text\html\r\n
Location: <url>\r\n\r\n

Result: Same error as before.

------

I put an HTTP-EQUIV=Refresh etc. into the header of an otherwise empty page (without the "Location: ..." directive) and it worked, but that's not the same thing.  NOTE: this redirect effectively disables the 'Back' button using Netscape, but not in IE 4.0, but that's a different issue.

Is there some sort of browser directive needed to make this work?  I rather doubt that due to the responses on this subject that I see here,  but it is a possibility.  I'm working on getting a version of this on my home computer to test there.
0
 
rickhall2000Commented:
You can use a JavaScript to change the location.  I used Perl for when I had to do it, but it is easily adaptable to C++.  In this, $filename had the URL I wanted.  

    print "<SCRIPT LANGUAGE=\"JavaScript\">";
    print "location=\"$filename\";";
    print "</SCRIPT>";

In the end, what the page needs to have in it is:

<SCRIPT LANGUAGE="JavaScript">
location="www.whatever.com";
</SCRIPT>
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
msbinderAuthor Commented:
I'll keep that in mind Rickhall.  To be honest I don't know much about JavaScript, but it's one of those things I have slated to take training on.   Also, JavaScript can be "turned off" on a browser which would probably take away the functionality I'm after.
0
 
krirobCommented:
first send
"Content-type: text/html\n"
to the browser only 1 "\n" then
"Location: www.here.com\n"

and it will work.


0
 
msbinderAuthor Commented:
That's the way it SHOULD work, krirob, but for some reason in my set up it doesn't.  I tested that permutation with the result listed above.  Placing \r\n or only \n doesn't matter for most browsers, and it doesn't matter for mine.  The number and location of \n is important, but the "right" one doesn't work in my environment.  There's something else I don't know about affecting this; probably the MS personal web server I'm using.
0
 
Web_TechCommented:
In perl this is what works.

print "Location: http://www.whereever.com\n\n\n\n";


You can replace the url with a variable but it must include the "http://" or you will get the errors that you are describing..

Web_Tech
0
 
thaxiCommented:
I am assuming you are doing this in C, if so, this is how I have done it successfully on my server.


void main (void)
  {
printf( "Status: 302 Moved Temporarily%c", 10 );
printf( "URI: %s%c", url, 10 );
printf( "Location: %s%c", url, 10 );
printf( "Method: GET%c%c", 10, 10 );
  }


where url is the string that can be passed to this CGI function.


I have used it and it works great with IE 4.01 and Netscape 4.x

-Aashish Thakker <thaxi>
athakker@usc.edu
0
 
thaxiCommented:
You can test to see if it works by going accessing


http://thaxi.usc.edu/cgi-bin/redirect.exe and specifying a URL you wish to redirect to..

e.g to go http://www.yahoo.com simply access

http://thaxi.usc.edu/cgi-bin/redirect.exe?http://www.yahoo.com

The source is exactly the same as above, except for checking to see if a URL was specified.

Let me know of you think my code answered your question.

Thanks,
Aashish <athakker@usc.edu>

-thaxi
0
 
msbinderAuthor Commented:
The problem, thaxi, is apparently not the Location: server directive (There is consistent concensus on the format that is supposed to work), but is likely to be an issue with the server itself.  I'm using the MS personal webserver for this.  What server are you using?  If not the MS personal webserver then perhaps you can check your redirection CGI on that server and let me know what you get.

BTW, I appreciate the feed back I've received so far.  It has at least directed the search toward where my actual problem exists.
0
 
thaxiCommented:
Hi msbinder, I am using a Netscape Enterprise Server and have CGI setup a directory-type. I am not sure I get your comment on the error you are getting. What error ARE you getting? when I run my code on my server, it is working. I am not sure why PWS would not work. If you are able to get other CGI programs working, I would think this should work, though I maybe wrong since I have never worked with Microsoft PWS. BTW the exact source for my program is as follows  :

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

void main (int argc, char *argv[])
  {
       if(argc==1) /* If no URL is specified, output an error message to user */
            {
             printf( "Content-type: text/html\n\n" ); /* Outut MIME Header*/
             printf( "<HTML>\n" );
             printf( "<HEAD>\n" );
             printf( "<TITLE>CGI Process Output</TITLE>\n" );
             printf( "No URL specified, cannot redirect to a blank URL\n");
             printf( "</HEAD>\n" );
             printf( "<BODY>\n" );
             exit(1); /*As there is no URL specified, we cannot redirect, hence exit */
            }
       /* Output following code to redirect client browser to specified URL */
       printf( "Status: 302 Moved Temporarily%c", 10 );
       printf( "URI: %s%c", argv[1], 10 );
       printf( "Location: %s%c", argv[1], 10 );
       printf( "Method: GET%c%c", 10, 10 );
  }



Let me know of this is any help at all...

-thaxi
0
 
thaxiCommented:
I am wondering if my code helped you? do let me know.

-thaxi
0
 
msbinderAuthor Commented:
Thanks thaxi, your code is very similar to mine.  It shouldn't matter which webserver is being used, but since that is where the CGI is being implemented, then it naturally follows that the problem is there since the same code works on other servers.  I haven't had a chance, recently, to investigate my MS PWS setup.  I'll be sure to post here if I DO find anything out.
0
 
thaxiCommented:
Hi msbinder,

I was wondering if you got the chance to take a look at your MS PWS config? I installed PWS on a 98 SE machone and created a virtual directory cgi-bin with only execute permissions. I placed my executable of the above code in this directory and it worked right-away!!
Let me know what sort of a configuration you have.
0
 
thaxiCommented:
Did it work for you?? if it did, maybe you want to assign your points to the person who answered your question....
0
 
thaxiCommented:
Is there an admin on here who oversees things? I dont seem to find anyone responding to this?
0
 
darinwCommented:
Moving question to PAQ.

darinw
Community Support
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now