Jaap has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

How can a connection to the browser be closed after a perl/cgi script has outputted some XML?
The script needs to do some more work but the browser won't start rendering the XML if the connection is not closed.

Replies are listed 'Best First'.
Re: How to close connection to browser
by chromatic (Archbishop) on Feb 25, 2003 at 18:32 UTC

    Close STDOUT.

Re: How to close connection to browser
by Coruscate (Sexton) on Feb 25, 2003 at 19:04 UTC

    You may also need to close STDERR. When I created a fork()ing script where the child needed to go off and continue working, I also had to close STDERR off. :)


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.

Re: How to close connection to browser
by cees (Curate) on Feb 25, 2003 at 20:21 UTC

    I know you mentioned that you are using perl/cgi, but if you are running this under mod_perl, then you can use the mod_perl hooks to register a cleanup handler to do your post request processing. This way Apache will close the connection for you , and then afterwards execute your cleanup handler for you.

    If you are using the mod_perl Apache::Registry module to run your cgi/perl script, then you could add something like this to your code:

    my $r = Apache->request(); my $sub = sub { # Do your post request processing here }; $r->register_cleanup($sub);

    Of course, your code will be dependant on mod_perl at this point and it won't run as a plain old CGI anymore, but I thought it was worth noting as an option.

Re: How to close connection to browser
by jacques (Priest) on Feb 25, 2003 at 19:16 UTC
    Couldn't you just flush the buffer?
Re: How to close connection to browser
by Abigail-II (Bishop) on Feb 26, 2003 at 12:31 UTC
    First of all, this isn't a Perl question. It's a CGI question; both the question and the answer are language independent.

    Second, it's not the CGI program that talks to the browser. A CGI program communicates with the server. So, you have to signal to the server you are ready sending output, and then the server can go on its merry way. Some servers will not send anything to the browser before they have collected all the output from the program - if only they want to sent a Content-length header. Signalling can often be done by closing stdout and stderr.

    Third, nowadays browsers and servers don't close the connection automatically right after sending the content of a single request - this is called the keepalive feature.

    Abigail

Re: How to close connection to browser
by Jaap (Curate) on Feb 25, 2003 at 22:17 UTC
    Thank you for all the comments.
    Now i know the answer, i realise it's kind of a RTFM question. My apologies for that.
    I am not using mod_perl.
    I'll also try to flush the buffer.
      You don't need to if you: close STDOUT will do this automatically for you.

      -- Joost downtime n. The period during which a system is error-free and immune from user input.