ObjFW  Check-in [c655fa54d2]

Overview
Comment:Improve OFURL.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c655fa54d2e2bd6bf03151c193e1ab524aaaf9a16742c16a9014c537a10594ff
User & Date: js on 2011-01-15 17:59:36
Other Links: manifest | tags
Context
2011-01-15
18:03
Allow + as prefix in -[OFString decimalValue]. check-in: 11c7661a42 user: js tags: trunk
17:59
Improve OFURL. check-in: c655fa54d2 user: js tags: trunk
16:14
Fix a missing include. check-in: b5da55a84c user: js tags: trunk
Changes

Modified src/OFURL.h from [e36be7b9d6] to [a030f72131].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	OFString *path;
	OFString *parameters;
	OFString *query;
	OFString *fragment;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFString *scheme;
@property (readonly, copy) OFString *host;
@property (readonly, assign) uint16_t port;
@property (readonly, copy) OFString *user;
@property (readonly, copy) OFString *password;
@property (readonly, copy) OFString *path;
@property (readonly, copy) OFString *parameters;
@property (readonly, copy) OFString *query;
@property (readonly, copy) OFString *fragment;
#endif

/**
 * \param str A string describing an URL
 * \return A new, autoreleased OFURL
 */
+ URLWithString: (OFString*)str;







|
|
|
|
|
|
|
|
|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	OFString *path;
	OFString *parameters;
	OFString *query;
	OFString *fragment;
}

#ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *scheme;
@property (copy) OFString *host;
@property (assign) uint16_t port;
@property (copy) OFString *user;
@property (copy) OFString *password;
@property (copy) OFString *path;
@property (copy) OFString *parameters;
@property (copy) OFString *query;
@property (copy) OFString *fragment;
#endif

/**
 * \param str A string describing an URL
 * \return A new, autoreleased OFURL
 */
+ URLWithString: (OFString*)str;
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
- initWithString: (OFString*)str;

/**
 * \return The scheme part of the URL
 */
- (OFString*)scheme;








/**
 * \return The host part of the URL
 */
- (OFString*)host;








/**
 * \return The port part of the URL
 */
- (uint16_t)port;








/**
 * \return The user part of the URL
 */
- (OFString*)user;








/**
 * \return The password part of the URL
 */
- (OFString*)password;








/**
 * \return The path part of the URL
 */
- (OFString*)path;








/**
 * \return The parameters part of the URL
 */
- (OFString*)parameters;








/**
 * \return The query part of the URL
 */
- (OFString*)query;








/**
 * \return The fragment part of the URL
 */
- (OFString*)fragment;







@end







>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>




>
>
>
>
>
>
>

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
- initWithString: (OFString*)str;

/**
 * \return The scheme part of the URL
 */
- (OFString*)scheme;

/**
 * Set the scheme part of the URL.
 *
 * \param scheme The scheme part of the URL to set
 */
- (void)setScheme: (OFString*)scheme;

/**
 * \return The host part of the URL
 */
- (OFString*)host;

/**
 * Set the host part of the URL.
 *
 * \param host The host part of the URL to set
 */
- (void)setHost: (OFString*)host;

/**
 * \return The port part of the URL
 */
- (uint16_t)port;

/**
 * Set the port part of the URL.
 *
 * \param port The port part of the URL to set
 */
- (void)setPort: (uint16_t)port;

/**
 * \return The user part of the URL
 */
- (OFString*)user;

/**
 * Set the user part of the URL.
 *
 * \param user The user part of the URL to set
 */
- (void)setUser: (OFString*)user;

/**
 * \return The password part of the URL
 */
- (OFString*)password;

/**
 * Set the password part of the URL.
 *
 * \param password The password part of the URL to set
 */
- (void)setPassword: (OFString*)password;

/**
 * \return The path part of the URL
 */
- (OFString*)path;

/**
 * Set the path part of the URL.
 *
 * \param path The path part of the URL to set
 */
- (void)setPath: (OFString*)path;

/**
 * \return The parameters part of the URL
 */
- (OFString*)parameters;

/**
 * Set the parameters part of the URL.
 *
 * \param parameters The parameters part of the URL to set
 */
- (void)setParameters: (OFString*)parameters;

/**
 * \return The query part of the URL
 */
- (OFString*)query;

/**
 * Set the query part of the URL.
 *
 * \param query The query part of the URL to set
 */
- (void)setQuery: (OFString*)query;

/**
 * \return The fragment part of the URL
 */
- (OFString*)fragment;

/**
 * Set the fragment part of the URL.
 *
 * \param fragment The fragment part of the URL to set
 */
- (void)setFragment: (OFString*)fragment;
@end

Modified src/OFURL.m from [5ee125a04b] to [84444b80b2].

20
21
22
23
24
25
26








27
28
29
30
31
32
33
#include <string.h>
#include <assert.h>

#import "OFURL.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"









@implementation OFURL
+ URLWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}








>
>
>
>
>
>
>
>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string.h>
#include <assert.h>

#import "OFURL.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#define ADD_STR_HASH(str)			\
	h = [str hash];				\
	OF_HASH_ADD(hash, h >> 24);		\
	OF_HASH_ADD(hash, (h >> 16) & 0xFF);	\
	OF_HASH_ADD(hash, (h >> 8) & 0xFF);	\
	OF_HASH_ADD(hash, h & 0xFF);

@implementation OFURL
+ URLWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}

187
188
189
190
191
192
193
























194
195
196
197
198
199
200
	if (![url->query isEqual: query])
		return NO;
	if (![url->fragment isEqual: fragment])
		return NO;

	return YES;
}

























- copy
{
	OFURL *new = [[OFURL alloc] init];

	new->scheme = [scheme copy];
	new->host = [host copy];







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	if (![url->query isEqual: query])
		return NO;
	if (![url->fragment isEqual: fragment])
		return NO;

	return YES;
}

- (uint32_t)hash
{
	uint32_t hash, h;

	OF_HASH_INIT(hash);

	ADD_STR_HASH(scheme);
	ADD_STR_HASH(host);

	OF_HASH_ADD(hash, (port >> 8) & 0xFF);
	OF_HASH_ADD(hash, port & 0xFF);

	ADD_STR_HASH(user);
	ADD_STR_HASH(password);
	ADD_STR_HASH(path);
	ADD_STR_HASH(parameters);
	ADD_STR_HASH(query);
	ADD_STR_HASH(fragment);

	OF_HASH_FINALIZE(hash);

	return hash;
}

- copy
{
	OFURL *new = [[OFURL alloc] init];

	new->scheme = [scheme copy];
	new->host = [host copy];
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
	return new;
}

- (OFString*)scheme
{
	return [[scheme copy] autorelease];
}












- (OFString*)host
{
	return [[host copy] autorelease];
}








- (uint16_t)port
{
	return port;
}






- (OFString*)user
{
	return [[user copy] autorelease];
}








- (OFString*)password
{
	return [[password copy] autorelease];
}








- (OFString*)path
{
	return [[path copy] autorelease];
}








- (OFString*)parameters
{
	return [[parameters copy] autorelease];
}








- (OFString*)query
{
	return [[query copy] autorelease];
}








- (OFString*)fragment
{
	return [[fragment copy] autorelease];
}








- (OFString*)description
{
	OFMutableString *desc = [OFMutableString
	    stringWithFormat: @"%s://", [scheme cString]];
	BOOL needPort = YES;








>
>
>
>
>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>





>
>
>
>
>
>
>







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
	return new;
}

- (OFString*)scheme
{
	return [[scheme copy] autorelease];
}

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

	OFString *old = scheme;
	scheme = [scheme_ copy];
	[old release];
}

- (OFString*)host
{
	return [[host copy] autorelease];
}

- (void)setHost: (OFString*)host_
{
	OFString *old = host;
	host = [host_ copy];
	[old release];
}

- (uint16_t)port
{
	return port;
}

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

- (OFString*)user
{
	return [[user copy] autorelease];
}

- (void)setUser: (OFString*)user_
{
	OFString *old = user;
	user = [user_ copy];
	[old release];
}

- (OFString*)password
{
	return [[password copy] autorelease];
}

- (void)setPassword: (OFString*)password_
{
	OFString *old = password;
	password = [password_ copy];
	[old release];
}

- (OFString*)path
{
	return [[path copy] autorelease];
}

- (void)setPath: (OFString*)path_
{
	OFString *old = path;
	path = [path_ copy];
	[old release];
}

- (OFString*)parameters
{
	return [[parameters copy] autorelease];
}

- (void)setParameters: (OFString*)parameters_
{
	OFString *old = parameters;
	parameters = [parameters_ copy];
	[old release];
}

- (OFString*)query
{
	return [[query copy] autorelease];
}

- (void)setQuery: (OFString*)query_
{
	OFString *old = query;
	query = [query_ copy];
	[old release];
}

- (OFString*)fragment
{
	return [[fragment copy] autorelease];
}

- (void)setFragment: (OFString*)fragment_
{
	OFString *old = fragment;
	fragment = [fragment_ copy];
	[old release];
}

- (OFString*)description
{
	OFMutableString *desc = [OFMutableString
	    stringWithFormat: @"%s://", [scheme cString]];
	BOOL needPort = YES;

Modified tests/OFURLTests.m from [8e8ba2b5ac] to [7ab756aa3f].

51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
	TEST(@"-[parameters]", [[u1 parameters] isEqual: @"p"])
	TEST(@"-[query]", [[u1 query] isEqual: @"q"])
	TEST(@"-[fragment]", [[u1 fragment] isEqual: @"f"])

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

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


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

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

	[pool drain];
}
@end







>










51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	TEST(@"-[parameters]", [[u1 parameters] isEqual: @"p"])
	TEST(@"-[query]", [[u1 query] isEqual: @"q"])
	TEST(@"-[fragment]", [[u1 fragment] isEqual: @"f"])

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

	TEST(@"-[isEqual:]", [u1 isEqual: u4] && ![u2 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