Re: Getting the content-type in a HTTP response
Re: Getting the content-type in a HTTP response
- Subject: Re: Getting the content-type in a HTTP response
- From: Nir Soffer <email@hidden>
- Date: Sun, 27 Jan 2008 02:54:52 +0200
On Jan 27, 2008, at 00:44, Nir Soffer wrote:
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.
Here is a working example:
//////// NSHTTPURLResponse_XYZAdditions.h
#import <Foundation/Foundation.h>
@interface NSHTTPURLResponse (XYZAdditions)
- (NSDictionary *)xyzHTTPHeaders;
@end
//////// NSHTTPURLResponse_XYZAdditions.m
#import "NSHTTPURLResponse_XYZAdditions.h"
static Boolean caseInsensitiveEqual (const void *a, const void *b)
{
return [(id)a compare:(id)b options:
NSCaseInsensitiveSearch | NSLiteralSearch] == NSOrderedSame;
}
static CFHashCode caseInsensitiveHash (const void *value)
{
return [[(id)value lowercaseString] hash];
}
@implementation NSHTTPURLResponse (XYZAdditions)
- (NSDictionary *)xyzHTTPHeaders;
{
NSDictionary *src = [self allHeaderFields];
CFDictionaryKeyCallBacks keyCallbacks = kCFTypeDictionaryKeyCallBacks;
keyCallbacks.equal = caseInsensitiveEqual;
keyCallbacks.hash = caseInsensitiveHash;
CFMutableDictionaryRef dest = CFDictionaryCreateMutable (
kCFAllocatorDefault,
[src count], // capacity
&keyCallbacks,
&kCFTypeDictionaryValueCallBacks
);
NSEnumerator *enumerator = [src keyEnumerator];
id key = nil;
while (key = [enumerator nextObject]) {
id value = [src objectForKey:key];
[(NSMutableDictionary *)dest setObject:value forKey:key];
}
return (NSDictionary *)dest;
}
@end
Example usage:
- (void)connection:(NSURLConnection *)con didReceiveResponse:
(NSURLResponse *)response
{
NSDictionary *headers = [(NSHTTPURLResponse*)response
xyzHTTPHeaders];
NSLog(@"content-type: %@", [headers objectForKey:@"coNtent-tyPe"]);
}
See also this thread: http://www.cocoabuilder.com/archive/message/
cocoa/2002/10/10/70653
Best Regards,
Nir Soffer
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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