Skip to content

Commit

Permalink
分块上传接口Complete增加复用header逻辑;Copy&Complete接口增加校验逻辑 (#59)
Browse files Browse the repository at this point in the history
* Copy&Complete接口增加校验逻辑

* 分块上传接口complete增加复用header逻辑

---------

Co-authored-by: huberyxxiao <[email protected]>
  • Loading branch information
Huberyxiao and huberyxxiao authored Jul 8, 2024
1 parent 7d18d97 commit a744fc6
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 18 deletions.
3 changes: 3 additions & 0 deletions cos_c_sdk/cos_multipart.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ cos_status_t *cos_do_complete_multipart_upload(const cos_request_options_t *opti
}
cos_fill_read_response_header(resp, resp_headers);
cos_fill_read_response_body(resp, resp_body);
if (resp->body_len == 0 || check_status_with_resp_body(&resp->body, resp->body_len, "ETag") != COS_TRUE){
cos_status_set(s, COSE_SERVICE_ERROR, COS_SERVER_ERROR_CODE, "Server Error");
}

return s;
}
Expand Down
3 changes: 3 additions & 0 deletions cos_c_sdk/cos_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ cos_status_t *cos_copy_object(const cos_request_options_t *options,
res = cos_copy_object_parse_from_body(options->pool, &resp->body, copy_object_param);
if (res != COSE_OK) cos_xml_error_status_set(s, res);

if (resp->body_len == 0 || check_status_with_resp_body(&resp->body, resp->body_len, "ETag") != COS_TRUE){
cos_status_set(s, COSE_SERVICE_ERROR, COS_SERVER_ERROR_CODE, "Server Error");
}
return s;
}

Expand Down
45 changes: 45 additions & 0 deletions cos_c_sdk/cos_resumable.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,20 @@ cos_status_t *cos_resumable_upload_file_without_cp(cos_request_options_t *option
cos_build_thread_params(thr_params, part_num, parent_pool, options, bucket, object, filepath,
headers , params, &upload_id, parts, results);

//提前拷贝header
int pos;
const cos_array_header_t *tarr;
const cos_table_entry_t *telts;
cos_table_t *cb_headers_temp = NULL;
if ( headers != NULL){
cb_headers_temp = cos_table_make(parent_pool, 1);
tarr = cos_table_elts(headers);
telts = (cos_table_entry_t*)tarr->elts;
for (pos = 0; pos < tarr->nelts; ++pos) {
apr_table_set(cb_headers_temp, telts[pos].key, telts[pos].val);
}
}

// init upload
cos_pool_create(&subpool, parent_pool);
options->pool = subpool;
Expand Down Expand Up @@ -600,6 +614,15 @@ cos_status_t *cos_resumable_upload_file_without_cp(cos_request_options_t *option
apr_table_set(cb_headers, COS_CALLBACK_VAR, apr_table_get(headers, COS_CALLBACK_VAR));
}
}
//增加header复用逻辑
if (cb_headers_temp != NULL){
if (cb_headers == NULL) cb_headers = cos_table_make(subpool, 1);
tarr = cos_table_elts(cb_headers_temp);
telts = (cos_table_entry_t*)tarr->elts;
for (pos = 0; pos < tarr->nelts; ++pos) {
apr_table_set(cb_headers, telts[pos].key, telts[pos].val);
}
}
s = cos_do_complete_multipart_upload(options, bucket, object, &upload_id,
&completed_part_list, cb_headers, NULL, resp_headers, resp_body);
s = cos_status_dup(parent_pool, s);
Expand Down Expand Up @@ -665,6 +688,19 @@ cos_status_t *cos_resumable_upload_file_with_cp(cos_request_options_t *options,
apr_file_remove(checkpoint_path->data, parent_pool);
}
}
//提前拷贝header
int pos;
const cos_array_header_t *tarr;
const cos_table_entry_t *telts;
cos_table_t *cb_headers_temp = NULL;
if ( headers != NULL){
cb_headers_temp = cos_table_make(parent_pool, 1);
tarr = cos_table_elts(headers);
telts = (cos_table_entry_t*)tarr->elts;
for (pos = 0; pos < tarr->nelts; ++pos) {
apr_table_set(cb_headers_temp, telts[pos].key, telts[pos].val);
}
}

if (need_init_upload) {
// init upload
Expand Down Expand Up @@ -801,6 +837,15 @@ cos_status_t *cos_resumable_upload_file_with_cp(cos_request_options_t *options,
apr_table_set(cb_headers, COS_CALLBACK_VAR, apr_table_get(headers, COS_CALLBACK_VAR));
}
}
//增加header复用逻辑
if (cb_headers_temp != NULL){
if (cb_headers == NULL) cb_headers = cos_table_make(subpool, 1);
tarr = cos_table_elts(cb_headers_temp);
telts = (cos_table_entry_t*)tarr->elts;
for (pos = 0; pos < tarr->nelts; ++pos) {
apr_table_set(cb_headers, telts[pos].key, telts[pos].val);
}
}
s = cos_do_complete_multipart_upload(options, bucket, object, &upload_id,
&completed_part_list, cb_headers, NULL, resp_headers, resp_body);
s = cos_status_dup(parent_pool, s);
Expand Down
2 changes: 2 additions & 0 deletions cos_c_sdk/cos_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const char COS_INCONSISTENT_ERROR_CODE[] = "InconsistentError";
const char COS_CREATE_QUEUE_ERROR_CODE[] = "CreateQueueFail";
const char COS_CREATE_THREAD_POOL_ERROR_CODE[] = "CreateThreadPoolFail";
const char COS_LACK_OF_CONTENT_LEN_ERROR_CODE[] = "LackOfContentLength";
const char COS_SERVER_ERROR_CODE[] = "ServerError";


cos_status_t *cos_status_create(cos_pool_t *p)
Expand All @@ -28,6 +29,7 @@ cos_status_t *cos_status_dup(cos_pool_t *p, cos_status_t *src)
dst->code = src->code;
dst->error_code = apr_pstrdup(p, src->error_code);
dst->error_msg = apr_pstrdup(p, src->error_msg);
dst->req_id = apr_pstrdup(p, src->req_id);
return dst;
}

Expand Down
1 change: 1 addition & 0 deletions cos_c_sdk/cos_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern const char COS_INCONSISTENT_ERROR_CODE[];
extern const char COS_CREATE_QUEUE_ERROR_CODE[];
extern const char COS_CREATE_THREAD_POOL_ERROR_CODE[];
extern const char COS_LACK_OF_CONTENT_LEN_ERROR_CODE[];
extern const char COS_SERVER_ERROR_CODE[];

COS_CPP_END

Expand Down
3 changes: 2 additions & 1 deletion cos_c_sdk/cos_sys_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ typedef apr_array_header_t cos_array_header_t;
#define CR (char) 13
#define CRLF "\x0d\x0a"

#define COS_VERSION "5.0.18"

#define COS_VERSION "5.0.19"
#define COS_VER "cos-sdk-c/" COS_VERSION

#define COS_HTTP_PREFIX "http://"
Expand Down
18 changes: 9 additions & 9 deletions cos_c_sdk/cos_sys_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,16 +557,16 @@ int cos_url_decode(const char *in, char *out)
}

/*
* Convert a string to a long long integer.
* Convert a string to a int64_t integer.
*
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
long long cos_strtoll(const char *nptr, char **endptr, int base)
int64_t cos_strtoll(const char *nptr, char **endptr, int base)
{
const char *s;
/* LONGLONG */
long long int acc, cutoff;
int64_t acc, cutoff;
int c;
int neg, any, cutlim;

Expand Down Expand Up @@ -610,7 +610,7 @@ long long cos_strtoll(const char *nptr, char **endptr, int base)
* followed by a legal input character, is too big. One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit. For instance, if the range for long longs is
* digit. For instance, if the range for int64_ts is
* [-9223372036854775808..9223372036854775807] and the input base
* is 10, cutoff will be set to 922337203685477580 and cutlim to
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
Expand Down Expand Up @@ -675,10 +675,10 @@ int64_t cos_atoi64(const char *nptr)
return cos_strtoull(nptr, NULL, 10);
}

unsigned long long cos_strtoull(const char *nptr, char **endptr, int base)
uint64_t cos_strtoull(const char *nptr, char **endptr, int base)
{
const char *s;
unsigned long long acc, cutoff;
uint64_t acc, cutoff;
int c;
int neg, any, cutlim;

Expand Down Expand Up @@ -706,8 +706,8 @@ unsigned long long cos_strtoull(const char *nptr, char **endptr, int base)
if (base == 0)
base = c == '0' ? 8 : 10;

cutoff = ULLONG_MAX / (unsigned long long)base;
cutlim = ULLONG_MAX % (unsigned long long)base;
cutoff = ULLONG_MAX / (uint64_t)base;
cutlim = ULLONG_MAX % (uint64_t)base;
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (isdigit(c))
c -= '0';
Expand All @@ -725,7 +725,7 @@ unsigned long long cos_strtoull(const char *nptr, char **endptr, int base)
errno = ERANGE;
} else {
any = 1;
acc *= (unsigned long long)base;
acc *= (uint64_t)base;
acc += c;
}
}
Expand Down
4 changes: 2 additions & 2 deletions cos_c_sdk/cos_sys_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int cos_url_decode(const char *in, char *out);
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
long long cos_strtoll(const char *nptr, char **endptr, int base);
int64_t cos_strtoll(const char *nptr, char **endptr, int base);

/*
* @brief Convert a string to int64_t.
Expand All @@ -81,7 +81,7 @@ int64_t cos_atoi64(const char *nptr);
* Ignores `locale' stuff. Assumes that the upper and lower case
* alphabets and digits are each contiguous.
**/
unsigned long long cos_strtoull(const char *nptr, char **endptr, int base);
uint64_t cos_strtoull(const char *nptr, char **endptr, int base);

/*
* @brief Convert a string to uint64_t.
Expand Down
2 changes: 1 addition & 1 deletion cos_c_sdk/cos_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ size_t cos_curl_default_header_callback(char *buffer, size_t size, size_t nitems

static void cos_curl_transport_headers_done(cos_curl_http_transport_t *t)
{
long http_code;
int32_t http_code;
CURLcode code;
const char *value;

Expand Down
19 changes: 19 additions & 0 deletions cos_c_sdk/cos_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ int is_should_retry_endpoint(const cos_status_t *s, const char *str){
}
#endif


int check_status_with_resp_body(cos_list_t *body, int64_t body_len,const char *target){
cos_list_t *current = body->next;
int target_len = strlen(target);
while (current != body)
{
cos_buf_t *buf = (cos_buf_t *)current;
uint8_t *p;
for (p = buf->start; p < buf->last - target_len +1 ; ++p)
{
if (memcmp(p, target, target_len) == 0) {
return COS_TRUE;
}
}
current = current->next;
}
return COS_FALSE;
}

char ** split(const char * s, char delim, int * returnSize) {
int n = strlen(s);
char ** ans = (char **)malloc(sizeof(char *) * n);
Expand Down
1 change: 1 addition & 0 deletions cos_c_sdk/cos_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int is_cos_domain(const cos_string_t *str);
int is_should_retry(const cos_status_t *s, const char *str);
int is_default_domain(const char *str);
int is_should_retry_endpoint(const cos_status_t *s, const char *str);
int check_status_with_resp_body(cos_list_t *body, int64_t body_len, const char *target);
int is_default_endpoint(const char *str);
int change_host_suffix(char **endpoint);
void change_endpoint_suffix(cos_string_t *endpoint);
Expand Down
4 changes: 2 additions & 2 deletions cos_c_sdk_test/cos_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1387,9 +1387,9 @@ void make_rand_string(cos_pool_t *p, int len, cos_string_t *data)
cos_str_set(data, str);
}

unsigned long get_file_size(const char *file_path)
int64_t get_file_size(const char *file_path)
{
unsigned long filesize = -1;
int64_t filesize = -1;
struct stat statbuff;

if(stat(file_path, &statbuff) < 0){
Expand Down
4 changes: 2 additions & 2 deletions cos_c_sdk_ut/cos_test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ cos_status_t *abort_test_multipart_upload(const cos_request_options_t *options,
return s;
}

unsigned long get_file_size(const char *file_path)
int64_t get_file_size(const char *file_path)
{
unsigned long filesize = -1;
int64_t filesize = -1;
struct stat statbuff;

if(stat(file_path, &statbuff) < 0){
Expand Down
2 changes: 1 addition & 1 deletion cos_c_sdk_ut/cos_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cos_status_t *init_test_multipart_upload(const cos_request_options_t *options, c
cos_status_t *abort_test_multipart_upload(const cos_request_options_t *options, const char *bucket_name,
const char *object_name, cos_string_t *upload_id);

unsigned long get_file_size(const char *file_path);
int64_t get_file_size(const char *file_path);

char *decrypt(const char *encrypted_str, cos_pool_t *pool);

Expand Down
2 changes: 2 additions & 0 deletions cos_c_sdk_ut/test_cos_resumable.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ void test_resumable_upload_without_checkpoint(CuTest *tc)
cos_str_set(&filename, "../../../cos_c_sdk_ut/test_3M.dat");

// upload object
apr_table_addn(headers, "test-header", "testheader");
clt_params = cos_create_resumable_clt_params_content(p, 1024 * 1024, 4, COS_FALSE, NULL);
s = cos_resumable_upload_file(options, &bucket, &object, &filename, headers, NULL,
clt_params, NULL, &resp_headers, &resp_body);
Expand Down Expand Up @@ -887,6 +888,7 @@ void test_resumable_upload_with_checkpoint(CuTest *tc)
cos_str_set(&filename, test_local_file);

// upload object
apr_table_addn(headers, "test-header", "testheader");
clt_params = cos_create_resumable_clt_params_content(p, 1024 * 100, 3, COS_TRUE, NULL);
s = cos_resumable_upload_file(options, &bucket, &object, &filename, headers, NULL,
clt_params, NULL, &resp_headers, &resp_body);
Expand Down
32 changes: 32 additions & 0 deletions cos_c_sdk_ut/test_cos_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,38 @@ void test_cos_status_parse_from_body(CuTest *tc){
CuAssertIntEquals(tc, s.error_code, COS_UNKNOWN_ERROR_CODE);
printf("test_cos_status_parse_from_body 400 1ok\n");
}
{
cos_list_t body;
cos_list_init(&body);
const char *buffer = "<root><Status>active</Status><Name>test</Name><ETag>test</ETag><ForcedReplacement>test</ForcedReplacement></DomainRule></root>";

cos_buf_t *b;
int len = strlen(buffer);
b = cos_create_buf(pool, len);
memcpy(b->pos, buffer, len);
b->last += len;
cos_list_add_tail(&b->node, &body);
cos_status_t s;
int res = check_status_with_resp_body(&body, strlen(buffer), "ETag");
CuAssertIntEquals(tc, res, COS_TRUE);
printf("test_cos_status_parse_from_body 400 1ok\n");
}
{
cos_list_t body;
cos_list_init(&body);
const char *buffer = "<root><Status>active</Status><Name>test</Name><ForcedReplacement>test</ForcedReplacement></DomainRule></root>";

cos_buf_t *b;
int len = strlen(buffer);
b = cos_create_buf(pool, len);
memcpy(b->pos, buffer, len);
b->last += len;
cos_list_add_tail(&b->node, &body);
cos_status_t s;
int res = check_status_with_resp_body(&body, strlen(buffer), "ETag");
CuAssertIntEquals(tc, res, COS_FALSE);
printf("test_cos_status_parse_from_body 400 1ok\n");
}
{
cos_list_t body;
cos_list_init(&body);
Expand Down

0 comments on commit a744fc6

Please sign in to comment.