ObjFW  Check-in [65afbedbd4]

Overview
Comment:OFURL: Allow all RFC 1808 compatible schemes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 65afbedbd40aade681832780347509fd891337ed682b1469200c15a827e49ce7
User & Date: js on 2014-07-07 22:28:00
Other Links: manifest | tags
Context
2014-07-10
07:35
OFHTTPClient: Minor improvements check-in: 4b8c4cac59 user: js tags: trunk
2014-07-07
22:28
OFURL: Allow all RFC 1808 compatible schemes check-in: 65afbedbd4 user: js tags: trunk
22:05
Add OFUnknownXMLEntityException check-in: 6e5a920567 user: js tags: trunk
Changes

Modified src/OFDataArray.m from [e58d501e5e] to [41037dbb74].

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
	return self;
}
#endif

- initWithContentsOfURL: (OFURL*)URL
{
	void *pool;
#ifdef OF_HAVE_SOCKETS
	OFHTTPClient *client;
	OFHTTPRequest *request;
	OFHTTPResponse *response;
	OFDictionary *headers;
	OFString *contentLength;
#endif
#ifdef OF_HAVE_FILES
	Class c = [self class];
#endif

	[self release];

	pool = objc_autoreleasePoolPush();

	if ([[URL scheme] isEqual: @"file"]) {

#ifdef OF_HAVE_FILES

		self = [[c alloc] initWithContentsOfFile: [URL path]];
		objc_autoreleasePoolPop(pool);
		return self;
#else
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
#endif
	}

#ifdef OF_HAVE_SOCKETS

	client = [OFHTTPClient client];
	request = [OFHTTPRequest requestWithURL: URL];
	response = [client performRequest: request];



	if ([response statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    exceptionWithRequest: request
				response: response];

	/*
	 * TODO: This can be optimized by allocating a data array with the
	 * capacity from the Content-Length header.
	 */
	self = [[response readDataArrayTillEndOfStream] retain];

	headers = [response headers];

	if ((contentLength = [headers objectForKey: @"Content-Length"]) != nil)

		if ([self count] != (size_t)[contentLength decimalValue])
			@throw [OFTruncatedDataException exception];
#else
	@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
#endif


	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithStringRepresentation: (OFString*)string







<
<
<
<
<
|
<








|
>

>

<
<
|
<

<
<

>
|
|
|
>
>

|
|
|
|

|
|
|
|
|

|
>
|
>
|
|
|
<

>







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
	return self;
}
#endif

- initWithContentsOfURL: (OFURL*)URL
{
	void *pool;





	OFString *scheme;

#ifdef OF_HAVE_FILES
	Class c = [self class];
#endif

	[self release];

	pool = objc_autoreleasePoolPush();

	scheme = [URL scheme];

#ifdef OF_HAVE_FILES
	if ([scheme isEqual: @"file"])
		self = [[c alloc] initWithContentsOfFile: [URL path]];


	else

#endif


#ifdef OF_HAVE_SOCKETS
	if ([scheme isEqual: @"http"] || [scheme isEqual: @"https"]) {
		OFHTTPClient *client = [OFHTTPClient client];
		OFHTTPRequest *request = [OFHTTPRequest requestWithURL: URL];
		OFHTTPResponse *response = [client performRequest: request];
		OFDictionary *headers;
		OFString *contentLength;

		if ([response statusCode] != 200)
			@throw [OFHTTPRequestFailedException
			    exceptionWithRequest: request
					response: response];

		/*
		 * TODO: This can be optimized by allocating a data array with
		 * the capacity from the Content-Length header.
		 */
		self = [[response readDataArrayTillEndOfStream] retain];

		headers = [response headers];
		if ((contentLength =
		    [headers objectForKey: @"Content-Length"]) != nil)
			if ([self count] !=
			    (size_t)[contentLength decimalValue])
				@throw [OFTruncatedDataException exception];
	} else

#endif
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];

	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithStringRepresentation: (OFString*)string

Modified src/OFHTTPClient.m from [ab4c72054a] to [7e8bacfd9a].

557
558
559
560
561
562
563

564
565
566
567
568
569
570
571
572
573
574
575
		[response setKeepAlive: true];

		_socket = [socket retain];
		_lastURL = [URL copy];
		_lastResponse = [response retain];
	}


	if (redirects > 0 && (status == 301 || status == 302 ||
	    status == 303 || status == 307) &&
	    (redirect = [serverHeaders objectForKey: @"Location"]) != nil &&
	    (_insecureRedirectsAllowed || [scheme isEqual: @"http"] ||
	    ![redirect hasPrefix: @"http://"])) {
		OFURL *newURL;
		bool follow;

		newURL = [OFURL URLWithString: redirect
				relativeToURL: URL];

		if ([_delegate respondsToSelector:







>




|







557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
		[response setKeepAlive: true];

		_socket = [socket retain];
		_lastURL = [URL copy];
		_lastResponse = [response retain];
	}

	/* FIXME: Case-insensitive check of redirect's scheme */
	if (redirects > 0 && (status == 301 || status == 302 ||
	    status == 303 || status == 307) &&
	    (redirect = [serverHeaders objectForKey: @"Location"]) != nil &&
	    (_insecureRedirectsAllowed || [scheme isEqual: @"http"] ||
	    [redirect hasPrefix: @"https://"])) {
		OFURL *newURL;
		bool follow;

		newURL = [OFURL URLWithString: redirect
				relativeToURL: URL];

		if ([_delegate respondsToSelector:

Modified src/OFString.m from [c9e153f6dd] to [1c567dbe1d].

874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897

898

899
900
901
902
903
904
905
906
907
908
909
910
911

912
913
914



915
916
917
918
919
920
921
922
923
924

925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942


943
944
945
946
947
948
949
950
951

952
953
954
955
956
957
958
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	void *pool;
#ifdef OF_HAVE_SOCKETS
	OFHTTPClient *client;
	OFHTTPRequest *request;
	OFHTTPResponse *response;
	OFDictionary *headers;
	OFString *contentType, *contentLength;
	OFDataArray *data;
#endif
#ifdef OF_HAVE_FILES
	Class c = [self class];
#endif

	[self release];

	pool = objc_autoreleasePoolPush();

	if ([[URL scheme] isEqual: @"file"]) {

#ifdef OF_HAVE_FILES

		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		objc_autoreleasePoolPop(pool);
		return self;
#else
		@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
#endif
	}

#ifdef OF_HAVE_SOCKETS

	client = [OFHTTPClient client];
	request = [OFHTTPRequest requestWithURL: URL];
	response = [client performRequest: request];




	if ([response statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    exceptionWithRequest: request
				response: response];

	headers = [response headers];

	if (encoding == OF_STRING_ENCODING_AUTODETECT &&
	    (contentType = [headers objectForKey: @"Content-Type"]) != nil) {

		contentType = [contentType lowercaseString];

		if ([contentType hasSuffix: @"charset=utf-8"])
			encoding = OF_STRING_ENCODING_UTF_8;
		if ([contentType hasSuffix: @"charset=iso-8859-1"])
			encoding = OF_STRING_ENCODING_ISO_8859_1;
		if ([contentType hasSuffix: @"charset=iso-8859-15"])
			encoding = OF_STRING_ENCODING_ISO_8859_15;
		if ([contentType hasSuffix: @"charset=windows-1252"])
			encoding = OF_STRING_ENCODING_WINDOWS_1252;
	}

	if (encoding == OF_STRING_ENCODING_AUTODETECT)
		encoding = OF_STRING_ENCODING_UTF_8;

	data = [response readDataArrayTillEndOfStream];

	if ((contentLength = [headers objectForKey: @"Content-Length"]) != nil)


		if ([data count] != (size_t)[contentLength decimalValue])
			@throw [OFTruncatedDataException exception];

	self = [[c alloc] initWithCString: (char*)[data items]
				 encoding: encoding
				   length: [data count]];
#else
	@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
#endif


	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithSerialization: (OFXMLElement*)element







<
<
<
<
<
|
<
<








|
>

>





<
<
|
<

<
<

>
|
|
|
>
>
>

|
|
|
|

|

|
|
>
|

|
|
|
|
|
|
|
|
|

|
|

|

|
>
>
|
|

|
|
|
|
<

>







874
875
876
877
878
879
880





881


882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898


899

900


901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946

947
948
949
950
951
952
953
954
955
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	void *pool;





	OFString *scheme;


#ifdef OF_HAVE_FILES
	Class c = [self class];
#endif

	[self release];

	pool = objc_autoreleasePoolPush();

	scheme = [URL scheme];

#ifdef OF_HAVE_FILES
	if ([scheme isEqual: @"file"]) {
		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];


	} else

#endif


#ifdef OF_HAVE_SOCKETS
	if ([scheme isEqual: @"http"] || [scheme isEqual: @"https"]) {
		OFHTTPClient *client = [OFHTTPClient client];
		OFHTTPRequest *request = [OFHTTPRequest requestWithURL: URL];
		OFHTTPResponse *response = [client performRequest: request];
		OFDictionary *headers;
		OFString *contentType, *contentLength;
		OFDataArray *data;

		if ([response statusCode] != 200)
			@throw [OFHTTPRequestFailedException
			    exceptionWithRequest: request
					response: response];

		headers = [response headers];

		if (encoding == OF_STRING_ENCODING_AUTODETECT &&
		    (contentType = [headers
		    objectForKey: @"Content-Type"]) != nil) {
			contentType = [contentType lowercaseString];

			if ([contentType hasSuffix: @"charset=utf-8"])
				encoding = OF_STRING_ENCODING_UTF_8;
			if ([contentType hasSuffix: @"charset=iso-8859-1"])
				encoding = OF_STRING_ENCODING_ISO_8859_1;
			if ([contentType hasSuffix: @"charset=iso-8859-15"])
				encoding = OF_STRING_ENCODING_ISO_8859_15;
			if ([contentType hasSuffix: @"charset=windows-1252"])
				encoding = OF_STRING_ENCODING_WINDOWS_1252;
		}

		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		data = [response readDataArrayTillEndOfStream];

		if ((contentLength = [headers
		    objectForKey: @"Content-Length"]) != nil)
			if ([data count] !=
			    (size_t)[contentLength decimalValue])
				@throw [OFTruncatedDataException exception];

		self = [[c alloc] initWithCString: (char*)[data items]
					 encoding: encoding
					   length: [data count]];
	} else

#endif
		@throw [OFUnsupportedProtocolException exception];

	objc_autoreleasePoolPop(pool);

	return self;
}

- initWithSerialization: (OFXMLElement*)element

Modified src/OFURL.m from [2209074f5b] to [5ebb65d404].

14
15
16
17
18
19
20

21
22
23
24
25
26
27
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>


#import "OFURL.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFXMLElement.h"

#import "OFInvalidArgumentException.h"







>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#import "OFURL.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFXMLElement.h"

#import "OFInvalidArgumentException.h"
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
	self = [super init];

	@try {
		char *tmp, *tmp2;

		if ((UTF8String2 = of_strdup([string UTF8String])) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithRequestedSize: [string
							     UTF8StringLength]];

		UTF8String = UTF8String2;



		if (!strncmp(UTF8String, "file://", 7)) {


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

			UTF8String += 8;
		} else
			@throw [OFInvalidFormatException exception];


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

		if ((tmp2 = strchr(UTF8String, '@')) != NULL) {







|
|



>
>
|
>
>
|
|
|
<
|
|
|
|
|
>
|
|
<
>







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
	self = [super init];

	@try {
		char *tmp, *tmp2;

		if ((UTF8String2 = of_strdup([string UTF8String])) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithRequestedSize:
			     [string UTF8StringLength]];

		UTF8String = UTF8String2;

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

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

		_scheme = [[OFString alloc]
		    initWithUTF8String: UTF8String

				length: tmp - UTF8String];

		UTF8String = tmp + 3;

		if ([_scheme isEqual: @"file"]) {
			_path = [[OFString alloc]
			    initWithUTF8String: UTF8String];
			return self;

		}

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

		if ((tmp2 = strchr(UTF8String, '@')) != NULL) {
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
			portString = [OFString stringWithUTF8String: tmp2];

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

			_port = [portString decimalValue];

			if (_port == 0)
				_port = 80;

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

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

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

				_fragment = [[OFString alloc]







<
<
<









|
|







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
			portString = [OFString stringWithUTF8String: tmp2];

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

			_port = [portString decimalValue];




			objc_autoreleasePoolPop(pool);
		} else {
			_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 alloc]
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
- (OFString*)scheme
{
	OF_GETTER(_scheme, true)
}

- (void)setScheme: (OFString*)scheme
{
	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFInvalidArgumentException exception];

	OF_SETTER(_scheme, scheme, true, 1)
}

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







<
<
<







364
365
366
367
368
369
370



371
372
373
374
375
376
377
- (OFString*)scheme
{
	OF_GETTER(_scheme, true)
}

- (void)setScheme: (OFString*)scheme
{



	OF_SETTER(_scheme, scheme, true, 1)
}

- (OFString*)host
{
	OF_GETTER(_host, true)
}
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
- (OFString*)path
{
	OF_GETTER(_path, true)
}

- (void)setPath: (OFString*)path
{
	if (([_scheme isEqual: @"http"] || [_scheme isEqual: @"https"]) &&
	    ![path hasPrefix: @"/"])
		@throw [OFInvalidArgumentException exception];

	OF_SETTER(_path, path, true, 1)
}

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







<
<
<
<







414
415
416
417
418
419
420




421
422
423
424
425
426
427
- (OFString*)path
{
	OF_GETTER(_path, true)
}

- (void)setPath: (OFString*)path
{




	OF_SETTER(_path, path, true, 1)
}

- (OFString*)parameters
{
	OF_GETTER(_parameters, true)
}
473
474
475
476
477
478
479
480

481
482
483
484
485
486
487
	else if (_user != nil)
		[ret appendFormat: @"%@@", _user];

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

	if (([_scheme isEqual: @"http"] && _port != 80) ||
	    ([_scheme isEqual: @"https"] && _port != 443))

		[ret appendFormat: @":%u", _port];

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

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







|
>







468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	else if (_user != nil)
		[ret appendFormat: @"%@@", _user];

	if (_host != nil)
		[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];

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

Modified tests/OFURLTests.m from [d80e8f78e3] to [364e1aed63].

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	TEST(@"-[query]", [[u1 query] isEqual: @"q"] && [u4 query] == nil)
	TEST(@"-[fragment]",
	    [[u1 fragment] isEqual: @"f"] && [u4 fragment] == nil)

	TEST(@"-[copy]", R(u4 = [[u1 copy] autorelease]))

	TEST(@"-[isEqual:]", [u1 isEqual: u4] && ![u2 isEqual: u3] &&
	    [[OFURL URLWithString: @"http://bar/"] isEqual: u3])

	TEST(@"-[hash:]", [u1 hash] == [u4 hash] && [u2 hash] != [u3 hash])

	EXPECT_EXCEPTION(@"Detection of invalid format",
	    OFInvalidFormatException, [OFURL URLWithString: @"http"])

	EXPECT_EXCEPTION(@"Detection of invalid scheme",
	    OFInvalidFormatException, [OFURL URLWithString: @"foo://"])

	[pool drain];
}
@end







|






<
<
<



72
73
74
75
76
77
78
79
80
81
82
83
84
85



86
87
88
	TEST(@"-[query]", [[u1 query] isEqual: @"q"] && [u4 query] == nil)
	TEST(@"-[fragment]",
	    [[u1 fragment] isEqual: @"f"] && [u4 fragment] == nil)

	TEST(@"-[copy]", R(u4 = [[u1 copy] autorelease]))

	TEST(@"-[isEqual:]", [u1 isEqual: u4] && ![u2 isEqual: u3] &&
	    [[OFURL URLWithString: @"HTTP://bar/"] isEqual: u3])

	TEST(@"-[hash:]", [u1 hash] == [u4 hash] && [u2 hash] != [u3 hash])

	EXPECT_EXCEPTION(@"Detection of invalid format",
	    OFInvalidFormatException, [OFURL URLWithString: @"http"])




	[pool drain];
}
@end