| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Mar-30 10:34 (EDT) | ||
| 6 | # Function: server side | ||
| 7 | # | ||
| 8 | # $Id: Server.pm,v 1.3 2011/01/07 22:34:00 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::Server; | ||
| 11 | use AC::MrGamoo::Debug 'server'; | ||
| 12 | use AC::MrGamoo::Protocol; | ||
| 13 | use AC::MrGamoo::Protocol; | ||
| 14 | |||
| 15 | use AC::MrGamoo::API::HB; | ||
| 16 | use AC::MrGamoo::API::Xfer; | ||
| 17 | use AC::MrGamoo::API::Del; | ||
| 18 | use AC::MrGamoo::API::Chk; | ||
| 19 | use AC::MrGamoo::API::Get; | ||
| 20 | use AC::MrGamoo::API::Put; | ||
| 21 | use AC::MrGamoo::API::JobCreate; | ||
| 22 | use AC::MrGamoo::API::JobAbort; | ||
| 23 | use AC::MrGamoo::API::TaskCreate; | ||
| 24 | use AC::MrGamoo::API::TaskAbort; | ||
| 25 | use AC::MrGamoo::API::TaskStatus; | ||
| 26 | use AC::MrGamoo::API::XferStatus; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | |||
| 30 | our @ISA = 'AC::DC::IO::TCP'; | ||
| 31 | |||
| 32 | my $HDRSIZE = AC::MrGamoo::Protocol->header_size(); | ||
| 33 | my $TIMEOUT = 2; | ||
| 34 | |||
| 35 | my %HANDLER = ( | ||
| 36 | heartbeat_request => 'AC::MrGamoo::API::HB', | ||
| 37 | scribl_get => 'AC::MrGamoo::API::Get', | ||
| 38 | scribl_put => 'AC::MrGamoo::API::Put', | ||
| 39 | scribl_del => 'AC::MrGamoo::API::Del', | ||
| 40 | scribl_stat => 'AC::MrGamoo::API::Chk', | ||
| 41 | |||
| 42 | mrgamoo_status => 'AC::MrGamoo::Kibitz::Server', | ||
| 43 | mrgamoo_filexfer => 'AC::MrGamoo::API::Xfer', | ||
| 44 | mrgamoo_filedel => 'AC::MrGamoo::API::Del', | ||
| 45 | mrgamoo_jobcreate => 'AC::MrGamoo::API::JobCreate', | ||
| 46 | mrgamoo_taskcreate => 'AC::MrGamoo::API::TaskCreate', | ||
| 47 | mrgamoo_jobabort => 'AC::MrGamoo::API::JobAbort', | ||
| 48 | mrgamoo_taskabort => 'AC::MrGamoo::API::TaskAbort', | ||
| 49 | mrgamoo_taskstatus => 'AC::MrGamoo::API::TaskStatus', | ||
| 50 | mrgamoo_xferstatus => 'AC::MrGamoo::API::XferStatus', | ||
| 51 | |||
| 52 | http => \&http, | ||
| 53 | # ... | ||
| 54 | |||
| 55 | ); | ||
| 56 | |||
| 57 | my %HTTP = ( | ||
| 58 | loadave => \&AC::MrGamoo::Stats::http_load, | ||
| 59 | stats => \&AC::MrGamoo::Stats::http_stats, | ||
| 60 | status => \&AC::MrGamoo::Stats::http_status, | ||
| 61 | peers => \&AC::MrGamoo::Kibitz::Peers::report, | ||
| 62 | jobs => \&AC::MrGamoo::Job::report, | ||
| 63 | tasks => \&AC::MrGamoo::Task::report, | ||
| 64 | xfers => \&AC::MrGamoo::Xfer::report, | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub new { | ||
| 68 | my $class = shift; | ||
| 69 | my $fd = shift; | ||
| 70 | my $ip = shift; | ||
| 71 | |||
| 72 | unless( $AC::MrGamoo::CONF->check_acl( $ip ) ){ | ||
| 73 | verbose("rejecting connection from $ip"); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | my $me = $class->SUPER::new( info => 'tcp mrgamoo server', from_ip => $ip ); | ||
| 78 | |||
| 79 | $me->start($fd); | ||
| 80 | $me->timeout_rel($TIMEOUT); | ||
| 81 | $me->set_callback('read', \&read); | ||
| 82 | $me->set_callback('timeout', \&timeout); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub timeout { | ||
| 86 | my $me = shift; | ||
| 87 | |||
| 88 | debug("connection timed out"); | ||
| 89 | $me->shut(); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub read { | ||
| 93 | my $me = shift; | ||
| 94 | my $evt = shift; | ||
| 95 | |||
| 96 | my($proto, $data, $content) = read_protocol_no_content( $me, $evt ); | ||
| 97 | return unless $proto; | ||
| 98 | |||
| 99 | # dispatch request | ||
| 100 | my $h = $HANDLER{ $proto->{type} }; | ||
| 101 | |||
| 102 | unless( $h ){ | ||
| 103 | verbose("unknown message type: $proto->{type}"); | ||
| 104 | $me->shut(); | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | eval { | ||
| 109 | $data = AC::MrGamoo::Protocol->decode_request($proto, $data) if $data && $proto->{type} ne 'http'; | ||
| 110 | }; | ||
| 111 | if(my $e = $@ ){ | ||
| 112 | problem("cannot decode request: $e"); | ||
| 113 | $me->shut(); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | debug("handling request - $proto->{type}"); | ||
| 118 | |||
| 119 | if( ref $h ){ | ||
| 120 | $h->( $me, $proto, $data, $content ); | ||
| 121 | }else{ | ||
| 122 | $h->handler( $me, $proto, $data, $content ); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | sub http { | ||
| 127 | my $me = shift; | ||
| 128 | my $proto = shift; | ||
| 129 | my $url = shift; | ||
| 130 | |||
| 131 | $url =~ s|^/||; | ||
| 132 | $url =~ s/%(..)/chr(hex($1))/eg; | ||
| 133 | my($base) = split m|/|, $url; | ||
| 134 | |||
| 135 | debug("http get $base"); | ||
| 136 | my $f = $HTTP{$base}; | ||
| 137 | $f ||= \&http_notfound; | ||
| 138 | my( $content, $code, $text ) = $f->($url); | ||
| 139 | $code ||= 200; | ||
| 140 | $text ||= 'OK'; | ||
| 141 | |||
| 142 | my $res = "HTTP/1.0 $code $text\r\n" | ||
| 143 | . "Server: AC/MrGamoo\r\n" | ||
| 144 | . "Connection: close\r\n" | ||
| 145 | . "Content-Type: text/plain; charset=UTF-8\r\n" | ||
| 146 | . "Content-Length: " . length($content) . "\r\n" | ||
| 147 | . "\r\n" | ||
| 148 | . $content ; | ||
| 149 | |||
| 150 | $me->write_and_shut($res); | ||
| 151 | } | ||
| 152 | |||
| 153 | ################################################################ | ||
| 154 | |||
| 155 | sub http_notfound { | ||
| 156 | my $url = shift; | ||
| 157 | |||
| 158 | return ("404 NOT FOUND\nThe requested url /$url was not found on this server.\nSo sorry.\n\n", 404, "Not Found"); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | 1; |