deploying cgi to psgi converted application in apache - perl

#!C:/perl/bin/perl.exe
use CGI;
my $q = CGI->new;
print $q->header('text/plain'),
"Hello ", $q->param('name');
#CONVERTED PSGI PAGE
#!C:/perl/bin/perl.exe
use CGI::PSGI;
my $app = sub {
my $env = shift;
my $q = CGI::PSGI->new($env);
return [
$q->psgi_header('text/plain'),
[ "Hello ", $q->param('name') ],
];
};
I run this cgi.pl in apache server as
http://localhost/cgi-bin/cgi.pl
but I cant able to run the converted psgi.pl in apache server
its displaying
please help
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#example.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.

CGI and PSGI are two different specifications of how a web server and an external program communicate.
Under CGI, the web server expects to receive text output from the program, consisting of the HTTP Response headers, a blank line, and the HTML rendered by the program.
The CGI module implements this logic for the apache server, and if the output from the program does not comply, apache reports the 500 error.
Under PSGI, the web server expects the program to return a three element list consisting of the HTTP response code, the HTTP Response headers, and the HTML rendered by the program.
So you can see that a program conforming to the PSGI spec would confuse the mod_cgi.
So you need to install an apache module that implements PSGI, or employ a Perl module (the CGI::PSGI docs suggest CGI::Emulate::PSGI ) that will accept your PSGI list and convert to CGI for you.

Related

Net::XMPP Perl module: Can this be used to check for chat messages on server?

I can now connect to my jabber server using the perl module Net::XMPP. Now I would like to know: Is it possible to monitor the server for incoming chat messages (NOT email messages) and act accordingly if one is recieved?
The basic code I currently have is:
#!/bin/perl -w
use strict;
use warnings;
use Net::XMPP;
my $con = new Net::XMPP::Client();
my $status = $con->Connect(
hostname => 'hostnamepart',
connectiontype => 'tcpip',
tls => 1,
ssl_ca_path =>'path for cert');
die('ERROR: XMPP connection failed') if ! defined($status);
The $status variable returns 1 when I run the code above which I assume means that I connected ok. However, now I am stuck! I have looked through the online documentation on the metacpan.org website for Net::XMPP but cannot figure out if it's even possible to do what I want. Any help appreciated.

IO::Socket::SSL fails with IIS 8.5

Following script runs successfully when run with Perl CLI ( Windows). But when run through IIS 8.5, it fails.
use IO::Socket::SSL;
print <<EOP;
Content-type: text/html
<html><body>
<h1>Hello world</h1>
EOP
print "</body></html>\n";
Error message is
502 - Web server received an invalid response while acting as a
gateway or proxy server.
When "use IO::Socket::SSL" line is deleted, it is successfully executed.
With "use IO::Socket", it is too successfully executed.
Is there something wrong with my IIS / CGI configuration?
Thank you for your help.
I added directory containing libcrypto-1_1-x64__.dll file into system PATH and rebooted the machine. It solved my issue.

Lua open socket error

I am using lua as module for nginx (openresty) to get files from remote host. My function:
function readfile(url)
local http = require ("socket.http")
if not http then
error("Couldn't open socket.http")
end
http.TIMEOUT = 5
local body, code = http.request(url)
if not body then
error("Couldn't read the remote file: " .. code)
end
return body
end
I have tested this code by using Siege. When I set the users more then 100 (for example), I catch this error:
2018/03/27 09:36:38 [info] 10#10: *91018 shutdown() failed (107: Socket not connected), client: 172.18.0.7, server: localhost
I have more errors when i set more users. What does it mean? Thank you for the help.
Don't use luasocket library with OpenResty. The resulting code would block on http.request().
I suppose that all nginx worker just blocked and it is the reason of these errors.
For you purpose you may use one of the libraries below:
lua-resty-http
lua-resty-http-simple
First is more fexible, allow to use secure transport.
Second has simpler API.
And both use internally nginx Lua cosocket API and are 100% nonblocking out of the box.
The lua-resty-http or lua-resty-http-simple do not work in the init_by_lua in http context.
it is fine to use it in the init context where blocking is not considered harmful.

Connect perl script to a websocket (using mojolicious)

Here is my issue : I have got an API using mojolicious, an external script perl and a JS file, and I would like to connect them this way: the external script launch a random POST request, if it is a success it have to send the message "Success" throught a websocket. If an error occures, It will have to send "Error". The websocket on the API will just relay the message for the JS which will use it.
How I imagine the code to be :
Inside the Mojolicious launcher script:
websocket '/foo' => sub {
$self->on(message => sub {
my ($self, $msg) = #_;
$self->send($msg);
});
};
when get a message send it
Inside the JS file:
var ws = new WebSocket('ws://api/foo');
ws.onmessage = function(msg){
if(msg == "Error") {console.log("got an error")};
else if(msg == "Success") {console.log("got a success")};
};
So, how can I connect my external script to the websocket, and be able to send "Error" or "Success"? (This external script has nothing to do with the web server, it s somewhere else, doing something else).
I understand that you want a plain perl-script who connects to the Mojolicious webserver where you have a websocket endpoint.
The external perl script should connect to the websocket server and send some messages. The websocket server can then redistribute those messages to other clients.
Check my github where you can find the above screnario.
Perl Mojolicious websockets

Simple SMPP Perl Script

I need your help/advices on my very short script I wrote in Perl in order to send SMS via SMPP protocol.
I got a SMS gateway which is perfectly working (sending SMS via HTML request or via web interface works), let's say this gateway has IP 192.168.1.15.
Its SMPP service is listening to TCP 2775 (I can successfully telnet to 2775, so I guess SMPP service is working on my SMS gateway)
my $smpp = Net::SMPP->new_transmitter(192.168.1.15,
port=>2775,
system_id =>"administrator",
password =>"passwdexample") or die;
$resp_pdu = $smpp->submit_sm(destination_addr => '+400123456789',
short_message => 'test message') or die;
die "Response indicated error: " . $Resp_PDU->explain_status() if $resp_pdu->status;
When I run the script, here the error I got :
Response indicated error: Incorrect BIND Status for given command (ESME_RINVBNDS TS=0x00000004) at C:\temp\smpptest.pl line .
Unfortunately, I haven't find so much help on internet, but according to this link : SMPP Errors Codes It says :
You must bind first before any other request is handled.
However, my bind is done with new_transmitter, and I don't get any errors at this point, so I don't understand how it cannot bind the TCP session (my credentials are corrects, I tried that in a telnet session).
That's the first time I use such a plugin, so maybe I'm missing something, and maybe someone has already met this error !
Many thanks for your help :-)
Try to quote the IP address:
Net::SMPP->new_transmitter("192.168.1.15", ...);