Permalink
Browse files

use Sebastian Riedel's version

  • Loading branch information...
xantus committed Feb 17, 2010
1 parent 35fa98c commit f6f998e14dc476382f28c80a8c11115ea345c2d9
Showing with 48 additions and 60 deletions.
  1. +48 −60 script/flash-policy-server
View
@@ -1,84 +1,72 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
-use FindBin;
-use lib "$FindBin::Bin/../mojo/lib";
+# Copyright (C) 2008-2010, Sebastian Riedel.
use strict;
use warnings;
-die "This needs to be run as root, since the flash socket policy server listens on port 843." unless ( $> == 0 && $< == 0 );
+# Use bundled libraries
+use FindBin;
+use lib "$FindBin::Bin/../lib";
-# flash policy socket server
-my $server = Custom::Daemon->new;
+# After all this time, somebody else with one eye... who ISN'T a clumsy
+# carpenter or a kid with a BB gun.
+use Mojo::IOLoop;
-$server->ioloop->listen(
- port => 843,
- cb => sub {
- my ( $self, $id ) = @_;
-
- $self->timer( $id => ( after => 5, cb => sub {
- $self->drop( $id );
- }));
-
- # Incoming data
- $self->read_cb( $id => sub {
- my ( $self, $id, $chunk ) = @_;
- if ( $chunk ) {
- warn "843: got policy request\n";
- # Got some data, time to write
- $self->writing( $id );
- }
- });
+# Run as root only
+die "Server needs to run as user root to be able to listen to port 843.\n"
+ unless $> == 0 && $< == 0;
- # Ready to write
- $self->write_cb( $id => sub {
- shift->finish( shift );
- return qq|<?xml version="1.0"?>
+# The loop
+my $loop = Mojo::IOLoop->new;
+
+# Flash policy xml
+my $xml = <<'EOF';
+<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*" to-ports="*" secure="false"/>
</cross-domain-policy>
-\0|;
- });
- }
-);
-
-$server->lock_file('/tmp/mojo_flash_policy.lock');
-$server->pid_file('/tmp/mojo_flash_policy.pid');
-
+EOF
-$server->run;
-
-package Custom::Daemon;
+# Flash policy server example
+$loop->listen(
+ port => 843,
+ cb => sub {
+ my ($loop, $id) = @_;
-use strict;
-use warnings;
+ # Read callback
+ $loop->read_cb(
+ $id => sub {
+ my ($loop, $id, $chunk) = @_;
-use base 'Mojo::Server::Daemon';
+ # Writing
+ $loop->writing($id);
+ }
+ );
-sub new {
- my $class = shift;
- $class->SUPER::new(@_);
-}
+ # Write callback
+ $loop->write_cb(
+ $id => sub {
+ my ($loop, $id) = @_;
-sub prepare_ioloop {
- my $self = shift;
+ # Finish
+ $loop->drop($id);
- # Lock callback
- $self->ioloop->lock_cb(sub { $self->accept_lock($_[1]) });
+ # Write XML
+ return $xml;
+ }
+ );
- # Unlock callback
- $self->ioloop->unlock_cb(sub { $self->accept_unlock });
+ # Reading only
+ $loop->not_writing($id);
+ }
+) or die "Couldn't create listen socket!\n";
- # Stop ioloop on HUP signal
- $SIG{HUP} = sub { $self->ioloop->stop };
+print "Starting server on port 843.\n";
- # Max clients
- $self->ioloop->max_connections($self->max_clients);
-
- print "Flash Socket Policy Server available on port 843.\n";
-}
+# Start loop
+$loop->start;
1;
-

0 comments on commit f6f998e

Please sign in to comment.