Ensure only ASCII is used in the User-Agent header.

The charset for HTTP headers has to be ASCII at best
and is ill defined at worst.

My Applikation has a Bundle Name with Umlauts in it
which results in bessy Webserver log files due to the
"strange" engoding of the App name.
To me it seems, that AFNetworking is meant to transcode the
App name to ASCII but the implementation is lacking and thus
only encodes to Latin1/ISO8859-1.
This patch - although not tested with Asian scripts - should
result in User-Agent strings always beeing ASCII while remaining
mostly unchanged.

So `Überland/0.16.27` becomes `Uberland/0.16.27` which is much
more standards compliant.
This commit is contained in:
Maximillian Dornseif 2013-10-02 23:16:53 +02:00 committed by Mattt Thompson
parent 86dd0a023c
commit a2a27614ab

View File

@ -205,7 +205,7 @@ NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) {
if (userAgent) {
if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
NSMutableString *mutableUserAgent = [userAgent mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, kCFStringTransformToLatin, false);
CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, CFSTR("Any-Latin; Latin-ASCII; [:^ASCII:] Remove"), false);
userAgent = mutableUserAgent;
}
[self setValue:userAgent forHTTPHeaderField:@"User-Agent"];