Re: Return a 200 0K status and send the content of the request later

Mithun Bhattacharya <[email protected]> Tue, 13 May 2025 21:45:35 -0500
Newsgroups gmane.comp.apache.mod-perl
Message-ID <CAHTPV9LRKgqvjuDcRkut=jSP9KSQRgZMHZMbDBozGi0C1ThXMw@mail.gmail.com>
--000000000000ddfc0006350f8b8a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

You should do the flush after the ContentType - content_type is header the
H3 is body of the response or at least it should be.

As for your second question, you are looking at a javascript based solution
- this isn't something a server side code can do. Look into a sleep
followed by a window.location.href. This wont help your long running
process though since the refresh will trigger another dump and tar. What
you need to do is send the header and flush followed by the system commands
- once rflush has happened the client side will wait forever without a
timeout.

On Tue, May 13, 2025 at 7:24=E2=80=AFPM Vincent Veyron <[email protected]=
> wrote:

> On Tue, 13 May 2025 14:57:15 -0500
> Mithun Bhattacharya <[email protected]> wrote:
>
> Hi Mithun,
>
> > Hence if you are writing a mod_perl handler for such an endpoint you
> return
> > success immediately and then start processing the request. In most case=
s
> > this is simply a matter of doing $r->print on the headers followed by
> print
> > of two new lines.
> >
>
> Nice!
>
> Your comment made me revisit mod_perl's documentation, and I found this :
>
>
> https://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Res=
ponse_Headers_Out
>
> The following works, where exportation($r) is the long running sub that
> builds a tar file and returns a page with a link to the file, for the use=
r
> to click. '<h3>Building tar file</h3>' does get sent first, then the rest
> of the response after completion of the sub.
>
>
>         $r->content_type('text/html; charset=3Dutf-8') ;
>
>         $r->print('<h3>Building tar file</h3>') ;
>
>         $r->rflush; # send the headers out
>
>         $r->print( exportation($r) ) ;
>
>         return Apache2::Const::OK ;
>
>
> This is great, I can thus send a message to the user, maybe with an
> estimated time of arrival. Thanks for the help.
>
> I have an another question, if I may.
>
> Is there a way I could do :
>
>         $r->print('<h3>Building tar file</h3>') ;
>
> every second or other until the tar file is built? I can't seem to think
> of one.
>
> My handler calls an sql script that dumps the database with system() :
>
>     @args =3D ('psql', '-f', '/path/to/script/to/export_raw_data.sql', '-=
v',
> 'id_client=3D' . $r->pnotes('session')->{id_client}, '-v',  'database=3D'=
 .
> $database, 'postgres') ;
>
>     system(@args) =3D=3D 0 or warn "system @args failed: $!" ;
>
> then waits for the resulting file before proceeding to compress and tar
> it, also with a system() call.
>
> How can I wrap this in a loop that re-sends the 'Building tar file'
> message until the file exists?
>
>
>
> --
>                                         Bien =C3=A0 vous, Vincent Veyron
>
> https://marica.fr/
> Logiciel de suivi des contentieux juridiques, des sinistres d'assurance e=
t
> des contrats
>

--000000000000ddfc0006350f8b8a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">You should do the flush=C2=A0after the ContentType - conte=
nt_type is header the H3 is body of the response or at least it should be.<=
div><br></div><div>As for your second=C2=A0question, you are looking at a j=
avascript based solution - this isn&#39;t=C2=A0something a server side code=
 can do. Look into a sleep followed by a window.location.href. This wont=C2=
=A0help your long running process though since the refresh will trigger ano=
ther dump and tar. What you need to do is send the header and flush=C2=A0fo=
llowed by the system commands - once rflush=C2=A0has happened the client si=
de will wait forever without a timeout.</div></div><br><div class=3D"gmail_=
quote gmail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, =
May 13, 2025 at 7:24=E2=80=AFPM Vincent Veyron &lt;<a href=3D"mailto:vv.lis=
[email protected]">[email protected]</a>&gt; wrote:<br></div><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid r=
gb(204,204,204);padding-left:1ex">On Tue, 13 May 2025 14:57:15 -0500<br>
Mithun Bhattacharya &lt;<a href=3D"mailto:[email protected]" target=3D"_blan=
k">[email protected]</a>&gt; wrote:<br>
<br>
Hi Mithun,<br>
<br>
&gt; Hence if you are writing a mod_perl handler for such an endpoint you r=
eturn<br>
&gt; success immediately and then start processing the request. In most cas=
es<br>
&gt; this is simply a matter of doing $r-&gt;print on the headers followed =
by print<br>
&gt; of two new lines.<br>
&gt; <br>
<br>
Nice!<br>
<br>
Your comment made me revisit mod_perl&#39;s documentation, and I found this=
 :<br>
<br>
<a href=3D"https://perl.apache.org/docs/2.0/user/coding/coding.html#Forcing=
_HTTP_Response_Headers_Out" rel=3D"noreferrer" target=3D"_blank">https://pe=
rl.apache.org/docs/2.0/user/coding/coding.html#Forcing_HTTP_Response_Header=
s_Out</a><br>
<br>
The following works, where exportation($r) is the long running sub that bui=
lds a tar file and returns a page with a link to the file, for the user to =
click. &#39;&lt;h3&gt;Building tar file&lt;/h3&gt;&#39; does get sent first=
, then the rest of the response after completion of the sub.<br>
<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $r-&gt;content_type(&#39;text/html; charset=3Du=
tf-8&#39;) ;<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $r-&gt;print(&#39;&lt;h3&gt;Building tar file&l=
t;/h3&gt;&#39;) ;<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $r-&gt;rflush; # send the headers out<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $r-&gt;print( exportation($r) ) ;<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return Apache2::Const::OK ;<br>
<br>
<br>
This is great, I can thus send a message to the user, maybe with an estimat=
ed time of arrival. Thanks for the help.<br>
<br>
I have an another question, if I may.<br>
<br>
Is there a way I could do :<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $r-&gt;print(&#39;&lt;h3&gt;Building tar file&l=
t;/h3&gt;&#39;) ;<br>
<br>
every second or other until the tar file is built? I can&#39;t seem to thin=
k of one.<br>
<br>
My handler calls an sql script that dumps the database with system() :<br>
<br>
=C2=A0 =C2=A0 @args =3D (&#39;psql&#39;, &#39;-f&#39;, &#39;/path/to/script=
/to/export_raw_data.sql&#39;, &#39;-v&#39;, &#39;id_client=3D&#39; . $r-&gt=
;pnotes(&#39;session&#39;)-&gt;{id_client}, &#39;-v&#39;,=C2=A0 &#39;databa=
se=3D&#39; . $database, &#39;postgres&#39;) ;<br>
<br>
=C2=A0 =C2=A0 system(@args) =3D=3D 0 or warn &quot;system @args failed: $!&=
quot; ;<br>
<br>
then waits for the resulting file before proceeding to compress and tar it,=
 also with a system() call. <br>
<br>
How can I wrap this in a loop that re-sends the &#39;Building tar file&#39;=
 message until the file exists?<br>
<br>
<br>
<br>
-- <br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Bien =C3=
=A0 vous, Vincent Veyron <br>
<br>
<a href=3D"https://marica.fr/" rel=3D"noreferrer" target=3D"_blank">https:/=
/marica.fr/</a> <br>
Logiciel de suivi des contentieux juridiques, des sinistres d&#39;assurance=
 et des contrats<br>
</blockquote></div>

--000000000000ddfc0006350f8b8a--