/
Retriever.cc
2013 lines (1751 loc) · 48.6 KB
/
Retriever.cc
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
//
// Retriever.cc
//
// Retriever: Crawl from a list of URLs and calls appropriate parsers. The
// parser notifies the Retriever object that it got something
// (got_* functions) and the Retriever object feed the databases
// and statistics accordingly.
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: Retriever.cc,v 1.94 2004/05/28 13:15:15 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#ifdef _MSC_VER /* _WIN32 */
# include <sys/types.h>
# include <winsock2.h>
#endif
#include "Retriever.h"
#include "htdig.h"
#include "HtWordList.h"
#include "WordRecord.h"
#include "URLRef.h"
#include "Server.h"
#include "Parsable.h"
#include "Document.h"
#include "StringList.h"
#include "WordType.h"
#include "Transport.h"
#include "HtHTTP.h" // For HTTP statistics
#include "md5.h"
#include "defaults.h"
#ifndef _MSC_VER /* _WIN32 */
#include <pwd.h>
#endif
#include <signal.h>
#include <stdio.h>
static int noSignal;
// no_store_phrases:
// If true, only store first occurrence of each word in a document
static bool no_store_phrases;
//*****************************************************************************
// Retriever::Retriever()
//
Retriever::Retriever(RetrieverLog flags):
words(*(HtConfiguration::config())),
words_to_add (100, 0.75)
{
HtConfiguration *config = HtConfiguration::config();
FILE *urls_parsed;
currenthopcount = 0;
max_hop_count = config->Value("max_hop_count", 999999);
no_store_phrases = !config->Boolean("store_phrases");
//
// Initialize the flags for the various HTML factors
//
// text_factor
factor[0] = FLAG_TEXT;
// title_factor
factor[1] = FLAG_TITLE;
// heading factor (now generic)
factor[2] = FLAG_HEADING;
factor[3] = FLAG_HEADING;
factor[4] = FLAG_HEADING;
factor[5] = FLAG_HEADING;
factor[6] = FLAG_HEADING;
factor[7] = FLAG_HEADING;
// img alt text
//factor[8] = FLAG_KEYWORDS;
factor[8] = FLAG_TEXT; // treat alt text as plain text, until it has
// its own FLAG and factor.
// keywords factor
factor[9] = FLAG_KEYWORDS;
// META description factor
factor[10] = FLAG_DESCRIPTION;
factor[11] = FLAG_AUTHOR;
doc = new Document();
minimumWordLength = config->Value("minimum_word_length", 3);
log = flags;
// if in restart mode
if (Retriever_noLog != log)
{
String filelog = config->Find("url_log");
char buffer[1024];
int l;
urls_parsed = fopen((char *) filelog, "r");
if (urls_parsed != 0)
{
// read all url discovered but not fetched before
while (fgets(buffer, sizeof(buffer), urls_parsed))
{
l = strlen(buffer);
buffer[l - 1] = 0;
Initial(buffer, 2);
}
fclose(urls_parsed);
}
unlink((char *) filelog);
}
check_unique_md5 = config->Boolean("check_unique_md5", 0);
check_unique_date = config->Boolean("check_unique_date", 0);
d_md5 = 0;
if (check_unique_md5)
{
d_md5 = Database::getDatabaseInstance(DB_HASH);
if (d_md5->OpenReadWrite(config->Find("md5_db"), 0666) != OK)
{
cerr << "DocumentDB::Open: " << config->Find("md5_db") << " " << strerror(errno) << "\n";
}
}
}
//*****************************************************************************
// Retriever::~Retriever()
//
Retriever::~Retriever()
{
if (d_md5)
d_md5->Close();
delete doc;
}
//*****************************************************************************
// void Retriever::setUsernamePassword(char *credentials)
//
void Retriever::setUsernamePassword(const char *credentials)
{
doc->setUsernamePassword(credentials);
}
//*****************************************************************************
// void Retriever::Initial(char *list, int from)
// Add a single URL to the list of URLs to visit.
// Since URLs are stored on a per server basis, we first need to find the
// the correct server to add the URL's path to.
//
// from == 0 urls in db.docs and no db.log
// from == 1 urls in start_url add url only if not already in the list
// from == 2 add url from db.log
// from == 3 urls in db.docs and there was a db.log
//
void Retriever::Initial(const String & list, int from)
{
//
// Split the list of urls up into individual urls.
//
StringList tokens(list, " \t");
String sig;
String url;
Server *server;
for (int i = 0; i < tokens.Count(); i++)
{
URL u(tokens[i]);
url = u.get(); // get before u.signature() resolves aliases
server = (Server *) servers[u.signature()];
if (debug > 2)
cout << "\t" << from << ":" << (int) log << ":" << url;
if (!server)
{
String robotsURL = u.signature();
robotsURL << "robots.txt";
StringList *localRobotsFile = GetLocal(robotsURL);
server = new Server(u, localRobotsFile);
servers.Add(u.signature(), server);
delete localRobotsFile;
}
if (from && visited.Exists(url))
{
if (debug > 2)
cout << " skipped" << endl;
continue;
}
else if (IsValidURL(url) != 1)
{
if (debug > 2)
cout << endl;
continue;
}
if (Retriever_noLog == log || from != 3)
{
if (debug > 2)
cout << " pushed";
server->push(u.get(), 0, 0, IsLocalURL(url.get()));
}
if (debug > 2)
cout << endl;
visited.Add(url, 0);
}
}
//*****************************************************************************
// void Retriever::Initial(List &list, int from)
//
void Retriever::Initial(List & list, int from)
{
list.Start_Get();
String *str;
// from == 0 is an optimisation for pushing url in update mode
// assuming that
// 1) there's many more urls in docdb
// 2) they're pushed first
// 3) there's no duplicate url in docdb
// then they don't need to be check against already pushed urls
// But 2) can be false with -l option
//
// FIXME it's nasty, what have to be test is :
// we have urls to push from db.docs but do we already have them in
// db.log? For this it's using a side effect with 'visited' and that
// urls in db.docs are only pushed via this method, and that db.log are pushed
// first, db.docs second, start_urls third!
//
if (!from && visited.Count())
{
from = 3;
}
while ((str = (String *) list.Get_Next()))
{
Initial(str->get(), from);
}
}
//*****************************************************************************
//
static void sigexit(int)
{
noSignal = 0; //don't exit here.. just set the flag.
}
static void sigpipe(int)
{
}
//*****************************************************************************
// static void sig_handlers
// initialise signal handlers
//
static void sig_handlers(void)
{
#ifndef _MSC_VER /* _WIN32 */
//POSIX SIGNALS
struct sigaction action;
/* SIGINT, SIGQUIT, SIGTERM */
action.sa_handler = sigexit;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGINT, &action, NULL) != 0)
reportError("Cannot install SIGINT handler\n");
if (sigaction(SIGQUIT, &action, NULL) != 0)
reportError("Cannot install SIGQUIT handler\n");
if (sigaction(SIGTERM, &action, NULL) != 0)
reportError("Cannot install SIGTERM handler\n");
if (sigaction(SIGHUP, &action, NULL) != 0)
reportError("Cannot install SIGHUP handler\n");
#else
//ANSI C signal handling - Limited to supported Windows signals.
signal(SIGINT, sigexit);
signal(SIGTERM, sigexit);
#endif //_MSC_VER /* _WIN32 */
}
static void sig_phandler(void)
{
#ifndef _MSC_VER /* _WIN32 */
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = sigpipe;
action.sa_flags = SA_RESTART;
if (sigaction(SIGPIPE, &action, NULL) != 0)
reportError("Cannot install SIGPIPE handler\n");
#endif //_MSC_VER /* _WIN32 */
}
//*****************************************************************************
// static void win32_check_messages
// Check WIN32 messages!
//
#ifdef _MSC_VER /* _WIN32 */
static void win32_check_messages(void)
{
// NEAL - NEEDS FINISHING/TESTING
#if 0
MSG msg = {0, 0, 0, 0};
int cDown = 0;
int controlDown = 0;
if( GetMessage(&msg, 0, 0, 0) )
{
switch(msg.message)
{
case WM_KEYDOWN:
{
if(LOWORD(msg.message)== 17)
controlDown = 1;
else if(LOWORD(msg.message) == 67)
{
cDown = 1;
}
}
break;
case WM_KEYUP:
{
if(LOWORD(msg.message) == 17)
controlDown = 0;
else if(LOWORD(msg.message) == 67)
cDown = 0;
}
break;
}
}
DispatchMessage(&msg);
#endif
}
#endif //_MSC_VER /* _WIN32 */
//*****************************************************************************
// void Retriever::Start()
// This is the main loop of the retriever. We will go through the
// list of paths stored for each server. While parsing the
// retrieved documents, new paths will be added to the servers. We
// return if no more paths need to be retrieved.
//
void Retriever::Start()
{
//
// Main digger loop. The todo list should initialy have the start
// URL and all the URLs which were seen in a previous dig. The
// loop will continue as long as there are more URLs to visit.
//
int more = 1;
Server *server;
URLRef *ref;
HtConfiguration *config = HtConfiguration::config();
//
// Always sig . The delay bother me but a bad db is worst
//
if (Retriever_noLog != log)
{
sig_handlers();
}
sig_phandler();
noSignal = 1;
///////
// Main loop. We keep on retrieving until a signal is received
// or all the servers' queues are empty.
///////
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
while (more && noSignal)
{
more = 0;
//
// Go through all the current servers in sequence.
// If they support persistent connections, we keep on popping
// from the same server queue until it's empty or we reach a maximum
// number of consecutive requests ("max_connection_requests").
// Or the loop may also continue for the infinite,
// if we set the "max_connection_requests" to -1.
// If the server doesn't support persistent connection, we take
// only an URL from it, then we skip to the next server.
//
// Since 15.05.02: even when persistent connections are activated
// we should wait for a 'server_wait_time' number of seconds
// after the 'max_connection_requests' value has been reached.
//
// Let's position at the beginning
servers.Start_Get();
int count;
// Maximum number of repeated requests with the same
// TCP connection (so on the same Server:Port).
int max_connection_requests;
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
while ((server = (Server *) servers.Get_NextElement()) && noSignal)
{
if (debug > 1)
cout << "pick: " << server->host() << ", # servers = " << servers.Count() << endl;
// We already know if a server supports HTTP pers. connections,
// because we asked it for the robots.txt file (constructor of
// the class).
// If the Server doesn't support persistent connections
// we turn it down to 1.
if (server->IsPersistentConnectionAllowed())
{
// Let's check for a '0' value (out of range)
// If set, we change it to 1.
if (config->Value("server", server->host(), "max_connection_requests") == 0)
max_connection_requests = 1;
else
max_connection_requests =
config->Value("server", server->host(), "max_connection_requests");
if (debug > 2)
{
cout << "> " << server->host() << " supports HTTP persistent connections";
if (max_connection_requests == -1)
cout << " (" << "infinite" << ")" << endl;
else
cout << " (" << max_connection_requests << ")" << endl;
}
}
else
{
// No HTTP persistent connections. So we request only 1 document.
max_connection_requests = 1;
if (debug > 2)
cout << "> " << server->host() << " with a traditional HTTP connection" << endl;
}
count = 0;
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
while (((max_connection_requests == -1) ||
(count < max_connection_requests)) && (ref = server->pop()) && noSignal)
{
count++;
//
// We have a URL to index, now. We need to register the
// fact that we are not done yet by setting the 'more'
// variable. So, we have to restart scanning the queue.
//
more = 1;
//
// Deal with the actual URL.
// We'll check with the server to see if we need to sleep()
// before parsing it.
//
parse_url(*ref);
delete ref;
// We reached the maximum number of connections (either with
// or without persistent connections) and we must pause and
// respect the 'net ethic'.
if ((max_connection_requests - count) == 0)
server->delay(); // This will pause if needed
// and reset the time
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
}
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
}
}
#ifdef _MSC_VER /* _WIN32 */
win32_check_messages();
#endif
// if we exited on signal
if (Retriever_noLog != log && !noSignal)
{
FILE *urls_parsed;
String filelog = config->Find("url_log");
// save url seen but not fetched
urls_parsed = fopen((char *) filelog, "w");
if (0 == urls_parsed)
{
reportError(form("Unable to create URL log file '%s'", filelog.get()));
}
else
{
servers.Start_Get();
while ((server = (Server *) servers.Get_NextElement()))
{
while (NULL != (ref = server->pop()))
{
fprintf(urls_parsed, "%s\n", (const char *) ref->GetURL().get());
delete ref;
}
}
fclose(urls_parsed);
}
}
words.Close();
}
//*****************************************************************************
// void Retriever::parse_url(URLRef &urlRef)
//
void Retriever::parse_url(URLRef & urlRef)
{
HtConfiguration *config = HtConfiguration::config();
URL url;
DocumentRef *ref;
int old_document;
time_t date;
static int index = 0;
static int local_urls_only = config->Boolean("local_urls_only");
static int mark_dead_servers = config->Boolean("ignore_dead_servers");
Server *server;
url.parse(urlRef.GetURL().get());
currenthopcount = urlRef.GetHopCount();
ref = docs[url.get()]; // It might be nice to have just an Exists() here
if (ref)
{
//
// We already have an entry for this document in our database.
// This means we can get the document ID and last modification
// time from there.
//
current_id = ref->DocID();
date = ref->DocTime();
if (ref->DocAccessed())
old_document = 1;
else // we haven't retrieved it yet, so we only have the first link
old_document = 0;
ref->DocBackLinks(ref->DocBackLinks() + 1); // we had a new link
ref->DocAccessed(time(0));
ref->DocState(Reference_normal);
currenthopcount = ref->DocHopCount();
}
else
{
//
// Never seen this document before. We need to create an
// entry for it. This implies that it gets a new document ID.
//
date = 0;
current_id = docs.NextDocID();
ref = new DocumentRef;
ref->DocID(current_id);
ref->DocURL(url.get());
ref->DocState(Reference_normal);
ref->DocAccessed(time(0));
ref->DocHopCount(currenthopcount);
ref->DocBackLinks(1); // We had to have a link to get here!
old_document = 0;
}
word_context.DocID(ref->DocID());
if (debug > 0)
{
//
// Display progress
//
cout << index++ << ':' << current_id << ':' << currenthopcount << ':' << url.get() << ": ";
cout.flush();
}
// Reset the document to clean out any old data
doc->Reset();
doc->Url(url.get());
doc->Referer(urlRef.GetReferer().get());
base = doc->Url();
// Retrieve document, first trying local file access if possible.
Transport::DocStatus status;
server = (Server *) servers[url.signature()];
StringList *local_filenames = GetLocal(url.get());
if (local_filenames)
{
if (debug > 1)
cout << "Trying local files" << endl;
status = doc->RetrieveLocal(date, local_filenames);
if (status == Transport::Document_not_local)
{
if (debug > 1)
cout << "Local retrieval failed, trying HTTP" << endl;
if (server && !server->IsDead() && !local_urls_only)
status = doc->Retrieve(server, date);
else
status = Transport::Document_no_host;
}
delete local_filenames;
}
else if (server && !server->IsDead() && !local_urls_only)
status = doc->Retrieve(server, date);
else
status = Transport::Document_no_host;
current_ref = ref;
//
// Determine what to do by looking at the status code returned by
// the Document retrieval process.
//
String shash;
String sx;
char bhash[16];
time_t ddate;
switch (status)
{
case Transport::Document_ok:
trackWords = 1;
if (check_unique_md5)
{
if (doc->StoredLength() > 0)
{
if (check_unique_date)
{
ddate = doc->ModTime();
if (ddate < time(NULL) - 10)
{ // Unknown date was set to current time
md5(bhash, doc->Contents(), doc->StoredLength(), &ddate, debug);
}
else
{
md5(bhash, doc->Contents(), doc->StoredLength(), 0, debug);
}
}
else
md5(bhash, doc->Contents(), doc->StoredLength(), 0, debug);
shash.append(bhash, MD5_LENGTH);
d_md5->Get(shash, sx);
if (!sx.empty())
{
if (debug > 1)
{
cout << " Detected duplicate by md5 hash" << endl;
}
words.Skip();
break; // Duplicate - don't index
}
else
{
d_md5->Put(shash, "x");
}
}
}
if (old_document)
{
if (doc->ModTime() == ref->DocTime())
{
words.Skip();
if (debug)
cout << " retrieved but not changed" << endl;
words.Skip();
break;
}
//
// Since we already had a record of this document and
// we were able to retrieve it, it must have changed
// since the last time we scanned it. This means that
// we need to assign a new document ID to it and mark
// the old one as obsolete.
//
words.Skip();
int backlinks = ref->DocBackLinks();
ref->DocState(Reference_obsolete);
docs.Add(*ref);
delete ref;
current_id = docs.NextDocID();
word_context.DocID(current_id);
ref = new DocumentRef;
ref->DocID(current_id);
ref->DocURL(url.get());
ref->DocState(Reference_normal);
ref->DocAccessed(time(0));
ref->DocHopCount(currenthopcount);
ref->DocBackLinks(backlinks);
if (debug)
cout << " (changed) ";
}
RetrievedDocument(*doc, url.get(), ref);
// Hey! If this document is marked noindex, don't even bother
// adding new words. Mark this as gone and get rid of it!
if (ref->DocState() == Reference_noindex)
{
if (debug > 1)
cout << " ( " << ref->DocURL() << " ignored)";
words.Skip();
}
else
words.Flush();
if (debug)
cout << " size = " << doc->Length() << endl;
if (urls_seen)
{
fprintf(urls_seen, "%s|%d|%s|%d|%d|1\n",
(const char *) url.get(), doc->Length(), doc->ContentType(),
(int) doc->ModTime(), currenthopcount);
}
break;
case Transport::Document_not_changed:
if (debug)
cout << " not changed" << endl;
words.Skip();
break;
case Transport::Document_not_found:
ref->DocState(Reference_not_found);
if (debug)
cout << " not found" << endl;
recordNotFound(url.get(), urlRef.GetReferer().get(), Transport::Document_not_found);
words.Skip();
break;
case Transport::Document_no_host:
ref->DocState(Reference_not_found);
if (debug)
cout << " host not found" << endl;
recordNotFound(url.get(), urlRef.GetReferer().get(), Transport::Document_no_host);
words.Skip();
// Mark the server as being down
if (server && mark_dead_servers)
server->IsDead(1);
break;
case Transport::Document_no_port:
ref->DocState(Reference_not_found);
if (debug)
cout << " host not found (port)" << endl;
recordNotFound(url.get(), urlRef.GetReferer().get(), Transport::Document_no_port);
words.Skip();
// Mark the server as being down
if (server && mark_dead_servers)
server->IsDead(1);
break;
case Transport::Document_not_parsable:
ref->DocState(Reference_noindex);
if (debug)
cout << " not Parsable" << endl;
words.Skip();
break;
case Transport::Document_redirect:
if (debug)
cout << " redirect" << endl;
ref->DocState(Reference_obsolete);
words.Skip();
got_redirect(doc->Redirected(), ref, (urlRef.GetReferer()).get());
break;
case Transport::Document_not_authorized:
ref->DocState(Reference_not_found);
if (debug)
cout << " not authorized" << endl;
words.Skip();
break;
case Transport::Document_not_local:
ref->DocState(Reference_not_found);
if (debug)
cout << " not local" << endl;
words.Skip();
break;
case Transport::Document_no_header:
ref->DocState(Reference_not_found);
if (debug)
cout << " no header" << endl;
words.Skip();
break;
case Transport::Document_connection_down:
ref->DocState(Reference_not_found);
if (debug)
cout << " connection down" << endl;
words.Skip();
break;
case Transport::Document_no_connection:
ref->DocState(Reference_not_found);
if (debug)
cout << " no connection" << endl;
words.Skip();
break;
case Transport::Document_not_recognized_service:
ref->DocState(Reference_not_found);
if (debug)
cout << " service not recognized" << endl;
// Mark the server as being down
if (server && mark_dead_servers)
server->IsDead(1);
words.Skip();
break;
case Transport::Document_other_error:
ref->DocState(Reference_not_found);
if (debug)
cout << " other error" << endl;
words.Skip();
break;
}
docs.Add(*ref);
delete ref;
}
//*****************************************************************************
// void Retriever::RetrievedDocument(Document &doc, const String &url, DocumentRef *ref)
// We found a document that needs to be parsed. Since we don't know the
// document type, we'll let the Document itself return an appropriate
// Parsable object which we can call upon to parse the document contents.
//
void Retriever::RetrievedDocument(Document & doc, const String & url, DocumentRef * ref)
{
n_links = 0;
current_ref = ref;
current_title = 0;
word_context.Anchor(0);
current_time = 0;
current_head = 0;
current_meta_dsc = 0;
//
// Create a parser object and let it have a go at the document.
// We will pass ourselves as a callback object for all the got_*()
// routines.
// This will generate the Parsable object as a specific parser
//
Parsable *parsable = doc.getParsable();
if (parsable)
parsable->parse(*this, *base);
else
{ // If we didn't get a parser, then we should get rid of this!
ref->DocState(Reference_noindex);
return;
}
// If just storing the first occurrence of each word in a document,
// we must now flush the words we saw in that document
if (no_store_phrases)
{
DictionaryCursor cursor;
char *key;
HtWordReference wordRef;
for (words_to_add.Start_Get (cursor);
(key = words_to_add.Get_Next(cursor)); )
{
word_entry *entry = (word_entry*) (words_to_add [key]);
wordRef.Location(entry->location);
wordRef.Flags(entry->flags);
wordRef.Word(key);
words.Replace(WordReference::Merge(wordRef, entry->context));
// How do I clean up properly?
delete entry;
}
words_to_add.Release ();
}
//
// We don't need to dispose of the parsable object since it will
// automatically be reused.
//
//
// Update the document reference
//
ref->DocHead((char *) current_head);
ref->DocMetaDsc((char *) current_meta_dsc);
if (current_time == 0)
ref->DocTime(doc.ModTime());
else
ref->DocTime(current_time);
ref->DocTitle((char *) current_title);
ref->DocSize(doc.Length());
ref->DocAccessed(time(0));
ref->DocLinks(n_links);
}
//*****************************************************************************
// int Retriever::Need2Get(const String &u)
// Return TRUE if we need to retrieve the given url. This will
// check the list of urls we have already visited.
//
int Retriever::Need2Get(const String & u)
{
static String url;
url = u;
return !visited.Exists(url);
}
//*****************************************************************************
// int Retriever::IsValidURL(const String &u)
// Return TRUE if we need to retrieve the given url. We will check
// for limits here.
//
int Retriever::IsValidURL(const String & u)
{
HtConfiguration *config = HtConfiguration::config();
Dictionary invalids;
Dictionary valids;
URL aUrl(u);
StringList tmpList;
// A list of bad extensions, separated by spaces or tabs
String t = config->Find(&aUrl, "bad_extensions");
String lowerp;
char *p = strtok(t, " \t");
while (p)
{
// Extensions are case insensitive
lowerp = p;
lowerp.lowercase();
invalids.Add(lowerp, 0);
p = strtok(0, " \t");