ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Processor.cpp
Revision: 18
Committed: 2002-12-09T21:40:12-08:00 (22 years, 6 months ago) by douglas
File size: 10218 byte(s)
Log Message:
Implemented more HttpHandler stuff.
Added news: protocol to those ignored by getLink().

File Contents

# User Rev Content
1 douglas 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 douglas 17 if (http.contentType().find("text/html") == 0)
69 douglas 1 {
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 douglas 17 page->setSize(http.contentLength());
122 douglas 1 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 douglas 17 inHeading = false, inComment = false, /*knowSize = page->getSize() > 0,
143     */ follow = true, answer = true;
144 douglas 1 unsigned startComment = 0, finishComment = 0;
145     string line;
146     while (http.good())
147     {
148     http.getline(line);
149     string heading;
150    
151     unsigned begin = 0;
152     while (begin < line.length())
153     {
154     unsigned open = line.find('<', begin);
155     unsigned close = line.find('>', begin);
156    
157     string next;
158     while (close == string::npos)
159     {
160     http.getline(next);
161     line += '\n' + next;
162     close = line.find('>', begin);
163     }
164    
165     // strangely this is necessary sometimes
166     if (open == string::npos) open = line.find('<', begin);
167    
168     string between = line.substr(begin, open - begin);
169     string tag = getTag(line, open, close);
170     string lowerTag(tag.length(), ' ');
171    
172     for (unsigned index = 0; index < tag.length(); index++)
173     {
174     lowerTag[index] = tolower(tag[index]);
175     }
176    
177     if (inHtml && !inComment)
178     {
179     if (inHead && inTitle)
180     {
181     title += between + "\n";
182     }
183    
184     if (inBody)
185     {
186     text += between + "\n";
187     }
188    
189     if (inBody && inHeading)
190     {
191     heading += between + "\n";
192     }
193     if (((lowerTag.find("meta ") == 0) || (lowerTag.find("meta\n")
194     == 0) || (lowerTag.find("meta ") == 0)) && inHead)
195     {
196     if (lowerTag.find("name=robots") != string::npos ||
197     lowerTag.find("name=\"robots\"") != string::npos)
198     {
199     unsigned start = lowerTag.find("content=\"") + 9;
200     unsigned finish = lowerTag.find('\"', start);
201    
202     string robots = lowerTag.substr(start, finish - start);
203    
204     if ((robots.find("noindex") != string::npos &&
205     robots.find("nofollow") != string::npos) ||
206     robots.find("none") != string::npos)
207     {
208     answer = false;
209     follow = false;
210     links.clear();
211    
212     return answer;
213     }
214     else if (robots.find("noindex") != string::npos)
215     {
216     answer = false;
217     }
218     else if (robots.find("nofollow") != string::npos)
219     {
220     follow = false;
221     links.clear();
222     }
223     }
224     else if (lowerTag.find("name=description") != string::npos
225     || lowerTag.find("name=\"description\"") !=
226     string::npos)
227     {
228     unsigned start = lowerTag.find("content=\"") + 9;
229     unsigned finish = lowerTag.find('\"', start);
230    
231     description = tag.substr(start, finish - start);
232     }
233     }
234    
235     if (((lowerTag.find("a ") == 0) || (lowerTag.find("a\n") == 0)
236     || (lowerTag.find("a ") == 0)) && inBody && follow)
237     {
238     if (lowerTag.find("href=\"") != string::npos)
239     {
240     unsigned start = lowerTag.find("href=\"") + 6;
241     unsigned finish = lowerTag.find('\"', start);
242    
243 douglas 15 string link = getLink(tag.substr(start, finish -
244     start), url);
245 douglas 1
246 douglas 17 if (link != "") links.insert(link);
247 douglas 1 }
248     else if (lowerTag.find("href=") != string::npos)
249     {
250     unsigned start = lowerTag.find("href=") + 5;
251     unsigned finish = lowerTag.find(' ', start);
252    
253     if (finish < close)
254     {
255 douglas 15 string link = getLink(tag.substr(start, finish -
256     start), url);
257 douglas 1
258 douglas 17 if (link != "") links.insert(link);
259 douglas 1 }
260     else
261     {
262 douglas 15 string link = getLink(tag.substr(start, close -
263     start), url);
264 douglas 1
265 douglas 17 if (link != "") links.insert(link);
266 douglas 1 }
267     }
268     }
269    
270     if ((lowerTag.find("img ") == 0) || (lowerTag.find("img\n") ==
271     0) || (lowerTag.find("img ")) && inBody)
272     {
273     if (lowerTag.find("alt=\"") != string::npos)
274     {
275     unsigned start = lowerTag.find("alt=\"") + 5;
276     unsigned finish = lowerTag.find('\"', start);
277    
278     text += tag.substr(start, finish - start) + ' ';
279     if (inHeading) heading += tag.substr(start, finish -
280     start) + ' ';
281     }
282     else if (lowerTag.find("alt=") != string::npos)
283     {
284     unsigned start = lowerTag.find("alt=") + 4;
285     unsigned finish = lowerTag.find(' ', start);
286    
287     if (finish < close)
288     {
289     text += tag.substr(start, finish - start) + ' ';
290     if (inHeading) heading += tag.substr(start, finish
291     - start) + ' ';
292     }
293     else
294     {
295     text += tag.substr(start, close - start) + ' ';
296     if (inHeading) heading += tag.substr(start, close -
297     start) + ' ';
298     }
299     }
300     }
301     }
302    
303     if (lowerTag.find("html") == 0) inHtml = true;
304     if (lowerTag.find("/html") == 0) inHtml = false;
305    
306     if (lowerTag.find("head") == 0) inHead = true;
307     if (lowerTag.find("/head") == 0) inHead = false;
308    
309     if (lowerTag.find("title") == 0) inTitle = true;
310     if (lowerTag.find("/title") == 0) inTitle = false;
311    
312     if (lowerTag.find("body") == 0 || lowerTag.find("noframes") == 0)
313     inBody = true;
314     if (lowerTag.find("/body") == 0 || lowerTag.find("/noframes") == 0)
315     inBody = false;
316    
317     if (lowerTag.find("h1") == 0 || lowerTag.find("h2") == 0 ||
318     lowerTag.find("h3") == 0 || lowerTag.find("h4") == 0 ||
319     lowerTag.find("h5") == 0 || lowerTag.find("h6") == 0)
320     inHeading = true;
321     if (lowerTag.find("/h1") == 0 || lowerTag.find("/h2") == 0 ||
322     lowerTag.find("/h3") == 0 || lowerTag.find("/h4") == 0 ||
323     lowerTag.find("/h5") == 0 || lowerTag.find("/h6") == 0)
324     {
325     if (heading != "") headings.push_back(heading);
326     inHeading = false;
327     }
328    
329     if (lowerTag.find("!--") == 0)
330     {
331     startComment = open;
332     inComment = true;
333     }
334     if (line.find("-->", begin) >= startComment && line.find("-->",
335     begin) != string::npos)
336     {
337     finishComment = line.find("-->", begin) + 3;
338     inComment = false;
339     }
340    
341     if (close == string::npos)
342     {
343     begin = close;
344     }
345     else
346     {
347     begin = close + 1;
348     }
349     }
350    
351     startComment = 0;
352     finishComment = 0;
353     }
354    
355     return answer;
356     }
357    
358 douglas 15 string Processor::getTag(const string& line, unsigned open, unsigned close)
359 douglas 1 {
360     string tag = line.substr(open + 1, close - open - 1);
361    
362     return tag;
363     }