1
/** **************************************************************************
4
* Copyright 2008 Bryan Ischo <bryan@ischo.com>
6
* This file is part of libs3.
8
* libs3 is free software: you can redistribute it and/or modify it under the
9
* terms of the GNU General Public License as published by the Free Software
10
* Foundation, version 3 of the License.
12
* In addition, as a special exception, the copyright holders give
13
* permission to link the code of this library and its programs with the
14
* OpenSSL library, and distribute linked combinations including the two.
16
* libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
17
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21
* You should have received a copy of the GNU General Public License version 3
22
* along with libs3, in a file named COPYING. If not, see
23
* <http://www.gnu.org/licenses/>.
25
************************************************************************** **/
34
typedef struct XmlCallbackData
38
S3ResponsePropertiesCallback *responsePropertiesCallback;
39
S3ListServiceCallback *listServiceCallback;
40
S3ResponseCompleteCallback *responseCompleteCallback;
43
string_buffer(ownerId, 256);
44
string_buffer(ownerDisplayName, 256);
45
string_buffer(bucketName, 256);
46
string_buffer(creationDate, 128);
50
static S3Status xmlCallback(const char *elementPath, const char *data,
51
int dataLen, void *callbackData)
53
XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
58
if (!strcmp(elementPath, "ListAllMyBucketsResult/Owner/ID")) {
59
string_buffer_append(cbData->ownerId, data, dataLen, fit);
61
else if (!strcmp(elementPath,
62
"ListAllMyBucketsResult/Owner/DisplayName")) {
63
string_buffer_append(cbData->ownerDisplayName, data, dataLen, fit);
65
else if (!strcmp(elementPath,
66
"ListAllMyBucketsResult/Buckets/Bucket/Name")) {
67
string_buffer_append(cbData->bucketName, data, dataLen, fit);
71
"ListAllMyBucketsResult/Buckets/Bucket/CreationDate")) {
72
string_buffer_append(cbData->creationDate, data, dataLen, fit);
76
if (!strcmp(elementPath, "ListAllMyBucketsResult/Buckets/Bucket")) {
77
// Parse date. Assume ISO-8601 date format.
78
time_t creationDate = parseIso8601Time(cbData->creationDate);
80
// Make the callback - a bucket just finished
81
S3Status status = (*(cbData->listServiceCallback))
82
(cbData->ownerId, cbData->ownerDisplayName,
83
cbData->bucketName, creationDate, cbData->callbackData);
85
string_buffer_initialize(cbData->bucketName);
86
string_buffer_initialize(cbData->creationDate);
92
/* Avoid compiler error about variable set but not used */
99
static S3Status propertiesCallback
100
(const S3ResponseProperties *responseProperties, void *callbackData)
102
XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
104
return (*(cbData->responsePropertiesCallback))
105
(responseProperties, cbData->callbackData);
109
static S3Status dataCallback(int bufferSize, const char *buffer,
112
XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
114
return simplexml_add(&(cbData->simpleXml), buffer, bufferSize);
118
static void completeCallback(S3Status requestStatus,
119
const S3ErrorDetails *s3ErrorDetails,
122
XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
124
(*(cbData->responseCompleteCallback))
125
(requestStatus, s3ErrorDetails, cbData->callbackData);
127
simplexml_deinitialize(&(cbData->simpleXml));
133
void S3_list_service(S3Protocol protocol, const char *accessKeyId,
134
const char *secretAccessKey, const char *hostName,
135
S3RequestContext *requestContext,
136
const S3ListServiceHandler *handler, void *callbackData)
138
// Create and set up the callback data
139
XmlCallbackData *data =
140
(XmlCallbackData *) malloc(sizeof(XmlCallbackData));
142
(*(handler->responseHandler.completeCallback))
143
(S3StatusOutOfMemory, 0, callbackData);
147
simplexml_initialize(&(data->simpleXml), &xmlCallback, data);
149
data->responsePropertiesCallback =
150
handler->responseHandler.propertiesCallback;
151
data->listServiceCallback = handler->listServiceCallback;
152
data->responseCompleteCallback = handler->responseHandler.completeCallback;
153
data->callbackData = callbackData;
155
string_buffer_initialize(data->ownerId);
156
string_buffer_initialize(data->ownerDisplayName);
157
string_buffer_initialize(data->bucketName);
158
string_buffer_initialize(data->creationDate);
160
// Set up the RequestParams
161
RequestParams params =
163
HttpRequestTypeGET, // httpRequestType
164
{ hostName, // hostName
166
protocol, // protocol
167
S3UriStylePath, // uriStyle
168
accessKeyId, // accessKeyId
169
secretAccessKey }, // secretAccessKey
173
0, // copySourceBucketName
178
0, // requestProperties
179
&propertiesCallback, // propertiesCallback
181
0, // toS3CallbackTotalSize
182
&dataCallback, // fromS3Callback
183
&completeCallback, // completeCallback
187
// Perform the request
188
request_perform(¶ms, requestContext);