MugunthKumar/MKNetworkKit — pull request #335 by classis (2013-05-09 12:05:53+00:00)
Title: Add IP address support zu MKNetworkEngine

Please add the following code modification or something similar to MKNetworkEngine to support IP addresses in addition to host names:

- (id) initWithHostName:(NSString*) hostName apiPath:(NSString*) apiPath customHeaderFields:(NSDictionary*) headers {

if((self = [super init])) {

self.apiPath = apiPath;
self.backgroundCacheQueue = dispatch_queue_create("com.mknetworkkit.cachequeue", DISPATCH_QUEUE_SERIAL);
self.operationQueue = dispatch_queue_create("com.mknetworkkit.operationqueue", DISPATCH_QUEUE_SERIAL);

if(hostName) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];

self.hostName = hostName;

//--------- Modified code start -----------
NSString *ipAddressPattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])"
"\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"
"\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"
"\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
NSPredicate *regExpPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ipAddressPattern];
if ([regExpPredicate evaluateWithObject:hostName]) {
struct sockaddr_in address;
address.sin_len = (__uint8_t) sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(80);
address.sin_addr.s_addr = inet_addr([hostName UTF8String]);

self.reachability = [Reachability reachabilityWithAddress:&address];
} else {
self.reachability = [Reachability reachabilityWithHostname:self.hostName];
}
//--------- Modified code end -----------
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[self.reachability startNotifier];
});
}

if(headers[@"User-Agent"] == nil) {

NSMutableDictionary *newHeadersDict = [headers mutableCopy];
NSString *userAgentString = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] infoDictionary][(NSString *)kCFBundleNameKey],
[[NSBundle mainBundle] infoDictionary][(NSString *)kCFBundleVersionKey]];
newHeadersDict[@"User-Agent"] = userAgentString;
self.customHeaders = newHeadersDict;
} else {
self.customHeaders = [headers mutableCopy];
}

self.customOperationSubclass = [MKNetworkOperation class];
}

return self;
}