Toggle navigation
Toggle navigation
This project
Loading...
Sign in
iOS
/
asi-http-request
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Ben Copsey
2009-07-13 15:29:28 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3ab29ef07e4d02f599f00c49afd8f374f501353e
3ab29ef0
1 parent
70389c80
Added tests for S3 GET/PUT/DELETE
Fix mime-type function
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
57 additions
and
13 deletions
Classes/ASIHTTPRequest.m
Classes/ASIS3Request.h
Classes/ASIS3Request.m
Classes/Tests/ASIS3RequestTests.h
Classes/Tests/ASIS3RequestTests.m
Classes/ASIHTTPRequest.m
View file @
3ab29ef
...
...
@@ -390,8 +390,7 @@ static NSError *ASITooMuchRedirectionError;
for
(
header
in
headers
)
{
CFHTTPMessageSetHeaderFieldValue
(
request
,
(
CFStringRef
)
header
,
(
CFStringRef
)[
requestHeaders
objectForKey
:
header
]);
}
// If this is a post request and we have data in memory send, add it to the request
if
([
self
postBody
])
{
CFHTTPMessageSetBody
(
request
,
(
CFDataRef
)
postBody
);
...
...
Classes/ASIS3Request.h
View file @
3ab29ef
...
...
@@ -15,7 +15,7 @@ extern NSString *const ASIS3AccessPolicyPrivate; // This is the default in S3 wh
extern
NSString
*
const
ASIS3AccessPolicyPublicRead
;
extern
NSString
*
const
ASIS3AccessPolicyPublicReadWrote
;
extern
NSString
*
const
ASIS3AccessPolicyAuthenticatedRead
;
bug
report
in
lighthouse
@interface
ASIS3Request
:
ASIHTTPRequest
{
// Your S3 access key. Set it on the request, or set it globally using [ASIS3Request setSharedAccessKey:]
...
...
Classes/ASIS3Request.m
View file @
3ab29ef
...
...
@@ -37,7 +37,7 @@ static NSString *sharedSecretAccessKey = nil;
+
(
id
)
requestWithBucket
:
(
NSString
*
)
bucket
path
:
(
NSString
*
)
path
{
ASIS3Request
*
request
=
[[[
ASIS3Request
alloc
]
initWithURL
:[
NSURL
URLWithString
:[
NSString
stringWithFormat
:
@"http://
%@.s3.amazonaws.com
/%@"
,
bucket
,
path
]]]
autorelease
];
ASIS3Request
*
request
=
[[[
ASIS3Request
alloc
]
initWithURL
:[
NSURL
URLWithString
:[
NSString
stringWithFormat
:
@"http://
s3.amazonaws.com/%@
/%@"
,
bucket
,
path
]]]
autorelease
];
[
request
setBucket
:
bucket
];
[
request
setPath
:
path
];
return
request
;
...
...
@@ -49,14 +49,14 @@ static NSString *sharedSecretAccessKey = nil;
[
request
setPostBodyFilePath
:
filePath
];
[
request
setShouldStreamPostDataFromDisk
:
YES
];
[
request
setRequestMethod
:
@"PUT"
];
[
request
setMimeType
:[
ASIS3Request
mimeTypeForFileAtPath
:
p
ath
]];
[
request
setMimeType
:[
ASIS3Request
mimeTypeForFileAtPath
:
fileP
ath
]];
return
request
;
}
+
(
id
)
listRequestWithBucket
:
(
NSString
*
)
bucket
prefix
:
(
NSString
*
)
prefix
maxResults
:
(
int
)
maxResults
marker
:
(
NSString
*
)
marker
{
ASIS3Request
*
request
=
[[[
ASIS3Request
alloc
]
initWithURL
:[
NSURL
URLWithString
:[
NSString
stringWithFormat
:
@"http://
%@.s3.amazonaws.com/
?prefix=/%@&max-keys=%hi&marker=%@"
,
bucket
,
prefix
,
maxResults
,
marker
]]]
autorelease
];
ASIS3Request
*
request
=
[[[
ASIS3Request
alloc
]
initWithURL
:[
NSURL
URLWithString
:[
NSString
stringWithFormat
:
@"http://
s3.amazonaws.com/%@
?prefix=/%@&max-keys=%hi&marker=%@"
,
bucket
,
prefix
,
maxResults
,
marker
]]]
autorelease
];
[
request
setBucket
:
bucket
];
return
request
;
}
...
...
@@ -74,12 +74,17 @@ static NSString *sharedSecretAccessKey = nil;
[
task
setLaunchPath
:
@"/usr/bin/file"
];
[
task
setArguments
:[
NSMutableArray
arrayWithObjects
:
@"-Ib"
,
path
,
nil
]];
NSPipe
*
p
ipe
=
[
NSPipe
pipe
];
[
task
setStandardOutput
:
p
ipe
];
NSPipe
*
outputP
ipe
=
[
NSPipe
pipe
];
[
task
setStandardOutput
:
outputP
ipe
];
NSFileHandle
*
file
=
[
p
ipe
fileHandleForReading
];
NSFileHandle
*
file
=
[
outputP
ipe
fileHandleForReading
];
[
task
launch
];
[
task
waitUntilExit
];
if
([
task
terminationStatus
]
!=
0
)
{
return
@"application/octet-stream"
;
}
NSString
*
mimeTypeString
=
[[[
NSString
alloc
]
initWithData
:[
file
readDataToEndOfFile
]
encoding
:
NSUTF8StringEncoding
]
autorelease
];
return
[[
mimeTypeString
componentsSeparatedByString
:
@";"
]
objectAtIndex
:
0
];
...
...
@@ -89,7 +94,7 @@ static NSString *sharedSecretAccessKey = nil;
-
(
void
)
setDate
:
(
NSDate
*
)
date
{
NSDateFormatter
*
dateFormatter
=
[[[
NSDateFormatter
alloc
]
init
]
autorelease
];
[
dateFormatter
setDateFormat
:
@"EEE, d MMM yyyy HH:mm:ss
zzzz
"
];
[
dateFormatter
setDateFormat
:
@"EEE, d MMM yyyy HH:mm:ss
Z
"
];
[
self
setDateString
:[
dateFormatter
stringFromDate
:
date
]];
}
...
...
@@ -135,10 +140,10 @@ static NSString *sharedSecretAccessKey = nil;
[
self
addRequestHeader
:
@"Authorization"
value
:
authorizationString
];
}
-
(
void
)
startRequest
-
(
void
)
main
{
[
self
generateS3Headers
];
[
super
startRequest
];
[
super
main
];
}
#pragma mark Shared access keys
...
...
Classes/Tests/ASIS3RequestTests.h
View file @
3ab29ef
...
...
@@ -17,5 +17,6 @@
}
-
(
void
)
testAuthenticationHeaderGeneration
;
//- (void)testREST;
@end
...
...
Classes/Tests/ASIS3RequestTests.m
View file @
3ab29ef
...
...
@@ -79,10 +79,49 @@
[
request
generateS3Headers
];
success
=
[[[
request
requestHeaders
]
valueForKey
:
@"Authorization"
]
isEqualToString
:
@"AWS 0PN5J17HBGZHT7JJ3X82:dxhSBHoI6eVSPcXJqEghlUzZMnY="
];
GHAssertTrue
(
success
,
@"Failed to generate the correct authorisation header for a list request"
);
}
// To run this test, uncomment and fill in your S3 access details
/*
- (void)testREST
{
NSString *secretAccessKey = @"my-secret-key";
NSString *accessKey = @"my-access-key";
NSString *bucket = @"bucket-name";
NSString *path = @"path/to/file";
// Create the fle
NSString *text = @"This is my content";
NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
[[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
// PUT the file
ASIS3Request *request = [ASIS3Request PUTRequestForFile:filePath withBucket:bucket path:path];
[request setRequestMethod:@"PUT"];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request start];
BOOL success = [[request responseString] isEqualToString:@""];
GHAssertTrue(success,@"Failed to PUT a file to S3");
// GET the file
request = [ASIS3Request requestWithBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setAccessKey:accessKey];
[request start];
success = [[request responseString] isEqualToString:@"This is my content"];
GHAssertTrue(success,@"Failed to GET the correct data from S3");
// DELETE the file
request = [ASIS3Request requestWithBucket:bucket path:path];
[request setSecretAccessKey:secretAccessKey];
[request setRequestMethod:@"DELETE"];
[request setAccessKey:accessKey];
[request start];
success = [[request responseString] isEqualToString:@""];
GHAssertTrue(success,@"Failed to DELETE the file from S3");
}
*/
@end
...
...
Please
register
or
login
to post a comment