ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/Search/trunk/Processor.cpp
(Generate patch)

Comparing trunk/Search/Processor.cpp (file contents):
Revision 1 by douglas, 2002-12-04T20:22:59-08:00 vs.
Revision 18 by douglas, 2002-12-09T21:40:12-08:00

# Line 65 | Line 65 | bool Processor::process(HttpHandler& htt
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;
# Line 107 | Line 107 | bool Processor::process(HttpHandler& htt
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);
# Line 142 | Line 139 | bool Processor::process(HttpHandler& htt
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,
143 <                follow = true, answer = true;
142 >                inHeading = false, inComment = false, /*knowSize = page->getSize() > 0,
143 > */              follow = true, answer = true;
144          unsigned startComment = 0, finishComment = 0;
145          string line;
146          while (http.good())
# Line 243 | Line 240 | bool Processor::process(HttpHandler& htt
240                                                  unsigned start = lowerTag.find("href=\"") + 6;
241                                                  unsigned finish = lowerTag.find('\"', start);
242  
243 <                                                string fred = tag.substr(start, finish - start);
244 <                                                string link = getLink(fred, url);
243 >                                                string link = getLink(tag.substr(start, finish -
244 >                                                        start), url);
245  
246 <                                                if (link != "bad link") links.insert(link);
246 >                                                if (link != "") links.insert(link);
247                                          }
248                                          else if (lowerTag.find("href=") != string::npos)
249                                          {
# Line 255 | Line 252 | bool Processor::process(HttpHandler& htt
252  
253                                                  if (finish < close)
254                                                  {
255 <                                                        string fred = tag.substr(start, finish - start);
256 <                                                        string link = getLink(fred, url);
255 >                                                        string link = getLink(tag.substr(start, finish -
256 >                                                                start), url);
257  
258 <                                                        if (link != "bad link") links.insert(link);
258 >                                                        if (link != "") links.insert(link);
259                                                  }
260                                                  else
261                                                  {
262 <                                                        string fred = tag.substr(start, close - start);
263 <                                                        string link = getLink(fred, url);
262 >                                                        string link = getLink(tag.substr(start, close -
263 >                                                                start), url);
264  
265 <                                                        if (link != "bad link") links.insert(link);
265 >                                                        if (link != "") links.insert(link);
266                                                  }
267                                          }
268                                  }
# Line 353 | Line 350 | bool Processor::process(HttpHandler& htt
350  
351                  startComment = 0;
352                  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                }
353          }
354  
355          return answer;
356   }
357  
358 < string Processor::getTag(string& line, unsigned open, unsigned close)
358 > string Processor::getTag(const string& line, unsigned open, unsigned close)
359   {
360          string tag = line.substr(open + 1, close - open - 1);
361  
362          return tag;
363   }
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 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines