Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 108 lines (85 sloc) 3.544 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // NSData+URLUserAgent.m
3 // Shovel
4 //
5 // Created by Uli Kusterer on 22.03.05.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright 2005 M. Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 #import "NSData+URLUserAgent.h"
29 #ifndef SVN_VERSION // C string with version number.
30 #import "svn_version.h"
31 #endif
32
33
34 static NSString* gUKNSDataDefaultDownloadUserAgent = nil;
35
36
37 @implementation NSData (UKURLUserAgent)
38
39 +(id) dataWithContentsOfURL: (NSURL*)url userAgent: (NSString*)agent
40 {
41 return [[[self alloc] initWithContentsOfURL: url userAgent: agent] autorelease];
42 }
43
44 +(id) dataWithContentsOfURL: (NSURL*)url userAgent: (NSString*)agent timeout: (NSTimeInterval)timeout
45 {
46 return [[[self alloc] initWithContentsOfURL: url userAgent: agent timeout: timeout] autorelease];
47 }
48
49
50
51 -(id) initWithContentsOfURL: (NSURL*)url userAgent: (NSString*)agent
52 {
53 return [self initWithContentsOfURL: url userAgent: agent timeout: 0];
54 }
55
56
57 -(id) initWithContentsOfURL: (NSURL*)url userAgent: (NSString*)agent timeout: (NSTimeInterval)timeout
58 {
59 [self autorelease]; // We'll get a new NSData object from NSURLConnection, so release self.
60
61 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
62 NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL: url];
63 NSString *boundary = @"0xKhTmLbOuNdArY";
64 NSURLResponse *response = nil;
65 NSError *error = nil;
66 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
67 if( !agent )
68 agent = [NSData defaultDownloadUserAgent];
69
70 // setting the headers:
71 [postRequest setHTTPMethod: @"GET"];
72 [postRequest setValue: contentType forHTTPHeaderField: @"Content-Type"];
73 [postRequest setValue: agent forHTTPHeaderField: @"User-Agent"];
74 if( timeout != 0 )
75 [postRequest setTimeoutInterval: timeout];
76
77 NSData* dt = [[NSURLConnection sendSynchronousRequest: postRequest returningResponse: &response error: &error] retain];
78 [pool release];
79
80 return dt;
81 }
82
83
84 +(void) setDefaultDownloadUserAgent: (NSString*)uaStr
85 {
86 if( uaStr != gUKNSDataDefaultDownloadUserAgent )
87 {
88 [gUKNSDataDefaultDownloadUserAgent autorelease];
89 gUKNSDataDefaultDownloadUserAgent = [uaStr retain];
90 }
91 }
92
93 +(NSString*) defaultDownloadUserAgent
94 {
95 if( !gUKNSDataDefaultDownloadUserAgent )
96 {
97 NSBundle* mb = [NSBundle mainBundle];
98 gUKNSDataDefaultDownloadUserAgent = [[NSString stringWithFormat: @"%@/%@ (UliKit/%s)",
99 [mb objectForInfoDictionaryKey: @"CFBundleExecutable"],
100 [mb objectForInfoDictionaryKey: @"CFBundleVersion"], SVN_VERSION] retain];
101 }
102
103 return gUKNSDataDefaultDownloadUserAgent;
104 }
105
106
107 @end
Something went wrong with that request. Please try again.