Re: Easy question about URLs
Re: Easy question about URLs
- Subject: Re: Easy question about URLs
- From: Jerry Krinock <email@hidden>
- Date: Fri, 09 Mar 2007 21:46:21 -0800
- Thread-topic: Easy question about URLs
on 07/03/09 10:51, James Bucanek at email@hidden wrote:
> Will the parameter portion of the URL survive the trip through [NSWorkspace
> openURL:...] through [NSDocumentController documentForURL:]? What's the best
> way to encode, add, and ultimately decode the parameter portion of the URL. I
> don't see any tools in NSURL for manipulating the parameter portion of an URL.
I use a catergory on NSString for this. Simple wrappers around some CF
functions, but noting a few gotchas that you don't want to have to learn the
hard way. Have fun!
@interface NSString (NSStringURLHelp)
- (NSString*)encodePercentEscapesPerRFC2396 ;
- (NSString*)encodePercentEscapesStrictlyPerRFC2396 ;
// Decodes any existing percent escapes which should not be encoded per
RFC 2396 sec. 2.4.3
// Encodes any characters which should be encoded per RFC 2396 sec.
2.4.3.
- (NSString*)encodePercentEscapesPerRFC2396ButNot:(NSString*)butNot
butAlso:(NSString*)butAlso ;
- (NSString*)decodeAllPercentEscapes ;
// I did an experiment to find out which ASCII characters are encoded,
// by encoding a string with all the nonalphanumeric characters
available on the
// Macintosh keyboard, with and without the shift key down. There were
fourteen:
// ` # % ^ [ ] { } \ | " < >
// You only see thirteen because the fourtheenth one is the space
character, " ".
// This agrees with the lists of "space" "delims" and "unwise" in by
RFC 2396 sec. 2.4.3
// Also, I found that all of the non-ASCII characters available on the
Macintosh
// keyboard by using option or shift+option are also encoded. Some of
these have
// two bytes of unicode to encode, for example ¤ for 0xC2A4
- (NSString*)normalizedURL ;
// The above may mangle javascript-schemed bookmarks by doing strict
RFC2396 encoding.
// I'm not sure; unless all characters required to be %escape encoded by
RFC2396
// should already by &escape encoded. Note that the & and ; characters
are not encoded
// by RFC2396.
@end
@implementation NSString (NSStringURLHelp)
- (NSString*)encodePercentEscapesPerRFC2396 {
return
(NSString*)[(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self, NULL, NULL, kCFStringEncodingUTF8) autorelease] ;
}
- (NSString*)encodePercentEscapesStrictlyPerRFC2396 {
CFStringRef decodedString = (CFStringRef)[self decodeAllPercentEscapes]
;
// The above may return NULL if url contains invalid escape sequences
like ème, èfe, è00 or è11,
// because CFURLCreateStringByReplacingPercentEscapes() isn't smart
enough to ignore them.
CFStringRef recodedString =
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, decodedString,
NULL, NULL, kCFStringEncodingUTF8);
// And then, if decodedString is NULL, recodedString will be NULL too.
// So, we recover from this rare but possible error by returning the
original self
// because it's "better than nothing".
NSString* answer = (recodedString != NULL) ? [(NSString*)recodedString
autorelease] : self ;
// Note that if recodedString is NULL, we don't want to CFRelease() it.
// Conveniently, unlike [nil release], CFRelease(NULL) causes a crash.
return answer ;
}
- (NSString*)encodePercentEscapesPerRFC2396ButNot:(NSString*)butNot
butAlso:(NSString*)butAlso {
return
(NSString*)[(NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self, (CFStringRef)butNot, (CFStringRef)butAlso,
kCFStringEncodingUTF8) autorelease] ;
}
- (NSString*)decodeAllPercentEscapes {
// CFURLCreateStringByReplacingPercentEscapes() seems to only replace
%[NUMBER] style escapes
return
(NSString*)[(NSString*)CFURLCreateStringByReplacingPercentEscapes(kCFAllocat
orDefault, (CFStringRef)self, CFSTR("")) autorelease] ;
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden