Skip to content

Commit

Permalink
Merge pull request #14 from waynieack/HTTP_Updates
Browse files Browse the repository at this point in the history
Http updates merge to my master
  • Loading branch information
Wayne Gatlin authored Sep 21, 2017
2 parents 53a16cb + d9bd75b commit 55033d4
Show file tree
Hide file tree
Showing 31 changed files with 1,754 additions and 702 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ nytprof.out
pm_to_blib
*~
*.tmp
*.log
*.log
138 changes: 62 additions & 76 deletions bin/mh
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ sub setup {
mkdir( "$config_parms{html_dir}/tv/clicktv", 0777 )
unless -d "$config_parms{html_dir}/tv/clicktv";

$config_parms{http_persist} = 1
unless defined($config_parms{http_persist});

$Time_Date = &time_date_stamp( $config_parms{time_format_log}, $Time ); # Needed by print_log

&open_logs;
Expand Down Expand Up @@ -886,9 +889,8 @@ sub setup {
# my $break = $config_parms{$port_name . "_break"};

$proto = 'tcp' unless $proto;
$datatype = 'buffered' if $port_name eq 'http';
$datatype = 'buffered'
if $config_parms{ $port_name . "_buffer" }; # Grandfathered syntax
$datatype = 'raw' if $port_name eq 'http';
$datatype = 'buffered' if $config_parms{ $port_name . "_buffer" }; # Grandfathered syntax
$datatype = '' unless $datatype;

# $break = "[\r\n]+" unless $break;
Expand All @@ -905,8 +907,8 @@ sub setup {
$Socket_Ports{$port_name}{address} = $address if $address;

# $Socket_Ports{$port_name}{break} = $break;

&socket_open($port_name);
$Socket_Ports{$port_name}{Socket_Item} = new Socket_Item( undef, undef, $port_name ) if $port_name eq 'http';
}

&AlexaBridge::startup; # Start the AlexaBridge sockets, in lib/AlexaBridge.pm
Expand Down Expand Up @@ -2493,7 +2495,7 @@ sub check_for_serial_data {
}
}

my ( $leave_socket_open_passes, $leave_socket_open_action, $leave_socket_open_check );
my ( $leave_socket_open_passes, $leave_socket_open_action, $leave_socket_open_check, $Close_Socket );
use vars '%socket_fork_data';

sub check_for_socket_data_http {
Expand Down Expand Up @@ -2529,72 +2531,31 @@ sub check_for_socket_data_http {
}
}

# Time to finish the http GET from 2 passes ago with a list of spoken data
# if ($leave_socket_open_passes and --$leave_socket_open_passes <= 0 and my $sock = $Socket_Ports{http}{socka}) {
if ( $leave_socket_open_action and ( --$leave_socket_open_passes <= 0 )
or ( $leave_socket_open_check and eval($leave_socket_open_check) ) )
{
my $sock = $Socket_Ports{http}{socka};
print "http: closing http port with action: s=$sock a=$leave_socket_open_action\n"
if $Debug{http};
my @data = eval($leave_socket_open_action);
print "Error in http lso action: $@\n" if $@;
my $html = &html_page( "", @data );
print $sock $html if $sock;
&socket_close('http');
$leave_socket_open_passes = 0;
$leave_socket_open_action = '';
$leave_socket_open_check = '';
}

# return if $leave_socket_open_passes; # Wait for previous request to finish
return if $leave_socket_open_action; # Wait for previous request to finish

my $loop_count = 0;
my $time_check = time;
while (1) {
my $http_socket = $Socket_Ports{http}{Socket_Item};
if ( ( $http_socket ) && ( my $http_data = $http_socket->said('http_server') ) ) {
( $leave_socket_open_passes, $Close_Socket ) = &http_process_request($Socket_Ports{http}{socka},$http_data);
if ( $main::Debug{http} ) {
my $peer = $Socket_Ports{http}{socka}->peername;
my ( $port, $iaddr ) = &main::unpack_sockaddr_in($peer) if $peer;
my $client_ip_address = &main::inet_ntoa($iaddr) if $iaddr;
print "http: Got data lsop=$leave_socket_open_passes cs=$Close_Socket ip=$client_ip_address p=$port\n";
}
}
# We only close sockets for legacy http 1.0 clients or if we see the "Connection: close" header - "$Close_Socket"
if ( $Close_Socket && $Socket_Ports{http}{socka} && !($leave_socket_open_passes eq -1) ) {
if ( $main::Debug{http} ) {
my $peer = $Socket_Ports{http}{socka}->peername;
my ( $port, $iaddr ) = &main::unpack_sockaddr_in($peer) if $peer;
my $client_ip_address = &main::inet_ntoa($iaddr) if $iaddr;
print "http: Closing socket cs=$Close_Socket lsop=$leave_socket_open_passes ip=$client_ip_address p=$port\n";
}
&socket_close('http');
}

# See if there is a http request
my $nfound = &socket_has_data( $Socket_Ports{http}{sock} );
#print "http: below nfound ....\n" if $Debug{http};
last unless $nfound > 0; # nfound = -1 means an error occurred
#print "http: below last unless nfound ....\n" if $Debug{http};
my $sock = $Socket_Ports{http}{sock}->accept();
#print "http: below sock ....\n" if $Debug{http};
last unless $sock; # Can be undef it socket was killed
#print "http: below last unless sock ....\n" if $Debug{http};
$sock->autoflush(1); # Not sure if this does anything?
$Socket_Ports{http}{socka} = $sock;

( $leave_socket_open_passes, $leave_socket_open_action ) = &http_process_request($sock);

my $time_diff = time - $time_check;
print "http: c=$loop_count td=$time_diff sop=$leave_socket_open_passes soa=$leave_socket_open_action. while loop\n"
if $Debug{http};
}

if ($leave_socket_open_action) {
last; # Wait for socket action to finish before we process the next request
}
else {
# forked socket needs no closing, and can not have a open_action
if ( $leave_socket_open_passes and $leave_socket_open_passes < 0 ) {
$leave_socket_open_passes = 0;
}
else {
# We must sleep here for a bit, or else Netscape sometimes
# says 'Document contains no data'. Guess we don't need this anymore :)
# select undef, undef, undef, .010;
#print "http: closing socket - ". $Info{'http_socket'}->peer ." ....\n" if $Debug{http};
&socket_close('http');
}
}

# Process more requests if present, for a faster server,
# but let mh do other things periodically
# last if $loop_count++ > 100;
last if $loop_count++ > 100 or $time_diff > 1;
}
}

sub check_for_socket_data {

Expand All @@ -2603,7 +2564,6 @@ sub check_for_socket_data {
# See which ports are active
# - could probably use a smarter select check here, rather than loop for each port
for my $port_name ( keys %Socket_Ports ) {
next if $port_name eq 'http'; # Deal with this elsewhere
# Need to use _flag var so active/inactive_this_pass is valid for 1 full pass.
$Socket_Ports{$port_name}{active_this_pass} = 0;
$Socket_Ports{$port_name}{active_this_pass} = 1
Expand Down Expand Up @@ -2631,7 +2591,7 @@ sub check_for_socket_data {
$Socket_Ports{$port_name}{client_ip_address} = $client_ip_address;
$Socket_Ports{$port_name}{client_port} = $client_port;
print "socket: $port_name nfound=$nfound client=$client_number"
if $Debug{$port_name};
if $Debug{$port_name} && $Debug{'socket'};
$server_active = 1;
last;
}
Expand Down Expand Up @@ -2671,7 +2631,7 @@ sub check_for_socket_data {
"$config_parms{data_dir}/logs/server.$Year_Month_Now.log",
"$port_name $client_ip_address";
print "\nsocket: $port_name active sock=$new_sock client=$client_ip_address.\n"
if $Debug{$port_name};
if $Debug{$port_name} && $Debug{'socket'};

$server_active = 1;
}
Expand Down Expand Up @@ -2748,12 +2708,12 @@ sub check_for_socket_data {
# When telnet closes, byte IS defined, but is empty, so check on ''
if ( !defined $data or $data eq '' ) {
print "socket: closing socket port $port_name\n"
if $Debug{$port_name};
if $Debug{$port_name} && $Debug{'socket'};
&socket_close($port_name);
}
else {
$Socket_Ports{$port_name}{data} .= $data if defined $data;
print "socket: port=$port_name data=$data.\n" if $Debug{$port_name};
print "socket: port=$port_name data=".$Socket_Ports{$port_name}{data}."\n" if $Debug{$port_name};

if ( my $echo = $config_parms{"${port_name}_echo"} ) {

Expand All @@ -2774,7 +2734,6 @@ sub check_for_socket_data {
# Look for 'records' based on break separator
# - this data may have been read from a previous pass
for my $port_name ( keys %Socket_Ports ) {
next if $port_name eq 'http'; # Deal with this elsewhere
next unless $Socket_Ports{$port_name}{data};

# If raw mode, return data as is
Expand All @@ -2795,7 +2754,7 @@ sub check_for_socket_data {
if ( my ( $record, $remainder ) = $Socket_Ports{$port_name}{data} =~ /(.+?)$break(.*)/s ) {
$Socket_Ports{$port_name}{data_record} = $record;
$Socket_Ports{$port_name}{data} = $remainder;
if ( $Debug{$port_name} ) {
if ( $Debug{$port_name} && $Debug{'socket'} ) {
print "socket: port=$port_name record=$record, r=$remainder.\n";

# print "socket: $port_name record=$record. hex=", unpack('H*', $record), "\n";
Expand Down Expand Up @@ -6698,8 +6657,15 @@ sub sig_handler_pipe {
}

sub sig_child_death {
use POSIX ":sys_wait_h";
local ($!, $?);
while ( (my $pid = waitpid(-1, WNOHANG)) > 0 ) {
&print_log( "***PID reaped $pid" . ($? ? " with exit $?" : '') ) if $::Debug{fork};
}
$SIG{CHLD} = \&sig_child_death; # in case of unreliable signals
}

# my $pid = wait;
sub sig_child_death2 {
# print "reaped $pid" . ($? ? " with exit $?" : '');
# Harvest potentially more than one dead child
use POSIX ":sys_wait_h";
Expand All @@ -6712,6 +6678,8 @@ sub sig_child_death {

} until $pid <= 0;

$SIG{CHLD} = \&sig_child_death;

# $pid = waitpid(-1, &WNOHANG);
# } until $pid == -1;
}
Expand All @@ -6720,7 +6688,12 @@ sub sig_child_death {
sub socket_close {
my ($port_name) = @_;
my $sock = $Socket_Ports{$port_name}{socka};
print "Closing socket port $port_name\n" if $Debug{$port_name};
if ( $main::Debug{$port_name} ) {
my $peer = $sock->peername;
my ( $port, $iaddr ) = &main::unpack_sockaddr_in($peer) if $peer;
my $client_ip_address = &main::inet_ntoa($iaddr) if $iaddr;
print "Closing socket port=$port_name ip=$client_ip_address p=$port\n"
}
unless ($sock) {
print "Error, socket_close sock port undefined for port $port_name\n";
}
Expand Down Expand Up @@ -7140,11 +7113,24 @@ sub speak {

# Intercept speak/log/print/last_response -> web wav
# - but not for wml response

# I can't find anything that uses this from http_server, but if it does then
# we will need to pass the $client_number, $requestnum to it and send it to
# html_ajax_long_poll.
if ( $leave_socket_open_action =~ /response/
and $leave_socket_open_action !~ /,'w'/ )
{
# Generate to_wave file unless webmute
if ( my $wav_file = &http_speak_to_wav_start( $phrase, $parms{voice}, $parms{compression} ) ) {
# I can't find anything that uses this from http_server, but if it does then
# we will need to pass the $client_number, $requestnum to it and send it to
# html_ajax_long_poll as commented below.
#my $socket = $Socket_Ports{http}{socka};
#my $sub = "html_page( \"\", (&main::http_speak_to_wav_finish(qq|$phrase|, qq|$wav_file|)) ) unless &Voice_Text::is_speaking_wav()\;";
#$leave_socket_open_action = undef;
#$leave_socket_open_check = undef;
#$leave_socket_open_passes = -1;
#&html_ajax_long_poll( $socket, undef, $sub, $client_number, $requestnum, 3);
$leave_socket_open_action = "&http_speak_to_wav_finish(qq|$phrase|, qq|$wav_file|)";
$leave_socket_open_check = "!&Voice_Text::is_speaking_wav()";
$leave_socket_open_passes = 999;
Expand Down
29 changes: 19 additions & 10 deletions code/common/android_server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -574,26 +574,27 @@ sub android_page {
# handle blank xsl name
my $style;
$style = qq|<?xml-stylesheet type="text/xsl" href="$xsl"?>| if $xsl;
return <<eof;
HTTP/1.0 200 OK
Server: MisterHouse
Content-type: text/xml
my $body = <<eof;
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
$style
<misterhouse>
$xml</misterhouse>
eof

my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: text/xml\r\n";
$output .= "Connection: close\r\n" if &http_close_socket;
$output .= "Content-Length: " . ( length $body ) . "\r\n";
$output .= "Date: " . time2str(time) . "\r\n";
$output .= "\r\n";
$output .= $body;
return $output;
}

sub android_usage {
my $html = <<eof;
HTTP/1.0 200 OK
Server: MisterHouse
Content-type: text/html
<html>
<head>
</head>
Expand Down Expand Up @@ -637,5 +638,13 @@ sub android_usage {
</html>
eof

return $html;
my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: text/html\r\n";
$output .= "Connection: close\r\n" if &http_close_socket;
$output .= "Content-Length: " . ( length $html ) . "\r\n";
$output .= "Date: " . time2str(time) . "\r\n";
$output .= "\r\n";
$output .= $html;
return $output;
}
54 changes: 30 additions & 24 deletions code/common/homebridge.pl
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,16 @@ sub hb_status {
print_log "[Homebridge]: Status request: item=$item state=$state status=[$data] type=$type"
if ($hb_debug);
}
return <<eof;
HTTP/1.0 200 OK
Server: MisterHouse
Content-Type: text/html
Cache-Control: no-cache
$data
eof
my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: text/html\r\n";
$output .= "Connection: close\r\n" if &http_close_socket;
$output .= "Content-Length: " . ( length $data ) . "\r\n";
$output .= "Cache-Control: no-cache\r\n";
$output .= "Date: " . time2str(time) . "\r\n";
$output .= "\r\n";
$output .= $data;
return $output;
}

sub hb_thermo_get_state {
Expand Down Expand Up @@ -268,14 +270,16 @@ sub hb_thermo_get_state {
print_log "[Homebridge]: Thermostat State request: item=$item mode=$mode status=[$data]\n"
if ($hb_debug);
}
return <<eof;
HTTP/1.0 200 OK
Server: MisterHouse
Content-Type: text/html
Cache-Control: no-cache
$data
eof
my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: text/html\r\n";
$output .= "Connection: close\r\n" if &http_close_socket;
$output .= "Content-Length: " . ( length $data ) . "\r\n";
$output .= "Cache-Control: no-cache\r\n";
$output .= "Date: " . time2str(time) . "\r\n";
$output .= "\r\n";
$output .= $data;
return $output;
}

sub hb_thermo_set_state {
Expand Down Expand Up @@ -347,14 +351,16 @@ sub hb_thermo_get_setpoint {
}
print_log "[Homebridge]: Thermostat Get Setpoint request: item=$item mode=$mode status=[$data]\n"
if ($hb_debug);
return <<eof;
HTTP/1.0 200 OK
Server: MisterHouse
Content-Type: text/html
Cache-Control: no-cache
$data
eof
my $output = "HTTP/1.1 200 OK\r\n";
$output .= "Server: MisterHouse\r\n";
$output .= "Content-type: text/html\r\n";
$output .= "Connection: close\r\n" if &http_close_socket;
$output .= "Content-Length: " . ( length $data ) . "\r\n";
$output .= "Cache-Control: no-cache\r\n";
$output .= "Date: " . time2str(time) . "\r\n";
$output .= "\r\n";
$output .= $data;
return $output;
}

sub hb_thermo_set_setpoint {
Expand Down
Loading

0 comments on commit 55033d4

Please sign in to comment.