ObjFW  Diff

Differences From Artifact [2267fdd898]:

To Artifact [fcf898c186]:


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
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







-
-
+
+



-
+


-
+




















-
+

-
+


-
+












-
+









-
+

-
-
+
+



-
+


-
-
-
-
+
+
+
+








-
+






-
+






-
+



-
-
+
+

-
+







			@throw [OFOutOfMemoryException
			     exceptionWithClass: [self class]
				  requestedSize: [string UTF8StringLength]];

		UTF8String = UTF8String2;

		if (!strncmp(UTF8String, "file://", 7)) {
			scheme = @"file";
			path = [[OFString alloc]
			_scheme = @"file";
			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String + 7];
			return self;
		} else if (!strncmp(UTF8String, "http://", 7)) {
			scheme = @"http";
			_scheme = @"http";
			UTF8String += 7;
		} else if (!strncmp(UTF8String, "https://", 8)) {
			scheme = @"https";
			_scheme = @"https";
			UTF8String += 8;
		} else
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		if ((tmp = strchr(UTF8String, '/')) != NULL) {
			*tmp = '\0';
			tmp++;
		}

		if ((tmp2 = strchr(UTF8String, '@')) != NULL) {
			char *tmp3;

			*tmp2 = '\0';
			tmp2++;

			if ((tmp3 = strchr(UTF8String, ':')) != NULL) {
				*tmp3 = '\0';
				tmp3++;

				user = [[OFString alloc]
				_user = [[OFString alloc]
				    initWithUTF8String: UTF8String];
				password = [[OFString alloc]
				_password = [[OFString alloc]
				    initWithUTF8String: tmp3];
			} else
				user = [[OFString alloc]
				_user = [[OFString alloc]
				    initWithUTF8String: UTF8String];

			UTF8String = tmp2;
		}

		if ((tmp2 = strchr(UTF8String, ':')) != NULL) {
			void *pool;
			OFString *portString;

			*tmp2 = '\0';
			tmp2++;

			host = [[OFString alloc]
			_host = [[OFString alloc]
			    initWithUTF8String: UTF8String];

			pool = objc_autoreleasePoolPush();
			portString = [OFString stringWithUTF8String: tmp2];

			if ([portString decimalValue] > 65535)
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

			port = [portString decimalValue];
			_port = [portString decimalValue];

			if (port == 0)
				port = 80;
			if (_port == 0)
				_port = 80;

			objc_autoreleasePoolPop(pool);
		} else {
			host = [[OFString alloc]
			_host = [[OFString alloc]
			    initWithUTF8String: UTF8String];

			if ([scheme isEqual: @"http"])
				port = 80;
			else if ([scheme isEqual: @"https"])
				port = 443;
			if ([_scheme isEqual: @"http"])
				_port = 80;
			else if ([_scheme isEqual: @"https"])
				_port = 443;
			else
				assert(0);
		}

		if ((UTF8String = tmp) != NULL) {
			if ((tmp = strchr(UTF8String, '#')) != NULL) {
				*tmp = '\0';

				fragment = [[OFString alloc]
				_fragment = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

			if ((tmp = strchr(UTF8String, '?')) != NULL) {
				*tmp = '\0';

				query = [[OFString alloc]
				_query = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

			if ((tmp = strchr(UTF8String, ';')) != NULL) {
				*tmp = '\0';

				parameters = [[OFString alloc]
				_parameters = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

			path = [[OFString alloc] initWithFormat: @"/%s",
								 UTF8String];
			_path = [[OFString alloc] initWithFormat: @"/%s",
								  UTF8String];
		} else
			path = @"";
			_path = @"";
	} @catch (id e) {
		[self release];
		@throw e;
	} @finally {
		free(UTF8String2);
	}

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
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







-
-
-
-
-
+
+
+
+
+










-
+





-
+




-
+




-
+







-
+

-
+



-
+


-
+







		return [self initWithString: string];

	self = [super init];

	@try {
		char *tmp;

		scheme = [URL->scheme copy];
		host = [URL->host copy];
		port = URL->port;
		user = [URL->user copy];
		password = [URL->password copy];
		_scheme = [URL->_scheme copy];
		_host = [URL->_host copy];
		_port = URL->_port;
		_user = [URL->_user copy];
		_password = [URL->_password copy];

		if ((UTF8String2 = strdup([string UTF8String])) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: [self class]
				  requestedSize: [string UTF8StringLength]];

		UTF8String = UTF8String2;

		if ((tmp = strchr(UTF8String, '#')) != NULL) {
			*tmp = '\0';
			fragment = [[OFString alloc]
			_fragment = [[OFString alloc]
			    initWithUTF8String: tmp + 1];
		}

		if ((tmp = strchr(UTF8String, '?')) != NULL) {
			*tmp = '\0';
			query = [[OFString alloc] initWithUTF8String: tmp + 1];
			_query = [[OFString alloc] initWithUTF8String: tmp + 1];
		}

		if ((tmp = strchr(UTF8String, ';')) != NULL) {
			*tmp = '\0';
			parameters = [[OFString alloc]
			_parameters = [[OFString alloc]
			    initWithUTF8String: tmp + 1];
		}

		if (*UTF8String == '/')
			path = [[OFString alloc]
			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String];
		else {
			void *pool;
			OFString *s;

			pool = objc_autoreleasePoolPush();

			if ([URL->path hasSuffix: @"/"])
			if ([URL->_path hasSuffix: @"/"])
				s = [OFString stringWithFormat: @"%@%s",
								URL->path,
								URL->_path,
								UTF8String];
			else
				s = [OFString stringWithFormat: @"%@/../%s",
								URL->path,
								URL->_path,
								UTF8String];

			path = [[s stringByStandardizingURLPath] copy];
			_path = [[s stringByStandardizingURLPath] copy];

			objc_autoreleasePoolPop(pool);
		}
	} @catch (id e) {
		[self release];
		@throw e;
	} @finally {
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
423

424
425
426
427
428

429
430
431

432
433
434


435
436
437
438
439

440
441
442
443
444

445
446
447

448
449

450
451
452
453
454

455
456
457

458
459

460
461
462
463
464

465
466
467

468
469

470
471
472
473
474
475

476
477
478
479
480



481
482
483
484
485
486
487
488




489
490
491


492
493
494


495
496
497
498

499
500
501


502
503
504


505
506
507


508
509
510


511
512
513
514
515
516
517
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

423
424
425
426
427

428
429
430

431
432


433
434
435
436
437
438

439
440
441
442
443

444
445
446

447
448

449
450
451
452
453

454
455
456

457
458

459
460
461
462
463

464
465
466

467
468

469
470
471
472
473
474

475

476



477
478
479
480
481
482
483




484
485
486
487
488


489
490
491


492
493




494
495


496
497
498


499
500
501


502
503
504


505
506
507
508
509
510
511
512
513







-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+






-
+




-
+

-
+

-
+

-
+

-
+

-
-
+
+

-
+

-
-
+
+

-
-
+
+

-
-
+
+











-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+











-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+










-
+


-
+

-
+




-
+




-
+


-
+

-
+




-
+


-
+

-
+




-
+


-
+

-
+




-
+


-
+

-
+




-
+


-
+

-
-
+
+




-
+




-
+


-
+

-
+




-
+


-
+

-
+




-
+


-
+

-
+





-
+
-

-
-
-
+
+
+




-
-
-
-
+
+
+
+

-
-
+
+

-
-
+
+
-
-
-
-
+

-
-
+
+

-
-
+
+

-
-
+
+

-
-
+
+







	}

	return self;
}

- (void)dealloc
{
	[scheme release];
	[host release];
	[user release];
	[password release];
	[path release];
	[parameters release];
	[query release];
	[fragment release];
	[_scheme release];
	[_host release];
	[_user release];
	[_password release];
	[_path release];
	[_parameters release];
	[_query release];
	[_fragment release];

	[super dealloc];
}

- (BOOL)isEqual: (id)object
{
	OFURL *otherURL;
	OFURL *URL;

	if (![object isKindOfClass: [OFURL class]])
		return NO;

	otherURL = object;
	URL = object;

	if (![otherURL->scheme isEqual: scheme])
	if (![URL->_scheme isEqual: _scheme])
		return NO;
	if (![otherURL->host isEqual: host])
	if (![URL->_host isEqual: _host])
		return NO;
	if (otherURL->port != port)
	if (URL->_port != _port)
		return NO;
	if (otherURL->user != user && ![otherURL->user isEqual: user])
	if (URL->_user != _user && ![URL->_user isEqual: _user])
		return NO;
	if (otherURL->password != password &&
	    ![otherURL->password isEqual: password])
	if (URL->_password != _password &&
	    ![URL->_password isEqual: _password])
		return NO;
	if (![otherURL->path isEqual: path])
	if (![URL->_path isEqual: _path])
		return NO;
	if (otherURL->parameters != parameters &&
	    ![otherURL->parameters isEqual: parameters])
	if (URL->_parameters != _parameters &&
	    ![URL->_parameters isEqual: _parameters])
		return NO;
	if (otherURL->query != query &&
	    ![otherURL->query isEqual: query])
	if (URL->_query != _query &&
	    ![URL->_query isEqual: _query])
		return NO;
	if (otherURL->fragment != fragment &&
	    ![otherURL->fragment isEqual: fragment])
	if (URL->_fragment != _fragment &&
	    ![URL->_fragment isEqual: _fragment])
		return NO;

	return YES;
}

- (uint32_t)hash
{
	uint32_t hash;

	OF_HASH_INIT(hash);

	OF_HASH_ADD_HASH(hash, [scheme hash]);
	OF_HASH_ADD_HASH(hash, [host hash]);
	OF_HASH_ADD(hash, (port & 0xFF00) >> 8);
	OF_HASH_ADD(hash, port & 0xFF);
	OF_HASH_ADD_HASH(hash, [user hash]);
	OF_HASH_ADD_HASH(hash, [password hash]);
	OF_HASH_ADD_HASH(hash, [path hash]);
	OF_HASH_ADD_HASH(hash, [parameters hash]);
	OF_HASH_ADD_HASH(hash, [query hash]);
	OF_HASH_ADD_HASH(hash, [fragment hash]);
	OF_HASH_ADD_HASH(hash, [_scheme hash]);
	OF_HASH_ADD_HASH(hash, [_host hash]);
	OF_HASH_ADD(hash, (_port & 0xFF00) >> 8);
	OF_HASH_ADD(hash, _port & 0xFF);
	OF_HASH_ADD_HASH(hash, [_user hash]);
	OF_HASH_ADD_HASH(hash, [_password hash]);
	OF_HASH_ADD_HASH(hash, [_path hash]);
	OF_HASH_ADD_HASH(hash, [_parameters hash]);
	OF_HASH_ADD_HASH(hash, [_query hash]);
	OF_HASH_ADD_HASH(hash, [_fragment hash]);

	OF_HASH_FINALIZE(hash);

	return hash;
}

- copy
{
	OFURL *copy = [[[self class] alloc] init];

	@try {
		copy->scheme = [scheme copy];
		copy->host = [host copy];
		copy->port = port;
		copy->user = [user copy];
		copy->password = [password copy];
		copy->path = [path copy];
		copy->parameters = [parameters copy];
		copy->query = [query copy];
		copy->fragment = [fragment copy];
		copy->_scheme = [_scheme copy];
		copy->_host = [_host copy];
		copy->_port = _port;
		copy->_user = [_user copy];
		copy->_password = [_password copy];
		copy->_path = [_path copy];
		copy->_parameters = [_parameters copy];
		copy->_query = [_query copy];
		copy->_fragment = [_fragment copy];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	return copy;
}

- (OFString*)scheme
{
	OF_GETTER(scheme, YES)
	OF_GETTER(_scheme, YES)
}

- (void)setScheme: (OFString*)scheme_
- (void)setScheme: (OFString*)scheme
{
	if (![scheme_ isEqual: @"http"] && ![scheme_ isEqual: @"https"])
	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	OF_SETTER(scheme, scheme_, YES, 1)
	OF_SETTER(_scheme, scheme, YES, 1)
}

- (OFString*)host
{
	OF_GETTER(host, YES)
	OF_GETTER(_host, YES)
}

- (void)setHost: (OFString*)host_
- (void)setHost: (OFString*)host
{
	OF_SETTER(host, host_, YES, 1)
	OF_SETTER(_host, host, YES, 1)
}

- (uint16_t)port
{
	return port;
	return _port;
}

- (void)setPort: (uint16_t)port_
- (void)setPort: (uint16_t)port
{
	port = port_;
	_port = port;
}

- (OFString*)user
{
	OF_GETTER(user, YES)
	OF_GETTER(_user, YES)
}

- (void)setUser: (OFString*)user_
- (void)setUser: (OFString*)user
{
	OF_SETTER(user, user_, YES, 1)
	OF_SETTER(_user, user, YES, 1)
}

- (OFString*)password
{
	OF_GETTER(password, YES)
	OF_GETTER(_password, YES)
}

- (void)setPassword: (OFString*)password_
- (void)setPassword: (OFString*)password
{
	OF_SETTER(password, password_, YES, 1)
	OF_SETTER(_password, password, YES, 1)
}

- (OFString*)path
{
	OF_GETTER(path, YES)
	OF_GETTER(_path, YES)
}

- (void)setPath: (OFString*)path_
- (void)setPath: (OFString*)path
{
	if (([scheme isEqual: @"http"] || [scheme isEqual: @"https"]) &&
	    ![path_ hasPrefix: @"/"])
	if (([_scheme isEqual: @"http"] || [_scheme isEqual: @"https"]) &&
	    ![path hasPrefix: @"/"])
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	OF_SETTER(path, path_, YES, 1)
	OF_SETTER(_path, path, YES, 1)
}

- (OFString*)parameters
{
	OF_GETTER(parameters, YES)
	OF_GETTER(_parameters, YES)
}

- (void)setParameters: (OFString*)parameters_
- (void)setParameters: (OFString*)parameters
{
	OF_SETTER(parameters, parameters_, YES, 1)
	OF_SETTER(_parameters, parameters, YES, 1)
}

- (OFString*)query
{
	OF_GETTER(query, YES)
	OF_GETTER(_query, YES)
}

- (void)setQuery: (OFString*)query_
- (void)setQuery: (OFString*)query
{
	OF_SETTER(query, query_, YES, 1)
	OF_SETTER(_query, query, YES, 1)
}

- (OFString*)fragment
{
	OF_GETTER(fragment, YES)
	OF_GETTER(_fragment, YES)
}

- (void)setFragment: (OFString*)fragment_
- (void)setFragment: (OFString*)fragment
{
	OF_SETTER(fragment, fragment_, YES, 1)
	OF_SETTER(_fragment, fragment, YES, 1)
}

- (OFString*)string
{
	OFMutableString *ret = [OFMutableString stringWithFormat: @"%@://",
								  scheme];
								  _scheme];
	BOOL needPort = YES;

	if ([scheme isEqual: @"file"]) {
		if (path != nil)
			[ret appendString: path];
	if ([_scheme isEqual: @"file"]) {
		if (_path != nil)
			[ret appendString: _path];

		return ret;
	}

	if (user != nil && password != nil)
		[ret appendFormat: @"%@:%@@", user, password];
	else if (user != nil)
		[ret appendFormat: @"%@@", user];
	if (_user != nil && _password != nil)
		[ret appendFormat: @"%@:%@@", _user, _password];
	else if (_user != nil)
		[ret appendFormat: @"%@@", _user];

	if (host != nil)
		[ret appendString: host];
	if (_host != nil)
		[ret appendString: _host];

	if (([scheme isEqual: @"http"] && port == 80) ||
	    ([scheme isEqual: @"https"] && port == 443))
	if (([_scheme isEqual: @"http"] && _port != 80) ||
	    ([_scheme isEqual: @"https"] && _port != 443))
		needPort = NO;

	if (needPort)
		[ret appendFormat: @":%d", port];
		[ret appendFormat: @":%u", _port];

	if (path != nil)
		[ret appendString: path];
	if (_path != nil)
		[ret appendString: _path];

	if (parameters != nil)
		[ret appendFormat: @";%@", parameters];
	if (_parameters != nil)
		[ret appendFormat: @";%@", _parameters];

	if (query != nil)
		[ret appendFormat: @"?%@", query];
	if (_query != nil)
		[ret appendFormat: @"?%@", _query];

	if (fragment != nil)
		[ret appendFormat: @"#%@", fragment];
	if (_fragment != nil)
		[ret appendFormat: @"#%@", _fragment];

	[ret makeImmutable];

	return ret;
}

- (OFString*)description