示例#1
0
int main(int argc, const char *argv[])
{
    tstring_t *tstr1 = NULL;
    tstring_t *tstr2 = NULL;

    tstr1 = tstring_new("hello world");
    tstr2 = tstring_new("");

    printf("tstr1: %s\n", tstring_data(tstr1));
    tstr1 = tstring_assign(tstr1, "hello world tstr1 after reassignment.");
    tstr1 = tstring_prepend(tstr1, "i'm precedence.");
    tstr1 = tstring_prepend_c(tstr1, 'O');
    printf("tstr1: %s\n", tstring_data(tstr1));

    tstr2 = tstring_assign(tstr2, "hello world tstr2.");
    printf("tstr2: %s\n", tstring_data(tstr2));

    tstring_t *tstr3 = NULL;
    tstr3 = tstring_new_len("hello world\0hello world", 23);

    printf("tstr3 size: %d\n", tstring_size(tstr3));

  
    tstring_t *tstr4 = tstring_new("hello world");
 
    printf("tstr1 equals to tstr4 ?: %d\n", tstring_equal(tstr1, tstr4));
    tstring_prepend(tstr4, "hi但是");
    tstring_insert(tstr4, 1, "willlllllllllllllll");
    printf("%s\n", tstring_data(tstr4));
    tstring_free(tstr1);
    tstring_free(tstr2);
    tstring_free(tstr3);
	tstring_free(tstr4);
}
oss_post_object_group_result_t *
client_post_object_group(oss_client_t *client,
		oss_post_object_group_request_t *request,
		unsigned short *retcode)
{
	assert(client != NULL);
	assert(request != NULL);

	curl_request_param_t *user_data = (curl_request_param_t *)malloc(sizeof(curl_request_param_t));
	user_data->send_buffer = (param_buffer_t *)malloc(sizeof(param_buffer_t));
	//user_data->send_buffer->ptr = (char *)malloc(sizeof(char) * 128 * 1024);
	user_data->send_buffer->fp = NULL;


	user_data->recv_buffer = (param_buffer_t *)malloc(sizeof(param_buffer_t));
	user_data->recv_buffer->ptr = (char *)malloc(sizeof(char) * MAX_RECV_BUFFER_SIZE);
	user_data->recv_buffer->fp = NULL;
	user_data->recv_buffer->left = MAX_RECV_BUFFER_SIZE;
	user_data->recv_buffer->allocated = MAX_RECV_BUFFER_SIZE;

	user_data->header_buffer = (param_buffer_t *)malloc(sizeof(param_buffer_t));
	user_data->header_buffer->ptr = (char *)malloc(sizeof(char) * MAX_HEADER_BUFFER_SIZE);
	user_data->header_buffer->fp = NULL;
	user_data->header_buffer->left = MAX_HEADER_BUFFER_SIZE;
	user_data->header_buffer->allocated = MAX_HEADER_BUFFER_SIZE;

	unsigned int bucket_name_len = strlen(request->bucket_name);
	unsigned int key_len = strlen(request->key);
	char *resource = (char *)malloc(sizeof(char) * (bucket_name_len + key_len) + 16);
	char *url = (char *)malloc(sizeof(char) *
			(bucket_name_len + key_len + strlen(client->endpoint) + 32));

	char header_host[256]  = {0};
	char header_date[48]  = {0};
	char header_auth[128] = {0};
	char *now;
	char part[256] = {0};
	unsigned int sign_len = 0;
	int parts = 0;
	unsigned int i = 0;

	oss_map_t *default_headers = oss_map_new(16);
	/**
	 * 构造参数,resource,url 赋值,
	 * */
	sprintf(resource, "/%s/%s?group", request->get_bucket_name(request),
			request->get_key(request));
	sprintf(url, "%s/%s/%s?group", client->endpoint, request->get_bucket_name(request),
			request->get_key(request));
	sprintf(header_host, "Host: %s", client->endpoint);
	now = (char *)oss_get_gmt_time();
	sprintf(header_date, "Date: %s", now);

	/**
	 * 请求头部构造
	 */
	oss_map_put(default_headers, OSS_DATE, now);
	oss_map_put(default_headers, OSS_CONTENT_TYPE, "application/x-www-form-urlencoded");
	
	/**
	 * 生成签名值
	 */
	char *sign = (char *)generate_authentication(client->access_key, OSS_HTTP_POST,
			default_headers, NULL, resource, &sign_len);

	sprintf(header_auth, "Authorization: OSS %s:%s", client->access_id, sign);

	oss_object_group_item_t **part_item = request->get_items(request, &parts);
	tstring_t *tstr_part_item = 
		tstring_new("<CreateFileGroup>");
	for (; i < parts; i++) {
		sprintf(part, "<Part><PartNumber>%d</PartNumber><PartName>%s</PartName><ETag>%s</ETag></Part>",
				(*(part_item + i))->get_part_number(*(part_item + i)),
				(*(part_item + i))->get_part_name(*(part_item + i)),
				(*(part_item + i))->get_etag(*(part_item + i)));
		tstring_append(tstr_part_item, part);
	}
	tstring_append(tstr_part_item, "</CreateFileGroup>\n");
	unsigned int tmp_len = strlen(tstring_data(tstr_part_item));
	char *tmp = (char *)malloc(sizeof(char) * (tmp_len + 1));
	strncpy(tmp, tstring_data(tstr_part_item), tmp_len);
	tmp[tmp_len] = '\0';

	user_data->send_buffer->ptr = tmp;
	//user_data->send_buffer->ptr = (char *)(tstring_data(tstr_part_item));
	user_data->send_buffer->left = tstring_size(tstr_part_item);
	user_data->send_buffer->allocated = tstring_size(tstr_part_item);
	/**
	 * 自定义 HTTP 请求头部
	 */
	struct curl_slist *http_headers = NULL;

	http_headers = curl_slist_append(http_headers, header_host);
	http_headers = curl_slist_append(http_headers, header_date);
	http_headers = curl_slist_append(http_headers, header_auth);

	/**
	 * 发送请求
	 */
	object_group_curl_operation(OSS_HTTP_POST, resource, url, http_headers, user_data);

	/**
	 * 释放 http_headers资源
	 */
	curl_slist_free_all(http_headers);
	tstring_free(tstr_part_item);
	oss_map_delete(default_headers);
	if(now != NULL) {
		free(now);
		now = NULL;
	}
	if(sign != NULL) {
		free(sign);
		sign = NULL;
	}
	if(resource != NULL) {
		free(resource);
		resource = NULL;
	}
	if(url != NULL) {
		free(url);
		url = NULL;
	}
	if (user_data->header_buffer->code != 200) {
		if (retcode != NULL)
			*retcode = oss_get_retcode_from_response(user_data->recv_buffer->ptr);
		oss_free_user_data(user_data);
		return NULL;
	} else {
		if (retcode != NULL) *retcode = 0;
		return construct_post_object_group_response(user_data);
	}
}