-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.cgi
executable file
·217 lines (198 loc) · 5.5 KB
/
search.cgi
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#! /usr/bin/perl
###############################################################################
#
# NAME
#
# search.cgi
#
# SYNOPSIS
#
# <form action="search.cgi">
# <input type="text" name="Search" /><br />
# <input type="checkbox" name="Stem" /><b>Stem words</b>
# <input type="submit" value="Search" />
# </form>
#
# DESCRIPTION
#
# This is a toy example CGI script written in Perl 5 to show how to
# interface SWISH++ to a web-based search form. For errors, it simply
# calls "die" and the message goes to the web server log file and the
# user sees "Internal Server Error." A real CGI script should report
# errors better. This is an exercise for the reader.
#
# SEE ALSO
#
# search(1), CGI(3)
#
# Larry Wall, et al. "Programming Perl," 3rd ed., O'Reilly and
# Associates, Inc., Sebastopol, CA, 2000.
#
###############################################################################
use lib qw( /usr/lib/swish++ );
# Put the path to where the WWW library is above.
require WWW;
$SWISH_BIN = '/usr/bin';
# The full path to the bin directory where you installed the
# SWISH++ executables.
$DOC_ROOT = '/var/www/docs/train';
# The top-level directory for your document tree presumeably
# where the index was generated from.
$INDEX_FILE = '/var/www/docs/train/swish++.index';
# The full path to the index file to be searched through.
#$SOCKET_FILE = '/tmp/search.socket';
# The full path to the socket file. Uncomment this only if you
# run 'search' as a daemon listening to a Unix domain socket.
#$SOCKET_ADDRESS = '*:1967';
# The host:port of the TCP socket. Uncomment this only if you
# run 'search' as a daemon listening to a TCP socket.
##
# Get Search parameter.
##
use CGI;
my $q = new CGI;
my $search = $q->param( 'Search' ) || die "no Search parameter";
##
# Add in specified options.
##
my @options;
push( @options, '-s' ) if $q->param( 'Stem' );
##
# Call 'search' either as a client or as a command. In a real CGI, as opposed
# to this toy example, you would have the code only for the case you are
# actually doing.
##
if ( $SOCKET_FILE || $SOCKET_ADDRESS ) {
use Socket;
if ( $SOCKET_FILE ) {
##
# Connect to the 'search' server via a Unix domain socket.
##
socket( SEARCH, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!";
connect( SEARCH, sockaddr_un( $SOCKET_FILE ) ) ||
die "connect: $!";
} else {
##
# Connect to the 'search' server via a TCP socket.
##
my( $host, $port ) = $SOCKET_ADDRESS =~ /(?:([^\s:]+):)?(\d+)/;
$host = 'localhost' if $host eq '' || $host =~ /^\*?$/;
my $iaddr = inet_aton( $host ) ||
die "$me: \"$host\": bad or unknown host\n";
socket( SEARCH, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) ||
die "socket: $!";
connect( SEARCH, sockaddr_in( $port, $iaddr ) ) ||
die "connect: $!";
}
##
# We *MUST* set autoflush for the socket filehandle otherwise the
# server thread will hang since I/O buffering will wait for the buffer
# to fill that will never happen since queries are short. See [Wall],
# p. 781.
##
select( (select( SEARCH ), $| = 1)[0] );
##
# We should end command-line options with "--" to signal the end of
# legitimate options. If not given, it may be possible for a user to
# give options in the search terms.
#
# We also *MUST* print a trailing newline since the server reads an
# entire line of input (so therefore it looks and waits for a newline).
##
print SEARCH "search @options -- $search\n";
} else {
##
# Zap dangerous characters before exposing to shell; escape rest.
##
$search =~ s/[^\s&'()*\-=\w]/ /g;
$search =~ s/([&'()*])/\\$1/g;
##
# Open a pipe from the 'search' command.
#
# We should end command-line options with "--" to signal the end of
# legitimate options. If not given, it may be possible for a user to
# give options in the search terms.
##
open( SEARCH, "$SWISH_BIN/search++ -i $INDEX_FILE @options -- $search |" )
|| die "open: $!";
}
##
# Print header HTML
##
print <<END;
Content-Type: text/html
<html>
<head><title>Résultats de la recherche</title></head>
<body>
<big><b>Résultats de la recherche</b></big><hr>
<table border="0" cellpadding="0" cellspacing="0">
END
##
# Read the search results back.
##
while ( <SEARCH> ) {
if ( /^# ignored: / ) {
##
# Get the ignored words so we can report them to the user.
##
$ignored = $';
next;
}
##
# Future releases of SWISH++ may emit other comments: ignore ones we
# don't know about.
##
next if /^#/;
my( $rank, $file, $size, $title ) = split( / /, $_, 4 );
chomp($title);
my $htmlFile=$file;
my $tgzFile=$file;
$htmlFile =~ s/collection/html_collection/;
$htmlFile =~ s/tex$/html/;
$tgzFile =~ s/tex/tgz/;
my $desc="-";
if (-e "$DOC_ROOT/$htmlFile") {
$desc = WWW::extract_description( "$DOC_ROOT/$htmlFile" );
WWW::hyperlink( $desc );
}
$size = int( $size / 1024 );
if ( $size ) {
$size .= 'K';
} else {
$size = '<1K';
}
print <<END;
<tr valign="top">
<td align="right">
$rank%
</td>
<td>
<dl>
<dt><b><a href="$file">$title</a></b> ($size)</dt>
<dd>$desc</dd>
<dd><a href="$tgzFile">Archive des sources</a></dd>
END
if (-e "$DOC_ROOT/$htmlFile") {
$title =~ s/tex$/html/;
print " <dt>Rendu en HTML</dt>\n <dd><b><a href='$htmlFile'>$title</a></b></dd>";
}
print <<END;
</dl>
</td>
</tr>
END
}
close( SEARCH ) || die "close: $!";
print "</table>\n";
if ( $? ) {
print "<b>malformed query</b>\n";
} else {
print "<p>ignored: $ignored\n" if $ignored;
}
##
# Print footer HTML
##
print <<END;
</body>
</html>
END