1 |
/* ============================================================================ |
2 |
* Douglas Thrift's Search Engine License |
3 |
* |
4 |
* Copyright (C) 2002-2004, 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 Page |
46 |
// |
47 |
// Douglas Thrift |
48 |
// |
49 |
// $Id$ |
50 |
|
51 |
#include "Page.h" |
52 |
|
53 |
Page::Page(string& url) : URL(url) |
54 |
{ |
55 |
size = 0; |
56 |
} |
57 |
|
58 |
void Page::setSize(unsigned size) |
59 |
{ |
60 |
this->size = size; |
61 |
} |
62 |
|
63 |
void Page::setTitle(string& title) |
64 |
{ |
65 |
this->title = title; |
66 |
} |
67 |
|
68 |
void Page::setDescription(string& description) |
69 |
{ |
70 |
this->description = description; |
71 |
} |
72 |
|
73 |
void Page::setText(string& text) |
74 |
{ |
75 |
this->text = text; |
76 |
} |
77 |
|
78 |
void Page::setHeadings(vector<string>& headings) |
79 |
{ |
80 |
this->headings = headings; |
81 |
} |
82 |
|
83 |
istream& operator>>(istream& is, Page& data) |
84 |
{ |
85 |
string line; |
86 |
#ifdef _OpenSSL_ |
87 |
bool set = false; |
88 |
#endif |
89 |
|
90 |
getline(is, line); |
91 |
|
92 |
if (line == " <page>") |
93 |
{ |
94 |
do |
95 |
{ |
96 |
getline(is, line); |
97 |
|
98 |
if (line.find(" <address>") == 0) |
99 |
{ |
100 |
unsigned start = line.find("<address>") + 9; |
101 |
unsigned finish = line.find("</address>", start); |
102 |
|
103 |
string address = line.substr(start, finish - start); |
104 |
|
105 |
entities(address, "<", '<'); |
106 |
entities(address, ">", '>'); |
107 |
entities(address, "&", '&'); |
108 |
|
109 |
data.setAddress(address); |
110 |
} |
111 |
else if (line.find(" <port>") == 0) |
112 |
{ |
113 |
unsigned start = line.find("<port>") + 6; |
114 |
unsigned finish = line.find("</port>", start); |
115 |
|
116 |
istringstream buffer(line.substr(start, finish - start)); |
117 |
unsigned port; |
118 |
|
119 |
buffer >> port; |
120 |
|
121 |
data.setPort(port); |
122 |
|
123 |
#ifdef _OpenSSL_ |
124 |
set = true; |
125 |
#endif |
126 |
} |
127 |
#ifdef _OpenSSL_ |
128 |
else if (line.find(" <tls>") == 0) |
129 |
{ |
130 |
unsigned start = line.find("<tls>") + 5; |
131 |
unsigned finish = line.find("</tls>", start); |
132 |
|
133 |
istringstream buffer(line.substr(start, finish - start)); |
134 |
bool tls; |
135 |
|
136 |
buffer.setf(ios_base::boolalpha); |
137 |
buffer >> tls; |
138 |
|
139 |
data.setTls(tls); |
140 |
|
141 |
if (!set && tls) data.setPort(443); |
142 |
} |
143 |
#endif |
144 |
else if (line.find(" <path>") == 0) |
145 |
{ |
146 |
unsigned start = line.find("<path>") + 6; |
147 |
unsigned finish = line.find("</path>", start); |
148 |
|
149 |
string path = line.substr(start, finish - start); |
150 |
|
151 |
entities(path, "<", '<'); |
152 |
entities(path, ">", '>'); |
153 |
entities(path, "&", '&'); |
154 |
|
155 |
data.setPath(path); |
156 |
} |
157 |
else if (line.find(" <size>") == 0) |
158 |
{ |
159 |
unsigned start = line.find("<size>") + 6; |
160 |
unsigned finish = line.find("</size>", start); |
161 |
|
162 |
istringstream buffer(line.substr(start, finish - start)); |
163 |
unsigned size; |
164 |
|
165 |
buffer >> size; |
166 |
|
167 |
data.setSize(size); |
168 |
} |
169 |
else if (line.find(" <title>") == 0) |
170 |
{ |
171 |
unsigned start = line.find("<title>") + 7; |
172 |
unsigned finish = line.find("</title>", start); |
173 |
|
174 |
string title = line.substr(start, finish - start); |
175 |
|
176 |
while (finish == string::npos) |
177 |
{ |
178 |
getline(is, line); |
179 |
finish = line.find("</title>"); |
180 |
title += '\n' + line.substr(0, finish - 0); |
181 |
} |
182 |
|
183 |
entities(title, "<", '<'); |
184 |
entities(title, ">", '>'); |
185 |
entities(title, "&", '&'); |
186 |
|
187 |
data.setTitle(title); |
188 |
} |
189 |
else if (line.find(" <description>") == 0) |
190 |
{ |
191 |
unsigned start = line.find("<description>") + 13; |
192 |
unsigned finish = line.find("</description>", start); |
193 |
|
194 |
string description = line.substr(start, finish - start); |
195 |
|
196 |
entities(description, "<", '<'); |
197 |
entities(description, ">", '>'); |
198 |
entities(description, "&", '&'); |
199 |
|
200 |
data.setDescription(description); |
201 |
} |
202 |
else if (line.find(" <text>") == 0) |
203 |
{ |
204 |
unsigned start = line.find("<text>") + 6; |
205 |
unsigned finish = line.find("</text>", start); |
206 |
|
207 |
string text = line.substr(start, finish - start); |
208 |
|
209 |
while (finish == string::npos) |
210 |
{ |
211 |
getline(is, line); |
212 |
finish = line.find("</text>"); |
213 |
text += '\n' + line.substr(0, finish - 0); |
214 |
} |
215 |
|
216 |
entities(text, "<", '<'); |
217 |
entities(text, ">", '>'); |
218 |
entities(text, "&", '&'); |
219 |
|
220 |
data.setText(text); |
221 |
} |
222 |
else if (line.find(" <heading>") == 0) |
223 |
{ |
224 |
unsigned start = line.find("<heading>") + 9; |
225 |
unsigned finish = line.find("</heading>", start); |
226 |
|
227 |
string heading = line.substr(start, finish - start); |
228 |
|
229 |
while (finish == string::npos) |
230 |
{ |
231 |
getline(is, line); |
232 |
finish = line.find("</heading>"); |
233 |
heading += line.substr(0, finish - 0); |
234 |
} |
235 |
|
236 |
entities(heading, "<", '<'); |
237 |
entities(heading, ">", '>'); |
238 |
entities(heading, "&", '&'); |
239 |
|
240 |
data.headings.push_back(heading); |
241 |
} |
242 |
} |
243 |
while (line != " </page>"); |
244 |
} |
245 |
|
246 |
return is; |
247 |
} |
248 |
|
249 |
ostream& operator<<(ostream& os, Page& data) |
250 |
{ |
251 |
string address = data.getAddress(); |
252 |
|
253 |
entities(address, '&', "&"); |
254 |
entities(address, '<', "<"); |
255 |
entities(address, '>', ">"); |
256 |
|
257 |
os << " <page>\n" << " <address>" << address << "</address>\n"; |
258 |
|
259 |
#ifndef _OpenSSL_ |
260 |
if (data.getPort() != 80) |
261 |
#else |
262 |
if ((data.getPort() != 80 && !data.getTls()) || (data.getPort() != 443 && |
263 |
data.getTls())) |
264 |
#endif |
265 |
{ |
266 |
os << " <port>" << data.getPort() << "</port>\n"; |
267 |
} |
268 |
|
269 |
#ifdef _OpenSSL_ |
270 |
os.setf(ios::boolalpha); |
271 |
os << " <tls>" << data.getTls() << "</tls>\n"; |
272 |
#endif |
273 |
|
274 |
string path = data.getPath(); |
275 |
|
276 |
entities(path, '&', "&"); |
277 |
entities(path, '<', "<"); |
278 |
entities(path, '>', ">"); |
279 |
|
280 |
os << " <path>" << path << "</path>\n"; |
281 |
|
282 |
os << " <size>" << data.getSize() << "</size>\n"; |
283 |
|
284 |
if(data.getTitle() != "") |
285 |
{ |
286 |
string title = data.getTitle(); |
287 |
|
288 |
entities(title, '&', "&"); |
289 |
entities(title, '<', "<"); |
290 |
entities(title, '>', ">"); |
291 |
|
292 |
os << " <title>" << title << "</title>\n"; |
293 |
} |
294 |
|
295 |
if(data.getDescription() != "") |
296 |
{ |
297 |
string description = data.getDescription(); |
298 |
|
299 |
entities(description, '&', "&"); |
300 |
entities(description, '<', "<"); |
301 |
entities(description, '>', ">"); |
302 |
|
303 |
os << " <description>" << description << "</description>\n"; |
304 |
} |
305 |
|
306 |
string text = data.getText(); |
307 |
|
308 |
entities(text, '&', "&"); |
309 |
entities(text, '<', "<"); |
310 |
entities(text, '>', ">"); |
311 |
|
312 |
os << " <text>" << text << "</text>\n"; |
313 |
|
314 |
for (int index = 0; index < data.getHeadings().size(); index++) |
315 |
{ |
316 |
string heading = data.getHeadings()[index]; |
317 |
|
318 |
entities(heading, '&', "&"); |
319 |
entities(heading, '<', "<"); |
320 |
entities(heading, '>', ">"); |
321 |
|
322 |
os << " <heading>" << heading << "</heading>\n"; |
323 |
} |
324 |
|
325 |
os << " </page>"; |
326 |
|
327 |
return os; |
328 |
} |