ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Processor.cpp
Revision: 21
Committed: 2002-12-10T14:44:50-08:00 (22 years, 6 months ago) by douglas
File size: 10195 byte(s)
Log Message:
Found the bug in private Processor.process() and fixed it.

File Contents

# Content
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 Processor
46 //
47 // Douglas Thrift
48 //
49 // Processor.cpp
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, "&nbsp;", ' ');
74 entities(title, "&lt;", '<');
75 entities(title, "&gt;", '>');
76 entities(title, "&quot;", '\"');
77 entities(title, "&amp;", '&');
78
79 entities(description, "&nbsp;", ' ');
80 entities(description, "&lt;", '<');
81 entities(description, "&gt;", '>');
82 entities(description, "&quot;", '\"');
83 entities(description, "&amp;", '&');
84
85 entities(text, "&nbsp;", ' ');
86 entities(text, "&lt;", '<');
87 entities(text, "&gt;", '>');
88 entities(text, "&quot;", '\"');
89 entities(text, "&amp;", '&');
90
91 for (int index = 0; index < headings.size(); index++)
92 {
93 entities(headings[index], "&nbsp;", ' ');
94 entities(headings[index], "&lt;", '<');
95 entities(headings[index], "&gt;", '>');
96 entities(headings[index], "&quot;", '\"');
97 entities(headings[index], "&amp;", '&');
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;
145 while (http.good())
146 {
147 http.getline(line);
148 string heading;
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 inHeading = true;
320 if (lowerTag.find("/h1") == 0 || lowerTag.find("/h2") == 0 ||
321 lowerTag.find("/h3") == 0 || lowerTag.find("/h4") == 0 ||
322 lowerTag.find("/h5") == 0 || lowerTag.find("/h6") == 0)
323 {
324 if (heading != "") headings.push_back(heading);
325 inHeading = false;
326 }
327
328 if (lowerTag.find("!--") == 0)
329 {
330 startComment = open;
331 inComment = true;
332 }
333 if (line.find("-->", begin) >= startComment && line.find("-->",
334 begin) != string::npos)
335 {
336 finishComment = line.find("-->", begin) + 3;
337 inComment = false;
338 }
339
340 if (close == string::npos)
341 {
342 begin = close;
343 }
344 else
345 {
346 begin = close + 1;
347 }
348 }
349
350 startComment = 0;
351 finishComment = 0;
352 }
353
354 return answer;
355 }
356
357 string Processor::getTag(const string& line, unsigned open, unsigned close)
358 {
359 string tag = line.substr(open + 1, close - open - 1);
360
361 return tag;
362 }