| 1 | // |
|---|
| 2 | // AtomApi.m |
|---|
| 3 | // TypePad-Uploader |
|---|
| 4 | // |
|---|
| 5 | // Created by Nicholas Gerakines on 5/1/06. |
|---|
| 6 | // Copyright 2006 __MyCompanyName__. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "AtomApi.h" |
|---|
| 10 | #import "AtomExtensions.h" |
|---|
| 11 | #import "NSDataAdditions.h" |
|---|
| 12 | |
|---|
| 13 | #define APIURL @"http://www.typepad.com/t/atom/"; |
|---|
| 14 | |
|---|
| 15 | @implementation AtomApi |
|---|
| 16 | |
|---|
| 17 | - (id) init { |
|---|
| 18 | if (self = [super init]) { |
|---|
| 19 | /* ... */ |
|---|
| 20 | dataarg = [[NSString alloc] init]; |
|---|
| 21 | atomdata = [[NSString alloc] init]; |
|---|
| 22 | apiurl = [[NSString alloc] init]; |
|---|
| 23 | resdata = [[NSString alloc] init]; |
|---|
| 24 | authuser = [[NSString alloc] init]; |
|---|
| 25 | authpass = [[NSString alloc] init]; |
|---|
| 26 | [self setRequiresAuth:NO]; |
|---|
| 27 | [self retain]; |
|---|
| 28 | } |
|---|
| 29 | return self; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | /* dataarg */ |
|---|
| 33 | - (NSString *) dataarg { return dataarg; } |
|---|
| 34 | |
|---|
| 35 | /* -setDataarg: */ |
|---|
| 36 | - (void) setDataarg: (NSString *) Dataarg { |
|---|
| 37 | //NSLog(@"in -setDataarg:, old value of dataarg: %@, changed to: %@", dataarg, Dataarg); |
|---|
| 38 | if (dataarg != Dataarg) { |
|---|
| 39 | [dataarg autorelease]; |
|---|
| 40 | dataarg = [Dataarg retain]; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /* atomdata */ |
|---|
| 45 | - (NSString *) atomdata { return atomdata; } |
|---|
| 46 | |
|---|
| 47 | /* -setAtomdata: */ |
|---|
| 48 | - (void) setAtomdata: (NSString *) Atomdata { |
|---|
| 49 | //NSLog(@"in -setAtomdata:, old value of atomdata: %@, changed to: %@", atomdata, Atomdata); |
|---|
| 50 | if (atomdata != Atomdata) { |
|---|
| 51 | [atomdata autorelease]; |
|---|
| 52 | atomdata = [Atomdata retain]; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | /* apiurl */ |
|---|
| 57 | - (NSString *) apiurl { return apiurl; } |
|---|
| 58 | |
|---|
| 59 | /* -setApiurl: */ |
|---|
| 60 | - (void) setApiurl: (NSString *) Apiurl { |
|---|
| 61 | //NSLog(@"in -setApiurl:, old value of apiurl: %@, changed to: %@", apiurl, Apiurl); |
|---|
| 62 | if (apiurl != Apiurl) { |
|---|
| 63 | [apiurl autorelease]; |
|---|
| 64 | apiurl = [Apiurl retain]; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /* resdata */ |
|---|
| 69 | - (NSString *) resdata { return resdata; } |
|---|
| 70 | |
|---|
| 71 | /* -setResdata: */ |
|---|
| 72 | - (void) setResdata: (NSString *) Resdata { |
|---|
| 73 | //NSLog(@"in -setResdata:, old value of resdata: %@, changed to: %@", resdata, Resdata); |
|---|
| 74 | if (resdata != Resdata) { |
|---|
| 75 | [resdata autorelease]; |
|---|
| 76 | resdata = [Resdata retain]; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /* authuser */ |
|---|
| 81 | - (NSString *) authuser { return authuser; } |
|---|
| 82 | |
|---|
| 83 | /* -setAuthuser: */ |
|---|
| 84 | - (void) setAuthuser: (NSString *) Authuser { |
|---|
| 85 | //NSLog(@"in -setAuthuser:, old value of authuser: %@, changed to: %@", authuser, Authuser); |
|---|
| 86 | [self setRequiresAuth:YES]; |
|---|
| 87 | if (authuser != Authuser) { |
|---|
| 88 | [authuser autorelease]; |
|---|
| 89 | authuser = [Authuser retain]; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /* authpass */ |
|---|
| 94 | - (NSString *) authpass { return authpass; } |
|---|
| 95 | |
|---|
| 96 | /* -setAuthpass: */ |
|---|
| 97 | - (void) setAuthpass: (NSString *) Authpass { |
|---|
| 98 | [self setRequiresAuth:YES]; |
|---|
| 99 | //NSLog(@"in -setAuthpass:, old value of authpass: %@, changed to: %@", authpass, Authpass); |
|---|
| 100 | if (authpass != Authpass) { |
|---|
| 101 | [authpass autorelease]; |
|---|
| 102 | authpass = [Authpass retain]; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | - (void) setAuth: (NSString *) user pass:(NSString *) pass { |
|---|
| 107 | [self setAuthuser:user]; |
|---|
| 108 | [self setAuthpass:pass]; |
|---|
| 109 | [self setRequiresAuth:YES]; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /* requiresAuth */ |
|---|
| 113 | - (BOOL) requiresAuth { return requiresAuth; } |
|---|
| 114 | |
|---|
| 115 | /* -setRequiresAuth: */ |
|---|
| 116 | - (void) setRequiresAuth: (BOOL) flag { |
|---|
| 117 | //NSLog(@"in -setRequiresAuth, old value of requiresAuth: %@, changed to: %@", (requiresAuth ? @"YES": @"NO"), (flag ? @"YES": @"NO") ); |
|---|
| 118 | requiresAuth = flag; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /* dealloc */ |
|---|
| 122 | - (void) dealloc { |
|---|
| 123 | [dataarg release]; |
|---|
| 124 | [atomdata release]; |
|---|
| 125 | [apiurl release]; |
|---|
| 126 | [resdata release]; |
|---|
| 127 | [authuser release]; |
|---|
| 128 | [authpass release]; |
|---|
| 129 | |
|---|
| 130 | dataarg = nil; |
|---|
| 131 | atomdata = nil; |
|---|
| 132 | apiurl = nil; |
|---|
| 133 | resdata = nil; |
|---|
| 134 | authuser = nil; |
|---|
| 135 | authpass = nil; |
|---|
| 136 | [super dealloc]; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /* begin class methods */ |
|---|
| 140 | - (NSString *) makeNonce { |
|---|
| 141 | NSDate *timestamp = [NSDate date]; |
|---|
| 142 | NSString *nonce = [[NSString stringWithFormat:@"%@:%@", [timestamp descriptionWithCalendarFormat:@"%Y-%m-%dT%H:%M:%SZ" timeZone:nil locale:nil], @"private"] stringUsingSHA1HexadecimalHash]; |
|---|
| 143 | return [NSString stringWithFormat:@"%@ %@", [timestamp descriptionWithCalendarFormat:@"%Y-%m-%dT%H:%M:%SZ" timeZone:nil locale:nil], nonce]; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | - (void) makerequest:(NSString *) reqtype { |
|---|
| 147 | // NSLog(@"Making request to url '%@'\n", apiurl); |
|---|
| 148 | NSURL *aURL = [NSURL URLWithString:apiurl]; |
|---|
| 149 | [aURL URLHandleUsingCache:NO]; |
|---|
| 150 | NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; |
|---|
| 151 | [request addValue:[aURL host] forHTTPHeaderField:@"Host"]; |
|---|
| 152 | [request addValue:@"Fence/0.8" forHTTPHeaderField:@"User-Agent"]; |
|---|
| 153 | // set auth if we need it. |
|---|
| 154 | if ([self requiresAuth]) { |
|---|
| 155 | // NSLog(@" XXX Auth required\n"); |
|---|
| 156 | NSString *login = [self authuser]; |
|---|
| 157 | NSString *password = [self authpass]; |
|---|
| 158 | NSString *nonce = [self makeNonce]; |
|---|
| 159 | NSString *encodedNonce = [nonce stringUsingBase64Encoding]; |
|---|
| 160 | NSString *created = [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%dT%H:%M:%SZ" timeZone:nil locale:nil]; |
|---|
| 161 | NSString *passwordDigest = [[[NSString stringWithFormat:@"%@%@%@", nonce, created, password] stringUsingSHA1RawHash] stringUsingBase64Encoding]; |
|---|
| 162 | [request addValue:@"WSSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"]; |
|---|
| 163 | NSString *wsse = [NSString stringWithFormat:@"UsernameToken Username=\"%@\", PasswordDigest=\"%@\", Created=\"%@\", Nonce=\"%@\"", login, passwordDigest, created, encodedNonce]; |
|---|
| 164 | [request addValue:wsse forHTTPHeaderField:@"X-WSSE"]; |
|---|
| 165 | } |
|---|
| 166 | [request addValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"]; |
|---|
| 167 | [request addValue:[NSString stringWithFormat:@"%d", [atomdata length]] forHTTPHeaderField: @"Content-Length"]; |
|---|
| 168 | [request setHTTPMethod:reqtype]; |
|---|
| 169 | [request setHTTPBody:[atomdata dataUsingEncoding:NSUTF8StringEncoding]]; |
|---|
| 170 | // [request addValue:@"close" forHTTPHeaderField:@"Connection"]; |
|---|
| 171 | NSURLResponse* response; |
|---|
| 172 | NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; |
|---|
| 173 | NSString *blork = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; |
|---|
| 174 | [self setResdata:blork]; |
|---|
| 175 | // NSLog(@"Response: \n%@\n", blork); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | - (NSString *) filename { |
|---|
| 179 | return [dataarg lastPathComponent]; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | @end |
|---|