50 |
|
|
51 |
|
#include "Processor.hpp" |
52 |
|
|
53 |
– |
Processor::Processor() |
54 |
– |
{ |
55 |
– |
page = new Page(); |
56 |
– |
} |
57 |
– |
|
58 |
– |
Processor::~Processor() |
59 |
– |
{ |
60 |
– |
delete page; |
61 |
– |
} |
62 |
– |
|
53 |
|
bool Processor::process(HttpHandler& http, URL& url) |
54 |
|
{ |
55 |
|
string title, description, text; |
76 |
|
entities(text, """, '\"'); |
77 |
|
entities(text, "&", '&'); |
78 |
|
|
79 |
< |
for (int index = 0; index < headings.size(); index++) |
79 |
> |
for (size_t index(0); index < headings.size(); index++) |
80 |
|
{ |
81 |
|
entities(headings[index], " ", ' '); |
82 |
|
entities(headings[index], "<", '<'); |
89 |
|
normalize(description); |
90 |
|
normalize(text); |
91 |
|
|
92 |
< |
for (int index0 = 0; index0 < headings.size(); index0++) |
92 |
> |
for (size_t index0(0); index0 < headings.size(); index0++) |
93 |
|
{ |
94 |
|
normalize(headings[index0]); |
95 |
|
} |
108 |
|
normalize(text); |
109 |
|
} |
110 |
|
|
111 |
< |
page->setSize(http.contentLength()); |
112 |
< |
page->setURL(url); |
113 |
< |
page->setTitle(title); |
114 |
< |
page->setDescription(description); |
115 |
< |
page->setText(text); |
116 |
< |
page->setHeadings(headings); |
111 |
> |
page.setSize(http.contentLength()); |
112 |
> |
page.setURL(url); |
113 |
> |
page.setTitle(title); |
114 |
> |
page.setDescription(description); |
115 |
> |
page.setText(text); |
116 |
> |
page.setHeadings(headings); |
117 |
|
|
118 |
|
return true; |
119 |
|
} |
122 |
|
{ |
123 |
|
links.clear(); |
124 |
|
|
125 |
< |
delete page; |
136 |
< |
|
137 |
< |
page = new Page(); |
125 |
> |
page = Page(); |
126 |
|
} |
127 |
|
|
128 |
|
bool Processor::process(HttpHandler& http, URL& url, string& title, string& |
129 |
|
description, string& text, vector<string>& headings) |
130 |
|
{ |
131 |
< |
bool inHtml = false, inHead = false, inTitle = false, inBody = false, |
132 |
< |
inHeading = false, inComment = false, follow = true, answer = true; |
131 |
> |
bool inHtml(false), inHead(false), inTitle(false), inBody(false), |
132 |
> |
inHeading(false), inComment(false), follow(true), answer(true); |
133 |
|
string line, heading; |
134 |
|
|
135 |
|
while (http.good()) |
136 |
|
{ |
137 |
|
http.getline(line); |
138 |
|
|
139 |
< |
unsigned begin = 0, startComment = 0; |
139 |
> |
unsigned begin(0), startComment(0); |
140 |
|
|
141 |
|
while (begin < line.length()) |
142 |
|
{ |
143 |
< |
unsigned open = line.find('<', begin); |
156 |
< |
unsigned close = line.find('>', begin); |
143 |
> |
unsigned open(line.find('<', begin)), close(line.find('>', begin)); |
144 |
|
string next; |
145 |
|
|
146 |
|
while (close == string::npos && http.good()) |
147 |
|
{ |
148 |
|
http.getline(next); |
149 |
+ |
|
150 |
|
line += '\n' + next; |
151 |
+ |
|
152 |
|
close = line.find('>', begin); |
153 |
|
} |
154 |
|
|
155 |
|
// strangely this is necessary sometimes |
156 |
|
if (open == string::npos) open = line.find('<', begin); |
157 |
|
|
158 |
< |
string between = line.substr(begin, open - begin); |
159 |
< |
string tag = getTag(line, open, close); |
171 |
< |
string lowerTag(tag.length(), ' '); |
172 |
< |
|
173 |
< |
for (unsigned index = 0; index < tag.length(); index++) |
174 |
< |
{ |
175 |
< |
lowerTag[index] = tolower(tag[index]); |
176 |
< |
} |
158 |
> |
string between(line.substr(begin, open - begin)), tag(getTag(line, |
159 |
> |
open, close)), lowerTag(tolower(tag)); |
160 |
|
|
161 |
|
if (inHtml && !inComment) |
162 |
|
{ |
163 |
|
if (inHead && inTitle) |
164 |
|
{ |
165 |
< |
title += between + "\n"; |
165 |
> |
title += between + '\n'; |
166 |
|
} |
167 |
|
|
168 |
|
if (inBody) |
169 |
|
{ |
170 |
< |
text += between + "\n"; |
170 |
> |
text += between + '\n'; |
171 |
|
} |
172 |
|
|
173 |
|
if (inBody && inHeading) |
174 |
|
{ |
175 |
< |
heading += between + "\n"; |
175 |
> |
heading += between + '\n'; |
176 |
|
} |
177 |
|
|
178 |
|
if (((lowerTag.find("meta ") == 0) || (lowerTag.find("meta\n") |
181 |
|
if (lowerTag.find("name=robots") != string::npos || |
182 |
|
lowerTag.find("name=\"robots\"") != string::npos) |
183 |
|
{ |
184 |
< |
unsigned start = lowerTag.find("content=\"") + 9; |
185 |
< |
unsigned finish = lowerTag.find('\"', start); |
186 |
< |
|
204 |
< |
string robots = lowerTag.substr(start, finish - start); |
184 |
> |
unsigned start(lowerTag.find("content=\"") + 9), |
185 |
> |
finish(lowerTag.find('\"', start)); |
186 |
> |
string robots(lowerTag.substr(start, finish - start)); |
187 |
|
|
188 |
|
if ((robots.find("noindex") != string::npos && |
189 |
|
robots.find("nofollow") != string::npos) || |
191 |
|
{ |
192 |
|
answer = false; |
193 |
|
follow = false; |
194 |
+ |
|
195 |
|
links.clear(); |
196 |
|
|
197 |
|
return answer; |
203 |
|
else if (robots.find("nofollow") != string::npos) |
204 |
|
{ |
205 |
|
follow = false; |
206 |
+ |
|
207 |
|
links.clear(); |
208 |
|
} |
209 |
|
} |
211 |
|
|| lowerTag.find("name=\"description\"") != |
212 |
|
string::npos) |
213 |
|
{ |
214 |
< |
unsigned start = lowerTag.find("content=\"") + 9; |
215 |
< |
unsigned finish = lowerTag.find('\"', start); |
214 |
> |
unsigned start(lowerTag.find("content=\"") + 9), |
215 |
> |
finish(lowerTag.find('\"', start)); |
216 |
|
|
217 |
|
description = tag.substr(start, finish - start); |
218 |
|
} |
223 |
|
{ |
224 |
|
if (lowerTag.find("href=\"") != string::npos) |
225 |
|
{ |
226 |
< |
unsigned start = lowerTag.find("href=\"") + 6; |
227 |
< |
unsigned finish = lowerTag.find('\"', start); |
226 |
> |
unsigned start(lowerTag.find("href=\"") + 6), |
227 |
> |
finish(lowerTag.find('\"', start)); |
228 |
|
|
229 |
|
string link = getLink(tag.substr(start, finish - |
230 |
|
start), url); |
233 |
|
} |
234 |
|
else if (lowerTag.find("href=") != string::npos) |
235 |
|
{ |
236 |
< |
unsigned start = lowerTag.find("href=") + 5; |
237 |
< |
unsigned finish = lowerTag.find(' ', start); |
236 |
> |
unsigned start(lowerTag.find("href=") + 5), |
237 |
> |
finish(lowerTag.find(' ', start)); |
238 |
|
|
239 |
|
if (finish < close) |
240 |
|
{ |
258 |
|
{ |
259 |
|
if (lowerTag.find("alt=\"") != string::npos) |
260 |
|
{ |
261 |
< |
unsigned start = lowerTag.find("alt=\"") + 5; |
262 |
< |
unsigned finish = lowerTag.find('\"', start); |
261 |
> |
unsigned start(lowerTag.find("alt=\"") + 5), |
262 |
> |
finish(lowerTag.find('\"', start)); |
263 |
|
|
264 |
|
text += tag.substr(start, finish - start) + ' '; |
265 |
+ |
|
266 |
|
if (inHeading) heading += tag.substr(start, finish - |
267 |
|
start) + ' '; |
268 |
|
} |
269 |
|
else if (lowerTag.find("alt=") != string::npos) |
270 |
|
{ |
271 |
< |
unsigned start = lowerTag.find("alt=") + 4; |
272 |
< |
unsigned finish = lowerTag.find(' ', start); |
271 |
> |
unsigned start(lowerTag.find("alt=") + 4), |
272 |
> |
finish(lowerTag.find(' ', start)); |
273 |
|
|
274 |
|
if (finish < close) |
275 |
|
{ |
276 |
|
text += tag.substr(start, finish - start) + ' '; |
277 |
+ |
|
278 |
|
if (inHeading) heading += tag.substr(start, finish |
279 |
|
- start) + ' '; |
280 |
|
} |
281 |
|
else |
282 |
|
{ |
283 |
|
text += tag.substr(start, close - start) + ' '; |
284 |
+ |
|
285 |
|
if (inHeading) heading += tag.substr(start, close - |
286 |
|
start) + ' '; |
287 |
|
} |
346 |
|
|
347 |
|
string Processor::getTag(const string& line, unsigned open, unsigned close) |
348 |
|
{ |
349 |
< |
string tag = line.substr(open + 1, close - open - 1); |
363 |
< |
|
364 |
< |
return tag; |
349 |
> |
return line.substr(open + 1, close - open - 1); |
350 |
|
} |