Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

execute JS in http response in perl

by gurudutt.rv (Initiate)
on Jul 24, 2012 at 12:12 UTC ( #983385=perlquestion: print w/replies, xml ) Need Help??
gurudutt.rv has asked for the wisdom of the Perl Monks concerning the following question:

I have a server application which accepts http(s) requests like this There are two steps to this request, first, when the above url is requested it responds with a JS code and this has to be executed in turn to complete the request to the server. When i open this in the browser, the whole path would work fine since browser executes JS. But how do i simulate the same in a perl script, the machine on which i run this is ubuntu 10.0.4 and does not have ui (so does not have firefox or other things to use www mechanize firefox and the likes) Following steps 1) Request 2) set xforwarded proto for the above request. 3) Execute the JS of the intermediate response to complete the request. Any help would be much appreciated.

Replies are listed 'Best First'.
Re: execute JS in http response in perl
by moritz (Cardinal) on Jul 24, 2012 at 12:32 UTC
Re: execute JS in http response in perl
by aitap (Curate) on Jul 24, 2012 at 13:30 UTC
    Do you really have to run JavaScript?
    For example, there is a script which logs on a forum, reads latest messages and decides which ones are posted by (just plain stupid) spam robots. Nearly half of the forum administration is javascript-driven, but all of it can be avoided by some simple things like:
    $ssid=$1 if $bot->content =~ m/name="ssid" value="([^"]+)"/; $bot->post($posturl,{ act => 2, reason => "spambot", ever => 1, a => $ +a, s => $s, t => $t, ssid => $ssid}); <...> ($key) = ($bot->content =~ /'(-8-0-[a-zA-Z0-9]+)'/) <...> $bot->content =~ /display='none'/ ? print " Removed\n" : warn "Failed +to remove the message, remove url $victim\n";
    You just need to read the javascript, understand it and do similar things yourself.
    Sorry if my advice was wrong.
Re: execute JS in http response in perl
by tobyink (Abbot) on Jul 24, 2012 at 13:53 UTC

    WWW::Scripter may be of use.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: execute JS in http response in perl
by bulk88 (Priest) on Jul 25, 2012 at 00:20 UTC
    Consider rewriting the JS code in Perl if its small enough, or the part of the JS code you want.
Re: execute JS in http response in perl
by Anonymous Monk on Jul 24, 2012 at 13:19 UTC
    It's usually not a good practice for the server to send JS to be blindly executed on the client side. Anyone could send anything and it would be blindly executed on the client side . . .

      Nonsense, this is how all browsers work - they blindly execute incoming scripts. I'm not saying there have never been any security issues doing this, but they've been few and far between. Why? Because the Javascript environment provided by browsers don't expose any potentially harmful system calls. For example, there does not exist a function in browsers' Javascript APIs which deletes a file from the filesystem.

      Of course, non-browser implementations of Javascript may expose whatever additional system functionality they like.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      This is why browsers sandbox the javascript they receive from all servers, so that it can't open files from the file system, fork processes, change the browser's GUI etc. These days many browsers even prevent javascript from opening new browser windows.

      So when you write Perl code that executes Javascript, you should make sure it's also sandboxed the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983385]
Approved by marto
[marto]: thanks erix

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-01-19 13:51 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (218 votes). Check out past polls.