-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebResourceInfo.cpp
303 lines (242 loc) · 7.61 KB
/
WebResourceInfo.cpp
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
#include "WebResourceInfo.h"
#include <istream>
#include <sstream>
#include <string>
#include <map>
#include <cstdlib> // long atol(char*);
using namespace std;
namespace ua { namespace kiev { namespace ukma { namespace downloader {
WebResourceInfo::WebResourceInfo(const string& url, const int statusCode, istream* headers)
:_url(url), _statusCode(statusCode)
{
if (_statusCode<1)
return ;
string param, value;
int i=0;
bool CRflag;
while(!headers->eof())
{
if (MIME.size()==0)
{
getline(*headers, value, ' ');
MIME.insert( mimepair("HTTP Version", value) );
getline(*headers, value, '\n');
CRflag = value[value.length()-1]=='\r' ? true : false;
if (CRflag) value.erase(value.length()-1);
size_t pos = value.find(' ');
string temp = value.substr(0,pos);
MIME.insert( mimepair("Response Code", temp) );
_statusCode = atoi(temp.c_str());
if (pos!=string::npos)
{
temp = value.substr(pos+1);
MIME.insert( mimepair("Response Message", temp) );
}
}
else
{
getline(*headers, param, ':');
headers->ignore();
getline(*headers, value, '\n');
if (CRflag) value.erase(value.length()-1);
MIME.insert( mimepair(param,value) );
if (headers->peek()== (CRflag ? '\r' : '\n'))
headers->ignore(5); // 5 just in case, 3 is enough ([CR]+LF+EOF)
}
}
}
WebResourceInfo::WebResourceInfo(const WebResourceInfo& copy)
: _url(copy._url), _statusCode(copy._statusCode), MIME(copy.MIME)
{
return ;
}
WebResourceInfo::~WebResourceInfo()
{
return ;
}
const string WebResourceInfo::getUrl() const
{
return _url;
}
const int WebResourceInfo::getStatusCode() const
{
return _statusCode;
}
bool WebResourceInfo::isMimePresent(const string& key) const
{
return MIME.count(key) ? true : false ;
}
const string WebResourceInfo::getMimeValue(const string& key) const
{
return MIME.count(key) ? MIME.find(key)->second : string() ;
}
const string WebResourceInfo::getStatusCodeAsString() const
{
switch(_statusCode)
{
case -1: return "Internal Error: coudln't perform connection";
case 0: return "Couldn't Resolve Host Name";
case 1: return "Infinite Redirection Loops";
default: if(!isMimePresent("Response Message"))
return "No Server Message Response";
else return MIME.find("Response Message")->second;
}
return "This status message should never appear!";
}
//////////////////////////////////////////////////////
// ---------------------Iterator---------------------
//////////////////////////////////////////////////////
const WebResourceInfo::iterator WebResourceInfo::begin()
{
return WebResourceInfo::iterator::iterator(MIME.begin());
}
const WebResourceInfo::iterator WebResourceInfo::end()
{
return WebResourceInfo::iterator::iterator(MIME.end());
}
WebResourceInfo::iterator::iterator(){}
WebResourceInfo::iterator::iterator(const iterator& copy):_iter(copy._iter) {}
WebResourceInfo::iterator::iterator(const mimemap::iterator& iter): _iter(iter){}
WebResourceInfo::iterator::~iterator(){}
WebResourceInfo::iterator& WebResourceInfo::iterator::operator=(const iterator& copy)
{
_iter = copy._iter;
return *this;
}
WebResourceInfo::iterator WebResourceInfo::iterator::operator ++()
{
mimemap::iterator temp = _iter;
++_iter;
return iterator(temp);
}
WebResourceInfo::iterator& WebResourceInfo::iterator::operator ++(int)
{
++_iter;
return *this;
}
bool WebResourceInfo::iterator::operator==(const iterator& iter) const
{
return _iter==iter._iter ? true : false ;
}
bool WebResourceInfo::iterator::operator!=(const iterator& iter) const
{
return !(iter._iter==_iter);
}
WebResourceInfo::mimepair WebResourceInfo::iterator::operator*() const
{
return *_iter;
}
///////////////////////////////////////////////////////
//-------------- EXTERNAL FUNCTIONS -------------------
///////////////////////////////////////////////////////
const string encodeUrl(const string& url, const char charToEnc)
{
string temp(url);
ostringstream enc;
enc<<"%"<<hex<<(int)charToEnc;
size_t pos;
while ((pos = temp.find(charToEnc))!=string::npos)
temp.replace(pos, 1, enc.str());
return temp;
}
const string getHostName(const WebResourceInfo& wri)
{
const string& url = wri.getUrl();
size_t pos = url.find("//");
if (pos == string::npos)
return url.substr(0, url.find('/'));
return url.substr(pos+2, url.find('/', pos+2)-pos-2);
}
const string getHostName(const string& url)
{
size_t pos = url.find("//");
if (pos == string::npos)
return url.substr(0, url.find('/'));
return url.substr(pos+2, url.find('/', pos+2)-pos-2);
}
const string getResourceFileName(const WebResourceInfo& wri)
{
const string& url = wri.getUrl();
size_t pos = url.find("//");
if (pos == string::npos)
return url.rfind('/')!=string::npos ? url.substr(url.rfind('/')+1) : string() ;
return url.substr(pos+2).rfind('/')!=string::npos ? url.substr(url.substr(pos+2).rfind('/')+pos+2+1) : string();
}
const string getResourceFileName(const string& url)
{
size_t pos = url.find("//");
if (pos == string::npos)
return url.rfind('/')!=string::npos ? url.substr(url.rfind('/')+1) : string() ;
return url.substr(pos+2).rfind('/')!=string::npos ? url.substr(url.substr(pos+2).rfind('/')+pos+2+1) : string();
}
const string getResourceDirs(const WebResourceInfo& wri)
{
const string& dirs = wri.getUrl();
size_t pos = dirs.find("//");
if (pos==string::npos)
return dirs.substr(dirs.find('/')+1, dirs.rfind('/')-dirs.find('/'));
return dirs.substr(dirs.find('/',pos+2)+1, dirs.rfind('/')-dirs.find('/',pos+2));
}
const string getResourceDirs(const string& url)
{
const string& dirs = url;
size_t pos = dirs.find("//");
if (pos==string::npos)
return dirs.substr(dirs.find('/')+1, dirs.rfind('/')-dirs.find('/'));
return dirs.substr(dirs.find('/',pos+2)+1, dirs.rfind('/')-dirs.find('/',pos+2));
}
const string getLocalFileName(const WebResourceInfo& wri)
{
const string& name(getResourceFileName(wri));
// const string& ext(getResourceExtension(wri));
size_t pos = name.find('?');
// name.replace(name.find('?'), 1, "-"); // '-' instead of '%3F'
return (name.empty() || name[0]=='?' ? "index" : "")
+ encodeUrl(name,'?')
+ ( wri.getMimeValue("Content-Type").find("text/html") != string::npos &&
name.find(".htm") == string::npos ? ".html" : "")
;
}
const string getLocalFileName(const string& url)
{
const string& name(getResourceFileName(url));
size_t pos = name.find('?');
return (name.empty() || name[0]=='?' ? "index" : "")
+ encodeUrl(name,'?')
+ (name.find(".htm") == string::npos ? ".html" : "")
;
}
const string getResourceExtension(const WebResourceInfo& wri)
{
const string& name = getResourceFileName(wri);
size_t pos = name.find('.');
if (pos==string::npos) return string();
return name.substr(pos+1, name.find('?')-pos-1);
}
const string getResourceExtension(const string& url)
{
const string& name = getResourceFileName(url);
size_t pos = name.find('.');
if (pos==string::npos) return string();
return name.substr(pos+1, name.find('?')-pos-1);
}
const size_t getFileSize(const WebResourceInfo& wri)
{
return wri.isMimePresent("Content-Length") ? atol(wri.getMimeValue("Content-Length").c_str()) : 0 ;
}
//////////////////////////////////////////////////////
// ---------------------Predicates--------------------
//////////////////////////////////////////////////////
bool isHostFound(const WebResourceInfo& wri)
{
return wri.getStatusCode()>0 ? true : false ;
}
bool isUrlFound(const WebResourceInfo& wri)
{
return wri.getStatusCode()>0 && wri.getStatusCode()!=404 ? true : false ;
}
bool canBeDownloaded(const WebResourceInfo& wri)
{
return wri.getStatusCode()==200 || wri.getStatusCode()/100==3 ? true : false ;
}
}}}} // namespace ua::kiev::ukma::downloader