ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/Indexer.cpp
Revision: 197
Committed: 2003-07-14T18:30:46-07:00 (21 years, 11 months ago) by douglas
File size: 8556 byte(s)
Log Message:
Fixed unlink on Windows.

File Contents

# Content
1 /* ============================================================================
2 * Douglas Thrift's Search Engine License
3 *
4 * Copyright (C) 2002-2003, 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 // $Id: Indexer.cpp,v 1.12 2003/07/15 01:30:46 douglas Exp $
50
51 #include "Indexer.h"
52
53 #ifndef _WIN32
54 #include <unistd.h>
55 #else // _WIN32
56 inline int unlink(const char* filename) { return DeleteFile(filename); }
57 #endif // _WIN32
58
59 Indexer::Indexer(string& indexFile, set<string>& domains, set<string>&
60 restrictions)
61 {
62 this->indexFile = indexFile;
63 this->domains = domains;
64 this->restrictions = restrictions;
65 }
66
67 void Indexer::index(string& begin)
68 {
69 unsigned separator = indexFile.rfind(slash);
70 string dtd = separator != string::npos ? indexFile.substr(0, separator) +
71 slash + "index.dtd" : "index.dtd";
72
73 ifstream fin(dtd.c_str());
74
75 if (!fin.is_open())
76 {
77 ofstream fout(dtd.c_str());
78
79 fout << "<!ELEMENT index (page*)>\n"
80 << "<!ELEMENT page (address, port?, path, title?, description?, ke"
81 << "ywords?, text,\n"
82 << " heading*)\n"
83 << ">\n"
84 << "<!ELEMENT address (#PCDATA)>\n"
85 << "<!ELEMENT port (#PCDATA)>\n"
86 << "<!ELEMENT path (#PCDATA)>\n"
87 << "<!ELEMENT size (#PCDATA)>\n"
88 << "<!ELEMENT title (#PCDATA)>\n"
89 << "<!ELEMENT description (#PCDATA)>\n"
90 << "<!ELEMENT text (#PCDATA)>\n"
91 << "<!ELEMENT heading (#PCDATA)>\n";
92
93 fout.close();
94 }
95
96 fin.close();
97
98 string lock = indexFile + ".lock";
99
100 ofstream fout(lock.c_str());
101 fout.close();
102 fout.open(indexFile.c_str());
103
104 fout << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>"
105 << "\n<!DOCTYPE index SYSTEM \"index.dtd\">\n"
106 << "<index>\n";
107
108 URL first(begin);
109
110 index(first, fout);
111
112 fout << "</index>\n";
113
114 fout.close();
115
116 unlink(lock.c_str());
117 }
118
119 void Indexer::index(URL& url, ofstream& fout, const string referer)
120 {
121 if (domains.find(url.getAddress() += url.getPort() != 80 ? ":" +
122 url.getPort() : "") != domains.end() && pages.find(url.getURL()) ==
123 pages.end())
124 {
125 if (checked.find(url.getAddress() += url.getPort() != 80 ? ":" +
126 url.getPort() : "") == checked.end())
127 {
128 robots(url);
129 }
130
131 if (!restricted(url))
132 {
133 if (http.handle(url, referer, true))
134 {
135 if (http.contentType().find("text/plain") == 0 ||
136 http.contentType().find("text/html") == 0)
137 {
138 http.clear();
139 if (!http.handle(url, referer)) exit(1);
140
141 cout << "Indexing " << url << "..." << flush;
142
143 if (processor.process(http, url))
144 {
145 Page page = processor.getPage();
146 fout << page << "\n";
147
148 cout << "done.\n";
149 }
150 else
151 {
152 cout << "canceled.\n";
153 }
154
155 pages.insert(url.getURL());
156 Set pageLinks = processor.getLinks();
157 processor.reset();
158
159 for (SetIterator link = pageLinks.begin(); link !=
160 pageLinks.end(); link++)
161 {
162 if (pages.find(*link) == pages.end())
163 {
164 links.push(URL(*link));
165 referers.push(url.getURL());
166 }
167 }
168 }
169 else
170 {
171 // unhandled content
172 }
173 }
174 else if (http.redirect() != "")
175 {
176 if (pages.find(http.redirect()) == pages.end())
177 {
178 links.push(URL(http.redirect()));
179 referers.push(url.getURL());
180 }
181 }
182
183 http.clear();
184 }
185 }
186
187 if (!links.empty())
188 {
189 URL next = links.front();
190 links.pop();
191
192 string referer = referers.front();
193 referers.pop();
194
195 if (debug) cerr << "next = " << next << "\n";
196
197 index(next, fout, referer);
198 }
199 }
200
201 bool Indexer::restricted(URL& url)
202 {
203 bool answer = false;
204
205 for (SetIterator itor = restrictions.begin(); itor != restrictions.end();
206 itor++)
207 {
208 URL checker = *itor;
209
210 if (url.getAddress() == checker.getAddress() && url.getPort() ==
211 checker.getPort())
212 {
213 if (url.getPath().find(checker.getPath()) == 0)
214 {
215 answer = true;
216 break;
217 }
218 }
219 }
220
221 return answer;
222 }
223
224 void Indexer::robots(URL& url)
225 {
226 URL robots = url;
227 robots.setPath("/robots.txt");
228
229 if (http.handle(robots))
230 {
231 cout << "Checking " << robots << "..." << flush;
232
233 string line;
234
235 bool record = false, hasVersion = false, hasName = false, hasAll =
236 false;
237 robot state = none;
238 Set restrictionsVersion, restrictionsName, restrictionsAll;
239
240 while (http.good())
241 {
242 http.getline(line);
243
244 unsigned comment = line.find('#');
245 if (comment != string::npos) line.erase(comment);
246
247 if (line == "" && comment == string::npos) record = false;
248 if (line == "") continue;
249
250 unsigned colon = line.find(':');
251
252 string field = line.substr(0, colon);
253 string value = line.substr(colon + 1);
254
255 normalize(value);
256
257 if (field == "User-agent" && value == agent(true))
258 {
259 state = version;
260 record = true;
261 hasVersion = true;
262 }
263 else if (field == "User-agent" && value == agent(false))
264 {
265 state = name;
266 record = true;
267 hasName = true;
268 }
269 else if (field == "User-agent" && value == "*")
270 {
271 state = all;
272 record = true;
273 hasAll = true;
274 }
275 else if (field == "Disallow" && record && value == "")
276 {
277 // no restrictions
278 }
279 else if (field == "Disallow" && record)
280 {
281 URL restriction = robots;
282 restriction.setPath(value);
283
284 switch (state)
285 {
286 case version:
287 restrictionsVersion.insert(restriction.getURL());
288 break;
289 case name:
290 restrictionsName.insert(restriction.getURL());
291 break;
292 case all:
293 restrictionsAll.insert(restriction.getURL());
294 break;
295 default:
296 break;
297 }
298 }
299 }
300
301 if (hasVersion)
302 {
303 state = version;
304 }
305 else if (hasName)
306 {
307 state = name;
308 }
309 else if (hasAll)
310 {
311 state = all;
312 }
313 else
314 {
315 state = none;
316 }
317
318 SetIterator itor;
319 switch (state)
320 {
321 case version:
322 for (itor = restrictionsVersion.begin(); itor !=
323 restrictionsVersion.end(); itor++)
324 {
325 restrictions.insert(*itor);
326 }
327 break;
328 case name:
329 for (itor = restrictionsName.begin(); itor !=
330 restrictionsName.end(); itor++)
331 {
332 restrictions.insert(*itor);
333 }
334 break;
335 case all:
336 for (itor = restrictionsAll.begin(); itor !=
337 restrictionsAll.end(); itor++)
338 {
339 restrictions.insert(*itor);
340 }
341 break;
342 default:
343 break;
344 }
345
346 cout << "done.\n";
347 }
348
349 http.clear();
350
351 checked.insert(url.getAddress() += url.getPort() != 80 ? ":" +
352 url.getPort() : "");
353 }