service.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /** **************************************************************************
  2. * service.c
  3. *
  4. * Copyright 2008 Bryan Ischo <[email protected]>
  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 Lesser General Public License as published by the Free
  10. * Software 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 Lesser General Public License
  22. * version 3 along with libs3, in a file named COPYING. If not, see
  23. * <https://www.gnu.org/licenses/>.
  24. *
  25. ************************************************************************** **/
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include "request.h"
  31. typedef struct XmlCallbackData
  32. {
  33. SimpleXml simpleXml;
  34. S3ResponsePropertiesCallback *responsePropertiesCallback;
  35. S3ListServiceCallback *listServiceCallback;
  36. S3ResponseCompleteCallback *responseCompleteCallback;
  37. void *callbackData;
  38. string_buffer(ownerId, 256);
  39. string_buffer(ownerDisplayName, 256);
  40. string_buffer(bucketName, 256);
  41. string_buffer(creationDate, 128);
  42. } XmlCallbackData;
  43. static S3Status xmlCallback(const char *elementPath, const char *data,
  44. int dataLen, void *callbackData)
  45. {
  46. XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
  47. int fit;
  48. if (data) {
  49. if (!strcmp(elementPath, "ListAllMyBucketsResult/Owner/ID")) {
  50. string_buffer_append(cbData->ownerId, data, dataLen, fit);
  51. }
  52. else if (!strcmp(elementPath,
  53. "ListAllMyBucketsResult/Owner/DisplayName")) {
  54. string_buffer_append(cbData->ownerDisplayName, data, dataLen, fit);
  55. }
  56. else if (!strcmp(elementPath,
  57. "ListAllMyBucketsResult/Buckets/Bucket/Name")) {
  58. string_buffer_append(cbData->bucketName, data, dataLen, fit);
  59. }
  60. else if (!strcmp
  61. (elementPath,
  62. "ListAllMyBucketsResult/Buckets/Bucket/CreationDate")) {
  63. string_buffer_append(cbData->creationDate, data, dataLen, fit);
  64. }
  65. }
  66. else {
  67. if (!strcmp(elementPath, "ListAllMyBucketsResult/Buckets/Bucket")) {
  68. // Parse date. Assume ISO-8601 date format.
  69. time_t creationDate = parseIso8601Time(cbData->creationDate);
  70. // Make the callback - a bucket just finished
  71. S3Status status = (*(cbData->listServiceCallback))
  72. (cbData->ownerId, cbData->ownerDisplayName,
  73. cbData->bucketName, creationDate, cbData->callbackData);
  74. string_buffer_initialize(cbData->bucketName);
  75. string_buffer_initialize(cbData->creationDate);
  76. return status;
  77. }
  78. }
  79. /* Avoid compiler error about variable set but not used */
  80. (void) fit;
  81. return S3StatusOK;
  82. }
  83. static S3Status propertiesCallback
  84. (const S3ResponseProperties *responseProperties, void *callbackData)
  85. {
  86. XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
  87. return (*(cbData->responsePropertiesCallback))
  88. (responseProperties, cbData->callbackData);
  89. }
  90. static S3Status dataCallback(int bufferSize, const char *buffer,
  91. void *callbackData)
  92. {
  93. XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
  94. return simplexml_add(&(cbData->simpleXml), buffer, bufferSize);
  95. }
  96. static void completeCallback(S3Status requestStatus,
  97. const S3ErrorDetails *s3ErrorDetails,
  98. void *callbackData)
  99. {
  100. XmlCallbackData *cbData = (XmlCallbackData *) callbackData;
  101. (*(cbData->responseCompleteCallback))
  102. (requestStatus, s3ErrorDetails, cbData->callbackData);
  103. simplexml_deinitialize(&(cbData->simpleXml));
  104. free(cbData);
  105. }
  106. void S3_list_service(S3Protocol protocol, const char *accessKeyId,
  107. const char *secretAccessKey, const char *securityToken,
  108. const char *hostName, const char *authRegion,
  109. int maxkeys, // WINSCP
  110. S3RequestContext *requestContext,
  111. int timeoutMs,
  112. const S3ListServiceHandler *handler, void *callbackData)
  113. {
  114. char queryParams[64]; // WINSCP
  115. queryParams[0] = '\0';
  116. // Create and set up the callback data
  117. XmlCallbackData *data =
  118. (XmlCallbackData *) malloc(sizeof(XmlCallbackData));
  119. if (!data) {
  120. (*(handler->responseHandler.completeCallback))
  121. (S3StatusOutOfMemory, 0, callbackData);
  122. return;
  123. }
  124. simplexml_initialize(&(data->simpleXml), &xmlCallback, data);
  125. data->responsePropertiesCallback =
  126. handler->responseHandler.propertiesCallback;
  127. data->listServiceCallback = handler->listServiceCallback;
  128. data->responseCompleteCallback = handler->responseHandler.completeCallback;
  129. data->callbackData = callbackData;
  130. string_buffer_initialize(data->ownerId);
  131. string_buffer_initialize(data->ownerDisplayName);
  132. string_buffer_initialize(data->bucketName);
  133. string_buffer_initialize(data->creationDate);
  134. if (maxkeys) { // WINSCP
  135. snprintf(queryParams, sizeof(queryParams), "max-keys=%d", maxkeys);
  136. }
  137. // Set up the RequestParams
  138. RequestParams params =
  139. {
  140. HttpRequestTypeGET, // httpRequestType
  141. { hostName, // hostName
  142. 0, // bucketName
  143. protocol, // protocol
  144. S3UriStylePath, // uriStyle
  145. accessKeyId, // accessKeyId
  146. secretAccessKey, // secretAccessKey
  147. securityToken, // securityToken
  148. authRegion }, // authRegion
  149. 0, // key
  150. queryParams[0] ? queryParams : 0, // queryParams WINSCP
  151. 0, // subResource
  152. 0, // copySourceBucketName
  153. 0, // copySourceKey
  154. 0, // getConditions
  155. 0, // startByte
  156. 0, // byteCount
  157. 0, // requestProperties
  158. &propertiesCallback, // propertiesCallback
  159. 0, // toS3Callback
  160. 0, // toS3CallbackTotalSize
  161. &dataCallback, // fromS3Callback
  162. &completeCallback, // completeCallback
  163. data, // callbackData
  164. timeoutMs // timeoutMs
  165. };
  166. // Perform the request
  167. request_perform(&params, requestContext);
  168. }