ObjFW  Diff

Differences From Artifact [ad4fe54b6f]:

To Artifact [6c7fd6999e]:

  • File src/OFURL.m — part of check-in [516517deb3] at 2016-08-21 14:00:20 on branch trunk — OFURL: Do not URL decode and reencode parts

    URL decoding and reencoding is not lossless: For example, if the query
    was foo=bar&qux=foo%25bar, it will be decoded to foo=bar&qux=foo&bar and
    then reencoded to foo=bar%25qux=foo%25bar, which is a different thing.

    The only way to solve this is to let the application handle the URL
    decoding and encoding according to its own rules, as those might be
    different depending on the application. (user: js, size: 9974) [annotate] [blame] [check-ins using]


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







+
-
-
+
+
-




-
-
+
+




















-
-
-
-
+
+
+
+

-
-
+
+











-
-
+
+











-
-
+
+













-
-
+
+





-
-
+
+





-
-
+
+


-
-
+
+
+
+
+








		if ((tmp = strstr(UTF8String, "://")) == NULL)
			@throw [OFInvalidFormatException exception];

		for (tmp2 = UTF8String; tmp2 < tmp; tmp2++)
			*tmp2 = tolower((unsigned char)*tmp2);

		_scheme = [[OFString alloc]
		_scheme = [[[OFString stringWithUTF8String: UTF8String
						    length: tmp - UTF8String]
		    initWithUTF8String: UTF8String
				length: tmp - UTF8String];
		    stringByURLDecoding] copy];

		UTF8String = tmp + 3;

		if ([_scheme isEqual: @"file"]) {
			_path = [[[OFString stringWithUTF8String:
			    UTF8String] stringByURLDecoding] copy];
			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String];

			objc_autoreleasePoolPop(pool);
			return self;
		}

		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 stringWithUTF8String:
				    UTF8String] stringByURLDecoding] copy];
				_password = [[[OFString stringWithUTF8String:
				    tmp3] stringByURLDecoding] copy];
				_user = [[OFString alloc]
				    initWithUTF8String: UTF8String];
				_password = [[OFString alloc]
				    initWithUTF8String: tmp3];
			} else
				_user = [[[OFString stringWithUTF8String:
				    UTF8String] stringByURLDecoding] copy];
				_user = [[OFString alloc]
				    initWithUTF8String: UTF8String];

			UTF8String = tmp2;
		}

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

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

			_host = [[[OFString stringWithUTF8String:
			    UTF8String] stringByURLDecoding] copy];
			_host = [[OFString alloc]
			    initWithUTF8String: UTF8String];

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

			if ([portString decimalValue] > 65535)
				@throw [OFInvalidFormatException exception];

			_port = [portString decimalValue];

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

			if ([_scheme isEqual: @"http"])
				_port = 80;
			else if ([_scheme isEqual: @"https"])
				_port = 443;
			else if ([_scheme isEqual: @"ftp"])
				_port = 21;
		}

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

				_fragment = [[[OFString stringWithUTF8String:
				    tmp + 1] stringByURLDecoding] copy];
				_fragment = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

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

				_query = [[[OFString stringWithUTF8String:
				    tmp + 1] stringByURLDecoding] copy];
				_query = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

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

				_parameters = [[[OFString stringWithUTF8String:
				    tmp + 1] stringByURLDecoding] copy];
				_parameters = [[OFString alloc]
				    initWithUTF8String: tmp + 1];
			}

			_path = [[[OFString stringWithUTF8String:
			    UTF8String] stringByURLDecoding] copy];
			UTF8String--;
			*UTF8String = '/';

			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String];
		}

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







-
-
+
+




-
-
+
+




-
-
+
+



-
-
+
+



-
+
-







			     exceptionWithRequestedSize:
			     [string UTF8StringLength]];

		UTF8String = UTF8String2;

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

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

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

		if (*UTF8String == '/')
			_path = [[[OFString stringWithUTF8String:
			    UTF8String + 1] stringByURLDecoding] copy];
			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String];
		else {
			OFString *path, *s;

			path = [[[OFString stringWithUTF8String:
			path = [OFString stringWithUTF8String: UTF8String];
			    UTF8String] stringByURLDecoding] copy];

			if ([URL->_path hasSuffix: @"/"])
				s = [URL->_path stringByAppendingString: path];
			else
				s = [OFString stringWithFormat: @"%@/../%@",
								URL->_path,
								path];
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
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







-
+



-
+
-






-
+
-
-

-
+


-
+






-
-
-
-
+
+
+
+
+
+
+

-
+


-
+


-
+







}

- (OFString*)string
{
	OFMutableString *ret = [OFMutableString string];
	void *pool = objc_autoreleasePoolPush();

	[ret appendFormat: @"%@://", [_scheme stringByURLEncoding]];
	[ret appendFormat: @"%@://", _scheme];

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

		objc_autoreleasePoolPop(pool);
		return ret;
	}

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

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

	if (!(([_scheme isEqual: @"http"] && _port == 80) ||
	    ([_scheme isEqual: @"https"] && _port == 443) ||
	    ([_scheme isEqual: @"ftp"] && _port == 21)))
		[ret appendFormat: @":%u", _port];

	if (_path != nil)
		[ret appendFormat: @"/%@",
		    [_path stringByURLEncodingWithIgnoredCharacters: "/"]];

	if (_path != nil) {
		if (![_path hasPrefix: @"/"])
			@throw [OFInvalidFormatException exception];

		[ret appendString: _path];
	}

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

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

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

	objc_autoreleasePoolPop(pool);

	[ret makeImmutable];

	return ret;
}