• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
URL string escape codes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.
  • Prev by Date: Re: NSRunLoop question
  • Next by Date: Re: How do I inhibi the I beam cursor in a NSTextView
  • Previous by thread: Re: URL string escape codes
  • Next by thread: NSRunLoop question
  • Index(es):
    • Date
    • Thread