A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label hack. Show all posts
Showing posts with label hack. Show all posts

Hack your own MySQL to log in as root

$> sudo service mysql stop

$> mysqld --skip-grant-tables

$> mysql

mysql> FLUSH PRIVILEGES;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

mysql> SELECT host, user, plugin FROM mysql.user;

(source)

How to disable Mojo debug logging

When setting MOJO_LOG_LEVEL does not work... Make the following hacks:

removing debug output

~ ============================================================================ Tuesday 12th February 2019

~~~ FIXES FOR OLD VERSION OF MOJO (Supervillain 8.03)

../local/lib/perl5/Mojolicious.pm
132:    $self->log->debug(qq{$method "$path" ($id)}) if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};

../local/lib/perl5/Mojolicious/Controller.pm
209:      $app->log->debug("$code $msg (${elapsed}s, $rps/s)") if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};

../local/lib/perl5/Mojolicious/Plugin/EPLRenderer.pm
18:    $c->app->log->debug("Rendering cached @{[$mt->name]}") if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};
30:      $c->app->log->debug(qq{Rendering inline template "$name"}) if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};
40:        $c->app->log->debug(qq{Rendering template "$name"}) if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};
46:        $c->app->log->debug(qq{Rendering template "$name" from DATA section}) if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};

../local/lib/perl5/Mojolicious/Routes.pm
95:  $app->log->debug('Routing to a callback') if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};
150:    $log->debug(qq{Routing to application "$class"}) if $ENV{BB_DEBUG} || $ENV{MOJO_DEBUG};
164:      $log->debug(qq{Routing to controller "$class" and action "$method"}) if $ENV{MOJO_DEBUG} || $ENV{BB_DEBUG};

~ ============================================================================ Thursday 7th March 2019

local/lib/perl5/Mojolicious.pm
123:          #return unless $ENV{MOJO_DEBUG}; # WS hack

local/lib/perl5/Mojolicious/Controller.pm
187:      return unless $ENV{MOJO_DEBUG};

local/lib/perl5/Mojolicious/Routes.pm
103:  $app->log->debug('Routing to a callback') if $ENV{MOJO_DEBUG};
170:      $log->debug(qq{Routing to controller "$class" and action "$method"}) if $ENV{MOJO_DEBUG};

x
491:>> /Users/will/dev/ats-broadcast-hub/local/lib/perl5/Mojolicious/Routes.pm:103:   $app->log->debug('Routing to a callback') if $ENV{MOJO_DEBUG};

~~~~ AND ~~~~

To fix CODE(0x7fde03335ca0) now:

vi local/lib/perl5/Mojolicious.pm +122

~~~ OLD VERSION OF MOJO

local/lib/perl5/Mojolicious.pm

  60 our $CODENAME = 'Supervillain';
  61 our $VERSION  = '8.03';

 119   # Start timer (ignore static files)
 120   my $stash = $c->stash;
 121   unless ($stash->{'mojo.static'} || $stash->{'mojo.started'}) {
 122     my $req    = $c->req;
 123     my $method = $req->method;
 124     my $path   = $req->url->path->to_abs_string;
 125     my $id     = $req->request_id;
 126     $self->log->debug(qq{$method "$path" ($id)});
 127     $c->helpers->timing->begin('mojo.timer');
 128   }

~~~ NEW VERSION OF MOJO

 119   # Start timer (ignore static files)
 120   my $stash = $c->stash;
 121   $self->log->debug(sub {
 122     my $req    = $c->req;
 123     my $method = $req->method;
 124     my $path   = $req->url->path->to_abs_string;
 125     my $id     = $req->request_id;
 126     $c->helpers->timing->begin('mojo.timer');
 127     return qq{$method "$path" ($id)};
 128   }) unless $stash->{'mojo.static'};
 129 

~~~ FIX FOR NEW VERSION (8.12)

vi local/lib/perl5/Mojolicious.pm

  61 our $CODENAME = 'Supervillain';
  62 our $VERSION  = '8.12';

 119   # Start timer (ignore static files)
 120   my $stash = $c->stash;
 121     my $req    = $c->req;
 122     my $method = $req->method;
 123     my $path   = $req->url->path->to_abs_string;
 124     my $id     = $req->request_id;
 125     $c->helpers->timing->begin('mojo.timer');
 126 if ($ENV{MOJO_DEBUG}) {
 127     $self->log->debug(qq{$method "$path" ($id)}) unless $stash->{'mojo.static'};
 128 }

vi local/lib/perl5/Mojolicious/Controller.pm

184     # Disable auto rendering and stop timer
185     my $app = $self->render_later->app;
186 #    $app->log->debug(sub {
187       my $timing  = $self->helpers->timing;
188       my $elapsed = $timing->elapsed('mojo.timer') // 0;
189       my $rps     = $timing->rps($elapsed) // '??';
190       my $code    = $res->code;
191       my $msg     = $res->message || $res->default_message($code);
192 #      return "$code $msg (${elapsed}s, $rps/s)";
193 #    }) unless $stash->{'mojo.static'};
194 if ($ENV{MOJO_DEBUG}) {
195       $app->log->debug("$code $msg (${elapsed}s, $rps/s)") unless $stash->{'mojo.static'}; # MOJO_DEBUG
196 }



Simple perl webserver

HTTP::Server::Simple

(contrast with python one-liner)

#!/usr/bin/perl

# https://metacpan.org/pod/HTTP::Server::Simple
# /versions/perl-5.8.7/bin/perl -MHTTP::Server::Simple test2.pl

use strict;
use warnings;

{
package MyWebServer;

use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);

my %dispatch = (
    '/hello' => \&resp_hello,
    '/' => \&resp_index,
);

sub handle_request {
    my $self = shift;
    my $cgi  = shift;

    my $path = $cgi->path_info();
    my $handler = $dispatch{$path};

    if (ref($handler) eq "CODE") {
        print "HTTP/1.0 200 OK\r\n";
        $handler->($cgi);

    } else {
        print "HTTP/1.0 404 Not found\r\n";
        print $cgi->header,
              $cgi->start_html('Not found'),
              $cgi->h1('Not found'),
              $cgi->end_html;
    }
}

sub resp_hello {
    my $cgi  = shift;   # CGI.pm object
    return if !ref $cgi;

    my $who = $cgi->param('name');

    print $cgi->header,
          $cgi->start_html("Hello"),
          $cgi->h1("Hello $who!"),
          $cgi->end_html;
}

sub resp_index {
    my $cgi = shift;
    print $cgi->header;
    open(my $fh,'<','index.html') or die "can't open index.html";
    local $/ = undef;
    my $out = <$fh>;
    close($fh);
    print $out;
}

}

# start the server on port 8080
my $pid = MyWebServer->new(8085)->background();
print "Use 'kill $pid' to stop server.\n";

(source)