简体   繁体   English

Perl重定向浏览器并继续处理

[英]Perl redirect browser and keep processing

This works fine (redirecting to a different domain): 这工作正常(重定向到不同的域):

#! /usr/bin/perl 
print "Location:http://AnyDomainBesidesMyOwn.com/\n\n"; 
close(STDOUT); close(STDIN); close(STDERR); 
[some long process]

But the following way stalls the browser, refusing to redirect until the long process is finished. 但是以下方式会阻止浏览器,拒绝重定向直到长进程完成。 The only change from above is that I'm redirecting to another page on the same domain the script is running from. 上面唯一的变化是我正在重定向到运行脚本的同一域上的另一个页面。

#! /usr/bin/perl 
print "Location:http://MyOwnDomain.com/\n\n"; 
close(STDOUT); close(STDIN); close(STDERR); 
[some long process]

I know I can get this to work by forking a new process, but there's got to be a simpler way...right? 我知道我可以通过分配一个新流程来实现这个目标,但必须有一个更简单的方法......对吗?

UPDATE: Here is the output I get from Live Headers in Firefox: 更新:这是我从Firefox中的Live Headers获得的输出:

http://example.com/test3.cgi 

GET /test3.cgi HTTP/1.1 
Host: example.com 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:18.0) Gecko/20100101 Firefox/18.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 

HTTP/1.1 302 Found 
Date: Sun, 27 Jan 2013 23:31:49 GMT 
Server: Apache 
Location: http://example.com/ 
Vary: Accept-Encoding 
Content-Encoding: gzip 
Content-Length: 187 
Keep-Alive: timeout=2, max=100 
Connection: Keep-Alive 
Content-Type: text/html; charset=iso-8859-1

And then after it deigns to redirect: 然后它决定重定向:

http: //example.com/ [Have to include the space b/c stackoverflow limits the number of links I can include in a post]

GET / HTTP/1.1 
Host: example.com 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:18.0) Gecko/20100101 Firefox/18.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 

HTTP/1.1 200 OK 
Date: Sun, 27 Jan 2013 23:31:54 GMT 
Server: Apache 
Accept-Ranges: bytes 
X-Mod-Pagespeed: 0.10.21.2-1381 [Same problem even on a domain w/o pagespeed installed]
Vary: Accept-Encoding 
Content-Encoding: gzip 
MS-Author-Via: DAV 
Cache-Control: max-age=0, no-cache 
Content-Length: 12189 
Keep-Alive: timeout=2, max=99 
Connection: Keep-Alive 
Content-Type: text/html

I believe I have found a solution after some trial and error. 我相信经过一些试验和错误后我找到了一个解决方案。 The key is to use fork() and exit the parent process. 关键是使用fork()并退出父进程。 Then close all the file handles in the child process: 然后关闭子进程中的所有文件句柄:

#!/usr/bin/perl

print "Location: ../myHomePage.htm\n\n"; 

exit 0 if fork();
close(STDIN);
close(STDOUT);
close(STDERR);

# do some long process
open(SM, "| /usr/sbin/sendmail -ti -odq); 

⋮ 

In my case, the mail program was taking several seconds to accept the message and queue it. 就我而言,邮件程序需要几秒钟才能接受邮件并将其排队。 The webpage seemed very sluggish. 网页似乎非常迟缓。 After implementing the above, the redirect happenes instantly and several seconds later the message appeares in the mail log. 实现上述操作后,重定向立即发生,几秒钟后消息将显示在邮件日志中。

You should exit after a redirect header sent out. 您应该在发送重定向标头后退出。 If I am correct a space is needed after 'Location:'. 如果我是正确的,在“位置:”之后需要一个空格。 You could use CGI redirect method to achive this. 您可以使用CGI重定向方法来实现此目的。

  #!/usr/bin/perl
  print "Location: http://MyOwnDomain.com/\n\n"; 
  close(STDOUT); close(STDIN); close(STDERR); 
  exit 0;

Redirect with CGI: http://perldoc.perl.org/CGI.html#GENERATING-A-REDIRECTION-HEADER 使用CGI重定向: http//perldoc.perl.org/CGI.html#GENERATING-A-REDIRECTION-HEADER

  use CGI; # load CGI routines
  $q = CGI->new; 
  print $q->redirect('http://somewhere.else/in/movie/land');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM