-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEtchURL.cpp
More file actions
422 lines (372 loc) · 10.6 KB
/
EtchURL.cpp
File metadata and controls
422 lines (372 loc) · 10.6 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* $Id$
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "util/EtchURL.h"
#include "capu/os/StringUtils.h"
EtchURL::EtchURL()
: terms(ETCH_DEFAULT_URL_TERMS_HASH_BIT_SIZE) {
}
EtchURL::EtchURL(const char* urlStr) {
if (urlStr == NULL) {
//already initialized
} else if (strlen(urlStr) > 0) {
EtchString str(urlStr);
if (parse(&str) != ETCH_OK) {
fragment.set("");
host.set("");
password.set("");
port = 0;
scheme.set("");
uri.set("");
user.set("");
}
}
}
EtchURL::EtchURL(const EtchString& urlStr) {
if (urlStr.c_str() == NULL) {
//already initialized
} else if (urlStr.length() > 0) {
if (parse(&urlStr) != ETCH_OK) {
fragment.set("");
host.set("");
password.set("");
port = 0;
scheme.set("");
uri.set("");
user.set("");
}
}
}
EtchURL::~EtchURL() {
params.clear();
terms.clear();
}
status_t EtchURL::parse(const EtchString* s) {
if (s == NULL)
return ETCH_EINVAL;
// s is scheme:[//[user[:password]@]host[:port]/]uri[;params][?terms][#fragment]
capu::int32_t result = s->leftFind(':');
status_t error_result;
if (result == -1)
return ETCH_ERROR;
//parsing of scheme
EtchString x[2];
//x[0] contains scheme
error_result = s->substring(0, result, &x[0]);
if (error_result != ETCH_OK)
return error_result;
//x[1] will contain rest of url [//[user[:password]@]host[:port]/]uri[;params][?terms][#fragment]
error_result = s->substring(result + 1, s->length()-(result + 1), &x[1]);
if (error_result != ETCH_OK)
return error_result;
setScheme(x[0]);
EtchString _s(x[1]);
// _s is [//[user[:password]@]host[:port]/]uri[;params][?terms][#fragment]
result = _s.leftFind('#');
if (result != -1) {
//parse the fragments
//x[0] will contain the rest of url [//[user[:password]@]host[:port]/]uri[;params][?terms]
error_result = _s.substring(0, result, &x[0]);
if (error_result != ETCH_OK)
return error_result;
//x[1] will contain the fragments
error_result = _s.substring(result + 1, _s.length()-(result + 1), &x[1]);
if (error_result != ETCH_OK)
return error_result;
//set the fragments
setFragment(x[1]);
_s = x[0];
}
// s is [//[user[:password]@]host[:port]/]uri[;params][?terms]
result = _s.leftFind('?');
if (result != -1) {
//parse terms
//x[0] will contain rest of url [//[user[:password]@]host[:port]/]uri[;params]
error_result = _s.substring(0, result, &x[0]);
if (error_result != ETCH_OK)
return error_result;
//x[1] will contain the terms
error_result = _s.substring(result + 1, _s.length()-(result + 1), &x[1]);
if (error_result != ETCH_OK)
return error_result;
//parse and sets the terms
error_result = parseTerms(&x[1]);
if (error_result != ETCH_OK)
return error_result;
_s = x[0];
}
// s is [//[user[:password]@]host[:port]/]uri[;params]
result = _s.leftFind(';');
if (result != -1) {
//parse terms
//x[0] will contain rest of url [//[user[:password]@]host[:port]/]uri
error_result = _s.substring(0, result, &x[0]);
if (error_result != ETCH_OK)
return error_result;
error_result = _s.substring(result + 1, _s.length()-(result + 1), &x[1]);
if (error_result != ETCH_OK)
return error_result;
//x[1] will contain the params
error_result = parseParams(&x[1]);
if (error_result != ETCH_OK)
return error_result;
_s = x[0];
}
// s is [//[user[:password]@]host[:port]/]uri
char *str = new char [_s.length() + 1];
etch_strcpy_s(str, _s.length() + 1, _s.c_str());
if ((str[0] == '/') && (str[1] == '/')) {
//eleminate "//"
_s.set(&str[2]);
// s is [user[:password]@]host[:port]/uri
//parse uri
result = _s.leftFind('/');
if (result != -1) {
//x[0] contains rest of uri [user[:password]@]host[:port]
error_result = _s.substring(0, result, &x[0]);
if (error_result != ETCH_OK)
return error_result;
//x[1] will contain the uri
error_result = _s.substring(result + 1, _s.length()-(result + 1), &x[1]);
if (error_result != ETCH_OK)
return error_result;
// parse [user[:password]@]host[:port]
error_result = parseHost(&x[0]);
if (error_result != ETCH_OK)
return error_result;
//now s contains the uri
_s = x[1];
} else {
// s is [user[:password]@]host[:port]
error_result = parseHost(&_s);
if (error_result != ETCH_OK)
return error_result;
//uri is not given
_s.set("");
}
}
setUri(_s);
delete [] str;
return ETCH_OK;
}
status_t EtchURL::parseHost(const EtchString* s) {
if (s == NULL) {
return ETCH_EINVAL;
}
// s is [user[:password]@]host[:port]
capu::int32_t result = s->leftFind('@');
EtchString x[2];
if (result != -1) {
if (s->substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (s->substring(result + 1, s->length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
parseUserPassword(&x[0]);
parseHostPort(&x[1]);
} else {
parseHostPort(s);
}
return ETCH_OK;
}
status_t EtchURL::parseUserPassword(const EtchString* s) {
if (s == NULL) {
return ETCH_EINVAL;
}
// s is user[:password]
capu::int32_t result = s->leftFind(':');
EtchString x[2];
if (result != -1) {
if (s->substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (s->substring(result + 1, s->length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
setUser(x[0]);
setPassword(x[1]);
} else {
setUser(*s);
}
return ETCH_OK;
}
status_t EtchURL::parseHostPort(const EtchString* s) {
if (s == NULL) {
return ETCH_EINVAL;
}
capu::int32_t result = s->leftFind(':');
EtchString x[2];
if (x != NULL) {
if (s->substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (s->substring(result + 1, s->length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
setHost(x[0]);
EtchString *p = &x[1];
capu::int32_t _port = atoi(p->c_str());
setPort(_port);
} else {
setHost(*s);
}
return ETCH_OK;
}
status_t EtchURL::parseParams(const EtchString* s) {
// s is param[;param]*
if (s == NULL) {
return ETCH_EINVAL;
}
if (s->length() == 0)
return ETCH_OK; //THERE IS NOTHING TO PARSE
EtchString _s = *s;
EtchString x[2];
capu::int32_t result = _s.leftFind(';');
while (result != -1) {
if (_s.substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (_s.substring(result + 1, _s.length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
addParam(x[0]);
_s.set(x[1].c_str());
result = _s.leftFind(';');
}
addParam(_s);
return ETCH_OK;
}
status_t EtchURL::parseTerms(const EtchString* s) {
if (s == NULL) {
return ETCH_EINVAL;
}
if (s->length() == 0)
return ETCH_OK; //THERE IS NOTHING TO PARSE
EtchString _s = *s;
EtchString x[2];
capu::int32_t result = _s.leftFind('&');
while (result != -1) {
if (_s.substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (_s.substring(result + 1, _s.length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
parseTerm(&x[0]);
_s.set(x[1].c_str());
result = _s.leftFind('&');
}
return parseTerm(&_s);
}
status_t EtchURL::parseTerm(const EtchString * s) {
if (s == NULL) {
return ETCH_EINVAL;
}
// s is name[=value]
if (s->length() == 0)
return ETCH_OK; //THERE IS NOTHING TO PARSE
capu::int32_t result = s->leftFind('=');
EtchString x[2];
if (s->substring(0, result, &x[0]) != ETCH_OK)
return ETCH_ERROR;
if (s->substring(result + 1, s->length()-(result + 1), &x[1]) != ETCH_OK)
return ETCH_ERROR;
if (result != -1)
addTerm(x[0], x[1]);
return ETCH_OK;
}
EtchString& EtchURL::getScheme() {
return scheme;
}
void EtchURL::setScheme(const EtchString& scheme) {
this->scheme.set(scheme.c_str());
}
status_t EtchURL::setScheme(EtchString* scheme) {
if (scheme != NULL) {
this->scheme.set(scheme->c_str());
return ETCH_OK;
}
return ETCH_EINVAL;
}
EtchString& EtchURL::getUser() {
return user;
}
void EtchURL::setUser(const EtchString& user) {
this->user.set(user.c_str());
}
EtchString& EtchURL::getPassword() {
return password;
}
void EtchURL::setPassword(const EtchString& password) {
this->password.set(password.c_str());
}
EtchString& EtchURL::getHost() {
return host;
}
void EtchURL::setHost(const EtchString& host) {
this->host.set(host.c_str());
}
capu::uint16_t EtchURL::getPort() {
return port;
}
void EtchURL::setPort(const capu::uint16_t port) {
this->port = port;
}
EtchString& EtchURL::getUri() {
return uri;
}
void EtchURL::setUri(const EtchString& uri) {
this->uri.set(uri.c_str());
}
capu::bool_t EtchURL::hasParams() {
return !(params.isEmpty());
}
void EtchURL::addParam(EtchString& param) {
this->params.add(param);
}
void EtchURL::clearParams() {
params.clear();
}
status_t EtchURL::addTerm(EtchString& name, EtchString& value) {
return terms.put(name, value);
}
status_t EtchURL::addTerm(EtchString& name, capu::int32_t value) {
char result [10];
EtchString str;
capu::StringUtils::Sprintf(result, 10, "%d", value);
str.set(result);
return addTerm(name, str);
}
status_t EtchURL::addTerm(EtchString& name, capu::bool_t value) {
char result [2];
EtchString str;
capu::StringUtils::Sprintf(result, 2, "%d", value);
str.set(result);
return addTerm(name, str);
}
status_t EtchURL::addTerm(EtchString& name, capu::double_t value) {
char result [10];
EtchString str;
capu::StringUtils::Sprintf(result, 10, "%f", value);
str.set(result);
return addTerm(name, str);
}
void EtchURL::clearTerms() {
terms.clear();
}
EtchString& EtchURL::getFragment() {
return fragment;
}
void EtchURL::setFragment(EtchString& fragment) {
this->fragment.set(fragment.c_str());
}
EtchList<EtchString>& EtchURL::getParams() {
return params;
}
EtchHashTable<EtchString, EtchString>& EtchURL::getTerms() {
return terms;
}