From 9acc230624868626c60ad5ee634ea48f90de3e71 Mon Sep 17 00:00:00 2001 From: Will Budic Date: Thu, 7 Dec 2023 08:09:14 +1100 Subject: [PATCH] Implemented compression for service. --- htdocs/cgi-bin/CNFServices.cgi | 30 +++++++++++++++---- htdocs/cgi-bin/rss_feeds_setup.cnf | 14 +++++++++ htdocs/cgi-bin/system/modules/CNFParser.pm | 11 +++++-- .../cgi-bin/system/modules/RSSFeedsPlugin.pm | 20 +++++++++++-- htdocs/cgi-bin/wsrc/feeds.js | 2 +- 5 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 htdocs/cgi-bin/rss_feeds_setup.cnf diff --git a/htdocs/cgi-bin/CNFServices.cgi b/htdocs/cgi-bin/CNFServices.cgi index 4b71978..3513927 100755 --- a/htdocs/cgi-bin/CNFServices.cgi +++ b/htdocs/cgi-bin/CNFServices.cgi @@ -17,6 +17,7 @@ use feature qw(signatures); # system requirments or/and unexpected perl compiler errors. ## use CGI::Carp qw(fatalsToBrowser set_message); +use Gzip::Faster; use lib "system/modules"; #use lib::relative "system/modules"; @@ -25,11 +26,16 @@ require CNFNode; our $GLOB_HTML_SERVE = "'{}/*.cgi' '{}/*.htm' '{}/*.html' '{}/*.md' '{}/*.txt'"; our $script_path = $0; $script_path =~ s/\w+.cgi$//; + +use constant VERSION => '1.1'; + + use constant LOG_Settings => q( <<@<%LOG> file = web_server.log - # Should it mirror to console too? - console = 1 + # Should it mirror to console too, in productation this must be zero, + # otherwise it will not output client. + console = 0 # Disable/enable output to file at all? enabled = 1 # Tail size cut, set to 0 if no tail cutting is desired. @@ -51,6 +57,7 @@ cgi { } ); + # my $p = $cgi->_body_params->{keyed}; # $p->{'service'} = ['contacts']; # $p->{'action'} = ['form']; @@ -60,6 +67,7 @@ cgi { # $p->{'service'} = ['feeds']; # $p->{'action'} = ['read']; # $p->{'feed'} = ['Life Hacker']; + # $p->{'setup'} = ['htdocs/cgi-bin/rss_feeds_setup.cnf']; ## @@ -70,12 +78,22 @@ cgi { sub CNFHTMLService($cgi) { - my $cnf = CNFParser -> new (undef,{ DO_ENABLED => 1, HAS_EXTENSIONS=>1, ANONS_ARE_PUBLIC => 1, CGI=>$cgi }); + + my $setup = $cgi->param('setup'); + my $cnf = CNFParser -> new ($setup,{ DO_ENABLED => 1, HAS_EXTENSIONS=>1, ANONS_ARE_PUBLIC => 1, CGI=>$cgi }); $cnf -> parse(undef,_getServiceScript($cgi)); my $ptr = $cnf->data(); $ptr = $ptr->{'PAGE'}; - #say $$ptr if $ptr; - $cgi -> render(text=>$$ptr); + $cgi-> add_response_header('Expires', '1s'); + $cgi-> add_response_header('Cache-Control', 'no-cache'); + + if( $ENV{'HTTP_ACCEPT_ENCODING'} =~ m/gzip/ ){ + $cgi-> add_response_header('Content-Encoding', 'gzip'); + $cgi-> add_response_header('Accept-Encoding','Vary'); + $cgi-> render(data=>gzip($$ptr)); + }else{ + $cgi-> render(text=>$$ptr); + } return 0 } @@ -149,10 +167,12 @@ of the Perl programming language through open discussion, collaboration, design, The Perl Weekly ( http://perlweekly.com/ ) is a newsletter including links to blog posts and other news items related to the Perl programming language.~ #`The Cipher Brief RSS Feed`https://www.thecipherbrief.com/feed` `The Cipher Brief is the go-to digital platform for the latest security news and high-level analysis. Each day, we offer multiple expert perspectives, engaging the private sector to find solutions and foster dialogue on what events mean for businesses and organizations around the world. More than just reporting on the news, The Cipher Brief helps readers understand what the news means to you.~ + #`Viral Now`https://viralnow.uk/feed/`3 business days`ViralNow is a dynamic online platform at the forefront of curating and delivering trending and viral content. ViralNow brings you the latest and most engaging stories, videos, and articles from around the world.~ #`The Sydney Morning Herald - World RSS Feed`http://www.smh.com.au/rssheadlines/world/article/rss.xml`1 business days` The Sydney Morning Herald is Australia's leading news source. The Sydney Morning Herald sets the standard for journalistic excellence for Sydney, Australia, and the rest of the world. From breaking news, to travel and fashion, The Sydney Morning Herald continues to transform the way Australians get their news.~ + #`Life Hacker`https://lifehacker.com/feed/rss`3 business days`Lifehacker's is an award-winning daily blog that features tips, shortcuts, and downloads that help you work and live smarter and more efficiently.`~ #`Politico`http://www.politico.com/rss/politicopicks.xml`3 business days`POLITICO strives to be the dominant source for news on politics and policy in power centers across every continent where access to reliable information, nonpartisan journ.lism and real-time tools create, inform and engage a global citizenry.~ diff --git a/htdocs/cgi-bin/rss_feeds_setup.cnf b/htdocs/cgi-bin/rss_feeds_setup.cnf new file mode 100644 index 0000000..4d702d1 --- /dev/null +++ b/htdocs/cgi-bin/rss_feeds_setup.cnf @@ -0,0 +1,14 @@ +!CNF3.0 +<< RSS_FEEDS __CONST____ + +ID`Name`URL`Expires`Description~ + +#`The Sydney Morning Herald - World RSS Feed`http://www.smh.com.au/rssheadlines/world/article/rss.xml` + 1 business days` +The Sydney Morning Herald is Australia's leading news source. The Sydney Morning Herald sets the standard for journalistic excellence for Sydney, Australia, and the rest of the world. From breaking news, to travel and fashion, The Sydney Morning Herald continues to transform the way Australians get their news.~ + +#`Life Hacker`https://lifehacker.com/feed/rss` + 13 business days` +Lifehacker's is an award-winning daily blog that features tips, shortcuts, and downloads that help you work and live smarter and more efficiently.`~ + +>> \ No newline at end of file diff --git a/htdocs/cgi-bin/system/modules/CNFParser.pm b/htdocs/cgi-bin/system/modules/CNFParser.pm index 61c7c0f..7e47b96 100644 --- a/htdocs/cgi-bin/system/modules/CNFParser.pm +++ b/htdocs/cgi-bin/system/modules/CNFParser.pm @@ -619,6 +619,7 @@ sub loadDataFile { my ($self,$e,$path,$v,$i)=@_; sub doDataInstruction_{ my ($self,$e,$v,$t,$d)=@_; my $add_as_SQLTable = $v =~ s/${meta('SQL_TABLE')}/""/sexi; my $isPostgreSQL = $v =~ s/${meta('SQL_PostgreSQL')}/""/sexi; + my $isConstant = $v =~ s/$meta_const//se; my $isHeader = 0; $v=~ s/^\s*//gm; foreach my $row(split(/~\s/,$v)){ @@ -664,11 +665,17 @@ sub doDataInstruction_{ my ($self,$e,$v,$t,$d)=@_; my $existing = $self->{'__DATA__'}{$e}; if(defined $existing){ + if($isConstant){ + return; #Not allowed META const protected to overwrite. + } if($isHeader){$isHeader=0;next} my @rows = @$existing; push @rows, [@a] if scalar @a >0; $self->{'__DATA__'}{$e} = \@rows }else{ + if($isConstant){ + $isConstant = 0; #These are constant to be made brand new entries. + } my @rows; push @rows, [@a]; $self->{'__DATA__'}{$e} = \@rows if scalar @a >0; } @@ -1345,8 +1352,8 @@ sub log { my $fh = File::ReadBackwards->new($logfile) or die $!; my @buffer; $buffer[@buffer] = $fh->readline() for (1..$tail_cnt); open (my $fhTemp, ">", "/tmp/$logfile") or die $!; - print $fhTemp $_ foreach (reverse @buffer); - close $fhTemp; + foreach my $ln(reverse @buffer){ print $fhTemp $ln if $ln} + close $fhTemp; move("/tmp/$logfile",$logfile) } } diff --git a/htdocs/cgi-bin/system/modules/RSSFeedsPlugin.pm b/htdocs/cgi-bin/system/modules/RSSFeedsPlugin.pm index 3911c4a..1a223df 100644 --- a/htdocs/cgi-bin/system/modules/RSSFeedsPlugin.pm +++ b/htdocs/cgi-bin/system/modules/RSSFeedsPlugin.pm @@ -16,7 +16,7 @@ use LWP::Simple; use Benchmark; -use constant VERSION => '1.0'; +use constant VERSION => '1.1'; # require CNFNode; # require CNFDateTime; @@ -49,10 +49,10 @@ sub process ($self, $parser, $property) { $data[$idx]=\@col; } if($cgi&&$cgi->param('action') eq 'list'){ - my $page = '

List Of Feeds

    '; + my $page = _css().'

    List Of Feeds

      '; for my $idx (1 .. $#data){ my @col = @{$data[$idx]}; - $page .= qq|
    1. + $page .= qq|
    2. $col[1]   [ $col[4] ]
      $col[3]
    3. \n|; } @@ -64,6 +64,20 @@ sub process ($self, $parser, $property) { $parser->data()->{$property} = \@data } +sub _css{ +<<__CSS; + +__CSS +} + sub collectFeeds($self, $parser) { my $property = $self->{property}; my %hdr; diff --git a/htdocs/cgi-bin/wsrc/feeds.js b/htdocs/cgi-bin/wsrc/feeds.js index e3bf89b..2dc1afe 100644 --- a/htdocs/cgi-bin/wsrc/feeds.js +++ b/htdocs/cgi-bin/wsrc/feeds.js @@ -28,7 +28,7 @@ function fetchFeed(feed) { $.post('CNFServices.cgi', {service:'feeds', action:'read', feed:feed}, displayFeeds).fail( function(response) { pnl.html(response.responseText); - pnl.fadeOut(30000); + // pnl.fadeOut(30000); } ); } -- 2.34.1