Skip navigation.
 
mlRe: Getting the content-type in a HTTP response
FROM : Nir Soffer
DATE : Sat Jan 26 23:44:27 2008

On Jan 26, 2008, at 01:57, Dave Carrigan wrote:

> So my next question is if it's possible to override NSDictionary's 
> comparison operator to be case insensitive (a la STL's maps) or if 
> I should just create a new dictionary that copies the old 
> dictionary with downcased keys. The former would be more 
> bulletproof since it wouldn't require other programmers to remember 
> to downcase all their dictionary lookups.


NSDictionary uses -isEqual: - you probably can't change it. You can 
create a CFMutableDictionaryRef with CFDictionaryKeyCallBacks that 
will do case insensitive compare, then copy the original keys and 
values to this dictionary and use it for accessing the values.

It can look like this:

static Boolean caseInsensitiveCompare (const void *a, const void *b);
static CFHashCode caseInsensitiveHash (const void *value);

@implementation NSHTTPURLResponse (XYZAdditions)

- (NSDictionary *)xyzHTTPHeaders;
{
   NSDictionary *src = [self allHeadersFields];

   CFDictionaryKeyCallBacks keyCallbacks;

   keyCallbacks.retain = CFRetain;
   keyCallbacks.release = CFRelease;
   keyCallbacks.copyDescription = CFCopyDescription;
   keyCallbacks.equal = caseInsensitiveCompare;
   keyCallbacks.hash = caseInsensitiveHash;

   CFMutableDictionaryRef dest = CFDictionaryCreateMutable (
       kCFAllocatorDefault,
           [src count], // capacity
           &keyCallbacks,
           &kCFTypeDictionaryValueCallBacks
   );

   // Now copy keys and values from src to dest

   return (NSDictionary *)dest;
}

@end

static Boolean caseInsensitiveCompare (const void *a, const void *b);
{
   // Assuming that keys are strings
   return [(NSString *)a caseInsensitiveCompare:(NSString *)b] == 
NSOrderedSame;
}

static CFHashCode caseInsensitiveHash (const void *value);
{
   // Assuming that keys are strings
   return [[value lower] hash];
}


Compiles in Mail.app :-)


Example usage:

- (void)connection:(NSURLConnection*)connection didReceiveResponse:
(NSURLResponse*)theResponse
{
   NSString* content_type = [[(NSHTTPURLResponse*)theResponse 
xyzHTTPHeaders] objectForKey:@"coNtent-tyPe"];
}



Best Regards,

Nir Soffer

Related mailsAuthorDate
mlGetting the content-type in a HTTP response Dave Carrigan Jan 25, 23:44
mlRe: Getting the content-type in a HTTP response Kyle Sluder Jan 26, 00:22
mlRe: Getting the content-type in a HTTP response Bill Garrison Jan 26, 00:35
mlRe: Getting the content-type in a HTTP response Dave Carrigan Jan 26, 00:57
mlRe: Getting the content-type in a HTTP response John Joyce Jan 26, 01:49
mlRe: Getting the content-type in a HTTP response Kyle Sluder Jan 26, 02:05
mlRe: Getting the content-type in a HTTP response Andy Lee Jan 26, 02:29
mlRe: Getting the content-type in a HTTP response Nir Soffer Jan 26, 23:44
mlRe: Getting the content-type in a HTTP response Nir Soffer Jan 27, 01:54
mlRe: Getting the content-type in a HTTP response Dave Carrigan Jan 28, 18:11