diff options
Diffstat (limited to '_site/cgi-bin/find_one_file.cgi')
| -rw-r--r-- | _site/cgi-bin/find_one_file.cgi | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/_site/cgi-bin/find_one_file.cgi b/_site/cgi-bin/find_one_file.cgi new file mode 100644 index 0000000..a28f8c4 --- /dev/null +++ b/_site/cgi-bin/find_one_file.cgi @@ -0,0 +1,184 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Storable qw(retrieve); +use Encode qw(decode_utf8); +use HTML::Escape qw(escape_html); +use Time::HiRes qw(gettimeofday tv_interval); +use BSD::Resource; + +# 1. Start Benchmark Timer +my $start_time = [gettimeofday]; +my $files_read = 0; # Track IO Activity + +# Configuration +my $max_parallel = 100; +my $lock_timeout = 30; +my $max_results = 1000; +my $min_query_len = 3; +my $index_file = 'search_index.dat'; +my $lock_dir = '/tmp/search_locks'; + +# Concurrency control +mkdir $lock_dir, 0777 unless -d $lock_dir; +my $active_count = 0; +my $now = time(); + +opendir(my $dh, $lock_dir); +while (my $file = readdir($dh)) { + next unless $file =~ /\.lock$/; + my $path = "$lock_dir/$file"; + my $mtime = (stat($path))[9] || 0; + ( $now - $mtime > $lock_timeout ) ? unlink($path) : $active_count++; +} +closedir($dh); + +# Too many search requests +if ($active_count >= $max_parallel) { + print "Content-Type: text/html\n\n"; + render_html("<p>Server busy. Please try again in a few seconds.</p>", "", (localtime)[5]+1900); + exit; +} + +my $lock_file = "$lock_dir/$$.lock"; +open(my $fh_lock, '>', $lock_file); +$files_read++; # IO for lock creation + +my $search_text = ''; +if (($ENV{QUERY_STRING} || '') =~ /^q=([^&]*)/) { + $search_text = decode_utf8($1 // ""); + $search_text =~ s/\P{Print}//g; + $search_text = substr($search_text, 0, 64); + $search_text =~ s/^\s+|\s+$//g; +} + +my $safe_search_text = escape_html($search_text); +my $year = (localtime)[5] + 1900; + +print "Content-Type: text/html\n\n"; + +if ($search_text eq '') { + final_output("<p>Please enter a search term above.</p>"); +} + +if (length($search_text) < $min_query_len) { + final_output("<p>Search term is too short. Please enter at least $min_query_len characters.</p>"); +} + +if (!-f $index_file) { + final_output("<p>Search temporarily unavailable.</p>"); +} + +# IO for index retrieval +my $index = retrieve($index_file); +$files_read++; + +my @results; +my $found = 0; + +foreach my $url (sort keys %$index) { + last if $found >= $max_results; + my $data = $index->{$url}; + + next unless $data->{c} =~ /(.{0,40})(\Q$search_text\E)(.{0,40})/is; + my ($before, $actual, $after) = ($1, $2, $3); + $found++; + + $after =~ s/\s\S*$// if length($after) > 25; + $before =~ s/^.*?\s// if length($before) > 25; + + $before = ($before =~ /\S/) ? ucfirst($before) : ""; + $actual = ($before eq "") ? ucfirst($actual) : $actual; + + my $snippet = escape_html($before) . "<b>" . escape_html($actual) . "</b>" . escape_html($after) . "..."; + + push @results, { + path => $url, + title => escape_html($data->{t}), + snippet => $snippet + }; +} + +my $list_html = ""; +if (@results == 0) { + $list_html = "<p>No results found for \"<b>$safe_search_text</b>\".</p>"; +} else { + $list_html = "<ul>" . join('', map { + "<li><a href=\"/$_->{path}\">$_->{title}</a><br><small>$_->{snippet}</small></li>" + } @results) . "</ul>"; +} + +final_output($list_html); + +sub final_output { + my ($content) = @_; + + # 2. Calculate Metrics just before rendering + my $elapsed = tv_interval($start_time, [gettimeofday]); + my $rusage = getrusage(); + my $user_cpu = $rusage->utime; + my $system_cpu = $rusage->stime; + my $max_rss = $rusage->maxrss; + + my $bench_html = <<"BENCH"; +<div style="background: #f4f4f4; padding: 10px; border-radius: 5px; font-family: monospace; font-size: 0.85em; margin-top: 20px; border: 1px solid #ddd;"> + <strong>Performance Metrics:</strong><br> + Total Time: @{[ sprintf("%.4f", $elapsed) ]} seconds<br> + User CPU: $user_cpu s<br> + System CPU: $system_cpu s<br> + Peak RAM: $max_rss KB<br> + Files Read: $files_read (IO Activity) +</div> +BENCH + + render_html($content . $bench_html, $safe_search_text, $year); + close($fh_lock) if $fh_lock; + unlink($lock_file) if -f $lock_file; + exit; +} + +sub render_html { + my ($content, $q_val, $yr) = @_; + print <<"HTML"; +<!DOCTYPE html> +<html lang="en-us"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Search</title> + <link rel="stylesheet" href="/assets/css/main.css"> + <link rel="stylesheet" href="/assets/css/skeleton.css"> +</head> +<body> + <div id="nav-container" class="container"> + <ul id="navlist" class="left"> + <li><a href="/" class="link-decor-none">hme</a></li> + <li><a href="/log/" class="link-decor-none">log</a></li> + <li><a href="/projects/" class="link-decor-none">poc</a></li> + <li><a href="/about/" class="link-decor-none">abt</a></li> + <li class="active"><a href="/cgi-bin/find.cgi" class="link-decor-none">sws</a></li> + <li><a href="/feed.xml" class="link-decor-none">rss</a></li> + </ul> + </div> + <main class="container" id="main"> + <div class="container"> + <h2>Search</h2> + <form action="" method="GET"> + <input id="search-box" type="text" name="q" value="$q_val"> + <input id="search-btn" type="submit" value="Search"> + </form> + $content + </div> + </main> + <div class="footer"> + <div class="container"> + <div class="twelve columns right container-2"> + <p id="footer-text">© ASCIIMX - $yr</p> + </div> + </div> + </div> +</body> +</html> +HTML +} |
