Skip to content

Instantly share code, notes, and snippets.

@0xced
Created May 26, 2010 13:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xced/414439 to your computer and use it in GitHub Desktop.
Save 0xced/414439 to your computer and use it in GitHub Desktop.
NSMutableURLRequest+CaseSensitive
/*
* NSMutableURLRequest+CaseSensitive is a category on NSMutableURLRequest
* that let you control the exact value of the HTTP header fields.
*
* NSMutableURLRequest documentation says:
* "The name of the header field to set. In keeping with the HTTP RFC,
* HTTP header field names are case-insensitive."
*
* Unfortunately, not all HTTP implementations are RFC compliant and some
* require case-sensitive header fields. To set such headers, use the
* provided methods with caseSensitive:YES
*
* Tested on Mac OS X 10.5.8 and 10.6.3
*/
#import <Foundation/NSURLRequest.h>
@interface NSMutableURLRequest (CaseSensitive)
- (void) setAllHTTPHeaderFields:(NSDictionary *)headerFields caseSensitive:(BOOL)caseSensitive;
- (void) setValue:(NSString *)value forHTTPHeaderField:(NSString *)field caseSensitive:(BOOL)caseSensitive;
- (void) addValue:(NSString *)value forHTTPHeaderField:(NSString *)field caseSensitive:(BOOL)caseSensitive;
@end
/*
Licensed under the MIT License
Copyright (c) 2010 Cédric Luthi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "NSMutableURLRequest+CaseSensitive.h"
// Requires APE Lite: http://unsanity.com/haxies/apesdk
#import "APELite.h"
#import <mach-o/dyld.h>
// original implementation pointer, see http://opensource.apple.com/source/CFNetwork/CFNetwork-129.20/HTTP/CFHTTPMessage.c
static CFStringRef (*_CFCapitalizeHeader)(CFStringRef) = NULL;
// set of fields added with caseSensitive:YES
static NSMutableSet *caseSensitiveHeaderFields = nil;
// Although _CFCapitalizeHeader does not contain "Create" or "Copy", the returned CFStringRef must be retained
static CFStringRef CapitalizeHeader(CFStringRef headerString)
{
if ([caseSensitiveHeaderFields containsObject:(NSString*)headerString])
return CFRetain(headerString);
else
return _CFCapitalizeHeader(headerString);
}
static void patchCFCapitalizeHeader(void)
{
if (_CFCapitalizeHeader != NULL)
return;
uint32_t i, count = _dyld_image_count();
for(i = 0; i < count; i++)
{
const char* imageName = _dyld_get_image_name(i);
if (strstr(imageName, "CFNetwork.framework"))
{
struct mach_header* imageHeader = (struct mach_header*)_dyld_get_image_header(i);
void* __CFCapitalizeHeader = APEFindSymbol(imageHeader, "__CFCapitalizeHeader");
if (__CFCapitalizeHeader)
_CFCapitalizeHeader = APEPatchCreate(__CFCapitalizeHeader, CapitalizeHeader);
else
NSLog(@"__CFCapitalizeHeader symbol not found");
break;
}
}
}
static void addCaseSensitiveHeaderFields(NSArray *fields)
{
patchCFCapitalizeHeader();
if (caseSensitiveHeaderFields == nil)
caseSensitiveHeaderFields = [[NSMutableSet alloc] init];
[caseSensitiveHeaderFields addObjectsFromArray:fields];
}
@implementation NSMutableURLRequest (CaseSensitive)
- (void) setAllHTTPHeaderFields:(NSDictionary *)headerFields caseSensitive:(BOOL)caseSensitive
{
if (caseSensitive)
addCaseSensitiveHeaderFields([headerFields allKeys]);
[self setAllHTTPHeaderFields:headerFields];
}
- (void) addValue:(NSString *)value forHTTPHeaderField:(NSString *)field caseSensitive:(BOOL)caseSensitive
{
if (caseSensitive)
addCaseSensitiveHeaderFields([NSArray arrayWithObject:field]);
[self addValue:value forHTTPHeaderField:field];
}
- (void) setValue:(NSString *)value forHTTPHeaderField:(NSString *)field caseSensitive:(BOOL)caseSensitive
{
if (caseSensitive)
addCaseSensitiveHeaderFields([NSArray arrayWithObject:field]);
[self setValue:value forHTTPHeaderField:field];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment