Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Logioniz / 1.pl
Created May 21, 2015 07:21
Mojo test inactivity_timeout long operation.
use Mojo::Base -strict;
use Mojo::IOLoop;
use Mojolicious::Lite;
use Test::More;
use Test::Mojo;
get '/' => sub {
my $c = shift->render_later;
$c->inactivity_timeout(40);
@Logioniz
Logioniz / server-epoll.c
Last active May 8, 2022 15:30
Non-blocking servers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>
@Logioniz
Logioniz / client-ev.pl
Created September 12, 2015 09:57
Non-blocking client
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK/;
use Data::Dumper;
use EV;
my $host="localhost";
my $port="3000";
socket(my $sock, PF_INET, SOCK_STREAM | SOCK_NONBLOCK, getprotobyname('tcp'));
@Logioniz
Logioniz / hosts.pl
Created September 12, 2015 14:32
detect host by dnsname
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo unpack_sockaddr_in inet_pton getnameinfo inet_ntop/;
use EV;
use Data::Dumper;
my $host = 'localhost';
my $port = '3000';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');
@Logioniz
Logioniz / 1.pl
Created September 14, 2015 16:09
ipv4, ipv6 connect async ev
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo/;
use EV;
my $host = 'localhost';
my $port = '27017';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');
my ($err, @hosts) = getaddrinfo($host, $port, {protocol => $tcp_proto});
@Logioniz
Logioniz / mojo_ioloop_dns.pl
Created September 29, 2015 11:05
Mojo::IOLoop::Net::DNS
#!/usr/bin/perl
package Mojo::IOLoop::Net::DNS;
use Mojo::Base -base;
use Net::DNS;
use Mojo::IOLoop;
use Scalar::Util 'weaken';
sub query {
@Logioniz
Logioniz / perl_ws_long_blocking_operation.pl
Created December 5, 2015 23:21
Example how handle long blocking operation when server asynchronous
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::IOLoop::ReadWriteFork;
@Logioniz
Logioniz / perl_ws_many_long_blocking_operations.pl
Last active December 9, 2015 08:00
Example how handle many long blocking operation when server asynchronous
#!/usr/bin/perl
use Mojo::Base -strict;
use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::IOLoop::ReadWriteFork;
plugin 'Mojolicious::Plugin::ForkCall';
@Logioniz
Logioniz / redirect.pl
Last active November 17, 2021 17:17
Redirect from http to https with mojolicious
#!/usr/bin/perl
# To run need listen two ports: 80, 443
# sudo morbo 12.pl -l https://*:443?cert%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.crt%26key%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.key%26verify%3D0x00 -l http://*:80
# go to url in browser http://your_ip/qwe/asd
use Mojo::Base -strict;
use Mojolicious::Lite;
@Logioniz
Logioniz / bad_simple_proxy_connect_only.pl
Last active December 25, 2015 20:07
1-st not good variant of http/https proxy via CONNECT only method. 2-nd - is good simple http/https proxy.
#!/usr/bin/perl
use Mojo::Base -strict;
use Mojo::IOLoop;
use Mojo::UserAgent;
my %connections;
Mojo::IOLoop->server({port => 3128} => sub {
my ($loop, $stream_client, $client_id) = @_;