How to integrate Dancer2::Core::HTTP helpers for rendering HTTP status codes? - perl

Dancer2 is a PSGI web application framework, we can use the "plackup" tool (provided by Plack) for launching the application:
plackup -p 5000 bin/app.psgi
The web application created using Dancer2 script can be viewed on localhost and port 5000.
http://localhost:5000
We can then write a get request as follows:
get '/hello/:name' => sub {
return "Hi there " . route_parameters->get('name');
};
For GET requests to "/hello/...", the code block provided above gets executed.
If the application is down, for GET requests to "/hello/...", the following error is shown:
curl: (7) Failed connect to localhost:5000; Connection refused
How do we integrate Dancer2::Core::HTTP helpers for rendering HTTP status codes for Dancer2 so that the following status code is shown when the app is down? Similarly, there are other error handlers too, how to integrate them as well?
status_500 status_error, status_internal_server_error

Related

How to connect and fetch page from web using HTTP?

Objective: To fetch webpage using package HTTP in julia:
Environment: CentOS7.4
Content of fetch.jl:
#! /opt/julia/julia-1.1.0/bin/julia
using HTTP, Cascadia, Gumbo;
function fetch()
url ="https://www.google.com";
#url ="https://en.wikipedia.org/Stack_Overflow";
#url ="https://stackoverflow.com/questions/tagged/julia";
parsedPage = parsehtml(String(HTTP.get(url)));
println(string(parsedPage));
end
fetch();
invoking julia>/home/julia/fetch.jl shows as follows:
...
ERROR: LoadError: IOError(Base.Error(connect: host is unreachable(EHOSTUNREACH)", -113), during request(https://www.google.com))
...
Observation:
a) These sites are opening in browser(Mozilla Firefox).
b) wget fetches the pages
c) Also, tried with i) switching off both firewall (using systemctl stop firewalld) and selinux (using setenforce 0).
ii) other urls. But, no success!
Please help me in connecting and fetching webpages using HTTP!
Your code seems to be valid.
Try connecting using plain sockets and see what happens:
julia> using Sockets
julia> t = connect("google.com", 80)
TCPSocket(Base.Libc.WindowsRawSocket(0x000000000000037c) open, 0 bytes waiting)
julia> println(t, "GET /")
julia> readline(t)
"HTTP/1.0 200 OK"

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

Telit 4G modem LE920-EUG, giving error on http commands, AT#HTTPCFG.. AT#HTTPQRY any http command not working

I have the Telit LE920-EUG 4G LTE module. I am trying to execute GET and POST http requests to a remote server. Though the PDP context is activating properly and I have internet access on the SIM that I'm using, I can't seem to be able to connect to a remote server and execute HTTP requests (both POST and GET) from the module.
I have tried two ways, one through direct HTTP commands supported by the module(All commands mentioned in the LE9x0 AT command reference guide), the commands sequence for which is mentioned below, but +CME ERROR: 100 occurs, and it's same for every http command(AT#HHTPQRY, AT#HTTPRCV) that I try to execute.
AT#SGACT=1,1
#SGACT: 31.81.208.1
OK
AT#HTTPCFG=0,"httpbin.org",80,0,,,0,120,1
+CME ERROR: 100
//No configuration details
AT#HTTPCFG?
+CME ERROR: 100
AT#HTTPCFG=?
+CME ERROR: 100
I have also tried the GET and POST commands after socket dialing. The socket connects but they are not receiving any data from the server or posting anything onto the server, the connection closes with a NO CARRIER. The command sequence that I'm using is given below
//Socket Dial
AT#SD=1,0,80,www.m2msupport.net
CONNECT
//GET commands sequence
GET /m2msupport/http_get_test.php HTTP/1.1
Host:www.m2msupport.net
Connection:keep-alive
//Connection closes with No Response
NO CARRIER
//Socket info shows the bytes sent
at#si=1
#SI: 1,86,0,0,0
OK

Kubernetes API using websocket from NodeJS

I am writing a NodeJS application which is supposed to get deploymentstatuses from the Kubernetes API using the websocket transport layer.
For this I use the socket.io-client module and I connect with the following snippet:
var url = 'wss://myurl:8443?watch=true&access_token=myaccesstoken';
var socket = ioClient.connect(url, {
reconnect: true,
transports : ['websocket'],
path : "/api/v1/namespaces/mynamespace/replicationcontrollers",
secure : true,
rejectUnauthorized: false,
verify : false});
This however gives me an unexpected error, 403. Testing this in extensions like Websocket Client to Chrome works perfectly fine. Also I receive a 200 if I try a path with less sensetive data, but not an upgrade to websocket.
I read somewhere the Kubernetes API doesn't treat the WebSocket-protocol correctly, perhaps this is related? I have also tried with other more native libraries to Node such as websocket and ws with the same result.
When adding a ?watch=true in a Kubernetes API call, the request does not use WebSocket's but instead will stream / chunk the response over the HTTP connection.