request.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /** **************************************************************************
  2. * request.h
  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. * <http://www.gnu.org/licenses/>.
  24. *
  25. ************************************************************************** **/
  26. #ifndef REQUEST_H
  27. #define REQUEST_H
  28. #include "libs3.h"
  29. #include "error_parser.h"
  30. #include "response_headers_handler.h"
  31. #include "util.h"
  32. // Describes a type of HTTP request (these are our supported HTTP "verbs")
  33. typedef enum
  34. {
  35. HttpRequestTypeGET,
  36. HttpRequestTypeHEAD,
  37. HttpRequestTypePUT,
  38. HttpRequestTypeCOPY,
  39. HttpRequestTypeDELETE,
  40. HttpRequestTypePOST,
  41. HttpRequestTypeInvalid
  42. } HttpRequestType;
  43. // This completely describes a request. A RequestParams is not required to be
  44. // allocated from the heap and its lifetime is not assumed to extend beyond
  45. // the lifetime of the function to which it has been passed.
  46. typedef struct RequestParams
  47. {
  48. // Request type, affects the HTTP verb used
  49. HttpRequestType httpRequestType;
  50. // Bucket context for request
  51. S3BucketContext bucketContext;
  52. // Key, if any
  53. const char *key;
  54. // Query params - ready to append to URI (i.e. ?p1=v1?p2=v2)
  55. const char *queryParams;
  56. // sub resource, like ?acl, ?location, ?torrent, ?logging
  57. const char *subResource;
  58. // If this is a copy operation, this gives the source bucket
  59. const char *copySourceBucketName;
  60. // If this is a copy operation, this gives the source key
  61. const char *copySourceKey;
  62. // Get conditions
  63. const S3GetConditions *getConditions;
  64. // Start byte
  65. size_t startByte;
  66. // Byte count
  67. size_t byteCount;
  68. // Put properties
  69. const S3PutProperties *putProperties;
  70. // Callback to be made when headers are available. Might not be called.
  71. S3ResponsePropertiesCallback *propertiesCallback;
  72. // Callback to be made to supply data to send to S3. Might not be called.
  73. S3PutObjectDataCallback *toS3Callback;
  74. // Number of bytes total that readCallback will supply
  75. int64_t toS3CallbackTotalSize;
  76. // Callback to be made that supplies data read from S3.
  77. // Might not be called.
  78. S3GetObjectDataCallback *fromS3Callback;
  79. // Callback to be made when request is complete. This will *always* be
  80. // called.
  81. S3ResponseCompleteCallback *completeCallback;
  82. // Data passed to the callbacks
  83. void *callbackData;
  84. // Request timeout. If 0, no timeout will be enforced
  85. int timeoutMs;
  86. } RequestParams;
  87. // This is the stuff associated with a request that needs to be on the heap
  88. // (and thus live while a curl_multi is in use).
  89. typedef struct Request
  90. {
  91. // These put the request on a doubly-linked list of requests in a
  92. // request context, *if* the request is in a request context (else these
  93. // will both be 0)
  94. struct Request *prev, *next;
  95. // The status of this Request, as will be reported to the user via the
  96. // complete callback
  97. S3Status status;
  98. // The HTTP code returned by the S3 server, if it is known. Would rather
  99. // not have to keep track of this but S3 doesn't always indicate its
  100. // errors the same way
  101. int httpResponseCode;
  102. // The HTTP headers to use for the curl request
  103. struct curl_slist *headers;
  104. // The CURL structure driving the request
  105. CURL *curl;
  106. // libcurl requires that the uri be stored outside of the curl handle
  107. char uri[MAX_URI_SIZE + 1];
  108. // Callback to be made when headers are available. Might not be called.
  109. S3ResponsePropertiesCallback *propertiesCallback;
  110. // Callback to be made to supply data to send to S3. Might not be called.
  111. S3PutObjectDataCallback *toS3Callback;
  112. // Number of bytes total that readCallback has left to supply
  113. int64_t toS3CallbackBytesRemaining;
  114. // Callback to be made that supplies data read from S3.
  115. // Might not be called.
  116. S3GetObjectDataCallback *fromS3Callback;
  117. // Callback to be made when request is complete. This will *always* be
  118. // called.
  119. S3ResponseCompleteCallback *completeCallback;
  120. // Data passed to the callbacks
  121. void *callbackData;
  122. // Handler of response headers
  123. ResponseHeadersHandler responseHeadersHandler;
  124. // This is set to nonzero after the properties callback has been made
  125. int propertiesCallbackMade;
  126. // Parser of errors
  127. ErrorParser errorParser;
  128. } Request;
  129. // Request functions
  130. // ----------------------------------------------------------------------------
  131. // Initialize the API
  132. S3Status request_api_initialize(const char *userAgentInfo, int flags,
  133. const char *hostName);
  134. // Deinitialize the API
  135. void request_api_deinitialize();
  136. // Perform a request; if context is 0, performs the request immediately;
  137. // otherwise, sets it up to be performed by context.
  138. void request_perform(const RequestParams *params, S3RequestContext *context);
  139. // Called by the internal request code or internal request context code when a
  140. // curl has finished the request
  141. void request_finish(Request *request);
  142. // Convert a CURLE code to an S3Status
  143. S3Status request_curl_code_to_status(CURLcode code);
  144. #endif /* REQUEST_H */