/usr/share/doc/swish-e/html/spider.html is in swish-e 2.4.7-3ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
***** GENERATED FILE *** DO NOT EDIT DIRECTLY - any changes will be LOST ******
swish-e.org mockup based on http://www.oswd.org/design/1773/prosimii/index2.html
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="./swish.css" media="screen" title="swish css" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="Last" href="./filter.html" />
<link rel="Prev" href="./search.cgi.html" />
<link rel="Up" href="./index.html" />
<link rel="Next" href="./filter.html" />
<link rel="Start" href="./index.html" />
<link rel="First" href="./readme.html" />
<title>Swish-e :: spider.pl - Example Perl program to spider web servers</title>
</head>
<body>
<!-- noindex -->
<!-- For non-visual user agents: -->
<div id="top"><a href="#main-copy" class="doNotDisplay doNotPrint">Skip to main content.</a></div>
<!-- ##### Header ##### -->
<div id="header">
<div class="superHeader">
<span>Related Sites:</span>
<a href="http://swishewiki.org/" title="swishe wiki">swish-e wiki</a> |
<a href="http://www.xmlsoft.org/" title="libxml2 home page">libxml2</a> |
<a href="http://www.zlib.net/" title="zlib home page">zlib</a> |
<a href="http://www.foolabs.com/xpdf/" title="xpdf home page">xpdf</a> |
<a href="http://dev.swish-e.org/browser" title="browse source code">Subversion</a>
</div>
<div class="midHeader">
<h1 class="headerTitle" lang="la">Swish-e</h1>
<div class="headerSubTitle">Simple Web Indexing System for Humans - Enhanced</div>
<br class="doNotDisplay doNotPrint" />
<div class="headerLinks">
<span class="doNotDisplay">Tools:</span>
<!-- don't know what platform, so link to download page -->
<a href="http://swish-e.org/download/index.html">download latest version</a>
</div>
</div>
</div>
<!-- index -->
<!-- noindex -->
<div class="subHeader">
<table width='100%'>
<tr>
<td align='left'>
<a href="http://swish-e.org/index.html">home</a> |
<a href="http://swish-e.org/support.html">support</a> |
<a href="http://swish-e.org/download/index.html">download</a>
</td>
<td align='right'>
<form method="get"
action="http://swish-e.org/search/index.html"
enctype="application/x-www-form-urlencoded"
class="srchform">
<label for="searchfield">Search for</label>
<input maxlength="200" value="" id="searchfield" size="30" name="query" type="text" alt="Search input field" />
<input value="search swish-e.org" name="submit" type="submit" class='button' />
</form>
</td>
</tr>
</table>
</div>
<!-- index -->
<div id="body-area" class="clearfix">
<div id="content-area">
<div id="main-copy">
<h1>spider.pl - Example Perl program to spider web servers</h1>
Swish-e version 2.4.7
<!-- noindex -->
<h2>Table of Contents</h2>
<div class="toc">
<ul class="toc">
<li>
<a href="#synopsis">SYNOPSIS</a>
</li>
<li>
<a href="#description">DESCRIPTION</a>
<ul class="toc">
<li>
<a href="#running_the_spider">Running the spider</a>
</li>
<li>
<a href="#robots_exclusion_rules_and_being_nice">Robots Exclusion Rules and being nice</a>
</li>
<li>
<a href="#duplicate_documents">Duplicate Documents</a>
</li>
<li>
<a href="#broken_relative_links">Broken relative links</a>
</li>
<li>
<a href="#compression">Compression</a>
</li>
</ul>
</li>
<li>
<a href="#requirements">REQUIREMENTS</a>
</li>
<li>
<a href="#configuration_file">CONFIGURATION FILE</a>
</li>
<li>
<a href="#configuration_options">CONFIGURATION OPTIONS</a>
</li>
<li>
<a href="#callback_functions">CALLBACK FUNCTIONS</a>
<ul class="toc">
<li>
<a href="#more_on_setting_flags">More on setting flags</a>
</li>
</ul>
</li>
<li>
<a href="#signals">SIGNALS</a>
</li>
<li>
<a href="#changes">CHANGES</a>
<ul class="toc">
<li>
<a href="#thu_sep_30_2004_changes_for_swish_e_2_4_3">Thu Sep 30 2004 - changes for Swish-e 2.4.3</a>
</li>
</ul>
</li>
<li>
<a href="#todo">TODO</a>
</li>
<li>
<a href="#copyright">COPYRIGHT</a>
</li>
<li>
<a href="#support">SUPPORT</a>
</li>
</ul>
</div>
<!-- index -->
<hr />
<div class="sub-section">
<h1><a name="synopsis"></a>SYNOPSIS</h1>
<pre class="pre-section"> spider.pl [<spider config file>] [<URL> ...]
# Spider using some common defaults and capture the output
# into a file
./spider.pl default http://myserver.com/ > output.txt
# or using a config file
spider.config:
@servers = (
{
base_url => 'http://myserver.com/',
email => 'me@myself.com',
# other spider settings described below
},
);
./spider.pl spider.config > output.txt
# or using the default config file SwishSpiderConfig.pl
./spider.pl > output.txt
# using with swish-e
./spider.pl spider.config | swish-e -c swish.config -S prog -i stdin
# or in two steps
./spider.pl spider.config > output.txt
swish-e -c swish.config -S prog -i stdin < output.txt
# or with compression
./spider.pl spider.config | gzip > output.gz
gzip -dc output.gz | swish-e -c swish.config -S prog -i stdin
# or having swish-e call the spider directly using the
# spider config file SwishSpiderConfig.pl:
swish-e -c swish.config -S prog -i spider.pl
# or the above but passing passing a parameter to the spider:
echo "SwishProgParameters spider.config" >> swish.config
echo "IndexDir spider.pl" >> swish.config
swish-e -c swish.config -S prog
Note: When running on some versions of Windows (e.g. Win ME and Win 98 SE)
you may need to tell Perl to run the spider directly:
perl spider.pl | swish-e -S prog -c swish.conf -i stdin
This pipes the output of the spider directly into swish.</pre>
</div>
<div class="sub-section">
<h1><a name="description"></a>DESCRIPTION</h1>
<p><i>spider.pl</i> is a program for fetching documnts from a web server,
and outputs the documents to STDOUT in a special format designed
to be read by Swish-e.</p>
<p>The spider can index non-text documents such as PDF and MS Word by use of
filter (helper) programs. These programs are not part of the Swish-e
distribution and must be installed separately. See the section on filtering
below.</p>
<p>A configuration file is noramlly used to control what documents are fetched
from the web server(s). The configuration file and its options are described
below. The is also a "default" config suitable for spidering.</p>
<p>The spider is designed to spider web pages and fetch documents from one
host at a time -- offsite links are not followed. But, you can configure
the spider to spider multiple sites in a single run.</p>
<p><i>spider.pl</i> is distributed with Swish-e and is installed in the swish-e
library directory at installation time. This directory (libexedir) can
be seen by running the command:</p>
<pre class="pre-section"> swish-e -h</pre>
<p>Typically on unix-type systems the spider is installed at:</p>
<pre class="pre-section"> /usr/local/lib/swish-e/spider.pl</pre>
<p>This spider stores all links in memory while processing and does not do
parallel requests.</p>
</div>
<div class="sub-section">
<h2><a name="running_the_spider"></a>Running the spider</h2>
<p>The output from <i>spider.pl</i> can be captured to a temporary file which is then
fed into swish-e:</p>
<pre class="pre-section"> ./spider.pl > docs.txt
swish-e -c config -S prog -i stdin < docs.txt</pre>
<p>or the output can be passed to swish-e via a pipe:</p>
<pre class="pre-section"> ./spider.pl | swish-e -c config -S prog -i stdin</pre>
<p>or the swish-e can run the spider directly:</p>
<pre class="pre-section"> swish-e -c config -S prog -i spider.pl</pre>
<p>One advantage of having Swish-e run <i>spider.pl</i> is that Swish-e knows
where to locate the program (based on libexecdir compiled into swish-e).</p>
<p>When running the spider <i>without</i> any parameters it looks for a configuration file
called <i>SwishSpiderConfig.pl</i> in the current directory. The spider will abort
with an error if this file is not found.</p>
<p>A configuration file can be specified as the first parameter to the spider:</p>
<pre class="pre-section"> ./spider.pl spider.config > output.txt</pre>
<p>If running the spider via Swish-e (i.e. Swish-e runs the spider) then use
the Swish-e config option <a href="swish-config.html#item_swishprogparameters">SwishProgParameters</a>
to specify the config file:</p>
<p>In swish.config:</p>
<pre class="pre-section"> # Use spider.pl as the external program:
IndexDir spider.pl
# And pass the name of the spider config file to the spider:
SwishProgParameters spider.config</pre>
<p>And then run Swish-e like this:</p>
<pre class="pre-section"> swish-e -c swish.config -S prog</pre>
<p>Finally, by using the special word "default" on the command line the spider will
use a default configuration that is useful for indexing most sites. It's a good
way to get started with the spider:</p>
<pre class="pre-section"> ./spider.pl default http://my_server.com/index.html > output.txt</pre>
<p>There's no "best" way to run the spider. I like to capture to a file
and then feed that into Swish-e.</p>
<p>The spider does require Perl's LWP library and a few other reasonably common
modules. Most well maintained systems should have these modules installed.
See <a href="#requirements">/"REQUIREMENTS"</a> below for more information. It's a good idea to check
that you are running a current version of these modules.</p>
<p>Note: the "prog" document source in Swish-e bypasses many Swish-e configuration
settings. For example, you cannot use the
<a href="swish-config.html#item_swishprogparameters">IndexOnly</a> directive with the "prog"
document source. This is by design to limit the overhead when using an
external program for providing documents to swish; after all, with "prog", if
you don't want to index a file, then don't give it to swish to index in the
first place.</p>
<p>So, for spidering, if you do not wish to index images, for example, you will
need to either filter by the URL or by the content-type returned from the web
server. See <a href="#callback_functions">/"CALLBACK FUNCTIONS"</a> below for more information.</p>
</div>
<div class="sub-section">
<h2><a name="robots_exclusion_rules_and_being_nice"></a>Robots Exclusion Rules and being nice</h2>
<p>By default, this script will not spider files blocked by <i>robots.txt</i>. In addition,
The script will check for <meta name="robots"..> tags, which allows finer
control over what files are indexed and/or spidered.
See <a href="http://www.robotstxt.org/wc/exclusion.html">http://www.robotstxt.org/wc/exclusion.html</a> for details.</p>
<p>This spider provides an extension to the <meta> tag exclusion, by adding a
<b>NOCONTENTS</b> attribute. This attribute turns on the <code>no_contents</code> setting, which
asks swish-e to only index the document's title (or file name if not title is found).</p>
<p>For example:</p>
<pre class="pre-section"> <META NAME="ROBOTS" CONTENT="NOCONTENTS, NOFOLLOW"></pre>
<p>says to just index the document's title, but don't index its contents, and don't follow
any links within the document. Granted, it's unlikely that this feature will ever be used...</p>
<p>If you are indexing your own site, and know what you are doing, you can disable robot
exclusion by the <code>ignore_robots_file</code> configuration parameter, described below. This
disables both <i>robots.txt</i> and the meta tag parsing. You may disable just the meta tag
parsing by using <code>ignore_robots_headers</code>.</p>
<p>This script only spiders one file at a time, so load on the web server is not that great.
And with libwww-perl-5.53_91 HTTP/1.1 keep alive requests can reduce the load on
the server even more (and potentially reduce spidering time considerably).</p>
<p>Still, discuss spidering with a site's administrator before beginning.
Use the <code>delay_sec</code> to adjust how fast the spider fetches documents.
Consider running a second web server with a limited number of children if you really
want to fine tune the resources used by spidering.</p>
</div>
<div class="sub-section">
<h2><a name="duplicate_documents"></a>Duplicate Documents</h2>
<p>The spider program keeps track of URLs visited, so a document is only indexed
one time.</p>
<p>The Digest::MD5 module can be used to create a "fingerprint" of every page
indexed and this fingerprint is used in a hash to find duplicate pages.
For example, MD5 will prevent indexing these as two different documents:</p>
<pre class="pre-section"> http://localhost/path/to/some/index.html
http://localhost/path/to/some/</pre>
<p>But note that this may have side effects you don't want. If you want this
file indexed under this URL:</p>
<pre class="pre-section"> http://localhost/important.html</pre>
<p>But the spider happens to find the exact content in this file first:</p>
<pre class="pre-section"> http://localhost/developement/test/todo/maybeimportant.html</pre>
<p>Then only that URL will be indexed.</p>
</div>
<div class="sub-section">
<h2><a name="broken_relative_links"></a>Broken relative links</h2>
<p>Sometimes web page authors use too many <code>/../</code> segments in relative URLs which reference
documents above the document root. Some web servers such as Apache will return a
400 Bad Request when requesting a document above the root. Other web servers such as
Micorsoft IIS/5.0 will try and "correct" these errors. This correction will lead to
loops when spidering.</p>
<p>The spider can fix these above-root links by placing the following in your spider config:</p>
<pre class="pre-section"> remove_leading_dots => 1,</pre>
<p>It is not on by default so that the spider can report the broken links (as 400 errors on
sane webservers).</p>
</div>
<div class="sub-section">
<h2><a name="compression"></a>Compression</h2>
<p>If The Perl module Compress::Zlib is installed the spider will send the</p>
<pre class="pre-section"> Accept-Encoding: gzip x-gzip</pre>
<p>header and uncompress the document if the server returns the header</p>
<pre class="pre-section"> Content-Encoding: gzip
Content-Encoding: x-gzip</pre>
<p>If The Perl distribution IO-Compress-Zlib is installed the spider will use
this module to uncompress "gzip" (x-gzip) and also "deflate" compressed
documents.</p>
<p>The "compress" method is not supported.</p>
<p>See RFC 2616 section 3.5 for more information.</p>
<p>MD5 checksomes are done on the compressed data.</p>
<p>MD5 may slow down indexing a tiny bit, so test with and without if speed is an
issue (which it probably isn't since you are spidering in the first place).
This feature will also use more memory.</p>
</div>
<div class="sub-section">
<h1><a name="requirements"></a>REQUIREMENTS</h1>
<p>Perl 5 (hopefully at least 5.00503) or later.</p>
<p>You must have the LWP Bundle on your computer. Load the LWP::Bundle via the CPAN.pm shell,
or download libwww-perl-x.xx from CPAN (or via ActiveState's ppm utility).
Also required is the the HTML-Parser-x.xx bundle of modules also from CPAN
(and from ActiveState for Windows).</p>
<pre class="pre-section"> http://search.cpan.org/search?dist=libwww-perl
http://search.cpan.org/search?dist=HTML-Parser</pre>
<p>You will also need Digest::MD5 if you wish to use the MD5 feature.
HTML::Tagset is also required.
Other modules may be required (for example, the pod2xml.pm module
has its own requirementes -- see perldoc pod2xml for info).</p>
<p>The spider.pl script, like everyone else, expects perl to live in /usr/local/bin.
If this is not the case then either add a symlink at /usr/local/bin/perl
to point to where perl is installed
or modify the shebang (#!) line at the top of the spider.pl program.</p>
<p>Note that the libwww-perl package does not support SSL (Secure Sockets Layer) (https)
by default. See <i>README.SSL</i> included in the libwww-perl package for information on
installing SSL support.</p>
</div>
<div class="sub-section">
<h1><a name="configuration_file"></a>CONFIGURATION FILE</h1>
<p>The spider configuration file is a read by the script as Perl code.
This makes the configuration a bit more complex than simple text config
files, but allows the spider to be configured programmatically.</p>
<p>For example, the config file can contain logic for testing URLs against regular
expressions or even against a database lookup while running.</p>
<p>The configuration file sets an array called <code>@servers</code>. This array can contain
one or more hash structures of parameters. Each hash structure is a configuration for
a single server.</p>
<p>Here's an example:</p>
<pre class="pre-section"> my %main_site = (
base_url => 'http://example.com',
same_hosts => 'www.example.com',
email => 'admin@example.com',
);
my %news_site = (
base_url => 'http://news.example.com',
email => 'admin@example.com',
);
@servers = ( \%main_site, \%news_site );
1;</pre>
<p>The above defines two Perl hashes (%main_site and %news_site) and then places
a *reference* (the backslash before the name of the hash) to each of those
hashes in the @servers array. The "1;" at the end is required at the end
of the file (Perl must see a true value at the end of the file).</p>
<p>The <code>config file path</code> is the first parameter passed to the spider script.</p>
<pre class="pre-section"> ./spider.pl F<config></pre>
<p>If you do not specify a config file then the spider will look for the file
<i>SwishSpiderConfig.pl</i> in the current directory.</p>
<p>The Swish-e distribution includes a <i>SwishSpiderConfig.pl</i> file with a few
example configurations. This example file is installed in the <i>prog-bin/</i>
documentation directory (on unix often this is
/usr/local/share/swish-e/prog-bin).</p>
<p>When the special config file name "default" is used:</p>
<pre class="pre-section"> SwishProgParameters default http://www.mysite/index.html [<URL>] [...]</pre>
<p>Then a default set of parameters are used with the spider. This is a good way to start
using the spider before attempting to create a configuration file.</p>
<p>The default settings skip any urls that look like images (well, .gif .jpeg
.png), and attempts to filter PDF and MS Word documents IF you have the
required filter programs installed (which are not part of the Swish-e
distribution). The spider will follow "a" and "frame" type of links only.</p>
<p>Note that if you do use a spider configuration file that the default configuration will NOT
be used (unless you set the "use_default_config" option in your config file).</p>
</div>
<div class="sub-section">
<h1><a name="configuration_options"></a>CONFIGURATION OPTIONS</h1>
<p>This describes the required and optional keys in the server configuration hash, in random order...</p>
<ul>
<li><a name="item_base_url"></a><a name="base_url"></a><b>base_url</b>
<p>This required setting is the starting URL for spidering.</p>
<p>This sets the first URL the spider will fetch. It does NOT limit spidering
to URLs at or below the level of the directory specified in this setting.
For that feature you need to use the <code>test_url</code> callback function.</p>
<p>Typically, you will just list one URL for the base_url. You may specify more
than one URL as a reference to a list and each will be spidered:</p>
<pre class="pre-section"> base_url => [qw! http://swish-e.org/ http://othersite.org/other/index.html !],</pre>
<p>but each site will use the same config opions. If you want to index two separate
sites you will likely rather add an additional configuration to the
@servers array.</p>
<p>You may specify a username and password:</p>
<pre class="pre-section"> base_url => 'http://user:pass@swish-e.org/index.html',</pre>
<p>If a URL is protected by Basic Authentication you will be prompted for a
username and password. The parameter <code>max_wait_time</code> controls how long to
wait for user entry before skipping the current URL. See also <code>credentials</code>
below.</p>
</li>
<li><a name="item_same_hosts"></a><a name="same_hosts"></a><b>same_hosts</b>
<p>This optional key sets equivalent <b>authority</b> name(s) for the site you are spidering.
For example, if your site is <code>www.mysite.edu</code> but also can be reached by
<code>mysite.edu</code> (with or without <code>www</code>) and also <code>web.mysite.edu</code> then:</p>
<p>Example:</p>
<pre class="pre-section"> $serverA{base_url} = 'http://www.mysite.edu/index.html';
$serverA{same_hosts} = ['mysite.edu', 'web.mysite.edu'];</pre>
<p>Now, if a link is found while spidering of:</p>
<pre class="pre-section"> http://web.mysite.edu/path/to/file.html</pre>
<p>it will be considered on the same site, and will actually spidered and indexed
as:</p>
<pre class="pre-section"> http://www.mysite.edu/path/to/file.html</pre>
<p>Note: This should probably be called <b>same_host_port</b> because it compares the URI <code>host:port</code>
against the list of host names in <code>same_hosts</code>. So, if you specify a port name in you will
want to specify the port name in the the list of hosts in <code>same_hosts</code>:</p>
<pre class="pre-section"> my %serverA = (
base_url => 'http://mytest.site.invalid:4444/',
same_hosts => [ qw/www.mytest.site.invalid:4444/ ],
email => 'my@email.address',
);</pre>
</li>
<li><a name="item_email"></a><a name="email"></a><b>email</b>
<p>This required key sets the email address for the spider. Set this to
your email address.</p>
</li>
<li><a name="item_agent"></a><a name="agent"></a><b>agent</b>
<p>This optional key sets the name of the spider.</p>
</li>
<li><a name="item_link_tags"></a><a name="link_tags"></a><b>link_tags</b>
<p>This optional tag is a reference to an array of tags. Only links found in these tags will be extracted.
The default is to only extract links from >a< tags.</p>
<p>For example, to extract tags from <code>a</code> tags and from <code>frame</code> tags:</p>
<pre class="pre-section"> my %serverA = (
base_url => 'http://mytest.site.invalid:4444/',
same_hosts => [ qw/www.mytest.site.invalid:4444/ ],
email => 'my@email.address',
link_tags => [qw/ a frame /],
);</pre>
</li>
<li><a name="item_use_default_config"></a><a name="use_default_config"></a><b>use_default_config</b>
<p>This option is new for Swish-e 2.4.3.</p>
<p>The spider has a hard-coded default configuration that's available when the spider
is run with the configuration file listed as "default":</p>
<pre class="pre-section"> ./spider.pl default <url></pre>
<p>This default configuration skips urls that match the regular expression:</p>
<pre class="pre-section"> /\.(?:gif|jpeg|png)$/i</pre>
<p>and the spider will attempt to use the SWISH::Filter module for filtering non-text
documents. (You still need to install programs to do the actual filtering, though).</p>
<p>Here's the basic config for the "default" mode:</p>
<pre class="pre-section"> @servers = (
{
email => 'swish@user.failed.to.set.email.invalid',
link_tags => [qw/ a frame /],
keep_alive => 1,
test_url => sub { $_[0]->path !~ /\.(?:gif|jpeg|png)$/i },
test_response => $response_sub,
use_head_requests => 1, # Due to the response sub
filter_content => $filter_sub,
} );</pre>
<p>The filter_content callback will be used if SWISH::Filter was loaded and ready to use.
This doesn't mean that filtering will work automatically -- you will likely need to install
aditional programs for filtering (like Xpdf or Catdoc).</p>
<p>The test_response callback will be set to test if a given content type can be filtered
by SWISH::Filter (if SWISH::Filter was loaded), otherwise, it will check for
content-type of text/* -- any text type of document.</p>
<p>Normally, if you specify your own config file:</p>
<pre class="pre-section"> ./spider.pl my_own_spider.config</pre>
<p>then you must setup those features available in the default setting in your own config
file. But, if you wish to build upon the "default" config file then set this option.</p>
<p>For example, to use the default config but specify your own email address:</p>
<pre class="pre-section"> @servers = (
{
email => my@email.address,
use_default_config => 1,
delay_sec => 0,
},
);
1;</pre>
<p>What this does is "merge" your config file with the default config file.</p>
</li>
<li><a name="item_delay_sec"></a><a name="delay_sec"></a><b>delay_sec</b>
<p>This optional key sets the delay in seconds to wait between requests. See the
LWP::RobotUA man page for more information. The default is 5 seconds.
Set to zero for no delay.</p>
<p>When using the keep_alive feature (recommended) the delay will be used only
where the previous request returned a "Connection: closed" header.</p>
</li>
<li><a name="item_delay_min"></a><a name="delay_min"></a><b>delay_min (deprecated)</b>
<p>Set the delay to wait between requests in minutes. If both delay_sec and
delay_min are defined, delay_sec will be used.</p>
</li>
<li><a name="item_max_wait_time"></a><a name="max_wait_time"></a><b>max_wait_time</b>
<p>This setting is the number of seconds to wait for data to be returned from
the request. Data is returned in chunks to the spider, and the timer is
reset each time a new chunk is reported. Therefore, documents (requests)
that take longer than this setting should not be aborted as long as some
data is received every max_wait_time seconds. The default it 30 seconds.</p>
<p>NOTE: This option has no effect on Windows.</p>
</li>
<li><a name="item_max_time"></a><a name="max_time"></a><b>max_time</b>
<p>This optional key will set the max minutes to spider. Spidering
for this host will stop after <code>max_time</code> minutes, and move on to the
next server, if any. The default is to not limit by time.</p>
</li>
<li><a name="item_max_files"></a><a name="max_files"></a><b>max_files</b>
<p>This optional key sets the max number of files to spider before aborting.
The default is to not limit by number of files. This is the number of requests
made to the remote server, not the total number of files to index (see <code>max_indexed</code>).
This count is displayted at the end of indexing as <code>Unique URLs</code>.</p>
<p>This feature can (and perhaps should) be use when spidering a web site where dynamic
content may generate unique URLs to prevent run-away spidering.</p>
</li>
<li><a name="item_max_indexed"></a><a name="max_indexed"></a><b>max_indexed</b>
<p>This optional key sets the max number of files that will be indexed.
The default is to not limit. This is the number of files sent to
swish for indexing (and is reported by <code>Total Docs</code> when spidering ends).</p>
</li>
<li><a name="item_max_size"></a><a name="max_size"></a><b>max_size</b>
<p>This optional key sets the max size of a file read from the web server.
This <b>defaults</b> to 5,000,000 bytes. If the size is exceeded the resource is
skipped and a message is written to STDERR if the DEBUG_SKIPPED debug flag is set.</p>
<p>Set max_size to zero for unlimited size. If the server returns a Content-Length
header then that will be used. Otherwise, the document will be checked for
size limitation as it arrives. That's a good reason to have your server send
Content-Length headers.</p>
<p>See also <code>use_head_requests</code> below.</p>
</li>
<li><a name="item_keep_alive"></a><a name="keep_alive"></a><b>keep_alive</b>
<p>This optional parameter will enable keep alive requests. This can dramatically speed
up spidering and reduce the load on server being spidered. The default is to not use
keep alives, although enabling it will probably be the right thing to do.</p>
<p>To get the most out of keep alives, you may want to set up your web server to
allow a lot of requests per single connection (i.e MaxKeepAliveRequests on Apache).
Apache's default is 100, which should be good.</p>
<p>When a connection is not closed the spider does not wait the "delay_sec"
time when making the next request. In other words, there is no delay in
requesting documents while the connection is open.</p>
<p>Note: try to filter as many documents as possible <b>before</b> making the request to the server. In
other words, use <code>test_url</code> to look for files ending in <code>.html</code> instead of using <code>test_response</code> to look
for a content type of <code>text/html</code> if possible.
Do note that aborting a request from <code>test_response</code> will break the
current keep alive connection.</p>
<p>Note: you must have at least libwww-perl-5.53_90 installed to use this feature.</p>
</li>
<li><a name="item_use_head_requests"></a><a name="use_head_requests"></a><b>use_head_requests</b>
<p>This option is new as of swish-e 2.4.3 and can effect the speed of spidering and the
load of the web server.</p>
<p>To understand this you will likely need to read about the <a href="#callback_functions">/"CALLBACK FUNCTIONS"</a>
below -- specifically about the <code>test_response</code> callback function. This option is
also only used when <code>keep_alive</code> is also enabled (although it could be debated that
it's useful without keep alives).</p>
<p>This option tells the spider to use http HEAD requests before each request.</p>
<p>Normally, the spider simply does a GET request and after receiving the first
chunk of data back from the web server calls the <code>test_response</code> callback
function (if one is defined in your config file). The <code>test_response</code>
callback function is a good place to test the content-type header returned from
the server and reject types that you do not want to index.</p>
<p>Now, *if* you are using the <code>keep_alive</code> feature then rejecting a document
will often (always?) break the keep alive connection.</p>
<p>So, what the <code>use_head_requests</code> option does is issue a HEAD request for every
document, checks for a Content-Length header (to check if the document is larger than
<code>max_size</code>, and then calls your <code>test_response</code> callback function. If your callback
function returns true then a GET request is used to fetch the document.</p>
<p>The idea is that by using HEAD requests instead of GET request a false return from
your <code>test_response</code> callback function (i.e. rejecting the document) will not
break the keep alive connection.</p>
<p>Now, don't get too excited about this. Before using this think about the ratio of
rejected documents to accepted documents. If you reject no documents then using this feature
will double the number of requests to the web server -- which will also double the number of
connections to the web server. But, if you reject a large percentage of documents then
this feature will help maximize the number of keep alive requests to the server (i.e.
reduce the number of separate connections needed).</p>
<p>There's also another problem with using HEAD requests. Some broken servers
may not respond correctly to HEAD requests (some issues a 500 error), but respond
fine to a normal GET request. This is something to watch out for.</p>
<p>Finally, if you do not have a <code>test_response</code> callback AND <code>max_size</code> is set to zero
then setting <code>use_head_requests</code> will have no effect.</p>
<p>And, with all other factors involved you might find this option has no effect at all.</p>
</li>
<li><a name="item_skip"></a><a name="skip"></a><b>skip</b>
<p>This optional key can be used to skip the current server. It's only purpose
is to make it easy to disable a specific server hash in a configuration file.</p>
</li>
<li><a name="item_debug"></a><a name="debug"></a><b>debug</b>
<p>Set this item to a comma-separated list of debugging options.</p>
<p>Options are currently:</p>
<pre class="pre-section"> errors, failed, headers, info, links, redirect, skipped, url</pre>
<p>Here are basically the levels:</p>
<pre class="pre-section"> errors => general program errors (not used at this time)
url => print out every URL processes
headers => prints the response headers
failed => failed to return a 200
skipped => didn't index for some reason
info => a little more verbose
links => prints links as they are extracted
redirect => prints out redirected URLs</pre>
<p>Debugging can be also be set by an environment variable SPIDER_DEBUG when running <i>spider.pl</i>.
You can specify any of the above debugging options, separated by a comma.</p>
<p>For example with Bourne type shell:</p>
<pre class="pre-section"> SPIDER_DEBUG=url,links spider.pl [....]</pre>
<p>Before Swish-e 2.4.3 you had to use the internal debugging constants or'ed together
like so:</p>
<pre class="pre-section"> debug => DEBUG_URL | DEBUG_FAILED | DEBUG_SKIPPED,</pre>
<p>You can still do this, but the string version is easier. In fact, if you want
to turn on debugging dynamically (for example in a test_url() callback
function) then you currently *must* use the DEBUG_* constants. The string is
converted to a number only at the start of spiderig -- after that the <code>debug</code>
parameter is converted to a number.</p>
</li>
<li><a name="item_quiet"></a><a name="quiet"></a><b>quiet</b>
<p>If this is true then normal, non-error messages will be supressed. Quiet mode can also
be set by setting the environment variable SPIDER_QUIET to any true value.</p>
<pre class="pre-section"> SPIDER_QUIET=1</pre>
</li>
<li><a name="item_max_depth"></a><a name="max_depth"></a><b>max_depth</b>
<p>The <code>max_depth</code> parameter can be used to limit how deeply to recurse a web site.
The depth is just a count of levels of web pages descended, and not related to
the number of path elements in a URL.</p>
<p>A max_depth of zero says to only spider the page listed as the <code>base_url</code>. A max_depth of one will
spider the <code>base_url</code> page, plus all links on that page, and no more. The default is to spider all
pages.</p>
</li>
<li><a name="item_ignore_robots_file"></a><a name="ignore_robots_file"></a><b>ignore_robots_file</b>
<p>If this is set to true then the robots.txt file will not be checked when spidering
this server. Don't use this option unless you know what you are doing.</p>
</li>
<li><a name="item_use_cookies"></a><a name="use_cookies"></a><b>use_cookies</b>
<p>If this is set then a "cookie jar" will be maintained while spidering. Some
(poorly written ;) sites require cookies to be enabled on clients.</p>
<p>This requires the HTTP::Cookies module.</p>
</li>
<li><a name="item_use_md5"></a><a name="use_md5"></a><b>use_md5</b>
<p>If this setting is true, then a MD5 digest "fingerprint" will be made from the content of every
spidered document. This digest number will be used as a hash key to prevent
indexing the same content more than once. This is helpful if different URLs
generate the same content.</p>
<p>Obvious example is these two documents will only be indexed one time:</p>
<pre class="pre-section"> http://localhost/path/to/index.html
http://localhost/path/to/</pre>
<p>This option requires the Digest::MD5 module. Spidering with this option might
be a tiny bit slower.</p>
</li>
<li><a name="item_validate_links"></a><a name="validate_links"></a><b>validate_links</b>
<p>Just a hack. If you set this true the spider will do HEAD requests all links (e.g. off-site links), just
to make sure that all your links work.</p>
</li>
<li><a name="item_credentials"></a><a name="credentials"></a><b>credentials</b>
<p>You may specify a username and password to be used automatically when spidering:</p>
<pre class="pre-section"> credentials => 'username:password',</pre>
<p>A username and password supplied in a URL will override this setting.
This username and password will be used for every request.</p>
<p>See also the <code>get_password</code> callback function below. <code>get_password</code>, if defined,
will be called when a page requires authorization.</p>
</li>
<li><a name="item_credential_timeout"></a><a name="credential_timeout"></a><b>credential_timeout</b>
<p>Sets the number of seconds to wait for user input when prompted for a username or password.
The default is 30 seconds.</p>
<p>Set this to zero to wait forever. Probably not a good idea.</p>
<p>Set to undef to disable asking for a password.</p>
<pre class="pre-section"> credential_timeout => undef,</pre>
</li>
<li><a name="item_remove_leading_dots"></a><a name="remove_leading_dots"></a><b>remove_leading_dots</b>
<p>Removes leading dots from URLs that might reference documents above the document root.
The default is to not remove the dots.</p>
</li>
</ul>
</div>
<div class="sub-section">
<h1><a name="callback_functions"></a>CALLBACK FUNCTIONS</h1>
<p>Callback functions can be defined in your parameter hash.
These optional settings are <i>callback</i> subroutines that are called while
processing URLs.</p>
<p>A little perl discussion is in order:</p>
<p>In perl, a scalar variable can contain a reference to a subroutine. The config example above shows
that the configuration parameters are stored in a perl <i>hash</i>.</p>
<pre class="pre-section"> my %serverA = (
base_url => 'http://mytest.site.invalid:4444/',
same_hosts => [ qw/www.mytest.site.invalid:4444/ ],
email => 'my@email.address',
link_tags => [qw/ a frame /],
);</pre>
<p>There's two ways to add a reference to a subroutine to this hash:</p>
<p>sub foo {
return 1;
}</p>
<pre class="pre-section"> my %serverA = (
base_url => 'http://mytest.site.invalid:4444/',
same_hosts => [ qw/www.mytest.site.invalid:4444/ ],
email => 'my@email.address',
link_tags => [qw/ a frame /],
test_url => \&foo, # a reference to a named subroutine
);</pre>
<p>Or the subroutine can be coded right in place:</p>
<pre class="pre-section"> my %serverA = (
base_url => 'http://mytest.site.invalid:4444/',
same_hosts => [ qw/www.mytest.site.invalid:4444/ ],
email => 'my@email.address',
link_tags => [qw/ a frame /],
test_url => sub { reutrn 1; },
);</pre>
<p>The above example is not very useful as it just creates a user callback function that
always returns a true value (the number 1). But, it's just an example.</p>
<p>The function calls are wrapped in an eval, so calling die (or doing something that dies) will just cause
that URL to be skipped. If you really want to stop processing you need to set $server->{abort} in your
subroutine (or send a kill -HUP to the spider).</p>
<p>The first two parameters passed are a URI object (to have access to the current URL), and
a reference to the current server hash. The <code>server</code> hash is just a global hash for holding data, and
useful for setting flags as described below.</p>
<p>Other parameters may be also passed in depending the the callback function,
as described below. In perl parameters are passed in an array called "@_".
The first element (first parameter) of that array is $_[0], and the second
is $_[1], and so on. Depending on how complicated your function is you may
wish to shift your parameters off of the @_ list to make working with them
easier. See the examples below.</p>
<p>To make use of these routines you need to understand when they are called, and what changes
you can make in your routines. Each routine deals with a given step, and returning false from
your routine will stop processing for the current URL.</p>
<ul>
<li><a name="item_test_url"></a><a name="test_url"></a><b>test_url</b>
<p><code>test_url</code> allows you to skip processing of urls based on the url before the request
to the server is made. This function is called for the <code>base_url</code> links (links you define in
the spider configuration file) and for every link extracted from a fetched web page.</p>
<p>This function is a good place to skip links that you are not interested in following. For example,
if you know there's no point in requesting images then you can exclude them like:</p>
<pre class="pre-section"> test_url => sub {
my $uri = shift;
return 0 if $uri->path =~ /\.(gif|jpeg|png)$/;
return 1;
},</pre>
<p>Or to write it another way:</p>
<pre class="pre-section"> test_url => sub { $_[0]->path !~ /\.(gif|jpeg|png)$/ },</pre>
<p>Another feature would be if you were using a web server where path names are
NOT case sensitive (e.g. Windows). You can normalize all links in this situation
using something like</p>
<pre class="pre-section"> test_url => sub {
my $uri = shift;
return 0 if $uri->path =~ /\.(gif|jpeg|png)$/;
$uri->path( lc $uri->path ); # make all path names lowercase
return 1;
},</pre>
<p>The important thing about <code>test_url</code> (compared to the other callback functions) is that
it is called while <i>extracting</i> links, not while actually fetching that page from the web
server. Returning false from <code>test_url</code> simple says to not add the URL to the list of links to
spider.</p>
<p>You may set a flag in the server hash (second parameter) to tell the spider to abort processing.</p>
<pre class="pre-section"> test_url => sub {
my $server = $_[1];
$server->{abort}++ if $_[0]->path =~ /foo\.html/;
return 1;
},</pre>
<p>You cannot use the server flags:</p>
<pre class="pre-section"> no_contents
no_index
no_spider</pre>
<p>This is discussed below.</p>
</li>
<li><a name="item_test_response"></a><a name="test_response"></a><b>test_response</b>
<p>This function allows you to filter based on the response from the remote server
(such as by content-type).</p>
<p>Web servers use a Content-Type: header to define the type of data returned from the server.
On a web server you could have a .jpeg file be a web page -- file extensions may not always
indicate the type of the file.</p>
<p>If you enable <code>use_head_requests</code> then this function is called after the
spider makes a HEAD request. Otherwise, this function is called while the web
pages is being fetched from the remote server, typically after just enought
data has been returned to read the response from the web server.</p>
<p>The test_response callback function is called with the following parameters:</p>
<pre class="pre-section"> ( $uri, $server, $response, $content_chunk )</pre>
<p>The $response variable is a HTTP::Response object and provies methods of examining
the server's response. The $content_chunk is the first chunk of data returned from
the server (if not a HEAD request).</p>
<p>When not using <code>use_head_requests</code> the spider requests a document in "chunks"
of 4096 bytes. 4096 is only a suggestion of how many bytes to return in each
chunk. The <code>test_response</code> routine is called when the first chunk is received
only. This allows ignoring (aborting) reading of a very large file, for
example, without having to read the entire file. Although not much use, a
reference to this chunk is passed as the forth parameter.</p>
<p>If you are spidering a site with many different types of content that you do
not wish to index (and cannot use a test_url callback to determine what docs to skip)
then you will see better performance using both the <code>use_head_requests</code> and <code>keep_alive</code>
features. (Aborting a GET request kills the keep-alive session.)</p>
<p>For example, to only index true HTML (text/html) pages:</p>
<pre class="pre-section"> test_response => sub {
my $content_type = $_[2]->content_type;
return $content_type =~ m!text/html!;
},</pre>
<p>You can also set flags in the server hash (the second parameter) to control indexing:</p>
<pre class="pre-section"> no_contents -- index only the title (or file name), and not the contents
no_index -- do not index this file, but continue to spider if HTML
no_spider -- index, but do not spider this file for links to follow
abort -- stop spidering any more files</pre>
<p>For example, to avoid index the contents of "private.html", yet still follow any links
in that file:</p>
<pre class="pre-section"> test_response => sub {
my $server = $_[1];
$server->{no_index}++ if $_[0]->path =~ /private\.html$/;
return 1;
},</pre>
<p>Note: Do not modify the URI object in this call back function.</p>
</li>
<li><a name="item_filter_content"></a><a name="filter_content"></a><b>filter_content</b>
<p>This callback function is called right before sending the content to swish.
Like the other callback function, returning false will cause the URL to be skipped.
Setting the <code>abort</code> server flag and returning false will abort spidering.</p>
<p>You can also set the <code>no_contents</code> flag.</p>
<p>This callback function is passed four parameters.
The URI object, server hash, the HTTP::Response object,
and a reference to the content.</p>
<p>You can modify the content as needed. For example you might not like upper case:</p>
<pre class="pre-section"> filter_content => sub {
my $content_ref = $_[3];
$$content_ref = lc $$content_ref;
return 1;
},</pre>
<p>I more reasonable example would be converting PDF or MS Word documents for
parsing by swish. Examples of this are provided in the <i>prog-bin</i> directory
of the swish-e distribution.</p>
<p>You may also modify the URI object to change the path name passed to swish for indexing.</p>
<pre class="pre-section"> filter_content => sub {
my $uri = $_[0];
$uri->host('www.other.host') ;
return 1;
},</pre>
<p>Swish-e's ReplaceRules feature can also be used for modifying the path name indexed.</p>
<p>Note: Swish-e now includes a method of filtering based on the SWISH::Filter
Perl modules. See the SwishSpiderConfig.pl file for an example how to use
SWISH::Filter in a filter_content callback function.</p>
<p>If you use the "default" configuration (i.e. pass "default" as the first parameter
to the spider) then SWISH::Filter is used automatically. This only adds code for
calling the programs to filter your content -- you still need to install applications
that do the hard work (like xpdf for pdf conversion and catdoc for MS Word conversion).</p>
<p>The a function included in the <i>spider.pl</i> for calling SWISH::Filter when using the "default"
config can also be used in your config file. There's a function called
swish_filter() that returns a list of two subroutines. So in your config you could
do:</p>
<pre class="pre-section"> my ($filter_sub, $response_sub ) = swish_filter();
@server = ( {
test_response => $response_sub,
filter_content => $filter_sub,
[...],
} );</pre>
<p>The $response_sub is not required, but is useful if using HEAD requests (<code>use_head_requests</code>):
It tests the content type from the server to see if there's any filters that can handle
the document. The $filter_sub does all the work of filtering a document.</p>
<p>Make sense? If not, then that's what the Swish-e list is for.</p>
</li>
<li><a name="item_spider_done"></a><a name="spider_done"></a><b>spider_done</b>
<p>This callback is called after processing a server (after each server listed
in the @servers array if more than one).</p>
<p>This allows your config file to do any cleanup work after processing.
For example, if you were keeping counts during, say, a test_response() callback
function you could use the spider_done() callback to print the results.</p>
</li>
<li><a name="item_output_function"></a><a name="output_function"></a><b>output_function</b>
<p>If defined, this callback function is called instead of printing the content
and header to STDOUT. This can be used if you want to store the output of the
spider before indexing.</p>
<p>The output_function is called with the following parameters:</p>
<pre class="pre-section"> ($server, $content, $uri, $response, $bytecount, $path);</pre>
<p>Here is an example that simply shows two of the params passed:</p>
<pre class="pre-section"> output_function => sub {
my ($server, $content, $uri, $response, $bytecount, $path) = @_;
print STDERR "passed: uri $uri, bytecount $bytecount...\n";
# no output to STDOUT for swish-e
}</pre>
<p>You can do almost the same thing with a filter_content callback.</p>
</li>
<li><a name="item_get_password"></a><a name="get_password"></a><b>get_password</b>
<p>This callback is called when a HTTP password is needed (i.e. after the server
returns a 401 error). The function can test the URI and Realm and then return
a username and password separated by a colon:</p>
<pre class="pre-section"> get_password => sub {
my ( $uri, $server, $response, $realm ) = @_;
if ( $uri->path =~ m!^/path/to/protected! && $realm eq 'private' ) {
return 'joe:secret931password';
}
return; # sorry, I don't know the password.
},</pre>
<p>Use the <code>credentials</code> setting if you know the username and password and they will
be the same for every request. That is, for a site-wide password.</p>
</li>
</ul>
<p>Note that you can create your own counters to display in the summary list when spidering
is finished by adding a value to the hash pointed to by <code>$server->{counts}</code>.</p>
<pre class="pre-section"> test_url => sub {
my $server = $_[1];
$server->{no_index}++ if $_[0]->path =~ /private\.html$/;
$server->{counts}{'Private Files'}++;
return 1;
},</pre>
<p>Each callback function <b>must</b> return true to continue processing the URL. Returning false will
cause processing of <i>the current</i> URL to be skipped.</p>
</div>
<div class="sub-section">
<h2><a name="more_on_setting_flags"></a>More on setting flags</h2>
<p>Swish (not this spider) has a configuration directive <code>NoContents</code> that will instruct swish to
index only the title (or file name), and not the contents. This is often used when
indexing binary files such as image files, but can also be used with html
files to index only the document titles.</p>
<p>As shown above, you can turn this feature on for specific documents by setting a flag in
the server hash passed into the <code>test_response</code> or <code>filter_content</code> subroutines.
For example, in your configuration file you might have the <code>test_response</code> callback set
as:</p>
<pre class="pre-section"> test_response => sub {
my ( $uri, $server, $response ) = @_;
# tell swish not to index the contents if this is of type image
$server->{no_contents} = $response->content_type =~ m[^image/];
return 1; # ok to index and spider this document
}</pre>
<p>The entire contents of the resource is still read from the web server, and passed
on to swish, but swish will also be passed a <code>No-Contents</code> header which tells
swish to enable the NoContents feature for this document only.</p>
<p>Note: Swish will index the path name only when <code>NoContents</code> is set, unless the document's
type (as set by the swish configuration settings <code>IndexContents</code> or <code>DefaultContents</code>) is
HTML <i>and</i> a title is found in the html document.</p>
<p>Note: In most cases you probably would not want to send a large binary file to swish, just
to be ignored. Therefore, it would be smart to use a <code>filter_content</code> callback routine to
replace the contents with single character (you cannot use the empty string at this time).</p>
<p>A similar flag may be set to prevent indexing a document at all, but still allow spidering.
In general, if you want completely skip spidering a file you return false from one of the
callback routines (<code>test_url</code>, <code>test_response</code>, or <code>filter_content</code>). Returning false from any of those
three callbacks will stop processing of that file, and the file will <b>not</b> be spidered.</p>
<p>But there may be some cases where you still want to spider (extract links) yet, not index the file. An example
might be where you wish to index only PDF files, but you still need to spider all HTML files to find
the links to the PDF files.</p>
<pre class="pre-section"> $server{test_response} = sub {
my ( $uri, $server, $response ) = @_;
$server->{no_index} = $response->content_type ne 'application/pdf';
return 1; # ok to spider, but don't index
}</pre>
<p>So, the difference between <code>no_contents</code> and <code>no_index</code> is that <code>no_contents</code> will still index the file
name, just not the contents. <code>no_index</code> will still spider the file (if it's <code>text/html</code>) but the
file will not be processed by swish at all.</p>
<p><b>Note:</b> If <code>no_index</code> is set in a <code>test_response</code> callback function then
the document <i>will not be filtered</i>. That is, your <code>filter_content</code>
callback function will not be called.</p>
<p>The <code>no_spider</code> flag can be set to avoid spiderering an HTML file. The file will still be indexed unless
<code>no_index</code> is also set. But if you do not want to index and spider, then simply return false from one of the three
callback funtions.</p>
</div>
<div class="sub-section">
<h1><a name="signals"></a>SIGNALS</h1>
<p>Sending a SIGHUP to the running spider will cause it to stop spidering. This is a good way to abort spidering, but
let swish index the documents retrieved so far.</p>
</div>
<div class="sub-section">
<h1><a name="changes"></a>CHANGES</h1>
<p>List of some of the changes</p>
</div>
<div class="sub-section">
<h2><a name="thu_sep_30_2004_changes_for_swish_e_2_4_3"></a>Thu Sep 30 2004 - changes for Swish-e 2.4.3</h2>
<p>Code reorganization and a few new featues. Updated docs a little tiny bit.
Introduced a few spelling mistakes.</p>
<ul>
<li><a name="item_config"></a><a name="config"></a><b>Config opiton: use_default_config</b>
<p>It used to be that you could run the spider like:</p>
<pre class="pre-section"> spider.pl default <some url></pre>
<p>and the spider would use its own internal config. But if you used your own
config file then the defaults were not used. This options allows you to merge
your config with the default config. Makes making small changes to the default
easy.</p>
</li>
<li><a name="item_config"></a><a name="config"></a><b>Config option: use_head_requests</b>
<p>Tells the spider to make a HEAD request before GET'ing the document from the web server.
Useful if you use keep_alive and have a test_response() callback that rejects many documents
(which breaks the connection).</p>
</li>
<li><a name="item_config"></a><a name="config"></a><b>Config option: spider_done</b>
<p>Callback to tell you (or tell your config as it may be) that the spider is done.
Useful if you need to do some extra processing when done spidering -- like record
counts to a file.</p>
</li>
<li><a name="item_config"></a><a name="config"></a><b>Config option: get_password</b>
<p>This callback is called when a document returns a 401 error needing a username
and password. Useful if spidering a site proteced with multiple passwords.</p>
</li>
<li><a name="item_config"></a><a name="config"></a><b>Config option: output_function</b>
<p>If defined spider.pl calls this instead of sending ouptut to STDOUT.</p>
</li>
<li><a name="item_config"></a><a name="config"></a><b>Config option: debug</b>
<p>Now you can use the words instead of or'ing the DEBUG_* constants together.</p>
</li>
</ul>
</div>
<div class="sub-section">
<h1><a name="todo"></a>TODO</h1>
<p>Add a "get_document" callback that is called right before making the "GET" request.
This would make it easier to use cached documents. You can do that now in a test_url
callback or in a test_response when using HEAD request.</p>
<p>Save state of the spider on SIGHUP so spidering could be restored at a later date.</p>
</div>
<div class="sub-section">
<h1><a name="copyright"></a>COPYRIGHT</h1>
<p>Copyright 2001 Bill Moseley</p>
<p>This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.</p>
</div>
<div class="sub-section">
<h1><a name="support"></a>SUPPORT</h1>
<p>Send all questions to the The SWISH-E discussion list.</p>
<p>See <a href="http://swish-e.org/">http://swish-e.org/</a></p>
</div>
</div><!-- /#main-copy -->
</div><!-- /#content-area -->
<div id="side-bar">
<!-- noindex -->
<ul class="menu"><li class="menuparent">
<a class="menu"
href="./index.html"
>Doc Overview</a>
<!-- noindex -->
<ul class="submenu"><li class="">
<a class="submenu"
href="./readme.html"
title="First time users">README</a>
</li><li class="">
<a class="submenu"
href="./install.html"
title="Installation and usage overview">Install</a>
</li><li class="">
<a class="submenu"
href="./changes.html"
title="Important changes from previous versions">Changes</a>
</li><li class="">
<a class="submenu"
href="./swish-config.html"
title="Directives that go in your Swish-e configuration file">Configuration</a>
</li><li class="">
<a class="submenu"
href="./swish-run.html"
title="Command line options for Swish-e binary">Running</a>
</li><li class="">
<a class="submenu"
href="./swish-search.html"
title="Swish-e's search language">Searching</a>
</li><li class="">
<a class="submenu"
href="./swish-faq.html"
>FAQ</a>
</li><li class="">
<a class="submenu"
href="./swish-bugs.html"
>Known issues</a>
</li><li class="">
<a class="submenu"
href="./swish-3.0.html"
>The Future</a>
</li><li class="">
<a class="submenu"
href="./swish-library.html"
title="Swish-e C API">C API</a>
</li><li class="">
<a class="submenu"
href="./api.html"
title="Perl interface to the Swish-e library">Perl API</a>
</li><li class="">
<a class="submenu"
href="./swish.cgi.html"
title="Example CGI/mod_perl script">Swish.cgi</a>
</li><li class="">
<a class="submenu"
href="./search.cgi.html"
title="Example Perl script using SWISH::API">Search.cgi</a>
</li><li class="">
<a class="thisfile"
href="./spider.html"
title="The Swish-e HTTP spider">Spider.pl »</a>
</li><li class="">
<a class="submenu"
href="./filter.html"
title="How to index non-text documents">Filters</a>
</li></ul>
<!-- index -->
</li></ul>
<!-- index -->
</div><!-- /#side-bar -->
</div><!-- /#body-area -->
<div id="footer">
<!-- noindex -->
<span class="doNotPrint">
Swish-e is distributed with <strong>no warranty</strong> under the terms of the <br />
<a href='http://swish-e.org/license.html'>Swish-e License</a>.<br />
Questions may be posted to the
<a href="http://swish-e.org/discuss.html" title="email list and list archive">Swish-e Discussion list</a>.
</span>
<p>
<strong>URI »</strong> http://swish-e.org/
•
<strong>Updated »</strong>Sun, 05 Apr 2009 01:58:35 UTC</p>
<!-- index -->
</div><!-- /#footer -->
</body>
</html>
|