1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Site search using Perl + CGI</title>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Site search using Perl + CGI</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="/assets/css/skeleton.css">
</head>
</head>
<body>
<div id="nav-container" class="container">
<ul id="navlist" class="left">
<li >
<a href="/" class="link-decor-none">hme</a>
</li>
<li class="active">
<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>
<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>
<div class="container">
<div class="container-2">
<h2 class="center" id="title">SITE SEARCH USING PERL + CGI</h2>
<h6 class="center">29 DECEMBER 2025</h5>
<br>
<div class="twocol justify"><p>Need a way to search site–number of articles are growing.</p>
<p>Searching site client-side using the RSS feed and JavaScript is not an option–
bloats the feed and breaks the site for Lynx and other text browsers.</p>
<p>Perl’s great for text processing–especially regex work. Few lines of Perl
could do a regex search and send the result back via CGI. OpenBSD httpd speaks
CGI, Perl and slowcgi are in the base systems. No dependencies. Works on every
conceivable browser.</p>
<p>Perl: traverse the directory with File::Find recursively. If search text is
found grab the file name, title and up to 50 chars of the first paragraph to
include in the search result.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>find(sub {
if (open my $fh, '<', $_) {
my $content = do { local $/; <$fh> };
close $fh;
if ($content =~ /\Q$search_text\E/i) {
my ($title) = $content =~ /<title>(.*?)<\/title>/is;
$title ||= $File::Find::name;
my ($p_content) = $content =~ /<p[^>]*>(.*?)<\/p>/is;
my $snippet = $p_content || "";
$snippet =~ s/<[^>]*>//g;
$snippet =~ s/\s+/ /g;
$snippet = substr($snippet, 0, 50);
$snippet .= "..." if length($p_content || "") > 50;
push @results, {
path => $File::Find::name,
title => $title,
snippet => $snippet
};
}
}
}, $dir);
</code></pre></div></div>
<p>Don’t need the Perl CGI module, httpd sets QUERY_STRING for the slowcgi script:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>my %params;
if ($ENV{QUERY_STRING}) {
foreach my $pair (split /&/, $ENV{QUERY_STRING}) {
my ($key, $value) = split /=/, $pair;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$params{$key} = $value;
}
}
</code></pre></div></div>
<p>Run the script as www user. Permissions: 554 (read + execute).</p>
<p>Running in OpenBSD chroot: Check Perl’s dynamic object dependencies:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ldd $(which perl)
/usr/bin/perl:
Start End Type Open Ref GrpRef Name
000008797e8e6000 000008797e8eb000 exe 1 0 0 /usr/bin/perl
0000087c1ffe5000 0000087c20396000 rlib 0 1 0 /usr/lib/libperl.so.26.0
0000087bf4508000 0000087bf4539000 rlib 0 2 0 /usr/lib/libm.so.10.1
0000087b9e801000 0000087b9e907000 rlib 0 2 0 /usr/lib/libc.so.102.0
0000087bba182000 0000087bba182000 ld.so 0 1 0 /usr/libexec/ld.so
</code></pre></div></div>
<p>Copy them over to chroot. Now should have /var/www/usr/bin/perl,
/usr/lib/libperl.so.26.0, and so on.</p>
<p>Troubleshooting: look for issues in logs or try executing the script in chroot:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ cat /var/log/messages | grep slowcgi
# chroot /var/www/ htdocs/path/to/script/script.cgi
</code></pre></div></div>
<p>The last command exposes any missing Perl modules in chroot and where to find
them. Copy them over as well.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>location "/cgi-bin/*" {
fastcgi socket "/run/slowcgi.sock"
}
</code></pre></div></div>
<p>in httpd.conf routes queries to slowcgi.</p>
</div>
<p class="post-author right">by W. D. Sadeep Madurange</p>
</div>
</div>
</main>
<div class="footer">
<div class="container">
<div class="twelve columns right container-2">
<p id="footer-text">© ASCIIMX - 2025</p>
</div>
</div>
</div>
</body>
</html>
|