Skip to content

Loading…

Port handling added to the engine #100

Closed
wants to merge 3 commits into from

2 participants

@stoto

I added port handling, because I have projects where I need to be able to use webservices running on port 8080.

Please review and pull my commits, It fits in nicely.

@MugunthKumar

Not all websites work when you suffix :80 by default.

@stoto

It won't be a problem now :)

@MugunthKumar

Handled it differently in the latest commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2012
  1. @stoto

    port handling

    stoto committed
Commits on Apr 8, 2012
  1. @stoto
  2. @stoto
This page is out of date. Refresh to see the latest.
Showing with 61 additions and 37 deletions.
  1. +23 −8 MKNetworkKit/MKNetworkEngine.h
  2. +38 −29 MKNetworkKit/MKNetworkEngine.m
View
31 MKNetworkKit/MKNetworkEngine.h
@@ -54,6 +54,21 @@
*/
- (id) initWithHostName:(NSString*) hostName customHeaderFields:(NSDictionary*) headers;
+
+/*!
+ * @abstract Initializes your network engine with a hostname, port, and custom header fields
+ *
+ * @discussion
+ * Creates an engine for a given host name
+ * The default headers you specify here will be appened to every operation created in this engine
+ * The hostname, if not null, initializes a Reachability notifier.
+ * Network reachability notifications are automatically taken care of by MKNetworkEngine
+ * Both parameters are optional
+ *
+ */
+- (id) initWithHostName:(NSString*) hostName port:(NSInteger) port customHeaderFields:(NSDictionary*) headers;
+
+
/*!
* @abstract Creates a simple GET Operation with a request URL
*
@@ -77,7 +92,7 @@
*
*/
-(MKNetworkOperation*) operationWithPath:(NSString*) path
- params:(NSMutableDictionary*) body;
+ params:(NSMutableDictionary*) body;
/*!
* @abstract Creates a simple GET Operation with a request URL, parameters and HTTP Method
@@ -90,8 +105,8 @@
* The HTTP Method is implicitly assumed to be GET
*/
-(MKNetworkOperation*) operationWithPath:(NSString*) path
- params:(NSMutableDictionary*) body
- httpMethod:(NSString*)method;
+ params:(NSMutableDictionary*) body
+ httpMethod:(NSString*)method;
/*!
* @abstract Creates a simple GET Operation with a request URL, parameters, HTTP Method and the SSL switch
@@ -106,9 +121,9 @@
* The previously mentioned methods operationWithPath: and operationWithPath:params: call this internally
*/
-(MKNetworkOperation*) operationWithPath:(NSString*) path
- params:(NSMutableDictionary*) body
- httpMethod:(NSString*)method
- ssl:(BOOL) useSSL;
+ params:(NSMutableDictionary*) body
+ httpMethod:(NSString*)method
+ ssl:(BOOL) useSSL;
/*!
@@ -150,8 +165,8 @@
* prepareHeaders:
*/
-(MKNetworkOperation*) operationWithURLString:(NSString*) urlString
- params:(NSMutableDictionary*) body
- httpMethod:(NSString*) method;
+ params:(NSMutableDictionary*) body
+ httpMethod:(NSString*) method;
/*!
* @abstract adds the custom default headers
View
67 MKNetworkKit/MKNetworkEngine.m
@@ -37,6 +37,7 @@
@interface MKNetworkEngine (/*Private Methods*/)
@property (strong, nonatomic) NSString *hostName;
+@property (assign, nonatomic) NSInteger port;
@property (strong, nonatomic) Reachability *reachability;
@property (strong, nonatomic) NSDictionary *customHeaders;
@property (assign, nonatomic) Class customOperationSubclass;
@@ -59,6 +60,7 @@ -(BOOL) isCacheEnabled;
@implementation MKNetworkEngine
@synthesize hostName = hostName_;
+@synthesize port = port_;
@synthesize reachability = reachability_;
@synthesize customHeaders = customHeaders_;
@synthesize customOperationSubclass = customOperationSubclass_;
@@ -89,37 +91,42 @@ +(void) initialize {
}
}
-- (id) initWithHostName:(NSString*) hostName customHeaderFields:(NSDictionary*) headers {
-
- if((self = [super init])) {
+- (id) initWithHostName:(NSString *)hostName customHeaderFields:(NSDictionary *)headers{
+ return [self initWithHostName:hostName port:80 customHeaderFields:headers];
+}
+
+- (id) initWithHostName:(NSString*) hostName port:(NSInteger)port customHeaderFields:(NSDictionary *)headers{
- if(hostName) {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(reachabilityChanged:)
- name:kReachabilityChangedNotification
- object:nil];
-
- self.hostName = hostName;
- self.reachability = [Reachability reachabilityWithHostname:self.hostName];
- [self.reachability startNotifier];
+ if((self = [super init])) {
+
+ if(hostName) {
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(reachabilityChanged:)
+ name:kReachabilityChangedNotification
+ object:nil];
+
+ self.hostName = hostName;
+ self.port = port;
+ self.reachability = [Reachability reachabilityWithHostname:self.hostName];
+ [self.reachability startNotifier];
+ }
+
+ if([headers objectForKey:@"User-Agent"] == nil) {
+
+ NSMutableDictionary *newHeadersDict = [headers mutableCopy];
+ NSString *userAgentString = [NSString stringWithFormat:@"%@/%@",
+ [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey],
+ [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey]];
+ [newHeadersDict setObject:userAgentString forKey:@"User-Agent"];
+ self.customHeaders = newHeadersDict;
+ } else {
+ self.customHeaders = headers;
+ }
+
+ self.customOperationSubclass = [MKNetworkOperation class];
}
- if([headers objectForKey:@"User-Agent"] == nil) {
-
- NSMutableDictionary *newHeadersDict = [headers mutableCopy];
- NSString *userAgentString = [NSString stringWithFormat:@"%@/%@",
- [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey],
- [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey]];
- [newHeadersDict setObject:userAgentString forKey:@"User-Agent"];
- self.customHeaders = newHeadersDict;
- } else {
- self.customHeaders = headers;
- }
-
- self.customOperationSubclass = [MKNetworkOperation class];
- }
-
- return self;
+ return self;
}
#pragma mark -
@@ -281,7 +288,9 @@ -(MKNetworkOperation*) operationWithPath:(NSString*) path
httpMethod:(NSString*)method
ssl:(BOOL) useSSL {
- NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@", useSSL ? @"https" : @"http", self.hostName, path];
+
+ NSString* portPart = self.port == 80 ? @"" : [NSString stringWithFormat:@":%d", self.port];
+ NSString *urlString = [NSString stringWithFormat:@"%@://%@%@/%@", useSSL ? @"https" : @"http", self.hostName, portPart, path];
return [self operationWithURLString:urlString params:body httpMethod:method];
}
Something went wrong with that request. Please try again.