1 |
/* ============================================================================ |
2 |
* Douglas Thrift's Search Engine License |
3 |
* |
4 |
* Copyright (C) 2002, Douglas Thrift. All Rights Reserved. |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions are met: |
7 |
* |
8 |
* 1. Redistributions of source code must retain the above copyright notice, |
9 |
* this list of conditions and the following disclaimer. |
10 |
* |
11 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
12 |
* this list of conditions and the following disclaimer in the documentation |
13 |
* and/or other materials provided with the distribution. |
14 |
* |
15 |
* 3. The end-user documentation included with the redistribution, if any, must |
16 |
* include the following acknowledgment: |
17 |
* |
18 |
* "This product includes software developed by Douglas Thrift |
19 |
* (http://computers.douglasthrift.net/searchengine/)." |
20 |
* |
21 |
* Alternately, this acknowledgment may appear in the software itself, if |
22 |
* and wherever such third-party acknowledgments normally appear. |
23 |
* |
24 |
* 4. The names "Douglas Thrift" and "Douglas Thrift's Search Engine" must not |
25 |
* be used to endorse or promote products derived from this software without |
26 |
* specific prior written permission. For written permission, please visit |
27 |
* http://www.douglasthrift.net/contact.cgi for contact information. |
28 |
* |
29 |
* 5. Products derived from this software may not be called "Douglas Thrift's |
30 |
* Search Engine", nor may "Douglas Thrift's Search Engine" appear in their |
31 |
* name, without prior written permission. |
32 |
* |
33 |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
34 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
35 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
36 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
37 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
38 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
39 |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
40 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
41 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
42 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
43 |
* ============================================================================ |
44 |
*/ |
45 |
// Douglas Thrift's Search Engine Indexer |
46 |
// |
47 |
// Douglas Thrift |
48 |
// |
49 |
// Indexer.cpp |
50 |
|
51 |
#include "Indexer.h" |
52 |
|
53 |
Indexer::Indexer(string& indexFile, set<string>& domains, |
54 |
set<string>& restrictions) |
55 |
{ |
56 |
this->indexFile = indexFile; |
57 |
this->domains = domains; |
58 |
this->restrictions = restrictions; |
59 |
} |
60 |
|
61 |
void Indexer::index(string& begin) |
62 |
{ |
63 |
ofstream fout(indexFile.c_str()); |
64 |
|
65 |
fout << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>" |
66 |
<< "\n<!DOCTYPE index SYSTEM \"index.dtd\">\n" |
67 |
<< "<index>\n"; |
68 |
|
69 |
URL first(begin); |
70 |
|
71 |
index(first, fout); |
72 |
|
73 |
fout << "</index>\n"; |
74 |
|
75 |
fout.close(); |
76 |
} |
77 |
|
78 |
void Indexer::index(URL& url, ofstream& fout) |
79 |
{ |
80 |
if (domains.find(url.getAddress() += url.getPort() != 80 ? ":" + |
81 |
url.getPort() : "") != domains.end() && pages.find(url.getURL()) == |
82 |
pages.end()) |
83 |
{ |
84 |
if (checked.find(url.getAddress() += url.getPort() != 80 ? ":" + |
85 |
url.getPort() : "") == checked.end()) |
86 |
{ |
87 |
robots(url); |
88 |
} |
89 |
|
90 |
if (!restricted(url)) |
91 |
{ |
92 |
if (http.handle(url, true)) |
93 |
{ |
94 |
if (http.contentType().find("text/plain") == 0 || |
95 |
http.contentType().find("text/html") == 0) |
96 |
{ |
97 |
http.clear(); |
98 |
if (!http.handle(url)) exit(1); |
99 |
|
100 |
cout << "Indexing " << url << "..." << flush; |
101 |
|
102 |
if (processor.process(http, url)) |
103 |
{ |
104 |
Page page = processor.getPage(); |
105 |
fout << page << "\n"; |
106 |
|
107 |
cout << "done.\n"; |
108 |
} |
109 |
else |
110 |
{ |
111 |
cout << "canceled.\n"; |
112 |
} |
113 |
|
114 |
pages.insert(url.getURL()); |
115 |
Set pageLinks = processor.getLinks(); |
116 |
processor.reset(); |
117 |
|
118 |
for (SetIterator link = pageLinks.begin(); link != |
119 |
pageLinks.end(); link++) |
120 |
{ |
121 |
if (pages.find(*link) == pages.end()) |
122 |
{ |
123 |
links.push(URL(*link)); |
124 |
} |
125 |
} |
126 |
} |
127 |
else |
128 |
{ |
129 |
// unhandled content |
130 |
} |
131 |
} |
132 |
else if (http.redirect() != "") |
133 |
{ |
134 |
if (pages.find(http.redirect()) == pages.end()) |
135 |
{ |
136 |
links.push(URL(http.redirect())); |
137 |
} |
138 |
} |
139 |
|
140 |
http.clear(); |
141 |
} |
142 |
} |
143 |
|
144 |
if (!links.empty()) |
145 |
{ |
146 |
URL next = links.front(); |
147 |
links.pop(); |
148 |
|
149 |
if (debug) cerr << "next = " << next << "\n"; |
150 |
|
151 |
index(next, fout); |
152 |
} |
153 |
} |
154 |
|
155 |
bool Indexer::restricted(URL& url) |
156 |
{ |
157 |
bool answer = false; |
158 |
|
159 |
for (SetIterator itor = restrictions.begin(); itor != restrictions.end(); |
160 |
itor++) |
161 |
{ |
162 |
URL checker = *itor; |
163 |
|
164 |
if (url.getAddress() == checker.getAddress() && url.getPort() == |
165 |
checker.getPort()) |
166 |
{ |
167 |
if (url.getPath().find(checker.getPath()) == 0) |
168 |
{ |
169 |
answer = true; |
170 |
break; |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
return answer; |
176 |
} |
177 |
|
178 |
void Indexer::robots(URL& url) |
179 |
{ |
180 |
URL robots = url; |
181 |
robots.setPath("/robots.txt"); |
182 |
|
183 |
if (http.handle(robots)) |
184 |
{ |
185 |
cout << "Checking " << robots << "..." << flush; |
186 |
|
187 |
string line; |
188 |
do http.getline(line); while (http.good() && line != ""); |
189 |
|
190 |
bool record = false, hasVersion = false, hasName = false, hasAll = |
191 |
false; |
192 |
robot state = none; |
193 |
Set restrictionsVersion, restrictionsName, restrictionsAll; |
194 |
|
195 |
while (http.good()) |
196 |
{ |
197 |
http.getline(line); |
198 |
|
199 |
unsigned comment = line.find('#'); |
200 |
if (comment != string::npos) line.erase(comment); |
201 |
|
202 |
if (line == "" && comment == string::npos) record = false; |
203 |
if (line == "") continue; |
204 |
|
205 |
unsigned colon = line.find(':'); |
206 |
|
207 |
string field = line.substr(0, colon); |
208 |
string value = line.substr(colon + 1); |
209 |
|
210 |
normalize(value); |
211 |
|
212 |
if (field == "User-agent" && value == agent(true)) |
213 |
{ |
214 |
state = version; |
215 |
record = true; |
216 |
hasVersion = true; |
217 |
} |
218 |
else if (field == "User-agent" && value == agent(false)) |
219 |
{ |
220 |
state = name; |
221 |
record = true; |
222 |
hasName = true; |
223 |
} |
224 |
else if (field == "User-agent" && value == "*") |
225 |
{ |
226 |
state = all; |
227 |
record = true; |
228 |
hasAll = true; |
229 |
} |
230 |
else if (field == "Disallow" && record && value == "") |
231 |
{ |
232 |
// no restrictions |
233 |
} |
234 |
else if (field == "Disallow" && record) |
235 |
{ |
236 |
URL restriction = robots; |
237 |
restriction.setPath(value); |
238 |
|
239 |
switch (state) |
240 |
{ |
241 |
case version: |
242 |
restrictionsVersion.insert(restriction.getURL()); |
243 |
break; |
244 |
case name: |
245 |
restrictionsName.insert(restriction.getURL()); |
246 |
break; |
247 |
case all: |
248 |
restrictionsAll.insert(restriction.getURL()); |
249 |
break; |
250 |
default: |
251 |
break; |
252 |
} |
253 |
} |
254 |
} |
255 |
|
256 |
if (hasVersion) |
257 |
{ |
258 |
state = version; |
259 |
} |
260 |
else if (hasName) |
261 |
{ |
262 |
state = name; |
263 |
} |
264 |
else if (hasAll) |
265 |
{ |
266 |
state = all; |
267 |
} |
268 |
else |
269 |
{ |
270 |
state = none; |
271 |
} |
272 |
|
273 |
SetIterator itor; |
274 |
switch (state) |
275 |
{ |
276 |
case version: |
277 |
for (itor = restrictionsVersion.begin(); itor != |
278 |
restrictionsVersion.end(); itor++) |
279 |
{ |
280 |
restrictions.insert(*itor); |
281 |
} |
282 |
break; |
283 |
case name: |
284 |
for (itor = restrictionsName.begin(); itor != |
285 |
restrictionsName.end(); itor++) |
286 |
{ |
287 |
restrictions.insert(*itor); |
288 |
} |
289 |
break; |
290 |
case all: |
291 |
for (itor = restrictionsAll.begin(); itor != |
292 |
restrictionsAll.end(); itor++) |
293 |
{ |
294 |
restrictions.insert(*itor); |
295 |
} |
296 |
break; |
297 |
default: |
298 |
break; |
299 |
} |
300 |
|
301 |
cout << "done.\n"; |
302 |
} |
303 |
|
304 |
http.clear(); |
305 |
|
306 |
checked.insert(url.getAddress() += url.getPort() != 80 ? ":" + |
307 |
url.getPort() : ""); |
308 |
} |