Thrift: Allow a custom User-Agent with the Cocoa THttpClient.

Reviewed By: mcslee

Test Plan: None.

Other Notes:
Submitted by Andrew McGeachie, the author of the Cocoa bindings.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665440 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cocoa/src/transport/THTTPClient.h b/lib/cocoa/src/transport/THTTPClient.h
index 802ff51..d9ea1a9 100644
--- a/lib/cocoa/src/transport/THTTPClient.h
+++ b/lib/cocoa/src/transport/THTTPClient.h
@@ -7,11 +7,14 @@
   NSMutableData * mRequestData;
   NSData * mResponseData;
   int mResponseDataOffset;
+  NSString * mUserAgent;
+  int mTimeout;
 }
 
 - (id) initWithURL: (NSURL *) aURL;
 
 - (id) initWithURL: (NSURL *) aURL 
+         userAgent: (NSString *) userAgent
            timeout: (int) timeout;
 
 - (void) setURL: (NSURL *) aURL;
diff --git a/lib/cocoa/src/transport/THTTPClient.m b/lib/cocoa/src/transport/THTTPClient.m
index 2e31a98..f5186e2 100644
--- a/lib/cocoa/src/transport/THTTPClient.m
+++ b/lib/cocoa/src/transport/THTTPClient.m
@@ -15,32 +15,48 @@
   [mRequest setHTTPMethod: @"POST"];
   [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"];
   [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
-  [mRequest setValue: @"Cocoa/THTTPClient" forHTTPHeaderField: @"User-Agent"];
+
+  NSString * userAgent = mUserAgent;
+  if (!userAgent) {
+    userAgent = @"Cocoa/THTTPClient";
+  }
+  [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
+
   [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
+  if (mTimeout) {
+    [mRequest setTimeoutInterval: mTimeout];
+  }
 }
 
 
 - (id) initWithURL: (NSURL *) aURL
 {
+  return [self initWithURL: aURL
+                 userAgent: nil
+                   timeout: 0];
+}
+
+
+- (id) initWithURL: (NSURL *) aURL 
+         userAgent: (NSString *) userAgent
+           timeout: (int) timeout
+{
   self = [super init];
+  if (!self) {
+    return nil;
+  }
+  
+  mTimeout = timeout;
+  if (userAgent) {
+    mUserAgent = [userAgent retain];
+  }
   mURL = [aURL retain];
 
   [self setupRequest];
 
   // create our request data buffer
   mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
-
-  return self;
-}
-
-
-- (id) initWithURL: (NSURL *) aURL 
-           timeout: (int) timeout
-{
-  self = [self initWithURL: aURL];
-
-  [mRequest setTimeoutInterval: timeout];
-
+    
   return self;
 }
 
@@ -58,6 +74,7 @@
 - (void) dealloc
 {
   [mURL release];
+  [mUserAgent release];
   [mRequest release];
   [mRequestData release];
   [mResponseData release];