javascript in perl code - perl

i using javascript file (json2.js) in perl-cgi code with apache. but when i run it on browser, its unable to find the source and return following error;
'Failed to load resource: the server responded with a status of 404 (Not Found)'
my doumentroot path is : /srv/www/cgi-bin and scripts' path: /srv/www/cgi-bin/scripts
i doing this;
print "<script type='text/javascript' src='./scripts/json2.js'></script>"; # Line 1
print "<script type='text/javascript' language='JavaScript'>
function setDetails(o,json_arrRef,arrSize){
alert('hello='+arrSize+' || ref='+json_arrRef); // till here it works fine
var json_obj = JSON.parse(json_arrRef);
}
</script>";
if I edit Line 1 as
print "<script type='javascript' src='./scripts/json2.js'></script>";
it finds the source but gives following error when 'JSON.parse()' is called;
'Uncaught SyntaxError: Unexpected token ILLEGAL'
am I doing something wrong?

Don't put static files anywhere in or below /cgi-bin/ as servers are often configured to treat that path is a special way
Do get your type attributes correct, the content-type for JavaScript is text/javascript and not just javascript (technically speaking it should be application/javascript, but pretend it is text/javascript for the sake of browsers)
Don't compare JavaScript errors in the browser with Perl code and do look at what the webserver is outputting (i.e. the HTML and JavaScript source and/or which requests give 404 or other HTTP error codes)

Replace "./scripts/json2.js" with "/scripts/json2.js".

Related

php fopen with ssh2.sftp is failing with special characters

The below is working just fine
$stream = fopen($connection_string . "/var/tmp/test.test", 'r');
But
$stream = fopen($connection_string . "/var/tmp/test.#test", 'r');
Fails with an error message:
Warning: fopen(): Unable to open ssh2.sftp://Resource id #108/var/tmp/test.#test on remote host in...
test.test and test#test are working fine, only the combination of test.#test is failing.
ssh2_scp_recv() handles test.#test just fine.
CentOS release 5.8 (Final)
PHP 5.2.17
Is this a php bug? Or some special character escaping is required?
it would be interesting to know how well it works with phpseclib. eg.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('filename.remote');
// copies filename.remote to filename.local from the SFTP server
$sftp->get('filename.remote', 'filename.local');
?>
src: http://phpseclib.sourceforge.net/sftp/examples.html#get
if phpseclib didn't work you could enable logging, with phpseclib, and see what the phpseclib logs say. eg. do define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); after you include Net/SFTP.php and then do $sftp->getLog() after you try to download the file.

Simple Javascript http request snippet but not work

The code is modified from some tutorials as below:
xmlhttp=new XMLHttpRequest();
var url = "get_data.pl";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send(null);
I want get_data.pl script get executed and return the running result(print a string "test"), but I only got all lines of get_data.pl in xmlhttp.responseText. How should I do?
get_data.pl as below:
#!C:/Perl/bin/perl.exe
use strict;
use warnings;
&main;
sub main()
{
print "test";// I'd like the script being executed by Perl interpreter and return the string "test" .
}
Yes, Gar, you're right, thanks. I'm using Apache and I modified the httpd.conf's handler line(un-comment and add .pl) as you said. Now the issue seems resolved but I got another error:
POST http: //localhost/get_data.pl 403 (Forbidden)
I put the get_data.pl in the htdocs folder and the security option(OS Win7) has already being set to execute permission. So why being forbidden could you help me again?
Yes I've run the .pl from command line and without error.
Normally the .pl was put in cgi-bin folder which is a brother folder with "htdocs".
When I put the .pl in /cgi-bin and modified url to "../cgi-bin/get_data.pl", I got an error 500 which I guess the server didn't find the file. So any other configuration I missed in httpd.conf? Anyway, I moved it to htdocs folder to avoid the error 500...
Please ensure that you added a handler to .pl files in your http server.

Mailgraph.cgi script error

I have installed rrdtool 1.4.7 and on testing with http://localhost/mailgraph.cgi.I get the error below:
Premature end of script headers: mailgraph.cgi, referer: http://localhost/mailgraph.cgi.
How can i get over this.Thanx in advance
mailgraph.cgi does not send out HTML headers correctly.
Check the apache error/access log for details.
You could check the script itself from commadn line.
./mailgraph.cgi
It should output soething like this:
Content-type: text/html
<HTML>,<DOCTYPE (or some other HTML coode)
Check this link: http://oreilly.com/openbook/cgi/ch12_01.html
regards,

lighttpd mod_redirect https to http specifically for media file types

I have a lighttpd 1.4.26 (ssl) configuration on a cent-os linux machine serving an HTML5 media application over HTTPS.
My goal is to serve media files via the application over HTTP from the same webserver.
If the webserver were located at https://www.media.com/ and all the media is located in various subfolders of http://www.media.com/sharedmedia/XXXXX, and I have relative links to any media file in the html for the pages served over http, then I want all requests to .mp3, .mp4, .webm, and .ogv files to be redirected to the EXACT SAME URL but using http instead of https...
My problem is I do not know how to write a url.redirect rule to perform this translation...
I have tried:
url.redirect = ( "https://^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
And when I visit this URL:
https://www.media.com/sharedmedia/X-MAC-MINI/Sports/Amazing%20Football%20Skills%20and%20Tricks.ogv
I am 301 permanently redirected to
http://www.media.com/sharedmedia/X-MAC-MINI/Sports/Amazing0Football0Skills0and0Tricks.ogv
Which is then also 301'ed to:
http:///sharedmedia/AFFINEGY-MAC-MINI/Sports/Amazing0Football0Skills0and0Tricks
Notice that the %20's that were in the very first url (urlencoded SPACE) were dropped from the URL leaving the trailing '0' in each case during the first redirect (I assume interpreted as %2 which holds an empty string), and that the http request is ALSO redirected erroniously to another URL that doesn't even contain the host value (www.media.com). Also, the extension is left off the second redirect...
I then tried a conditional version after that:
$HTTP["socket"] =~ ":443$"
{
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
}
..which results in lighttpd simply crashing on startup, so I can't even test it. Lighttpd startup error message follows:
Starting lighttpd: 2011-08-31 16:19:15: (configfile.c.907) source: find /etc/lighttpd/conf.d -maxdepth 1 -name '*.conf' -exec cat {} \; line: 44 pos: 1 parser failed somehow near here: (EOL)
2011-08-31 16:19:15: (configfile.c.907) source: /etc/lighttpd/lighttpd.conf line: 331 pos: 1 parser failed somehow near here: (EOL)
Any ideas what I'm doing wrong?
Here's what I did wrong:
You need a nested "IF" to use the %n notation in the destination IP rule...
I had
$HTTP["socket"] =~ ":443$"
{
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1" )
}
But I needed
$HTTP["socket"] =~ ":443$" {
$HTTP["host"] == (.*) {
url.redirect = ( "^(.*)\.(ogv|mp4|mp3|webm)$" => "http://%1/$1.$2" )
}
}
Now... the %1 in "http://%1/$1" refers to the match in "$HTTP["host"] == (.*)" while $1 refers to the match in the url redirect parens, and $2 refers to the match for the second set of parens in the url redirect source...
Is it just me or is this shit TOTALLY undocumented? I can only find on google people complaining of not being able to get this to work and no one seems to have a single answer for how it works...
I'm now stuck making the EXACT same thing happen in APACHE, and I can't find a good documentation on .htaccess 301 redirects for it either...

I can't catch php exceptions using try....catch

I'm having a problem with PHP Exceptions. Even if I try to execute this code:
try {
$some->knownMethodWithError();
} catch(Zend_Exception $exp){
echo 'Error!: ' . $exp->getMessage();
}
My apache/php served web page always display a 500 Error. I mean,
echo 'Error!: ' . $exp->getMessage();
never is executed. I've tested with a redirection instead of that echo, but it doesn't work. Is there some php.ini directive that cause this behavior, or could it be something else?.
This happens in my Zend Framework based project.
Also, your code will only catch Zend_Exception.
If you have custom Exceptions maybe catch(Exception $e) will get more chances to catch all of them
maybe you could run the script with full logs. Change your php.ini on the server on the line :
error_reporting=E_ALL | E_STRICT
or better run it on an IDE environment with a debugger.
A 500 error isn't a PHP exception, it's happening above the code level. A 500 error means that there was an error while PHP was trying to parse your script (probably). Possibly your code has a syntax error.