@ -9,6 +9,7 @@ use Data::Dumper;
use File::Basename ;
use File::Basename ;
my $ ledLabel : shared = "LED is turned off" ;
my $ ledLabel : shared = "LED is turned off" ;
my $ ledFreq : shared = 10 ;
# auto-flush on socket
# auto-flush on socket
@ -85,7 +86,6 @@ sub parse_http
$ client - > recv ( $ buf , 1024 ) ;
$ client - > recv ( $ buf , 1024 ) ;
$ data . = $ buf ;
$ data . = $ buf ;
} while ( $ data !~ /\r\n\r\n/s ) ;
} while ( $ data !~ /\r\n\r\n/s ) ;
#print "Query: $data\n";
my % resp ;
my % resp ;
@ -121,6 +121,23 @@ sub parse_http
$ resp { method } = 'ERROR' ;
$ resp { method } = 'ERROR' ;
$ resp { error } = 'Invalid HTTP request' ;
$ resp { error } = 'Invalid HTTP request' ;
}
}
if ( $ resp { method } eq 'POST' )
{
my $ remaining = join ( "\r\n" , @ lines ) ;
my $ cnt_len = $ resp { fields } { 'Content-Length' } ;
while ( length ( $ remaining ) < $ cnt_len )
{
my $ buf = "" ;
$ client - > recv ( $ buf , 1024 ) ;
$ remaining . = $ buf ;
}
$ resp { postData } = $ remaining ;
my % pargs = split /[=\&]/ , $ remaining ;
$ resp { postArgs } = \ % pargs ;
}
}
}
else
else
{
{
@ -131,7 +148,7 @@ sub parse_http
return \ % resp ;
return \ % resp ;
}
}
sub error _response
sub simpl e_response
{
{
my ( $ code , $ msg ) = @ _ ;
my ( $ code , $ msg ) = @ _ ;
@ -183,7 +200,7 @@ sub process_http
my ( $ httpReq ) = @ _ ;
my ( $ httpReq ) = @ _ ;
if ( $ httpReq - > { method } eq 'ERROR' )
if ( $ httpReq - > { method } eq 'ERROR' )
{
{
return error _response ( 400 , $ httpReq - > { error } ) ;
return simpl e_response( 400 , $ httpReq - > { error } ) ;
}
}
if ( $ httpReq - > { url } =~ /\.json$/ )
if ( $ httpReq - > { url } =~ /\.json$/ )
@ -238,13 +255,11 @@ sub process_http
}
}
else
else
{
{
return error _response ( 404 , "File not found" ) ;
return simpl e_response( 404 , "File not found" ) ;
}
}
}
}
# TODO
return simple_response ( 400 , "Invalid HTTP request" ) ;
return error_response ( 400 , "Invalid HTTP request" ) ;
}
}
sub getMenu
sub getMenu
@ -339,6 +354,7 @@ sub readUserPages
sub process_user_comm_led
sub process_user_comm_led
{
{
my ( $ http ) = @ _ ;
my ( $ http ) = @ _ ;
my $ loadData = '' ;
if ( $ http - > { urlArgs } { reason } eq "button" )
if ( $ http - > { urlArgs } { reason } eq "button" )
{
{
@ -357,8 +373,20 @@ sub process_user_comm_led
$ ledLabel = "LED is turned off" ;
$ ledLabel = "LED is turned off" ;
}
}
}
}
elsif ( $ http - > { urlArgs } { reason } eq "submit" )
{
if ( exists $ http - > { postArgs } { frequency } )
{
$ ledFreq = $ http - > { postArgs } { frequency } ;
}
return simple_response ( 204 , "OK" ) ;
}
elsif ( $ http - > { urlArgs } { reason } eq "load" )
{
$ loadData = ', "frequency": ' . $ ledFreq ;
}
my $ r = '{"text": "' . $ ledLabel . '"}' ;
my $ r = '{"text": "' . $ ledLabel . '"' . $ loadData . ' }' ;
return content_response ( $ r , $ http - > { url } ) ;
return content_response ( $ r , $ http - > { url } ) ;
}
}