1 |
|
/* ============================================================================ |
2 |
|
* Douglas Thrift's Search Engine License |
3 |
|
* |
4 |
< |
* Copyright (C) 2002, Douglas Thrift. All Rights Reserved. |
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 |
|
* |
65 |
|
string title, description, text; |
66 |
|
vector<string> headings; |
67 |
|
|
68 |
< |
if (html(http)) |
68 |
> |
if (http.contentType().find("text/html") == 0) |
69 |
|
{ |
70 |
|
if (!process(http, url, title, description, text, headings)) return |
71 |
|
false; |
107 |
|
} |
108 |
|
else |
109 |
|
{ |
110 |
– |
bool knowSize = page->getSize() > 0; |
111 |
– |
|
110 |
|
string line; |
111 |
|
while (http.good()) |
112 |
|
{ |
113 |
|
http.getline(line); |
114 |
|
|
115 |
|
text += line + "\n"; |
118 |
– |
|
119 |
– |
if (!knowSize) page->setSize(page->getSize() + line.length() + 1); |
116 |
|
} |
117 |
|
|
118 |
|
normalize(text); |
119 |
|
} |
120 |
|
|
121 |
+ |
page->setSize(http.contentLength()); |
122 |
|
page->setURL(url); |
123 |
|
page->setTitle(title); |
124 |
|
page->setDescription(description); |
139 |
|
description, string& text, vector<string>& headings) |
140 |
|
{ |
141 |
|
bool inHtml = false, inHead = false, inTitle = false, inBody = false, |
142 |
< |
inHeading = false, inComment = false, knowSize = page->getSize() > 0, |
146 |
< |
follow = true, answer = true; |
142 |
> |
inHeading = false, inComment = false, follow = true, answer = true; |
143 |
|
unsigned startComment = 0, finishComment = 0; |
144 |
|
string line; |
145 |
|
while (http.good()) |
154 |
|
unsigned close = line.find('>', begin); |
155 |
|
|
156 |
|
string next; |
157 |
< |
while (close == string::npos) |
157 |
> |
while (close == string::npos && http.good()) |
158 |
|
{ |
159 |
|
http.getline(next); |
160 |
|
line += '\n' + next; |
239 |
|
unsigned start = lowerTag.find("href=\"") + 6; |
240 |
|
unsigned finish = lowerTag.find('\"', start); |
241 |
|
|
242 |
< |
string fred = tag.substr(start, finish - start); |
243 |
< |
string link = getLink(fred, url); |
242 |
> |
string link = getLink(tag.substr(start, finish - |
243 |
> |
start), url); |
244 |
|
|
245 |
< |
if (link != "bad link") links.insert(link); |
245 |
> |
if (link != "") links.insert(link); |
246 |
|
} |
247 |
|
else if (lowerTag.find("href=") != string::npos) |
248 |
|
{ |
251 |
|
|
252 |
|
if (finish < close) |
253 |
|
{ |
254 |
< |
string fred = tag.substr(start, finish - start); |
255 |
< |
string link = getLink(fred, url); |
254 |
> |
string link = getLink(tag.substr(start, finish - |
255 |
> |
start), url); |
256 |
|
|
257 |
< |
if (link != "bad link") links.insert(link); |
257 |
> |
if (link != "") links.insert(link); |
258 |
|
} |
259 |
|
else |
260 |
|
{ |
261 |
< |
string fred = tag.substr(start, close - start); |
262 |
< |
string link = getLink(fred, url); |
261 |
> |
string link = getLink(tag.substr(start, close - |
262 |
> |
start), url); |
263 |
|
|
264 |
< |
if (link != "bad link") links.insert(link); |
264 |
> |
if (link != "") links.insert(link); |
265 |
|
} |
266 |
|
} |
267 |
|
} |
349 |
|
|
350 |
|
startComment = 0; |
351 |
|
finishComment = 0; |
356 |
– |
|
357 |
– |
if (!knowSize) page->setSize(page->getSize() + line.length() + 1); |
358 |
– |
} |
359 |
– |
|
360 |
– |
return answer; |
361 |
– |
} |
362 |
– |
|
363 |
– |
bool Processor::html(HttpHandler& http) |
364 |
– |
{ |
365 |
– |
bool answer = false; |
366 |
– |
|
367 |
– |
string line; |
368 |
– |
http.getline(line); |
369 |
– |
|
370 |
– |
while (http.good()) |
371 |
– |
{ |
372 |
– |
string field; |
373 |
– |
http.getline(field, ' '); |
374 |
– |
if (field == "") break; |
375 |
– |
http.getline(line); |
376 |
– |
|
377 |
– |
if (field == "Content-Type:" || field == "Content-type:") |
378 |
– |
{ |
379 |
– |
if (line.find("text/html") != string::npos) |
380 |
– |
{ |
381 |
– |
answer = true; |
382 |
– |
} |
383 |
– |
} |
384 |
– |
|
385 |
– |
if (field == "Content-Length:" || field == "Content-length:") |
386 |
– |
{ |
387 |
– |
page->setSize(strtoul(line.c_str(), 0, 0)); |
388 |
– |
} |
352 |
|
} |
353 |
|
|
354 |
|
return answer; |
355 |
|
} |
356 |
|
|
357 |
< |
string Processor::getTag(string& line, unsigned open, unsigned close) |
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 |
|
} |
400 |
– |
|
401 |
– |
string Processor::getLink(string& link, URL& url) |
402 |
– |
{ |
403 |
– |
string hyperlink = "bad link"; |
404 |
– |
|
405 |
– |
if (link.find('#') != string::npos) |
406 |
– |
{ |
407 |
– |
unsigned pound = link.find('#'); |
408 |
– |
link.erase(pound); |
409 |
– |
} |
410 |
– |
|
411 |
– |
if (link.find("://") != string::npos) |
412 |
– |
{ |
413 |
– |
if (link.find("http://") == 0) hyperlink = link; |
414 |
– |
} |
415 |
– |
else if (link.find("mailto:") == 0) |
416 |
– |
{ |
417 |
– |
// do nothing we are not evil spammers! |
418 |
– |
} |
419 |
– |
else if (link.find("//") == 0) |
420 |
– |
{ |
421 |
– |
hyperlink = "http:" + link; |
422 |
– |
} |
423 |
– |
else if (link.find('/') == 0) |
424 |
– |
{ |
425 |
– |
hyperlink = url.getURL(); |
426 |
– |
|
427 |
– |
unsigned path = hyperlink.find('/', 7); |
428 |
– |
hyperlink.erase(path); |
429 |
– |
|
430 |
– |
hyperlink += link; |
431 |
– |
} |
432 |
– |
else if (link == "") |
433 |
– |
{ |
434 |
– |
// a blank link is useless |
435 |
– |
} |
436 |
– |
else |
437 |
– |
{ |
438 |
– |
hyperlink = url.getURL(); |
439 |
– |
string path = url.getPath(); |
440 |
– |
|
441 |
– |
unsigned cutoff = hyperlink.rfind(path); |
442 |
– |
hyperlink.erase(cutoff); |
443 |
– |
|
444 |
– |
unsigned dir = path.rfind('/') + 1; |
445 |
– |
path.erase(dir); |
446 |
– |
|
447 |
– |
while (link.find("../") == 0) |
448 |
– |
{ |
449 |
– |
unsigned dot = path.rfind('/') - 1; |
450 |
– |
unsigned up = path.rfind('/', dot) + 1; |
451 |
– |
|
452 |
– |
path.erase(up); |
453 |
– |
link.erase(0, 3); |
454 |
– |
} |
455 |
– |
while (link.find("./") == 0) |
456 |
– |
{ |
457 |
– |
link.erase(0, 2); |
458 |
– |
} |
459 |
– |
|
460 |
– |
hyperlink += path + link; |
461 |
– |
} |
462 |
– |
|
463 |
– |
return hyperlink; |
464 |
– |
} |