Skip to content
Browse files

removed prepareToSign

  • Loading branch information...
1 parent bcc8301 commit b14170a3e7ea2e6d65c907901fe53aa1931646b4 @stevedekorte stevedekorte committed Oct 2, 2012
Showing with 37 additions and 20 deletions.
  1. +7 −0 S3Kit/S3Kit/BS3Requestor.h
  2. +30 −20 S3Kit/S3Kit/BS3Requestor.m
View
7 S3Kit/S3Kit/BS3Requestor.h
@@ -20,6 +20,7 @@
{
}
+@property (nonatomic, retain) NSDate *date;
@property (nonatomic, retain) NSString *bucketName;
@property (nonatomic, retain) NSString *resourcePath;
@property (nonatomic, retain) NSString *accessKey;
@@ -38,6 +39,12 @@
// HTTP method of, MD5 hash of the HTTP body, etc.
// so don't change those on the returned object
+- (NSString *)stringToSign;
+- (NSString *)authorizationHeader;
+
- (NSMutableURLRequest *)composedRequest;
+- (NSMutableURLRequest *)signedRequest;
+
+- (void)show;
@end
View
50 S3Kit/S3Kit/BS3Requestor.m
@@ -25,6 +25,7 @@ @implementation BS3Requestor
@synthesize requestClass;
@synthesize usesSSL;
+@synthesize date;
- (id)init
{
@@ -33,6 +34,8 @@ - (id)init
parameters = [NSMutableDictionary dictionary];
HTTPMethod = @"GET";
requestClass = [NSMutableURLRequest class];
+ date = [NSDate date];
+ [allHTTPHeaderFields setObject:[self dateHeader] forKey:@"Date"];
usesSSL = YES;
return self;
}
@@ -91,9 +94,12 @@ - (NSString *)composedURLString
{
NSString *scheme = usesSSL ? @"https" : @"http";
- NSString *urlString = [NSString stringWithFormat:@"%@://s3.amazonaws.com/%@%@",
- scheme, bucketName, resourcePath];
+ //NSString *urlString = [NSString stringWithFormat:@"%@://s3.amazonaws.com/%@%@",
+ // scheme, bucketName, resourcePath];
+ NSString *urlString = [NSString stringWithFormat:@"%@://%@.s3.amazonaws.com%@",
+ scheme, bucketName, resourcePath];
+
NSString *parameterString = [self stringForParameters];
if (parameterString.length > 0)
@@ -107,14 +113,14 @@ - (NSString *)composedURLString
- (NSString *)dateHeader
{
- return [BS3Requestor dateHeaderForDate:[NSDate date]];
+ return [BS3Requestor dateHeaderForDate:date];
}
- (NSString *)stringToSign
{
NSString *contentMd5 = [self.allHTTPHeaderFields objectForKey:@"Content-Md5"];
NSString *contentType = [self.allHTTPHeaderFields objectForKey:@"Content-Type"];
- NSString *date = [self.allHTTPHeaderFields objectForKey:@"Date"];
+ NSString *dateString = [self.allHTTPHeaderFields objectForKey:@"Date"];
NSString *result = [NSString stringWithFormat:@""
@"%@\n" // HTTP Method
@@ -126,7 +132,7 @@ - (NSString *)stringToSign
self.HTTPMethod,
contentMd5 ? contentMd5 : @"",
contentType ? contentType : @"",
- date,
+ dateString,
@"",
[NSString stringWithFormat:@"/%@%@", self.bucketName, self.resourcePath]];
@@ -140,22 +146,20 @@ - (NSString *)authorizationHeader
return [NSString stringWithFormat:@"AWS %@:%@", accessKey, authToken];
}
-- (NSMutableURLRequest *)composedRequest
+- (void)setHTTPBody:(NSData *)body
{
- // setup remaining attributes
-
- [allHTTPHeaderFields setObject:[self dateHeader] forKey:@"Date"];
-
- if (HTTPBody)
- {
- [allHTTPHeaderFields setObject:[HTTPBody md5Signature] forKey:@"Content-Md5"];
- }
+ HTTPBody = body;
+ [allHTTPHeaderFields setObject:[HTTPBody md5Signature] forKey:@"Content-Md5"];
+}
+
+- (NSMutableURLRequest *)composedRequest
+{
NSString *urlString = [self composedURLString];
NSMutableURLRequest *request = [[requestClass alloc] initWithURL:[NSURL URLWithString:urlString]];
// copy attributes to request
-
+
request.HTTPMethod = HTTPMethod;
for (NSString *key in [allHTTPHeaderFields allKeys])
@@ -168,19 +172,25 @@ - (NSMutableURLRequest *)composedRequest
request.HTTPBody = HTTPBody;
}
- // sign the header
-
+ return request;
+}
+
+- (NSMutableURLRequest *)signedRequest
+{
+ NSMutableURLRequest *request = [self composedRequest];
[request setValue:[self authorizationHeader] forHTTPHeaderField:@"Authorization"];
return request;
}
- (void)show
{
- printf("bucketName = %s\n", [bucketName UTF8String]);
+ printf("bucketName = %s\n", [bucketName UTF8String]);
printf("resourcePath = %s\n", [resourcePath UTF8String]);
- printf("HTTPMethod = %s\n", [self.HTTPMethod UTF8String]);
- printf("parameters = %s\n", [[parameters description] UTF8String]);
+ printf("composedURLString = %s\n", [[self composedURLString] UTF8String]);
+ printf("HTTPMethod = %s\n", [self.HTTPMethod UTF8String]);
+ printf("parameters = %s\n", [[parameters description] UTF8String]);
printf("allHTTPHeaderFields = %s\n", [[allHTTPHeaderFields description] UTF8String]);
+ printf("stringToSign = [[%s]]\n", [[self stringToSign] UTF8String]);
}
@end

0 comments on commit b14170a

Please sign in to comment.
Something went wrong with that request. Please try again.