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 |
– |
|
59 |
|
void Indexer::index(string& begin) |
60 |
|
{ |
61 |
< |
unsigned separator = indexFile.rfind(slash); |
62 |
< |
string dtd = separator != string::npos ? indexFile.substr(0, separator) + |
63 |
< |
slash + "index.dtd" : "index.dtd"; |
72 |
< |
|
61 |
> |
unsigned separator(indexFile.rfind(slash)); |
62 |
> |
string dtd(separator != string::npos ? indexFile.substr(0, separator) + |
63 |
> |
slash + "index.dtd" : "index.dtd"); |
64 |
|
ifstream fin(dtd.c_str()); |
65 |
|
|
66 |
|
if (!fin.is_open()) |
85 |
|
|
86 |
|
fin.close(); |
87 |
|
|
88 |
< |
string lock = indexFile + ".lock"; |
98 |
< |
|
88 |
> |
string lock(indexFile + ".lock"); |
89 |
|
ofstream fout(lock.c_str()); |
90 |
+ |
|
91 |
|
fout.close(); |
92 |
|
fout.open(indexFile.c_str()); |
93 |
|
|
98 |
|
URL first(begin); |
99 |
|
|
100 |
|
index(first, fout); |
101 |
+ |
|
102 |
+ |
while (!links.empty()) |
103 |
+ |
{ |
104 |
+ |
URL next(links.front()); |
105 |
+ |
string referer(referers.front()); |
106 |
+ |
|
107 |
+ |
links.pop(); |
108 |
+ |
referers.pop(); |
109 |
+ |
|
110 |
+ |
if (debug) cerr << "next = " << next << "\n"; |
111 |
+ |
|
112 |
+ |
index(next, fout, referer); |
113 |
+ |
} |
114 |
|
|
115 |
|
fout << "</index>\n"; |
116 |
|
|
119 |
|
unlink(lock.c_str()); |
120 |
|
} |
121 |
|
|
122 |
< |
void Indexer::index(URL& url, ofstream& fout, const string referer) |
122 |
> |
void Indexer::index(URL& url, ofstream& fout, const string& referer) |
123 |
|
{ |
124 |
|
if (domains.find(url.getAddress()) != domains.end() && |
125 |
|
pages.find(url.getURL()) == pages.end()) |
138 |
|
http.contentType().find("text/html") == 0) |
139 |
|
{ |
140 |
|
http.clear(); |
141 |
+ |
|
142 |
|
if (!http.handle(url, referer)) exit(1); |
143 |
|
|
144 |
|
cout << "Indexing " << url << " ... " << flush; |
145 |
|
|
146 |
|
if (processor.process(http, url)) |
147 |
|
{ |
148 |
< |
Page page = processor.getPage(); |
148 |
> |
Page page(processor.getPage()); |
149 |
> |
|
150 |
|
fout << page << "\n"; |
151 |
|
|
152 |
|
cout << "done.\n"; |
157 |
|
} |
158 |
|
|
159 |
|
pages.insert(url.getURL()); |
160 |
< |
Set pageLinks = processor.getLinks(); |
160 |
> |
|
161 |
> |
Set pageLinks(processor.getLinks()); |
162 |
> |
|
163 |
|
processor.reset(); |
164 |
|
|
165 |
< |
for (SetIterator link = pageLinks.begin(); link != |
165 |
> |
for (SetIterator link(pageLinks.begin()); link != |
166 |
|
pageLinks.end(); link++) |
167 |
|
{ |
168 |
|
if (pages.find(*link) == pages.end()) |
169 |
|
{ |
170 |
< |
links.push(URL(*link)); |
170 |
> |
links.push(*link); |
171 |
|
referers.push(url.getURL()); |
172 |
|
} |
173 |
|
} |
181 |
|
{ |
182 |
|
if (pages.find(http.redirect()) == pages.end()) |
183 |
|
{ |
184 |
< |
links.push(URL(http.redirect())); |
184 |
> |
links.push(http.redirect()); |
185 |
|
referers.push(url.getURL()); |
186 |
|
} |
187 |
|
} |
189 |
|
http.clear(); |
190 |
|
} |
191 |
|
} |
184 |
– |
|
185 |
– |
if (!links.empty()) |
186 |
– |
{ |
187 |
– |
URL next = links.front(); |
188 |
– |
links.pop(); |
189 |
– |
|
190 |
– |
string referer = referers.front(); |
191 |
– |
referers.pop(); |
192 |
– |
|
193 |
– |
if (debug) cerr << "next = " << next << "\n"; |
194 |
– |
|
195 |
– |
index(next, fout, referer); |
196 |
– |
} |
192 |
|
} |
193 |
|
|
194 |
|
bool Indexer::restricted(URL& url) |
195 |
|
{ |
196 |
< |
bool answer = false; |
196 |
> |
bool answer(false); |
197 |
|
|
198 |
< |
for (SetIterator itor = restrictions.begin(); itor != restrictions.end(); |
198 |
> |
for (SetIterator itor(restrictions.begin()); itor != restrictions.end(); |
199 |
|
itor++) |
200 |
|
{ |
201 |
< |
URL checker = *itor; |
201 |
> |
URL checker(*itor); |
202 |
|
|
203 |
|
if (url.getAddress() == checker.getAddress() && url.getPort() == |
204 |
|
checker.getPort()) |
206 |
|
if (url.getPath().find(checker.getPath()) == 0) |
207 |
|
{ |
208 |
|
answer = true; |
209 |
+ |
|
210 |
|
break; |
211 |
|
} |
212 |
|
} |
217 |
|
|
218 |
|
void Indexer::robots(URL& url) |
219 |
|
{ |
220 |
< |
URL robots = url; |
220 |
> |
URL robots(url); |
221 |
> |
|
222 |
|
robots.setPath("/robots.txt"); |
223 |
|
|
224 |
|
if (http.handle(robots)) |
226 |
|
cout << "Checking " << robots << " ... " << flush; |
227 |
|
|
228 |
|
string line; |
229 |
< |
|
230 |
< |
bool record = false, hasVersion = false, hasName = false, hasAll = |
234 |
< |
false; |
235 |
< |
robot state = none; |
229 |
> |
bool record(false), hasVersion(false), hasName(false), hasAll(false); |
230 |
> |
robot state(none); |
231 |
|
Set restrictionsVersion, restrictionsName, restrictionsAll; |
232 |
|
|
233 |
|
while (http.good()) |
234 |
|
{ |
235 |
|
http.getline(line); |
236 |
|
|
237 |
< |
unsigned comment = line.find('#'); |
237 |
> |
unsigned comment(line.find('#')); |
238 |
> |
|
239 |
|
if (comment != string::npos) line.erase(comment); |
240 |
|
|
241 |
|
if (line == "" && comment == string::npos) record = false; |
242 |
|
if (line == "") continue; |
243 |
|
|
244 |
< |
unsigned colon = line.find(':'); |
245 |
< |
|
246 |
< |
string field = line.substr(0, colon); |
251 |
< |
string value = line.substr(colon + 1); |
244 |
> |
unsigned colon(line.find(':')); |
245 |
> |
string field(line.substr(0, colon)); |
246 |
> |
string value(line.substr(colon + 1)); |
247 |
|
|
248 |
|
normalize(value); |
249 |
|
|
271 |
|
} |
272 |
|
else if (field == "Disallow" && record) |
273 |
|
{ |
274 |
< |
URL restriction = robots; |
274 |
> |
URL restriction(robots); |
275 |
> |
|
276 |
|
restriction.setPath(value); |
277 |
|
|
278 |
|
switch (state) |
286 |
|
case all: |
287 |
|
restrictionsAll.insert(restriction.getURL()); |
288 |
|
break; |
293 |
– |
default: |
294 |
– |
break; |
289 |
|
} |
290 |
|
} |
291 |
|
} |
308 |
|
} |
309 |
|
|
310 |
|
SetIterator itor; |
311 |
+ |
|
312 |
|
switch (state) |
313 |
|
{ |
314 |
|
case version: |
326 |
|
} |
327 |
|
break; |
328 |
|
case all: |
329 |
< |
for (itor = restrictionsAll.begin(); itor != |
330 |
< |
restrictionsAll.end(); itor++) |
329 |
> |
for (itor = restrictionsAll.begin(); itor != restrictionsAll.end(); |
330 |
> |
itor++) |
331 |
|
{ |
332 |
|
restrictions.insert(*itor); |
333 |
|
} |
334 |
|
break; |
340 |
– |
default: |
341 |
– |
break; |
335 |
|
} |
336 |
|
|
337 |
|
cout << "done.\n"; |