1 |
/* ============================================================================ |
2 |
* Douglas Thrift's Search Engine License |
3 |
* |
4 |
* Copyright (C) 2002-2004, 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 Processor |
46 |
// |
47 |
// Douglas Thrift |
48 |
// |
49 |
// $Id: Processor.cpp,v 1.10 2004/02/21 02:58:09 douglas Exp $ |
50 |
|
51 |
#include "Processor.h" |
52 |
|
53 |
Processor::Processor() |
54 |
{ |
55 |
page = new Page(); |
56 |
} |
57 |
|
58 |
Processor::~Processor() |
59 |
{ |
60 |
delete page; |
61 |
} |
62 |
|
63 |
bool Processor::process(HttpHandler& http, URL& url) |
64 |
{ |
65 |
string title, description, text; |
66 |
vector<string> headings; |
67 |
|
68 |
if (http.contentType().find("text/html") == 0) |
69 |
{ |
70 |
if (!process(http, url, title, description, text, headings)) return |
71 |
false; |
72 |
|
73 |
entities(title, " ", ' '); |
74 |
entities(title, "<", '<'); |
75 |
entities(title, ">", '>'); |
76 |
entities(title, """, '\"'); |
77 |
entities(title, "&", '&'); |
78 |
|
79 |
entities(description, " ", ' '); |
80 |
entities(description, "<", '<'); |
81 |
entities(description, ">", '>'); |
82 |
entities(description, """, '\"'); |
83 |
entities(description, "&", '&'); |
84 |
|
85 |
entities(text, " ", ' '); |
86 |
entities(text, "<", '<'); |
87 |
entities(text, ">", '>'); |
88 |
entities(text, """, '\"'); |
89 |
entities(text, "&", '&'); |
90 |
|
91 |
for (int index = 0; index < headings.size(); index++) |
92 |
{ |
93 |
entities(headings[index], " ", ' '); |
94 |
entities(headings[index], "<", '<'); |
95 |
entities(headings[index], ">", '>'); |
96 |
entities(headings[index], """, '\"'); |
97 |
entities(headings[index], "&", '&'); |
98 |
} |
99 |
|
100 |
normalize(title); |
101 |
normalize(description); |
102 |
normalize(text); |
103 |
for (int index0 = 0; index0 < headings.size(); index0++) |
104 |
{ |
105 |
normalize(headings[index0]); |
106 |
} |
107 |
} |
108 |
else |
109 |
{ |
110 |
string line; |
111 |
while (http.good()) |
112 |
{ |
113 |
http.getline(line); |
114 |
|
115 |
text += line + "\n"; |
116 |
} |
117 |
|
118 |
normalize(text); |
119 |
} |
120 |
|
121 |
page->setSize(http.contentLength()); |
122 |
page->setURL(url); |
123 |
page->setTitle(title); |
124 |
page->setDescription(description); |
125 |
page->setText(text); |
126 |
page->setHeadings(headings); |
127 |
|
128 |
return true; |
129 |
} |
130 |
|
131 |
void Processor::reset() |
132 |
{ |
133 |
links.clear(); |
134 |
delete page; |
135 |
page = new Page(); |
136 |
} |
137 |
|
138 |
bool Processor::process(HttpHandler& http, URL& url, string& title, string& |
139 |
description, string& text, vector<string>& headings) |
140 |
{ |
141 |
bool inHtml = false, inHead = false, inTitle = false, inBody = false, |
142 |
inHeading = false, inComment = false, follow = true, answer = true; |
143 |
unsigned startComment = 0, finishComment = 0; |
144 |
string line, heading; |
145 |
|
146 |
while (http.good()) |
147 |
{ |
148 |
http.getline(line); |
149 |
|
150 |
unsigned begin = 0; |
151 |
while (begin < line.length()) |
152 |
{ |
153 |
unsigned open = line.find('<', begin); |
154 |
unsigned close = line.find('>', begin); |
155 |
|
156 |
string next; |
157 |
while (close == string::npos && http.good()) |
158 |
{ |
159 |
http.getline(next); |
160 |
line += '\n' + next; |
161 |
close = line.find('>', begin); |
162 |
} |
163 |
|
164 |
// strangely this is necessary sometimes |
165 |
if (open == string::npos) open = line.find('<', begin); |
166 |
|
167 |
string between = line.substr(begin, open - begin); |
168 |
string tag = getTag(line, open, close); |
169 |
string lowerTag(tag.length(), ' '); |
170 |
|
171 |
for (unsigned index = 0; index < tag.length(); index++) |
172 |
{ |
173 |
lowerTag[index] = tolower(tag[index]); |
174 |
} |
175 |
|
176 |
if (inHtml && !inComment) |
177 |
{ |
178 |
if (inHead && inTitle) |
179 |
{ |
180 |
title += between + "\n"; |
181 |
} |
182 |
|
183 |
if (inBody) |
184 |
{ |
185 |
text += between + "\n"; |
186 |
} |
187 |
|
188 |
if (inBody && inHeading) |
189 |
{ |
190 |
heading += between + "\n"; |
191 |
} |
192 |
if (((lowerTag.find("meta ") == 0) || (lowerTag.find("meta\n") |
193 |
== 0) || (lowerTag.find("meta ") == 0)) && inHead) |
194 |
{ |
195 |
if (lowerTag.find("name=robots") != string::npos || |
196 |
lowerTag.find("name=\"robots\"") != string::npos) |
197 |
{ |
198 |
unsigned start = lowerTag.find("content=\"") + 9; |
199 |
unsigned finish = lowerTag.find('\"', start); |
200 |
|
201 |
string robots = lowerTag.substr(start, finish - start); |
202 |
|
203 |
if ((robots.find("noindex") != string::npos && |
204 |
robots.find("nofollow") != string::npos) || |
205 |
robots.find("none") != string::npos) |
206 |
{ |
207 |
answer = false; |
208 |
follow = false; |
209 |
links.clear(); |
210 |
|
211 |
return answer; |
212 |
} |
213 |
else if (robots.find("noindex") != string::npos) |
214 |
{ |
215 |
answer = false; |
216 |
} |
217 |
else if (robots.find("nofollow") != string::npos) |
218 |
{ |
219 |
follow = false; |
220 |
links.clear(); |
221 |
} |
222 |
} |
223 |
else if (lowerTag.find("name=description") != string::npos |
224 |
|| lowerTag.find("name=\"description\"") != |
225 |
string::npos) |
226 |
{ |
227 |
unsigned start = lowerTag.find("content=\"") + 9; |
228 |
unsigned finish = lowerTag.find('\"', start); |
229 |
|
230 |
description = tag.substr(start, finish - start); |
231 |
} |
232 |
} |
233 |
|
234 |
if (((lowerTag.find("a ") == 0) || (lowerTag.find("a\n") == 0) |
235 |
|| (lowerTag.find("a ") == 0)) && inBody && follow) |
236 |
{ |
237 |
if (lowerTag.find("href=\"") != string::npos) |
238 |
{ |
239 |
unsigned start = lowerTag.find("href=\"") + 6; |
240 |
unsigned finish = lowerTag.find('\"', start); |
241 |
|
242 |
string link = getLink(tag.substr(start, finish - |
243 |
start), url); |
244 |
|
245 |
if (link != "") links.insert(link); |
246 |
} |
247 |
else if (lowerTag.find("href=") != string::npos) |
248 |
{ |
249 |
unsigned start = lowerTag.find("href=") + 5; |
250 |
unsigned finish = lowerTag.find(' ', start); |
251 |
|
252 |
if (finish < close) |
253 |
{ |
254 |
string link = getLink(tag.substr(start, finish - |
255 |
start), url); |
256 |
|
257 |
if (link != "") links.insert(link); |
258 |
} |
259 |
else |
260 |
{ |
261 |
string link = getLink(tag.substr(start, close - |
262 |
start), url); |
263 |
|
264 |
if (link != "") links.insert(link); |
265 |
} |
266 |
} |
267 |
} |
268 |
|
269 |
if ((lowerTag.find("img ") == 0) || (lowerTag.find("img\n") == |
270 |
0) || (lowerTag.find("img ")) && inBody) |
271 |
{ |
272 |
if (lowerTag.find("alt=\"") != string::npos) |
273 |
{ |
274 |
unsigned start = lowerTag.find("alt=\"") + 5; |
275 |
unsigned finish = lowerTag.find('\"', start); |
276 |
|
277 |
text += tag.substr(start, finish - start) + ' '; |
278 |
if (inHeading) heading += tag.substr(start, finish - |
279 |
start) + ' '; |
280 |
} |
281 |
else if (lowerTag.find("alt=") != string::npos) |
282 |
{ |
283 |
unsigned start = lowerTag.find("alt=") + 4; |
284 |
unsigned finish = lowerTag.find(' ', start); |
285 |
|
286 |
if (finish < close) |
287 |
{ |
288 |
text += tag.substr(start, finish - start) + ' '; |
289 |
if (inHeading) heading += tag.substr(start, finish |
290 |
- start) + ' '; |
291 |
} |
292 |
else |
293 |
{ |
294 |
text += tag.substr(start, close - start) + ' '; |
295 |
if (inHeading) heading += tag.substr(start, close - |
296 |
start) + ' '; |
297 |
} |
298 |
} |
299 |
} |
300 |
} |
301 |
|
302 |
if (lowerTag.find("html") == 0) inHtml = true; |
303 |
if (lowerTag.find("/html") == 0) inHtml = false; |
304 |
|
305 |
if (lowerTag.find("head") == 0) inHead = true; |
306 |
if (lowerTag.find("/head") == 0) inHead = false; |
307 |
|
308 |
if (lowerTag.find("title") == 0) inTitle = true; |
309 |
if (lowerTag.find("/title") == 0) inTitle = false; |
310 |
|
311 |
if (lowerTag.find("body") == 0 || lowerTag.find("noframes") == 0) |
312 |
inBody = true; |
313 |
if (lowerTag.find("/body") == 0 || lowerTag.find("/noframes") == 0) |
314 |
inBody = false; |
315 |
|
316 |
if (lowerTag.find("h1") == 0 || lowerTag.find("h2") == 0 || |
317 |
lowerTag.find("h3") == 0 || lowerTag.find("h4") == 0 || |
318 |
lowerTag.find("h5") == 0 || lowerTag.find("h6") == 0) |
319 |
{ |
320 |
heading = ""; |
321 |
inHeading = true; |
322 |
} |
323 |
if (lowerTag.find("/h1") == 0 || lowerTag.find("/h2") == 0 || |
324 |
lowerTag.find("/h3") == 0 || lowerTag.find("/h4") == 0 || |
325 |
lowerTag.find("/h5") == 0 || lowerTag.find("/h6") == 0) |
326 |
{ |
327 |
if (heading != "") headings.push_back(heading); |
328 |
inHeading = false; |
329 |
} |
330 |
|
331 |
if (lowerTag.find("!--") == 0) |
332 |
{ |
333 |
startComment = open; |
334 |
inComment = true; |
335 |
} |
336 |
if (line.find("-->", begin) >= startComment && line.find("-->", |
337 |
begin) != string::npos) |
338 |
{ |
339 |
finishComment = line.find("-->", begin) + 3; |
340 |
inComment = false; |
341 |
} |
342 |
|
343 |
if (close == string::npos) |
344 |
{ |
345 |
begin = close; |
346 |
} |
347 |
else |
348 |
{ |
349 |
begin = close + 1; |
350 |
} |
351 |
} |
352 |
|
353 |
startComment = 0; |
354 |
finishComment = 0; |
355 |
} |
356 |
|
357 |
return answer; |
358 |
} |
359 |
|
360 |
string Processor::getTag(const string& line, unsigned open, unsigned close) |
361 |
{ |
362 |
string tag = line.substr(open + 1, close - open - 1); |
363 |
|
364 |
return tag; |
365 |
} |