URL string escape codes
URL string escape codes
- Subject: URL string escape codes
- From: Kirk Kerekes <email@hidden>
- Date: Wed, 14 Aug 2002 11:18:31 -0500
On Wednesday, August 14, 2002, at 10:05 AM, email@hidden.
com wrote:
Date: Wed, 14 Aug 2002 16:05:29 +0100
From: Chris Ridd <email@hidden>
To: CocoaDev <email@hidden>
Subject: Re: Re(2): URL string escape codes
Jens Bauer <email@hidden> wrote:
I'd like to add that '+' becomes space as well, but since you've read the
RFCs already, you should know that.
Furthermore, reading the RFCs should be enough.
If it's not in the RFCs, it's probably because not expected to be
implemented. ;)
CF has functions which do this escaping and unescaping already, and
strictly follow the RFCs:
CFURLCreateStringByAddingPercentEscapes
CFURLCreateStringByReplacingPercentEscapes
I'm not sure if they've been exposed in NSURL or not, but since CFURLs are
NSURLs this is trivial.
CFURLCreateStringByReplacingPercentEscapes fails if it finds a "%" not used
as an escape-marker. Additionally, CFURLCreateStringByAddingPercentEscapes
will gleefully double-escape existing escape-sequences.
These routines are probably just fine for manipulating URLs created by
RFC-compliant software.
However, if you are accepting URL string content from users who do not have
RFC-compliant wetware (using mailto: urls with embedded user-supplied
subject/body for example) you may want to clean up the string in advance
before passing it to the CF routines.
I wrap both the above CF routines in shells that pre-process the URL to
eliminate the elements that will cause the CF routines to choke, and the
URLEncodingOfString function pre-DEcodes the URL to avoid double-encoding.
As a result, my wrapper functions look like:
NSString * URLEncodingOfString(NSString * string)
{// escape embedded %-signs that don't appear to actually be escape
sequences,
// and pre-decode the result to avoid double-encoding
NSString * preppedString = URLDecodingOfString(string);
return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)
strippedString,NULL, NULL,kCFStringEncodingUTF8);
}
NSString * URLDecodingOfString(NSString * string)
{
// escape embedded %-signs that don't appear to actually be escape sequences
NSString * preppedString = escapePercentsInString(string);
return (NSString *)
CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, (CFStringRef)
preppedString, (CFStringRef) @"");
}
For brevity I have not provided the source for my escapePercentsInString()
but anyone who wants a copy can email me.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.