~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/libs3/src/service.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-07-16 09:56:24 UTC
  • mfrom: (0.3.11)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20120716095624-azr2w4hbhei1rxmx
Tags: upstream-0.48
ImportĀ upstreamĀ versionĀ 0.48

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** **************************************************************************
2
 
 * service.c
3
 
 * 
4
 
 * Copyright 2008 Bryan Ischo <bryan@ischo.com>
5
 
 * 
6
 
 * This file is part of libs3.
7
 
 * 
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.
11
 
 *
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.
15
 
 *
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
19
 
 * details.
20
 
 *
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/>.
24
 
 *
25
 
 ************************************************************************** **/
26
 
 
27
 
#include <ctype.h>
28
 
#include <stdlib.h>
29
 
#include <string.h>
30
 
#include <time.h>
31
 
#include "request.h"
32
 
 
33
 
 
34
 
typedef struct XmlCallbackData
35
 
{
36
 
    SimpleXml simpleXml;
37
 
    
38
 
    S3ResponsePropertiesCallback *responsePropertiesCallback;
39
 
    S3ListServiceCallback *listServiceCallback;
40
 
    S3ResponseCompleteCallback *responseCompleteCallback;
41
 
    void *callbackData;
42
 
 
43
 
    string_buffer(ownerId, 256);
44
 
    string_buffer(ownerDisplayName, 256);
45
 
    string_buffer(bucketName, 256);
46
 
    string_buffer(creationDate, 128);
47
 
} XmlCallbackData;
48
 
 
49
 
 
50
 
static S3Status xmlCallback(const char *elementPath, const char *data,
51
 
                            int dataLen, void *callbackData)
52
 
{
53
 
    XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
54
 
 
55
 
    int fit;
56
 
 
57
 
    if (data) {
58
 
        if (!strcmp(elementPath, "ListAllMyBucketsResult/Owner/ID")) {
59
 
            string_buffer_append(cbData->ownerId, data, dataLen, fit);
60
 
        }
61
 
        else if (!strcmp(elementPath, 
62
 
                         "ListAllMyBucketsResult/Owner/DisplayName")) {
63
 
            string_buffer_append(cbData->ownerDisplayName, data, dataLen, fit);
64
 
        }
65
 
        else if (!strcmp(elementPath, 
66
 
                         "ListAllMyBucketsResult/Buckets/Bucket/Name")) {
67
 
            string_buffer_append(cbData->bucketName, data, dataLen, fit);
68
 
        }
69
 
        else if (!strcmp
70
 
                 (elementPath, 
71
 
                  "ListAllMyBucketsResult/Buckets/Bucket/CreationDate")) {
72
 
            string_buffer_append(cbData->creationDate, data, dataLen, fit);
73
 
        }
74
 
    }
75
 
    else {
76
 
        if (!strcmp(elementPath, "ListAllMyBucketsResult/Buckets/Bucket")) {
77
 
            // Parse date.  Assume ISO-8601 date format.
78
 
            time_t creationDate = parseIso8601Time(cbData->creationDate);
79
 
 
80
 
            // Make the callback - a bucket just finished
81
 
            S3Status status = (*(cbData->listServiceCallback))
82
 
                (cbData->ownerId, cbData->ownerDisplayName,
83
 
                 cbData->bucketName, creationDate, cbData->callbackData);
84
 
 
85
 
            string_buffer_initialize(cbData->bucketName);
86
 
            string_buffer_initialize(cbData->creationDate);
87
 
 
88
 
            return status;
89
 
        }
90
 
    }
91
 
 
92
 
    /* Avoid compiler error about variable set but not used */
93
 
    (void) fit;
94
 
 
95
 
    return S3StatusOK;
96
 
}
97
 
 
98
 
 
99
 
static S3Status propertiesCallback
100
 
    (const S3ResponseProperties *responseProperties, void *callbackData)
101
 
{
102
 
    XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
103
 
    
104
 
    return (*(cbData->responsePropertiesCallback))
105
 
        (responseProperties, cbData->callbackData);
106
 
}
107
 
 
108
 
 
109
 
static S3Status dataCallback(int bufferSize, const char *buffer,
110
 
                             void *callbackData)
111
 
{
112
 
    XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
113
 
 
114
 
    return simplexml_add(&(cbData->simpleXml), buffer, bufferSize);
115
 
}
116
 
 
117
 
 
118
 
static void completeCallback(S3Status requestStatus,
119
 
                             const S3ErrorDetails *s3ErrorDetails,
120
 
                             void *callbackData)
121
 
{
122
 
    XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
123
 
 
124
 
    (*(cbData->responseCompleteCallback))
125
 
        (requestStatus, s3ErrorDetails, cbData->callbackData);
126
 
 
127
 
    simplexml_deinitialize(&(cbData->simpleXml));
128
 
 
129
 
    free(cbData);
130
 
}
131
 
 
132
 
 
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)
137
 
{
138
 
    // Create and set up the callback data
139
 
    XmlCallbackData *data = 
140
 
        (XmlCallbackData *) malloc(sizeof(XmlCallbackData));
141
 
    if (!data) {
142
 
        (*(handler->responseHandler.completeCallback))
143
 
            (S3StatusOutOfMemory, 0, callbackData);
144
 
        return;
145
 
    }
146
 
 
147
 
    simplexml_initialize(&(data->simpleXml), &xmlCallback, data);
148
 
 
149
 
    data->responsePropertiesCallback =
150
 
        handler->responseHandler.propertiesCallback;
151
 
    data->listServiceCallback = handler->listServiceCallback;
152
 
    data->responseCompleteCallback = handler->responseHandler.completeCallback;
153
 
    data->callbackData = callbackData;
154
 
 
155
 
    string_buffer_initialize(data->ownerId);
156
 
    string_buffer_initialize(data->ownerDisplayName);
157
 
    string_buffer_initialize(data->bucketName);
158
 
    string_buffer_initialize(data->creationDate);
159
 
    
160
 
    // Set up the RequestParams
161
 
    RequestParams params =
162
 
    {
163
 
        HttpRequestTypeGET,                           // httpRequestType
164
 
        { hostName,                                   // hostName
165
 
          0,                                          // bucketName
166
 
          protocol,                                   // protocol
167
 
          S3UriStylePath,                             // uriStyle
168
 
          accessKeyId,                                // accessKeyId
169
 
          secretAccessKey },                          // secretAccessKey
170
 
        0,                                            // key
171
 
        0,                                            // queryParams
172
 
        0,                                            // subResource
173
 
        0,                                            // copySourceBucketName
174
 
        0,                                            // copySourceKey
175
 
        0,                                            // getConditions
176
 
        0,                                            // startByte
177
 
        0,                                            // byteCount
178
 
        0,                                            // requestProperties
179
 
        &propertiesCallback,                          // propertiesCallback
180
 
        0,                                            // toS3Callback
181
 
        0,                                            // toS3CallbackTotalSize
182
 
        &dataCallback,                                // fromS3Callback
183
 
        &completeCallback,                            // completeCallback
184
 
        data                                          // callbackData
185
 
    };
186
 
 
187
 
    // Perform the request
188
 
    request_perform(&params, requestContext);
189
 
}
190
 
 
191