#
# SIMPLE mod_perl DBI example...
#
# OpenBSD 7.7
#
# $ su
# $ pkg_add apache-httpd ap2-mod_perl mariadb-server
# $ mysql_install_db
# $ mysql -u root
# mysql> CREATE DATABASE count;
# mysql> USE count;
# mysql> CREATE TABLE count (name VARCHAR(255) PRIMARY KEY NOT NULL, value INT NOT NULL);
# mysql> INSERT INTO count VALUES ('matt',0);
# mysql> CREATE USER 'matt'@'localhost' IDENTIFIED BY 'abc123';
# mysql> GRANT ALL PRIVILEGES ON *.* TO 'matt'@'localhost';
# mysql> FLUSH PRIVILEGES;
# mysql> exit
#
# vi /etc/apache2/httpd2.conf
# LoadModule perl_module /usr/local/lib/apache2/mod_perl.so
# PerlModule hello
#
# SetHandler modperl
# PerlResponseHandler hello
#
#
# $ cp hello.pm /usr/local/libdata/perl5/site_perl/amd64-openbsd/
# $ rcctl enable mysqld
# $ rcctl start mysqld
# $ rcctl enable apache2
# $ rcctl start apache2
# $ apachectl restart
# $ curl http://localhost/hello
#
# PROGRAMMER: Matthew W. Coan
# E-MAIL: matthewcoan1976@hotmail.com
# WWW: http://slack.net/~mcoan/
#
package hello;
use strict;
use warnings;
use DBI;
use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);
my $username = "matt";
my $password = "abc123";
my $dbh = 0;
sub x2c {
my $string = shift;
$string =~ s/\+/ /g;
$string =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;
return $string;
}
sub parse {
my $request = shift;
my %params;
my $content;
if($request->method eq "GET") {
$content = $request->args;
}
else {
$request->read($content, $request->header_in("Content-length"));
}
my @pairs = split(/[&;]/, $content);
foreach my $pair (@pairs) {
my ($key,$value) = split('=',$pair,2);
$params{&x2c($key)} = &x2c($value);
}
return \%params;
}
sub display_count {
my $request = shift;
my $count = shift;
$request->content_type('text/html');
$request->print(<
Counter V1.0
count=$count