Re: HTTP extensions to NSMutableURLRequest erroneously modify headers
Re: HTTP extensions to NSMutableURLRequest erroneously modify headers
- Subject: Re: HTTP extensions to NSMutableURLRequest erroneously modify headers
- From: email@hidden
- Date: Wed, 27 Jul 2005 20:23:41 +1000
I've tried poking around with NSHTTPURLProtocol, where the headers
are ultimately stored, but I'm missing a piece of the puzzle...
while header values are set via the setProperty:forKey:inRequest:
class method (with the header name as the key), this isn't enough
to have the original NSMutableHTTPURLRequest notice them... there's
some other storage somewhere I've missed.
Actually, I was confused with my own debugging stuff - turns out HTTP
headers, along with other HTTP data (cookies, etc), is stored in an
NSHTTPURLRequestParameters instance inside NSURLRequestInternal
(which is inside NSURLRequest). Why HTTP parameters are in a generic
NSURLRequest I don't know - let alone not using the
setProperty:forKey:inRequest: method as the documentation states they
should - it seems like Apple's definitely not eating their own dog
food on this one. :/
Anyway, now that I've figured that out I've been able to write valid
versions of setValue:forHTTPHeaderField:, which work nicely -
although since NSHTTPURLRequestParameters doesn't have any getters or
setters, I'm having to access it's instance variables directly...
gross, but it works (and I can only assume this is what Apple are
doing anyway).
So, for those who might be watching, or reviewing the list archives,
if you need a workaround for the setValue:forHTTPHeaderField:'s
capitalisation bug, I have (which appears to work, although I make no
guarantees):
(header)
@interface NSURLRequest (_parametersForWriting)
- (id)_parametersForWriting;
@end
@interface NSURLRequest (NSHTTPURLRequestFixes)
- (NSString*)valueForHTTPHeaderFieldWithoutCapitalisation:(NSString*)
fieldName;
@end
@interface NSMutableURLRequest (NSMutableHTTPURLRequestFixes)
- (void)addValue:(NSString*)value
forHTTPHeaderFieldWithoutCapitalisation:(NSString*)fieldName;
- (void)setValue:(NSString*)value
forHTTPHeaderFieldWithoutCapitalisation:(NSString*)fieldName;
@end
(implementation/source)
@implementation NSURLRequest (NSHTTPURLRequestFixes)
- (NSString*)valueForHTTPHeaderFieldWithoutCapitalisation:(NSString*)
fieldName {
NSHTTPURLRequestParameters *params = [self _parametersForWriting];
if (nil != params) {
if (nil != params->fields) {
return [params->fields objectForKey:fieldName];
}
} else {
NSLog(@"%s %s:%d - Unable to obtain parameters for writing.
\n", __FILE__, __func__, __LINE__);
}
return nil;
}
@end
@implementation NSMutableURLRequest (NSMutableHTTPURLRequestFixes)
- (void)addValue:(NSString*)value
forHTTPHeaderFieldWithoutCapitalisation:(NSString*)fieldName {
NSString *existingValue = [self
valueForHTTPHeaderFieldWithoutCapitalisation:fieldName];
NSString *newValue;
if (nil != existingValue) {
newValue = [existingValue stringByAppendingFormat:@",%@",
value];
} else {
newValue = value;
}
[self setValue:newValue
forHTTPHeaderFieldWithoutCapitalisation:fieldName];
}
- (void)setValue:(NSString*)value
forHTTPHeaderFieldWithoutCapitalisation:(NSString*)fieldName {
NSHTTPURLRequestParameters *params = [self _parametersForWriting];
if (nil != params) {
if (nil == params->fields) {
params->fields = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:value, fieldName, nil];
} else {
[params->fields setObject:value forKey:fieldName];
}
} else {
NSLog(@"%s %s:%d - Unable to obtain parameters for writing.
\n", __FILE__, __func__, __LINE__);
}
}
@end
Also, as an aside, anyone have any idea how to get XCode's visualiser
to work on code outside the current project - e.g. stuff in
Foundation? That *should* have been how I reverse engineered this,
but unfortunately I couldn't get it to work like that. :(
Wade Tregaskis (AIM/iChat, Yahoo & Skype: wadetregaskis, ICQ:
40056898, MSN: email@hidden, AV iChat & email:
email@hidden, Jabber: email@hidden)
-- Sed quis custodiet ipsos custodes?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden