2 * Copyright (c) 2008,2009 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 * - collect speed statistics
27 /* use a relative path here to avoid conflicting with Perl's config.h. */
28 #include "../config/config.h"
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
58 #include <curl/curl.h>
60 /* Constant renamed after version 7.10.7 */
61 #ifndef CURLINFO_RESPONSE_CODE
62 #define CURLINFO_RESPONSE_CODE CURLINFO_HTTP_CODE
65 /* We don't need OpenSSL's kerberos support, and it's broken in
67 #define OPENSSL_NO_KRB5
69 #ifdef HAVE_OPENSSL_HMAC_H
70 # include <openssl/hmac.h>
72 # ifdef HAVE_CRYPTO_HMAC_H
73 # include <crypto/hmac.h>
81 #include <openssl/err.h>
82 #include <openssl/ssl.h>
83 #include <openssl/md5.h>
85 /* Maximum key length as specified in the S3 documentation
86 * (*excluding* null terminator) */
87 #define S3_MAX_KEY_LENGTH 1024
89 #define AMAZON_SECURITY_HEADER "x-amz-security-token"
90 #define AMAZON_BUCKET_CONF_TEMPLATE "\
91 <CreateBucketConfiguration>\n\
92 <LocationConstraint>%s</LocationConstraint>\n\
93 </CreateBucketConfiguration>"
95 #define AMAZON_WILDCARD_LOCATION "*"
97 /* parameters for exponential backoff in the face of retriable errors */
100 #define EXPONENTIAL_BACKOFF_START_USEC G_USEC_PER_SEC/100
101 /* double at each retry */
102 #define EXPONENTIAL_BACKOFF_BASE 2
103 /* retry 14 times (for a total of about 3 minutes spent waiting) */
104 #define EXPONENTIAL_BACKOFF_MAX_RETRIES 14
106 /* general "reasonable size" parameters */
107 #define MAX_ERROR_RESPONSE_LEN (100*1024)
109 /* Results which should always be retried */
110 #define RESULT_HANDLING_ALWAYS_RETRY \
111 { 400, S3_ERROR_RequestTimeout, 0, S3_RESULT_RETRY }, \
112 { 409, S3_ERROR_OperationAborted, 0, S3_RESULT_RETRY }, \
113 { 412, S3_ERROR_PreconditionFailed, 0, S3_RESULT_RETRY }, \
114 { 500, S3_ERROR_InternalError, 0, S3_RESULT_RETRY }, \
115 { 501, S3_ERROR_NotImplemented, 0, S3_RESULT_RETRY }, \
116 { 0, 0, CURLE_COULDNT_CONNECT, S3_RESULT_RETRY }, \
117 { 0, 0, CURLE_COULDNT_RESOLVE_HOST, S3_RESULT_RETRY }, \
118 { 0, 0, CURLE_PARTIAL_FILE, S3_RESULT_RETRY }, \
119 { 0, 0, CURLE_OPERATION_TIMEOUTED, S3_RESULT_RETRY }, \
120 { 0, 0, CURLE_SEND_ERROR, S3_RESULT_RETRY }, \
121 { 0, 0, CURLE_RECV_ERROR, S3_RESULT_RETRY }, \
122 { 0, 0, CURLE_GOT_NOTHING, S3_RESULT_RETRY }
125 * Data structures and associated functions
129 /* (all strings in this struct are freed by s3_free()) */
135 char *bucket_location;
144 guint64 max_send_speed;
145 guint64 max_recv_speed;
147 /* information from the last request */
149 guint last_response_code;
150 s3_error_code_t last_s3_error_code;
151 CURLcode last_curl_code;
152 guint last_num_retries;
153 void *last_response_body;
154 guint last_response_body_size;
159 s3_write_func write_func;
160 s3_reset_func reset_func;
163 gboolean headers_done;
164 gboolean int_write_done;
168 /* Callback function to examine headers one-at-a-time
170 * @note this is the same as CURLOPT_HEADERFUNCTION
172 * @param data: The pointer to read data from
173 * @param size: The size of each "element" of the data buffer in bytes
174 * @param nmemb: The number of elements in the data buffer.
175 * So, the buffer's size is size*nmemb bytes.
176 * @param stream: the header_data (an opaque pointer)
178 * @return The number of bytes written to the buffer or
179 * CURL_WRITEFUNC_PAUSE to pause.
180 * If it's the number of bytes written, it should match the buffer size
182 typedef size_t (*s3_header_func)(void *data, size_t size, size_t nmemb, void *stream);
188 /* (see preprocessor magic in s3.h) */
190 static char * s3_error_code_names[] = {
191 #define S3_ERROR(NAME) #NAME
196 /* Convert an s3 error name to an error code. This function
197 * matches strings case-insensitively, and is appropriate for use
198 * on data from the network.
200 * @param s3_error_code: the error name
201 * @returns: the error code (see constants in s3.h)
203 static s3_error_code_t
204 s3_error_code_from_name(char *s3_error_name);
206 /* Convert an s3 error code to a string
208 * @param s3_error_code: the error code to convert
209 * @returns: statically allocated string
212 s3_error_name_from_code(s3_error_code_t s3_error_code);
218 /* result handling is specified by a static array of result_handling structs,
219 * which match based on response_code (from HTTP) and S3 error code. The result
220 * given for the first match is used. 0 acts as a wildcard for both response_code
221 * and s3_error_code. The list is terminated with a struct containing 0 for both
222 * response_code and s3_error_code; the result for that struct is the default
225 * See RESULT_HANDLING_ALWAYS_RETRY for an example.
228 S3_RESULT_RETRY = -1,
233 typedef struct result_handling {
235 s3_error_code_t s3_error_code;
240 /* Lookup a result in C{result_handling}.
242 * @param result_handling: array of handling specifications
243 * @param response_code: response code from operation
244 * @param s3_error_code: s3 error code from operation, if any
245 * @param curl_code: the CURL error, if any
246 * @returns: the matching result
249 lookup_result(const result_handling_t *result_handling,
251 s3_error_code_t s3_error_code,
255 * Precompiled regular expressions */
256 static regex_t etag_regex, error_name_regex, message_regex, subdomain_regex,
263 /* Check if a string is non-empty
265 * @param str: string to check
266 * @returns: true iff str is non-NULL and not "\0"
268 static gboolean is_non_empty_string(const char *str);
270 /* Construct the URL for an Amazon S3 REST request.
272 * A new string is allocated and returned; it is the responsiblity of the caller.
274 * @param hdl: the S3Handle object
275 * @param verb: capitalized verb for this request ('PUT', 'GET', etc.)
276 * @param bucket: the bucket being accessed, or NULL for none
277 * @param key: the key being accessed, or NULL for none
278 * @param subresource: the sub-resource being accessed (e.g. "acl"), or NULL for none
279 * @param use_subdomain: if TRUE, a subdomain of s3.amazonaws.com will be used
282 build_url(const char *bucket,
284 const char *subresource,
286 gboolean use_subdomain,
289 /* Create proper authorization headers for an Amazon S3 REST
290 * request to C{headers}.
292 * @note: C{X-Amz} headers (in C{headers}) must
294 * - be in alphabetical order
295 * - have no spaces around the colon
296 * (don't yell at me -- see the Amazon Developer Guide)
298 * @param hdl: the S3Handle object
299 * @param verb: capitalized verb for this request ('PUT', 'GET', etc.)
300 * @param bucket: the bucket being accessed, or NULL for none
301 * @param key: the key being accessed, or NULL for none
302 * @param subresource: the sub-resource being accessed (e.g. "acl"), or NULL for none
303 * @param md5_hash: the MD5 hash of the request body, or NULL for none
304 * @param use_subdomain: if TRUE, a subdomain of s3.amazonaws.com will be used
306 static struct curl_slist *
307 authenticate_request(S3Handle *hdl,
311 const char *subresource,
312 const char *md5_hash,
313 gboolean use_subdomain);
317 /* Interpret the response to an S3 operation, assuming CURL completed its request
318 * successfully. This function fills in the relevant C{hdl->last*} members.
320 * @param hdl: The S3Handle object
321 * @param body: the response body
322 * @param body_len: the length of the response body
323 * @param etag: The response's ETag header
324 * @param content_md5: The hex-encoded MD5 hash of the request body,
325 * which will be checked against the response's ETag header.
326 * If NULL, the header is not checked.
327 * If non-NULL, then the body should have the response headers at its beginnning.
328 * @returns: TRUE if the response should be retried (e.g., network error)
331 interpret_response(S3Handle *hdl,
333 char *curl_error_buffer,
337 const char *content_md5);
339 /* Perform an S3 operation. This function handles all of the details
340 * of retryig requests and so on.
342 * The concepts of bucket and keys are defined by the Amazon S3 API.
343 * See: "Components of Amazon S3" - API Version 2006-03-01 pg. 8
345 * Individual sub-resources are defined in several places. In the REST API,
346 * they they are represented by a "flag" in the "query string".
347 * See: "Constructing the CanonicalizedResource Element" - API Version 2006-03-01 pg. 60
349 * @param hdl: the S3Handle object
350 * @param verb: the HTTP request method
351 * @param bucket: the bucket to access, or NULL for none
352 * @param key: the key to access, or NULL for none
353 * @param subresource: the "sub-resource" to request (e.g. "acl") or NULL for none
354 * @param query: the query string to send (not including th initial '?'),
356 * @param read_func: the callback for reading data
357 * Will use s3_empty_read_func if NULL is passed in.
358 * @param read_reset_func: the callback for to reset reading data
359 * @param size_func: the callback to get the number of bytes to upload
360 * @param md5_func: the callback to get the MD5 hash of the data to upload
361 * @param read_data: pointer to pass to the above functions
362 * @param write_func: the callback for writing data.
363 * Will use s3_counter_write_func if NULL is passed in.
364 * @param write_reset_func: the callback for to reset writing data
365 * @param write_data: pointer to pass to C{write_func}
366 * @param progress_func: the callback for progress information
367 * @param progress_data: pointer to pass to C{progress_func}
368 * @param result_handling: instructions for handling the results; see above.
369 * @returns: the result specified by result_handling; details of the response
370 * are then available in C{hdl->last*}
373 perform_request(S3Handle *hdl,
377 const char *subresource,
379 s3_read_func read_func,
380 s3_reset_func read_reset_func,
381 s3_size_func size_func,
382 s3_md5_func md5_func,
384 s3_write_func write_func,
385 s3_reset_func write_reset_func,
387 s3_progress_func progress_func,
388 gpointer progress_data,
389 const result_handling_t *result_handling);
392 * a CURLOPT_WRITEFUNCTION to save part of the response in memory and
393 * call an external function if one was provided.
396 s3_internal_write_func(void *ptr, size_t size, size_t nmemb, void * stream);
399 * a function to reset to our internal buffer
402 s3_internal_reset_func(void * stream);
405 * a CURLOPT_HEADERFUNCTION to save the ETag header only.
408 s3_internal_header_func(void *ptr, size_t size, size_t nmemb, void * stream);
411 compile_regexes(void);
414 * Static function implementations
416 static s3_error_code_t
417 s3_error_code_from_name(char *s3_error_name)
421 if (!s3_error_name) return S3_ERROR_Unknown;
423 /* do a brute-force search through the list, since it's not sorted */
424 for (i = 0; i < S3_ERROR_END; i++) {
425 if (g_strcasecmp(s3_error_name, s3_error_code_names[i]) == 0)
429 return S3_ERROR_Unknown;
433 s3_error_name_from_code(s3_error_code_t s3_error_code)
435 if (s3_error_code >= S3_ERROR_END)
436 s3_error_code = S3_ERROR_Unknown;
438 return s3_error_code_names[s3_error_code];
442 s3_curl_supports_ssl(void)
444 static int supported = -1;
445 if (supported == -1) {
446 #if defined(CURL_VERSION_SSL)
447 curl_version_info_data *info = curl_version_info(CURLVERSION_NOW);
448 if (info->features & CURL_VERSION_SSL)
461 s3_curl_throttling_compat(void)
463 /* CURLOPT_MAX_SEND_SPEED_LARGE added in 7.15.5 */
464 #if LIBCURL_VERSION_NUM >= 0x070f05
465 curl_version_info_data *info;
467 /* check the runtime version too */
468 info = curl_version_info(CURLVERSION_NOW);
469 return info->version_num >= 0x070f05;
476 lookup_result(const result_handling_t *result_handling,
478 s3_error_code_t s3_error_code,
481 while (result_handling->response_code
482 || result_handling->s3_error_code
483 || result_handling->curl_code) {
484 if ((result_handling->response_code && result_handling->response_code != response_code)
485 || (result_handling->s3_error_code && result_handling->s3_error_code != s3_error_code)
486 || (result_handling->curl_code && result_handling->curl_code != curl_code)) {
491 return result_handling->result;
494 /* return the result for the terminator, as the default */
495 return result_handling->result;
499 is_non_empty_string(const char *str)
501 return str && str[0] != '\0';
505 build_url(const char *bucket,
507 const char *subresource,
509 gboolean use_subdomain,
513 char *esc_bucket = NULL, *esc_key = NULL;
516 url = g_string_new("http");
518 g_string_append(url, "s");
520 g_string_append(url, "://");
523 if (use_subdomain && bucket)
524 g_string_append_printf(url, "%s.s3.amazonaws.com/", bucket);
526 g_string_append(url, "s3.amazonaws.com/");
529 if (!use_subdomain && bucket) {
530 esc_bucket = curl_escape(bucket, 0);
531 if (!esc_bucket) goto cleanup;
532 g_string_append_printf(url, "%s", esc_bucket);
534 g_string_append(url, "/");
538 esc_key = curl_escape(key, 0);
539 if (!esc_key) goto cleanup;
540 g_string_append_printf(url, "%s", esc_key);
544 if (subresource || query)
545 g_string_append(url, "?");
548 g_string_append(url, subresource);
550 if (subresource && query)
551 g_string_append(url, "&");
554 g_string_append(url, query);
557 if (esc_bucket) curl_free(esc_bucket);
558 if (esc_key) curl_free(esc_key);
560 return g_string_free(url, FALSE);
563 static struct curl_slist *
564 authenticate_request(S3Handle *hdl,
568 const char *subresource,
569 const char *md5_hash,
570 gboolean use_subdomain)
577 GByteArray *md = NULL;
578 char *auth_base64 = NULL;
579 struct curl_slist *headers = NULL;
580 char *esc_bucket = NULL, *esc_key = NULL;
581 GString *auth_string = NULL;
584 static const char *wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
585 static const char *month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
586 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
590 /* Build the string to sign, per the S3 spec.
591 * See: "Authenticating REST Requests" - API Version 2006-03-01 pg 58
595 auth_string = g_string_new(verb);
596 g_string_append(auth_string, "\n");
598 /* Content-MD5 header */
600 g_string_append(auth_string, md5_hash);
601 g_string_append(auth_string, "\n");
603 /* Content-Type is empty*/
604 g_string_append(auth_string, "\n");
607 /* calculate the date */
610 if (!gmtime_s(&tmp, &t)) g_debug("localtime error");
612 if (!gmtime_r(&t, &tmp)) perror("localtime");
614 date = g_strdup_printf("%s, %02d %s %04d %02d:%02d:%02d GMT",
615 wkday[tmp.tm_wday], tmp.tm_mday, month[tmp.tm_mon], 1900+tmp.tm_year,
616 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
618 g_string_append(auth_string, date);
619 g_string_append(auth_string, "\n");
621 if (is_non_empty_string(hdl->user_token)) {
622 g_string_append(auth_string, AMAZON_SECURITY_HEADER);
623 g_string_append(auth_string, ":");
624 g_string_append(auth_string, hdl->user_token);
625 g_string_append(auth_string, ",");
626 g_string_append(auth_string, STS_PRODUCT_TOKEN);
627 g_string_append(auth_string, "\n");
630 /* CanonicalizedResource */
631 g_string_append(auth_string, "/");
634 g_string_append(auth_string, bucket);
636 esc_bucket = curl_escape(bucket, 0);
637 if (!esc_bucket) goto cleanup;
638 g_string_append(auth_string, esc_bucket);
642 if (bucket && (use_subdomain || key))
643 g_string_append(auth_string, "/");
646 esc_key = curl_escape(key, 0);
647 if (!esc_key) goto cleanup;
648 g_string_append(auth_string, esc_key);
652 g_string_append(auth_string, "?");
653 g_string_append(auth_string, subresource);
656 /* run HMAC-SHA1 on the canonicalized string */
657 md = g_byte_array_sized_new(EVP_MAX_MD_SIZE+1);
659 HMAC_Init_ex(&ctx, hdl->secret_key, (int) strlen(hdl->secret_key), EVP_sha1(), NULL);
660 HMAC_Update(&ctx, (unsigned char*) auth_string->str, auth_string->len);
661 HMAC_Final(&ctx, md->data, &md->len);
662 HMAC_CTX_cleanup(&ctx);
663 auth_base64 = s3_base64_encode(md);
665 /* append the new headers */
666 if (is_non_empty_string(hdl->user_token)) {
667 /* Devpay headers are included in hash. */
668 buf = g_strdup_printf(AMAZON_SECURITY_HEADER ": %s", hdl->user_token);
669 headers = curl_slist_append(headers, buf);
672 buf = g_strdup_printf(AMAZON_SECURITY_HEADER ": %s", STS_PRODUCT_TOKEN);
673 headers = curl_slist_append(headers, buf);
677 buf = g_strdup_printf("Authorization: AWS %s:%s",
678 hdl->access_key, auth_base64);
679 headers = curl_slist_append(headers, buf);
682 if (md5_hash && '\0' != md5_hash[0]) {
683 buf = g_strdup_printf("Content-MD5: %s", md5_hash);
684 headers = curl_slist_append(headers, buf);
688 buf = g_strdup_printf("Date: %s", date);
689 headers = curl_slist_append(headers, buf);
695 g_byte_array_free(md, TRUE);
697 g_string_free(auth_string, TRUE);
703 interpret_response(S3Handle *hdl,
705 char *curl_error_buffer,
709 const char *content_md5)
711 long response_code = 0;
712 regmatch_t pmatch[2];
713 char *error_name = NULL, *message = NULL;
714 char *body_copy = NULL;
717 if (!hdl) return FALSE;
719 if (hdl->last_message) g_free(hdl->last_message);
720 hdl->last_message = NULL;
722 /* bail out from a CURL error */
723 if (curl_code != CURLE_OK) {
724 hdl->last_curl_code = curl_code;
725 hdl->last_message = g_strdup_printf("CURL error: %s", curl_error_buffer);
729 /* CURL seems to think things were OK, so get its response code */
730 curl_easy_getinfo(hdl->curl, CURLINFO_RESPONSE_CODE, &response_code);
731 hdl->last_response_code = response_code;
733 /* check ETag, if present */
734 if (etag && content_md5 && 200 == response_code) {
735 if (etag && g_strcasecmp(etag, content_md5))
736 hdl->last_message = g_strdup("S3 Error: Possible data corruption (ETag returned by Amazon did not match the MD5 hash of the data sent)");
742 if (200 <= response_code && response_code < 400) {
743 /* 2xx and 3xx codes won't have a response body we care about */
744 hdl->last_s3_error_code = S3_ERROR_None;
748 /* Now look at the body to try to get the actual Amazon error message. Rather
749 * than parse out the XML, just use some regexes. */
751 /* impose a reasonable limit on body size */
752 if (body_len > MAX_ERROR_RESPONSE_LEN) {
753 hdl->last_message = g_strdup("S3 Error: Unknown (response body too large to parse)");
755 } else if (!body || body_len == 0) {
756 hdl->last_message = g_strdup("S3 Error: Unknown (empty response body)");
757 return TRUE; /* perhaps a network error; retry the request */
760 /* use strndup to get a zero-terminated string */
761 body_copy = g_strndup(body, body_len);
762 if (!body_copy) goto cleanup;
764 if (!s3_regexec_wrap(&error_name_regex, body_copy, 2, pmatch, 0))
765 error_name = find_regex_substring(body_copy, pmatch[1]);
767 if (!s3_regexec_wrap(&message_regex, body_copy, 2, pmatch, 0))
768 message = find_regex_substring(body_copy, pmatch[1]);
771 hdl->last_s3_error_code = s3_error_code_from_name(error_name);
775 hdl->last_message = message;
776 message = NULL; /* steal the reference to the string */
787 /* a CURLOPT_READFUNCTION to read data from a buffer. */
789 s3_buffer_read_func(void *ptr, size_t size, size_t nmemb, void * stream)
791 CurlBuffer *data = stream;
792 guint bytes_desired = (guint) size * nmemb;
794 /* check the number of bytes remaining, just to be safe */
795 if (bytes_desired > data->buffer_len - data->buffer_pos)
796 bytes_desired = data->buffer_len - data->buffer_pos;
798 memcpy((char *)ptr, data->buffer + data->buffer_pos, bytes_desired);
799 data->buffer_pos += bytes_desired;
801 return bytes_desired;
805 s3_buffer_size_func(void *stream)
807 CurlBuffer *data = stream;
808 return data->buffer_len;
812 s3_buffer_md5_func(void *stream)
814 CurlBuffer *data = stream;
815 GByteArray req_body_gba = {(guint8 *)data->buffer, data->buffer_len};
817 return s3_compute_md5_hash(&req_body_gba);
821 s3_buffer_reset_func(void *stream)
823 CurlBuffer *data = stream;
824 data->buffer_pos = 0;
827 /* a CURLOPT_WRITEFUNCTION to write data to a buffer. */
829 s3_buffer_write_func(void *ptr, size_t size, size_t nmemb, void *stream)
831 CurlBuffer * data = stream;
832 guint new_bytes = (guint) size * nmemb;
833 guint bytes_needed = data->buffer_pos + new_bytes;
835 /* error out if the new size is greater than the maximum allowed */
836 if (data->max_buffer_size && bytes_needed > data->max_buffer_size)
839 /* reallocate if necessary. We use exponential sizing to make this
840 * happen less often. */
841 if (bytes_needed > data->buffer_len) {
842 guint new_size = MAX(bytes_needed, data->buffer_len * 2);
843 if (data->max_buffer_size) {
844 new_size = MIN(new_size, data->max_buffer_size);
846 data->buffer = g_realloc(data->buffer, new_size);
847 data->buffer_len = new_size;
850 return 0; /* returning zero signals an error to libcurl */
852 /* actually copy the data to the buffer */
853 memcpy(data->buffer + data->buffer_pos, ptr, new_bytes);
854 data->buffer_pos += new_bytes;
856 /* signal success to curl */
860 /* a CURLOPT_READFUNCTION that writes nothing. */
862 s3_empty_read_func(G_GNUC_UNUSED void *ptr, G_GNUC_UNUSED size_t size, G_GNUC_UNUSED size_t nmemb, G_GNUC_UNUSED void * stream)
868 s3_empty_size_func(G_GNUC_UNUSED void *stream)
874 s3_empty_md5_func(G_GNUC_UNUSED void *stream)
876 static const GByteArray empty = {(guint8 *) "", 0};
878 return s3_compute_md5_hash(&empty);
881 /* a CURLOPT_WRITEFUNCTION to write data that just counts data.
882 * s3_write_data should be NULL or a pointer to an gint64.
885 s3_counter_write_func(G_GNUC_UNUSED void *ptr, size_t size, size_t nmemb, void *stream)
887 gint64 *count = (gint64*) stream, inc = nmemb*size;
889 if (count) *count += inc;
894 s3_counter_reset_func(void *stream)
896 gint64 *count = (gint64*) stream;
898 if (count) *count = 0;
902 /* a CURLOPT_READFUNCTION to read data from a file. */
904 s3_file_read_func(void *ptr, size_t size, size_t nmemb, void * stream)
906 HANDLE *hFile = (HANDLE *) stream;
909 ReadFile(hFile, ptr, (DWORD) size*nmemb, &bytes_read, NULL);
914 s3_file_size_func(void *stream)
916 HANDLE *hFile = (HANDLE *) stream;
917 DWORD size = GetFileSize(hFile, NULL);
919 if (INVALID_FILE_SIZE == size) {
927 s3_file_md5_func(void *stream)
929 #define S3_MD5_BUF_SIZE (10*1024)
930 HANDLE *hFile = (HANDLE *) stream;
931 guint8 buf[S3_MD5_BUF_SIZE];
934 GByteArray *ret = NULL;
936 g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN));
938 ret = g_byte_array_sized_new(S3_MD5_HASH_BYTE_LEN);
939 g_byte_array_set_size(ret, S3_MD5_HASH_BYTE_LEN);
942 while (ReadFile(hFile, buf, S3_MD5_BUF_SIZE, &bytes_read, NULL)) {
943 MD5_Update(&md5_ctx, buf, bytes_read);
945 MD5_Final(ret->data, &md5_ctx);
947 g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN));
949 #undef S3_MD5_BUF_SIZE
953 s3_file_reset_func(void *stream)
955 g_assert(INVALID_SET_FILE_POINTER != SetFilePointer(hFile, 0, NULL, FILE_BEGIN));
958 /* a CURLOPT_WRITEFUNCTION to write data to a file. */
960 s3_file_write_func(void *ptr, size_t size, size_t nmemb, void *stream)
962 HANDLE *hFile = (HANDLE *) stream;
965 WriteFile(hFile, ptr, (DWORD) size*nmemb, &bytes_written, NULL);
966 return bytes_written;
971 curl_debug_message(CURL *curl G_GNUC_UNUSED,
975 void *unused G_GNUC_UNUSED)
979 char **lines, **line;
986 case CURLINFO_HEADER_IN:
987 lineprefix="Hdr In: ";
990 case CURLINFO_HEADER_OUT:
991 lineprefix="Hdr Out: ";
995 /* ignore data in/out -- nobody wants to see that in the
1000 /* split the input into lines */
1001 message = g_strndup(s, (gsize) len);
1002 lines = g_strsplit(message, "\n", -1);
1005 for (line = lines; *line; line++) {
1006 if (**line == '\0') continue; /* skip blank lines */
1007 g_debug("%s%s", lineprefix, *line);
1015 perform_request(S3Handle *hdl,
1019 const char *subresource,
1021 s3_read_func read_func,
1022 s3_reset_func read_reset_func,
1023 s3_size_func size_func,
1024 s3_md5_func md5_func,
1026 s3_write_func write_func,
1027 s3_reset_func write_reset_func,
1028 gpointer write_data,
1029 s3_progress_func progress_func,
1030 gpointer progress_data,
1031 const result_handling_t *result_handling)
1033 gboolean use_subdomain;
1035 s3_result_t result = S3_RESULT_FAIL; /* assume the worst.. */
1036 CURLcode curl_code = CURLE_OK;
1037 char curl_error_buffer[CURL_ERROR_SIZE] = "";
1038 struct curl_slist *headers = NULL;
1039 S3InternalData int_writedata = {{NULL, 0, 0, MAX_ERROR_RESPONSE_LEN}, NULL, NULL, NULL, FALSE, FALSE, NULL};
1040 gboolean should_retry;
1042 gulong backoff = EXPONENTIAL_BACKOFF_START_USEC;
1043 /* corresponds to PUT, HEAD, GET, and POST */
1044 int curlopt_upload = 0, curlopt_nobody = 0, curlopt_httpget = 0, curlopt_post = 0;
1045 /* do we want to examine the headers */
1046 const char *curlopt_customrequest = NULL;
1047 /* for MD5 calculation */
1048 GByteArray *md5_hash = NULL;
1049 gchar *md5_hash_hex = NULL, *md5_hash_b64 = NULL;
1050 size_t request_body_size = 0;
1052 g_assert(hdl != NULL && hdl->curl != NULL);
1056 use_subdomain = is_non_empty_string(hdl->bucket_location);
1057 url = build_url(bucket, key, subresource, query, use_subdomain, hdl->use_ssl);
1058 if (!url) goto cleanup;
1060 /* libcurl may behave strangely if these are not set correctly */
1061 if (!strncmp(verb, "PUT", 4)) {
1063 } else if (!strncmp(verb, "GET", 4)) {
1064 curlopt_httpget = 1;
1065 } else if (!strncmp(verb, "POST", 5)) {
1067 } else if (!strncmp(verb, "HEAD", 5)) {
1070 curlopt_customrequest = verb;
1074 request_body_size = size_func(read_data);
1078 md5_hash = md5_func(read_data);
1080 md5_hash_b64 = s3_base64_encode(md5_hash);
1081 md5_hash_hex = s3_hex_encode(md5_hash);
1082 g_byte_array_free(md5_hash, TRUE);
1086 /* Curl will use fread() otherwise */
1087 read_func = s3_empty_read_func;
1091 int_writedata.write_func = write_func;
1092 int_writedata.reset_func = write_reset_func;
1093 int_writedata.write_data = write_data;
1095 /* Curl will use fwrite() otherwise */
1096 int_writedata.write_func = s3_counter_write_func;
1097 int_writedata.reset_func = s3_counter_reset_func;
1098 int_writedata.write_data = NULL;
1104 curl_slist_free_all(headers);
1106 curl_error_buffer[0] = '\0';
1107 if (read_reset_func) {
1108 read_reset_func(read_data);
1110 /* calls write_reset_func */
1111 s3_internal_reset_func(&int_writedata);
1113 /* set up the request */
1114 headers = authenticate_request(hdl, verb, bucket, key, subresource,
1115 md5_hash_b64, is_non_empty_string(hdl->bucket_location));
1117 if (hdl->use_ssl && hdl->ca_info) {
1118 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_CAINFO, hdl->ca_info)))
1122 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_VERBOSE, hdl->verbose)))
1125 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_DEBUGFUNCTION,
1126 curl_debug_message)))
1129 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_ERRORBUFFER,
1130 curl_error_buffer)))
1132 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOPROGRESS, 1)))
1134 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_FOLLOWLOCATION, 1)))
1136 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_URL, url)))
1138 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HTTPHEADER,
1141 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_WRITEFUNCTION, s3_internal_write_func)))
1143 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_WRITEDATA, &int_writedata)))
1145 /* Note: we always have to set this apparently, for consistent "end of header" detection */
1146 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HEADERFUNCTION, s3_internal_header_func)))
1148 /* Note: if set, CURLOPT_HEADERDATA seems to also be used for CURLOPT_WRITEDATA ? */
1149 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HEADERDATA, &int_writedata)))
1151 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_PROGRESSFUNCTION, progress_func)))
1153 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_PROGRESSDATA, progress_data)))
1156 /* CURLOPT_INFILESIZE_LARGE added in 7.11.0 */
1157 #if LIBCURL_VERSION_NUM >= 0x070b00
1158 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)request_body_size)))
1161 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_INFILESIZE, (long)request_body_size)))
1164 /* CURLOPT_MAX_{RECV,SEND}_SPEED_LARGE added in 7.15.5 */
1165 #if LIBCURL_VERSION_NUM >= 0x070f05
1166 if (s3_curl_throttling_compat()) {
1167 if (hdl->max_send_speed)
1168 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)hdl->max_send_speed)))
1171 if (hdl->max_recv_speed)
1172 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)hdl->max_recv_speed)))
1177 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_HTTPGET, curlopt_httpget)))
1179 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_UPLOAD, curlopt_upload)))
1181 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_POST, curlopt_post)))
1183 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_NOBODY, curlopt_nobody)))
1185 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_CUSTOMREQUEST,
1186 curlopt_customrequest)))
1190 if (curlopt_upload) {
1191 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READFUNCTION, read_func)))
1193 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READDATA, read_data)))
1196 /* Clear request_body options. */
1197 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READFUNCTION,
1200 if ((curl_code = curl_easy_setopt(hdl->curl, CURLOPT_READDATA,
1205 /* Perform the request */
1206 curl_code = curl_easy_perform(hdl->curl);
1209 /* interpret the response into hdl->last* */
1210 curl_error: /* (label for short-circuiting the curl_easy_perform call) */
1211 should_retry = interpret_response(hdl, curl_code, curl_error_buffer,
1212 int_writedata.resp_buf.buffer, int_writedata.resp_buf.buffer_pos, int_writedata.etag, md5_hash_hex);
1214 /* and, unless we know we need to retry, see what we're to do now */
1215 if (!should_retry) {
1216 result = lookup_result(result_handling, hdl->last_response_code,
1217 hdl->last_s3_error_code, hdl->last_curl_code);
1219 /* break out of the while(1) unless we're retrying */
1220 if (result != S3_RESULT_RETRY)
1224 if (retries >= EXPONENTIAL_BACKOFF_MAX_RETRIES) {
1225 /* we're out of retries, so annotate hdl->last_message appropriately and bail
1227 char *m = g_strdup_printf("Too many retries; last message was '%s'", hdl->last_message);
1228 if (hdl->last_message) g_free(hdl->last_message);
1229 hdl->last_message = m;
1230 result = S3_RESULT_FAIL;
1236 backoff *= EXPONENTIAL_BACKOFF_BASE;
1239 if (result != S3_RESULT_OK) {
1240 g_debug(_("%s %s failed with %d/%s"), verb, url,
1241 hdl->last_response_code,
1242 s3_error_name_from_code(hdl->last_s3_error_code));
1247 if (headers) curl_slist_free_all(headers);
1248 g_free(md5_hash_b64);
1249 g_free(md5_hash_hex);
1251 /* we don't deallocate the response body -- we keep it for later */
1252 hdl->last_response_body = int_writedata.resp_buf.buffer;
1253 hdl->last_response_body_size = int_writedata.resp_buf.buffer_pos;
1254 hdl->last_num_retries = retries;
1261 s3_internal_write_func(void *ptr, size_t size, size_t nmemb, void * stream)
1263 S3InternalData *data = (S3InternalData *) stream;
1266 if (!data->headers_done)
1269 /* call write on internal buffer (if not full) */
1270 if (data->int_write_done) {
1273 bytes_saved = s3_buffer_write_func(ptr, size, nmemb, &data->resp_buf);
1275 data->int_write_done = TRUE;
1278 /* call write on user buffer */
1279 if (data->write_func) {
1280 return data->write_func(ptr, size, nmemb, data->write_data);
1287 s3_internal_reset_func(void * stream)
1289 S3InternalData *data = (S3InternalData *) stream;
1291 s3_buffer_reset_func(&data->resp_buf);
1292 data->headers_done = FALSE;
1293 data->int_write_done = FALSE;
1295 if (data->reset_func) {
1296 data->reset_func(data->write_data);
1301 s3_internal_header_func(void *ptr, size_t size, size_t nmemb, void * stream)
1303 static const char *final_header = "\r\n";
1305 regmatch_t pmatch[2];
1306 S3InternalData *data = (S3InternalData *) stream;
1308 header = g_strndup((gchar *) ptr, (gsize) size*nmemb);
1309 if (!s3_regexec_wrap(&etag_regex, header, 2, pmatch, 0))
1310 data->etag = find_regex_substring(header, pmatch[1]);
1311 if (!strcmp(final_header, header))
1312 data->headers_done = TRUE;
1318 compile_regexes(void)
1322 /* using POSIX regular expressions */
1323 struct {const char * str; int flags; regex_t *regex;} regexes[] = {
1324 {"<Code>[[:space:]]*([^<]*)[[:space:]]*</Code>", REG_EXTENDED | REG_ICASE, &error_name_regex},
1325 {"^ETag:[[:space:]]*\"([^\"]+)\"[[:space:]]*$", REG_EXTENDED | REG_ICASE | REG_NEWLINE, &etag_regex},
1326 {"<Message>[[:space:]]*([^<]*)[[:space:]]*</Message>", REG_EXTENDED | REG_ICASE, &message_regex},
1327 {"^[a-z0-9](-*[a-z0-9]){2,62}$", REG_EXTENDED | REG_NOSUB, &subdomain_regex},
1328 {"(/>)|(>([^<]*)</LocationConstraint>)", REG_EXTENDED | REG_ICASE, &location_con_regex},
1331 char regmessage[1024];
1335 for (i = 0; regexes[i].str; i++) {
1336 reg_result = regcomp(regexes[i].regex, regexes[i].str, regexes[i].flags);
1337 if (reg_result != 0) {
1338 size = regerror(reg_result, regexes[i].regex, regmessage, sizeof(regmessage));
1339 g_error(_("Regex error: %s"), regmessage);
1343 #else /* ! HAVE_REGEX_H */
1344 /* using PCRE via GLib */
1345 struct {const char * str; int flags; regex_t *regex;} regexes[] = {
1346 {"<Code>\\s*([^<]*)\\s*</Code>",
1347 G_REGEX_OPTIMIZE | G_REGEX_CASELESS,
1349 {"^ETag:\\s*\"([^\"]+)\"\\s*$",
1350 G_REGEX_OPTIMIZE | G_REGEX_CASELESS,
1352 {"<Message>\\s*([^<]*)\\s*</Message>",
1353 G_REGEX_OPTIMIZE | G_REGEX_CASELESS,
1355 {"^[a-z0-9]((-*[a-z0-9])|(\\.[a-z0-9])){2,62}$",
1356 G_REGEX_OPTIMIZE | G_REGEX_NO_AUTO_CAPTURE,
1358 {"(/>)|(>([^<]*)</LocationConstraint>)",
1360 &location_con_regex},
1366 for (i = 0; regexes[i].str; i++) {
1367 *(regexes[i].regex) = g_regex_new(regexes[i].str, regexes[i].flags, 0, &err);
1369 g_error(_("Regex error: %s"), err->message);
1379 * Public function implementations
1382 gboolean s3_init(void)
1384 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
1385 static gboolean init = FALSE, ret;
1387 /* n.b. curl_global_init is called in common-src/glib-util.c:glib_init() */
1389 g_static_mutex_lock (&mutex);
1391 ret = compile_regexes();
1394 g_static_mutex_unlock(&mutex);
1399 s3_curl_location_compat(void)
1401 curl_version_info_data *info;
1403 info = curl_version_info(CURLVERSION_NOW);
1404 return info->version_num > 0x070a02;
1408 s3_bucket_location_compat(const char *bucket)
1410 return !s3_regexec_wrap(&subdomain_regex, bucket, 0, NULL, 0);
1414 s3_open(const char *access_key,
1415 const char *secret_key,
1416 const char *user_token,
1417 const char *bucket_location,
1422 hdl = g_new0(S3Handle, 1);
1423 if (!hdl) goto error;
1425 hdl->verbose = FALSE;
1426 hdl->use_ssl = s3_curl_supports_ssl();
1428 g_assert(access_key);
1429 hdl->access_key = g_strdup(access_key);
1430 g_assert(secret_key);
1431 hdl->secret_key = g_strdup(secret_key);
1433 hdl->user_token = g_strdup(user_token);
1436 hdl->bucket_location = g_strdup(bucket_location);
1439 hdl->ca_info = g_strdup(ca_info);
1441 hdl->curl = curl_easy_init();
1442 if (!hdl->curl) goto error;
1452 s3_free(S3Handle *hdl)
1457 g_free(hdl->access_key);
1458 g_free(hdl->secret_key);
1459 if (hdl->user_token) g_free(hdl->user_token);
1460 if (hdl->bucket_location) g_free(hdl->bucket_location);
1461 if (hdl->curl) curl_easy_cleanup(hdl->curl);
1468 s3_reset(S3Handle *hdl)
1471 /* We don't call curl_easy_reset here, because doing that in curl
1472 * < 7.16 blanks the default CA certificate path, and there's no way
1473 * to get it back. */
1474 if (hdl->last_message) {
1475 g_free(hdl->last_message);
1476 hdl->last_message = NULL;
1479 hdl->last_response_code = 0;
1480 hdl->last_curl_code = 0;
1481 hdl->last_s3_error_code = 0;
1482 hdl->last_num_retries = 0;
1484 if (hdl->last_response_body) {
1485 g_free(hdl->last_response_body);
1486 hdl->last_response_body = NULL;
1489 hdl->last_response_body_size = 0;
1494 s3_error(S3Handle *hdl,
1495 const char **message,
1496 guint *response_code,
1497 s3_error_code_t *s3_error_code,
1498 const char **s3_error_name,
1499 CURLcode *curl_code,
1503 if (message) *message = hdl->last_message;
1504 if (response_code) *response_code = hdl->last_response_code;
1505 if (s3_error_code) *s3_error_code = hdl->last_s3_error_code;
1506 if (s3_error_name) *s3_error_name = s3_error_name_from_code(hdl->last_s3_error_code);
1507 if (curl_code) *curl_code = hdl->last_curl_code;
1508 if (num_retries) *num_retries = hdl->last_num_retries;
1510 /* no hdl? return something coherent, anyway */
1511 if (message) *message = "NULL S3Handle";
1512 if (response_code) *response_code = 0;
1513 if (s3_error_code) *s3_error_code = 0;
1514 if (s3_error_name) *s3_error_name = NULL;
1515 if (curl_code) *curl_code = 0;
1516 if (num_retries) *num_retries = 0;
1521 s3_verbose(S3Handle *hdl, gboolean verbose)
1523 hdl->verbose = verbose;
1527 s3_set_max_send_speed(S3Handle *hdl, guint64 max_send_speed)
1529 if (!s3_curl_throttling_compat())
1532 hdl->max_send_speed = max_send_speed;
1538 s3_set_max_recv_speed(S3Handle *hdl, guint64 max_recv_speed)
1540 if (!s3_curl_throttling_compat())
1543 hdl->max_recv_speed = max_recv_speed;
1549 s3_use_ssl(S3Handle *hdl, gboolean use_ssl)
1551 gboolean ret = TRUE;
1552 if (use_ssl & !s3_curl_supports_ssl()) {
1555 hdl->use_ssl = use_ssl;
1561 s3_strerror(S3Handle *hdl)
1563 const char *message;
1564 guint response_code;
1565 const char *s3_error_name;
1569 char s3_info[256] = "";
1570 char response_info[16] = "";
1571 char curl_info[32] = "";
1572 char retries_info[32] = "";
1574 s3_error(hdl, &message, &response_code, NULL, &s3_error_name, &curl_code, &num_retries);
1577 message = "Unknown S3 error";
1579 g_snprintf(s3_info, sizeof(s3_info), " (%s)", s3_error_name);
1581 g_snprintf(response_info, sizeof(response_info), " (HTTP %d)", response_code);
1583 g_snprintf(curl_info, sizeof(curl_info), " (CURLcode %d)", curl_code);
1585 g_snprintf(retries_info, sizeof(retries_info), " (after %d retries)", num_retries);
1587 return g_strdup_printf("%s%s%s%s%s", message, s3_info, curl_info, response_info, retries_info);
1590 /* Perform an upload. When this function returns, KEY and
1591 * BUFFER remain the responsibility of the caller.
1593 * @param self: the s3 device
1594 * @param bucket: the bucket to which the upload should be made
1595 * @param key: the key to which the upload should be made
1596 * @param buffer: the data to be uploaded
1597 * @param buffer_len: the length of the data to upload
1598 * @returns: false if an error ocurred
1601 s3_upload(S3Handle *hdl,
1604 s3_read_func read_func,
1605 s3_reset_func reset_func,
1606 s3_size_func size_func,
1607 s3_md5_func md5_func,
1609 s3_progress_func progress_func,
1610 gpointer progress_data)
1612 s3_result_t result = S3_RESULT_FAIL;
1613 static result_handling_t result_handling[] = {
1614 { 200, 0, 0, S3_RESULT_OK },
1615 RESULT_HANDLING_ALWAYS_RETRY,
1616 { 0, 0, 0, /* default: */ S3_RESULT_FAIL }
1619 g_assert(hdl != NULL);
1621 result = perform_request(hdl, "PUT", bucket, key, NULL, NULL,
1622 read_func, reset_func, size_func, md5_func, read_data,
1623 NULL, NULL, NULL, progress_func, progress_data,
1626 return result == S3_RESULT_OK;
1630 /* Private structure for our "thunk", which tracks where the user is in the list
1632 struct list_keys_thunk {
1633 GSList *filename_list; /* all pending filenames */
1635 gboolean in_contents; /* look for "key" entities in here */
1636 gboolean in_common_prefixes; /* look for "prefix" entities in here */
1638 gboolean is_truncated;
1647 /* Functions for a SAX parser to parse the XML from Amazon */
1650 list_start_element(GMarkupParseContext *context G_GNUC_UNUSED,
1651 const gchar *element_name,
1652 const gchar **attribute_names G_GNUC_UNUSED,
1653 const gchar **attribute_values G_GNUC_UNUSED,
1655 GError **error G_GNUC_UNUSED)
1657 struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data;
1659 thunk->want_text = 0;
1660 if (g_strcasecmp(element_name, "contents") == 0) {
1661 thunk->in_contents = 1;
1662 } else if (g_strcasecmp(element_name, "commonprefixes") == 0) {
1663 thunk->in_common_prefixes = 1;
1664 } else if (g_strcasecmp(element_name, "prefix") == 0 && thunk->in_common_prefixes) {
1665 thunk->want_text = 1;
1666 } else if (g_strcasecmp(element_name, "key") == 0 && thunk->in_contents) {
1667 thunk->want_text = 1;
1668 } else if (g_strcasecmp(element_name, "istruncated")) {
1669 thunk->want_text = 1;
1670 } else if (g_strcasecmp(element_name, "nextmarker")) {
1671 thunk->want_text = 1;
1676 list_end_element(GMarkupParseContext *context G_GNUC_UNUSED,
1677 const gchar *element_name,
1679 GError **error G_GNUC_UNUSED)
1681 struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data;
1683 if (g_strcasecmp(element_name, "contents") == 0) {
1684 thunk->in_contents = 0;
1685 } else if (g_strcasecmp(element_name, "commonprefixes") == 0) {
1686 thunk->in_common_prefixes = 0;
1687 } else if (g_strcasecmp(element_name, "key") == 0 && thunk->in_contents) {
1688 thunk->filename_list = g_slist_prepend(thunk->filename_list, thunk->text);
1690 } else if (g_strcasecmp(element_name, "prefix") == 0 && thunk->in_common_prefixes) {
1691 thunk->filename_list = g_slist_prepend(thunk->filename_list, thunk->text);
1693 } else if (g_strcasecmp(element_name, "istruncated") == 0) {
1694 if (thunk->text && g_strncasecmp(thunk->text, "false", 5) != 0)
1695 thunk->is_truncated = TRUE;
1696 } else if (g_strcasecmp(element_name, "nextmarker") == 0) {
1697 if (thunk->next_marker) g_free(thunk->next_marker);
1698 thunk->next_marker = thunk->text;
1704 list_text(GMarkupParseContext *context G_GNUC_UNUSED,
1708 GError **error G_GNUC_UNUSED)
1710 struct list_keys_thunk *thunk = (struct list_keys_thunk *)user_data;
1712 if (thunk->want_text) {
1713 if (thunk->text) g_free(thunk->text);
1714 thunk->text = g_strndup(text, text_len);
1718 /* Perform a fetch from S3; several fetches may be involved in a
1719 * single listing operation */
1721 list_fetch(S3Handle *hdl,
1724 const char *delimiter,
1726 const char *max_keys,
1729 s3_result_t result = S3_RESULT_FAIL;
1730 static result_handling_t result_handling[] = {
1731 { 200, 0, 0, S3_RESULT_OK },
1732 RESULT_HANDLING_ALWAYS_RETRY,
1733 { 0, 0, 0, /* default: */ S3_RESULT_FAIL }
1735 const char* pos_parts[][2] = {
1737 {"delimiter", delimiter},
1739 {"max-keys", max_keys},
1745 gboolean have_prev_part = FALSE;
1747 /* loop over possible parts to build query string */
1748 query = g_string_new("");
1749 for (i = 0; pos_parts[i][0]; i++) {
1750 if (pos_parts[i][1]) {
1752 g_string_append(query, "&");
1754 have_prev_part = TRUE;
1755 esc_value = curl_escape(pos_parts[i][1], 0);
1756 g_string_append_printf(query, "%s=%s", pos_parts[i][0], esc_value);
1757 curl_free(esc_value);
1761 /* and perform the request on that URI */
1762 result = perform_request(hdl, "GET", bucket, NULL, NULL, query->str,
1763 NULL, NULL, NULL, NULL, NULL,
1764 S3_BUFFER_WRITE_FUNCS, buf, NULL, NULL,
1767 if (query) g_string_free(query, TRUE);
1773 s3_list_keys(S3Handle *hdl,
1776 const char *delimiter,
1780 * max len of XML variables:
1781 * bucket: 255 bytes (p12 API Version 2006-03-01)
1782 * key: 1024 bytes (p15 API Version 2006-03-01)
1783 * size per key: 5GB bytes (p6 API Version 2006-03-01)
1784 * size of size 10 bytes (i.e. 10 decimal digits)
1785 * etag: 44 (observed+assumed)
1786 * owner ID: 64 (observed+assumed)
1787 * owner DisplayName: 255 (assumed)
1788 * StorageClass: const (p18 API Version 2006-03-01)
1790 static const guint MAX_RESPONSE_LEN = 1000*2000;
1791 static const char *MAX_KEYS = "1000";
1792 struct list_keys_thunk thunk;
1793 GMarkupParseContext *ctxt = NULL;
1794 static GMarkupParser parser = { list_start_element, list_end_element, list_text, NULL, NULL };
1796 s3_result_t result = S3_RESULT_FAIL;
1797 CurlBuffer buf = {NULL, 0, 0, MAX_RESPONSE_LEN};
1801 thunk.filename_list = NULL;
1803 thunk.next_marker = NULL;
1805 /* Loop until S3 has given us the entire picture */
1807 s3_buffer_reset_func(&buf);
1808 /* get some data from S3 */
1809 result = list_fetch(hdl, bucket, prefix, delimiter, thunk.next_marker, MAX_KEYS, &buf);
1810 if (result != S3_RESULT_OK) goto cleanup;
1812 /* run the parser over it */
1813 thunk.in_contents = FALSE;
1814 thunk.in_common_prefixes = FALSE;
1815 thunk.is_truncated = FALSE;
1816 thunk.want_text = FALSE;
1818 ctxt = g_markup_parse_context_new(&parser, 0, (gpointer)&thunk, NULL);
1820 if (!g_markup_parse_context_parse(ctxt, buf.buffer, buf.buffer_pos, &err)) {
1821 if (hdl->last_message) g_free(hdl->last_message);
1822 hdl->last_message = g_strdup(err->message);
1823 result = S3_RESULT_FAIL;
1827 if (!g_markup_parse_context_end_parse(ctxt, &err)) {
1828 if (hdl->last_message) g_free(hdl->last_message);
1829 hdl->last_message = g_strdup(err->message);
1830 result = S3_RESULT_FAIL;
1834 g_markup_parse_context_free(ctxt);
1836 } while (thunk.next_marker);
1839 if (err) g_error_free(err);
1840 if (thunk.text) g_free(thunk.text);
1841 if (thunk.next_marker) g_free(thunk.next_marker);
1842 if (ctxt) g_markup_parse_context_free(ctxt);
1843 if (buf.buffer) g_free(buf.buffer);
1845 if (result != S3_RESULT_OK) {
1846 g_slist_free(thunk.filename_list);
1849 *list = thunk.filename_list;
1855 s3_read(S3Handle *hdl,
1858 s3_write_func write_func,
1859 s3_reset_func reset_func,
1860 gpointer write_data,
1861 s3_progress_func progress_func,
1862 gpointer progress_data)
1864 s3_result_t result = S3_RESULT_FAIL;
1865 static result_handling_t result_handling[] = {
1866 { 200, 0, 0, S3_RESULT_OK },
1867 RESULT_HANDLING_ALWAYS_RETRY,
1868 { 0, 0, 0, /* default: */ S3_RESULT_FAIL }
1871 g_assert(hdl != NULL);
1872 g_assert(write_func != NULL);
1874 result = perform_request(hdl, "GET", bucket, key, NULL, NULL,
1875 NULL, NULL, NULL, NULL, NULL, write_func, reset_func, write_data,
1876 progress_func, progress_data, result_handling);
1878 return result == S3_RESULT_OK;
1882 s3_delete(S3Handle *hdl,
1886 s3_result_t result = S3_RESULT_FAIL;
1887 static result_handling_t result_handling[] = {
1888 { 204, 0, 0, S3_RESULT_OK },
1889 { 404, S3_ERROR_NoSuchBucket, 0, S3_RESULT_OK },
1890 RESULT_HANDLING_ALWAYS_RETRY,
1891 { 0, 0, 0, /* default: */ S3_RESULT_FAIL }
1894 g_assert(hdl != NULL);
1896 result = perform_request(hdl, "DELETE", bucket, key, NULL, NULL,
1897 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1900 return result == S3_RESULT_OK;
1904 s3_make_bucket(S3Handle *hdl,
1908 s3_result_t result = S3_RESULT_FAIL;
1909 static result_handling_t result_handling[] = {
1910 { 200, 0, 0, S3_RESULT_OK },
1911 { 404, S3_ERROR_NoSuchBucket, 0, S3_RESULT_RETRY },
1912 RESULT_HANDLING_ALWAYS_RETRY,
1913 { 0, 0, 0, /* default: */ S3_RESULT_FAIL }
1915 regmatch_t pmatch[4];
1916 char *loc_end_open, *loc_content;
1917 CurlBuffer buf = {NULL, 0, 0, 0}, *ptr = NULL;
1918 s3_read_func read_func = NULL;
1919 s3_reset_func reset_func = NULL;
1920 s3_md5_func md5_func = NULL;
1921 s3_size_func size_func = NULL;
1923 g_assert(hdl != NULL);
1925 if (is_non_empty_string(hdl->bucket_location) &&
1926 0 != strcmp(AMAZON_WILDCARD_LOCATION, hdl->bucket_location)) {
1927 if (s3_bucket_location_compat(bucket)) {
1929 buf.buffer = g_strdup_printf(AMAZON_BUCKET_CONF_TEMPLATE, hdl->bucket_location);
1930 buf.buffer_len = (guint) strlen(buf.buffer);
1932 buf.max_buffer_size = buf.buffer_len;
1933 read_func = s3_buffer_read_func;
1934 reset_func = s3_buffer_reset_func;
1935 size_func = s3_buffer_size_func;
1936 md5_func = s3_buffer_md5_func;
1938 hdl->last_message = g_strdup_printf(_(
1939 "Location constraint given for Amazon S3 bucket, "
1940 "but the bucket name (%s) is not usable as a subdomain."), bucket);
1945 result = perform_request(hdl, "PUT", bucket, NULL, NULL, NULL,
1946 read_func, reset_func, size_func, md5_func, ptr,
1947 NULL, NULL, NULL, NULL, NULL, result_handling);
1949 if (result == S3_RESULT_OK ||
1950 (is_non_empty_string(hdl->bucket_location) && result != S3_RESULT_OK
1951 && hdl->last_s3_error_code == S3_ERROR_BucketAlreadyOwnedByYou)) {
1952 /* verify the that the location constraint on the existing bucket matches
1953 * the one that's configured.
1955 result = perform_request(hdl, "GET", bucket, NULL, "location", NULL,
1956 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1957 NULL, NULL, result_handling);
1959 /* note that we can check only one of the three AND conditions above
1960 * and infer that the others are true
1962 if (result == S3_RESULT_OK && is_non_empty_string(hdl->bucket_location)) {
1963 /* return to the default state of failure */
1964 result = S3_RESULT_FAIL;
1966 if (body) g_free(body);
1967 /* use strndup to get a null-terminated string */
1968 body = g_strndup(hdl->last_response_body, hdl->last_response_body_size);
1970 hdl->last_message = g_strdup(_("No body received for location request"));
1972 } else if ('\0' == body[0]) {
1973 hdl->last_message = g_strdup(_("Empty body received for location request"));
1977 if (!s3_regexec_wrap(&location_con_regex, body, 4, pmatch, 0)) {
1978 loc_end_open = find_regex_substring(body, pmatch[1]);
1979 loc_content = find_regex_substring(body, pmatch[3]);
1981 /* The case of an empty string is special because XML allows
1982 * "self-closing" tags
1984 if (0 == strcmp(AMAZON_WILDCARD_LOCATION, hdl->bucket_location) &&
1985 '/' != loc_end_open[0])
1986 hdl->last_message = g_strdup(_("A wildcard location constraint is "
1987 "configured, but the bucket has a non-empty location constraint"));
1988 else if (strcmp(AMAZON_WILDCARD_LOCATION, hdl->bucket_location)?
1989 strncmp(loc_content, hdl->bucket_location, strlen(hdl->bucket_location)) :
1990 ('\0' != loc_content[0]))
1991 hdl->last_message = g_strdup(_("The location constraint configured "
1992 "does not match the constraint currently on the bucket"));
1994 result = S3_RESULT_OK;
1996 hdl->last_message = g_strdup(_("Unexpected location response from Amazon S3"));
2002 if (body) g_free(body);
2004 return result == S3_RESULT_OK;
2009 s3_delete_bucket(S3Handle *hdl,
2012 return s3_delete(hdl, bucket, NULL);