From: David Reiss Date: Wed, 23 Jan 2008 20:35:39 +0000 (+0000) Subject: Thrift: Allow a custom User-Agent with the Cocoa THttpClient. X-Git-Tag: 0.2.0~1032 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=b0232b39e365b25d2aab5e6dc76e4b40cb37d74f;p=common%2Fthrift.git 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 802ff51e..d9ea1a95 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 2e31a984..f5186e20 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 { - self = [super init]; - mURL = [aURL retain]; - - [self setupRequest]; - - // create our request data buffer - mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; - - return self; + return [self initWithURL: aURL + userAgent: nil + timeout: 0]; } - (id) initWithURL: (NSURL *) aURL + userAgent: (NSString *) userAgent timeout: (int) timeout { - self = [self initWithURL: aURL]; + self = [super init]; + if (!self) { + return nil; + } + + mTimeout = timeout; + if (userAgent) { + mUserAgent = [userAgent retain]; + } + mURL = [aURL retain]; - [mRequest setTimeoutInterval: timeout]; + [self setupRequest]; + // create our request data buffer + mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; + return self; } @@ -58,6 +74,7 @@ - (void) dealloc { [mURL release]; + [mUserAgent release]; [mRequest release]; [mRequestData release]; [mResponseData release];