Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to sleep(), or use a while() in the background?

by taint (Chaplain)
on May 07, 2016 at 01:00 UTC ( [id://1162421]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, Monks!
I'm attempting to create temporary links (prevent "hot linking"). So what I'm attempting, is to create a symlink on initiation, and set a timer (sleep / while) that when reached, unlinks the symlink. It all works just fine, except, that it sort of "hangs" the script/web page, while the timer is counting down. I first attempted this with the sleep function. But am now trying with a while(). Here's what I have now

#!/usr/bin/perl -w use strict; use feature qw(say); use POSIX qw(strftime); my $ctime= strftime "%H-%M", localtime; my $dlfile= "../PATH_TO/SOME_FILE"; my $dlname= "SOMENAME"; my $newname= "./ANOTHER_PATH/$ctime-$dlname"; my $maxtime= 30; my $args = ('-s'); print "content-type:text/html; charset=utf-8\n\n"; say qq~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" + /> <title>Download $dlname</title> </head> <body <h1>Download $dlname</h1> ~; system("/bin/ln $args $dlfile $newname"); while ($maxtime > 0) { $maxtime--; sleep(1); say qq~<a href="$newname">download</a>~; if ($maxtime == 0) { unlink $newname; say qq~<h3>Sorry! $newname no longer exists!</h3>~; } } say qq~<p> <br /> </p> </body></html> ~; exit
I'm sure this is really simple. But not for me, today. :/
Thanks, for any pointers!

--Chris

EDIT: fix a couple of typos. Move a block.

¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Replies are listed 'Best First'.
Re: How to sleep(), or use a while() in the background?
by Anonymous Monk on May 07, 2016 at 03:41 UTC

        Well. After attempting every possible iteration, or combination available to me via Process::Simple, and the example listed in perlfaq8:

        system("cmd &")
        I have concluded that it is not possible to perform my desired action(s) with sleep. Where is it is called from the web (via a function that also delivers a web page).

        So don't try this at home, kids! ;-)

        --Chris

        ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      Thanks for the reply, Anonymous Monk

      The Proc::Background looks interesting, and that's good advice, regarding the "shell interpolation". In my humble defense; I'm just trying to push some parts together that'll accomplish the task. I'll safely arrange everything, when I've finally found all the necessary parts. Advice well taken, and appreciated. :)

      --Chris

      ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: How to sleep(), or use a while() in the background?
by pryrt (Abbot) on May 10, 2016 at 01:08 UTC

    Would you be open to alternative methods? Since you are serving up the page providing the link thru cgi, why not do the same with the file itself? Generate a url to the same script, or a different one, with a param or path_info() which is time dependent (but not just the plaintext epoch time, or your hotlinker will figure it out). Maybe generate it using Authen::OATH with a longer timestep than default. Then, in the script which serves the file, you would verify that the time parameter was still valid before serving the file.

      Thanks for the reply, pryrt.

      No. I'm not at all opposed to alternate approaches. In fact, in my frustration, I seriously considered simply using some form of CAPTCHA. But in the end decided not to, as the files should appear to be available, when search engines crawl the tree, and CAPTCHA won't really reveal the files (their names/locations). While the (original) method would. The fact that the links(s) are temporary, is unknown to them. :)

      I think, because all my previous attempts end up running the sleep timer in the browser, and thereby make it appear to the (end) user, that the browser is still retrieving data from the site. That I should probably break out the timer, and unlink functions out into a separate file/module, and parametrize the timestamp generated initially, and pass it to the second script (timer/unlinker). I'll need to give this some more thought. :)

      Thanks again, pryrt, for taking the time to respond.

      --Chris

      ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1162421]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (1)
As of 2024-04-24 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found