From 1d2d499ebebdf3d44da4831568a4b512e1c5e8e0 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 27 Jul 2010 20:39:40 +0200 Subject: [PATCH] slurp index.html from __DATA__ just once --- scripts/RFID-JSONP-server.pl | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/scripts/RFID-JSONP-server.pl b/scripts/RFID-JSONP-server.pl index 9f1e3d6..34dfaa9 100755 --- a/scripts/RFID-JSONP-server.pl +++ b/scripts/RFID-JSONP-server.pl @@ -28,6 +28,12 @@ use lib 'lib'; use RFID::Serial::3M810; my $rfid = RFID::Serial::3M810->new; +my $index_html; +{ + local $/ = undef; + $index_html = ; +} + sub http_server { my $server = IO::Socket::INET->new( @@ -41,24 +47,12 @@ sub http_server { print "Server $0 ready at $server_url\n"; - sub static { - my ($client,$path) = @_; - - return unless $path eq '/'; - - print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"; - while() { - print $client $_; - } - - return $path; - } - while (my $client = $server->accept()) { $client->autoflush(1); my $request = <$client>; warn "WEB << $request\n" if $debug; + my $path; if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { my $method = $1; @@ -70,8 +64,10 @@ sub http_server { } warn "WEB << param: ",dump( $param ) if $debug; } - if ( my $path = static( $client,$1 ) ) { - warn "WEB >> $path" if $debug; + $path = $method; + + if ( $path eq '/' ) { + print $client "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n$index_html"; } elsif ( $method =~ m{/scan} ) { my $tags = $rfid->scan; my $json = { @@ -138,7 +134,7 @@ http_server; __DATA__ -3M RFID +RFID JSONP