1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/***************************************************************************
* Copyright (C) 2004 by Paulo Moura Guedes *
* moura@kdewebdev.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <kresolver.h>
#include "url.h"
#include "mstring.h"
#include "../utils/utils.h"
#include <kcharsets.h>
Node::LinkType Url::resolveLinkType(QString const& url)
{
QString aux(url);
aux = KURL::decode_string(aux);
if(aux.isNull())
return Node::relative;
if(findWord(url, "FILE:") != -1)
return Node::file_href;
else if(findWord(KCharsets::resolveEntities(url), "MAILTO:") != -1)
return Node::mailto;
else if( (int)url.find(":/") != -1)
return Node::href;
else
return Node::relative;
}
KURL Url::normalizeUrl(QString const& string_url, LinkStatus const& link_parent, QString const& document_root)
{
QString _string_url = string_url.stripWhiteSpace();
QString s_url;
KURL base_url;
// resolve base url
if(link_parent.hasBaseURI())
base_url = link_parent.baseURI();
else
base_url = link_parent.absoluteUrl();
// resolve relative url
if(_string_url.isEmpty())
return base_url;
else if(Url::hasProtocol(_string_url))
return KURL(_string_url);
else
{
s_url.prepend(base_url.protocol() + "://" + base_url.host());
if(_string_url[0] == '/') {
if(!base_url.protocol().startsWith("http")) {
s_url.append(document_root);
}
}
else {
s_url.append(base_url.directory(true, false) + "/");
}
if( (_string_url[0] == ';' || // parameters
_string_url[0] == '?' || // query
_string_url[0] == '#') ) // fragment or reference
{
s_url.append(base_url.fileName(false));
}
s_url.append(_string_url);
KURL url(s_url);
if(base_url.hasUser())
url.setUser(base_url.user());
if(base_url.hasPass())
url.setPass(base_url.pass());
url.setPort(base_url.port());
url.cleanPath();
// kdDebug(23100) << "Normalized URL: "
// << KCharsets::resolveEntities(KURL::decode_string(url.url())) << endl;
return KURL(KCharsets::resolveEntities(KURL::decode_string(url.url())));
}
}
KURL Url::normalizeUrl(QString const& string_url)
{
QString qs_url(KCharsets::resolveEntities(string_url.stripWhiteSpace()));
if(qs_url[0] == '/')
{
KURL url;
url.setPath(qs_url);
url.cleanPath();
return url;
}
else
{
if(!Url::hasProtocol(qs_url))
qs_url.prepend("http://");
KURL url(qs_url);
url.cleanPath();
return url;
}
}
bool Url::existUrl(KURL const& url, vector<LinkStatus*> const& v)
{
if(url.prettyURL().isEmpty())
return true;
for(uint i = 0; i != v.size(); ++i)
if(v[i]->absoluteUrl() == url)
return true;
return false;
}
/**
www.iscte.pt, iscte.pt => true;
iscte.pt, www.iscte.pt => true;
www.iscte.pt, alunos.iscte.pt => true; (if restrict = false)
www.iscte.pt, alunos.iscte.pt => false; (if restrict = true)
alunos.iscte.pt, www.iscte.pt => false;
alunos.iscte.pt, iscte.pt => false.
*/
// FIXME - Rename this function to sameDomain
bool Url::equalHost(QString const& host1, QString const& host2, bool restrict)
{
//Q_ASSERT(!host1.isEmpty());
//Q_ASSERT(!host2.isEmpty()); // this fails if href="javascript:......."
//if(host2.isEmpty())
//return false;
if(host1 == host2)
return true;
QString host1_(KNetwork::KResolver::normalizeDomain(host1));
QString host2_(KNetwork::KResolver::normalizeDomain(host2));
removeLastCharIfExists(host1_, '/');
removeLastCharIfExists(host2_, '/');
vector<QString> v1 = tokenizeWordsSeparatedByDots(host1_);
vector<QString> v2 = tokenizeWordsSeparatedByDots(host2_);
uint const size1 = v1.size();
uint const size2 = v2.size();
if( !(size1 >= 1 && size2 >= 1) && // localhost would have size = 1
!(host1_[0].isNumber() || host2_[0].isNumber()) ) // not (host == IP)
{
kdDebug(23100) << "Invalid host: " << host2 << endl;
return false;
}
vector<QString>::size_type aux = 0;
vector<QString>::size_type aux2 = 0;
if(v1[0] == "www")
aux = 1;
if(v2[0] == "www")
aux2 = 1;
if((size2 - aux2 < size1 - aux) && restrict) // e.g. paradigma.co.pt < linkstatus.paradigma.co.pt
return false;
if(restrict && (size2 - aux2 > size1 - aux)) // e.g. linkstatus.paradigma.co.pt > paradigma.co.pt
return false;
int i = 1;
while( ((int)(size1 - i) >= (int)aux) && ((int)(size2 - i) >= (int)aux) )
{
if( !(v1[size1 - i] == v2[size2 - i]) )
return false;
++i;
}
return true;
}
/* This should be done by parsing but I wan't to know when some new scheme comes along :) */
bool Url::hasProtocol(QString const& url)
{
QString s_url(url);
s_url.stripWhiteSpace();
if(s_url[0] == '/')
return false;
else
{
KURL url = KURL::fromPathOrURL(s_url);
if(!url.protocol().isEmpty())
return true;
/*
if(s_url.startsWith("http:") ||
s_url.startsWith("https:") ||
s_url.startsWith("ftp:") ||
s_url.startsWith("sftp:") ||
s_url.startsWith("webdav:") ||
s_url.startsWith("webdavs:") ||
s_url.startsWith("finger:") ||
s_url.startsWith("fish:") ||
s_url.startsWith("imap:") ||
s_url.startsWith("imaps:") ||
s_url.startsWith("lan:") ||
s_url.startsWith("ldap:") ||
s_url.startsWith("pop3:") ||
s_url.startsWith("pop3s:") ||
s_url.startsWith("smtp:") ||
s_url.startsWith("smtps:") ||
s_url.startsWith("file:") ||
s_url.startsWith("news:") ||
s_url.startsWith("gopher:") ||
s_url.startsWith("mailto:") ||
s_url.startsWith("telnet:") ||
s_url.startsWith("prospero:") ||
s_url.startsWith("wais:") ||
s_url.startsWith("nntp:") )
{
return true;
}
*/
else
return false;
}
}
/**
http://linkstatus.paradigma.co.pt/en/index.html&bix=bix -> /en/index.html&bix=bix
*/
QString Url::convertToLocal(LinkStatus const* ls)
{
KURL url = ls->absoluteUrl();
KURL base_url = ls->rootUrl();
if(base_url == url)
return "./" + url.fileName();
else
return KURL::relativeURL(base_url, url);
}
/**
If url2 has the same domain has url1 returns true.
If restrict, sourceforge.net != quanta.sourceforge.net.
Else is equal.
*/
bool Url::localDomain(KURL const& url1, KURL const& url2, bool restrict)
{
if(url1.protocol() != url2.protocol())
{
//kdDebug(23100) << "NOT localDomain" << endl;
return false;
}
else if(!url1.hasHost())
{
//kdDebug(23100) << "localDomain" << endl;
return true;
}
else
{
//return ::equalHost(url1.host(), url2.host(), restrict);
if(Url::equalHost(url1.host(), url2.host(), restrict))
{
//kdDebug(23100) << "localDomain" << endl;
return true;
}
else
{
//kdDebug(23100) << "NOT localDomain" << endl;
return false;
}
}
}
/**
Returns true if url2 is a parent of url1.
*/
bool Url::parentDir(KURL const& url1, KURL const& url2)
{
if(url1.protocol() != url2.protocol())
return false;
else if(!url1.hasHost())
return url2.isParentOf(url1);
else
{
if(!equalHost(url1.host(), url2.host()))
return false;
vector<QString> tokens_1 = tokenizeWordsSeparatedBy(url1.directory(true, false), QChar('/'));
vector<QString> tokens_2 = tokenizeWordsSeparatedBy(url2.directory(true, false), QChar('/'));
if(tokens_1.size() == 0)
return false;
//if(tokens_2.size() > tokens_1.size() or tokens_2.size() == 0)
//return true;
vector<QString>::size_type size = 0;
if(tokens_1.size() < tokens_2.size())
size = tokens_1.size();
else
size = tokens_2.size();
for(vector<QString>::size_type i = 0; i != size; ++i)
{
if(tokens_2[i] != tokens_1[i])
return true;
}
}
return false;
}
bool Url::externalLink(KURL const& url1, KURL const& url2, bool restrict)
{
if(url1.protocol() != url2.protocol())
{
kdDebug(23100) << "externalLink" << endl;
return true;
}
else if(!url1.hasHost() && !url2.hasHost())
{
kdDebug(23100) << "NOT externalLink" << endl;
return false;
}
else
return !Url::equalHost(url1.host(), url2.host(), restrict);
}
|