Skip to content

Commit

Permalink
Merge pull request #21 from aliyun/proxy
Browse files Browse the repository at this point in the history
proxy
  • Loading branch information
qiyuewuyi authored Dec 28, 2016
2 parents 05ff953 + ae5caf0 commit 0181077
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 4 deletions.
4 changes: 3 additions & 1 deletion oss_c_sdk/aos_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ typedef apr_array_header_t aos_array_header_t;
#define CR (char) 13
#define CRLF "\x0d\x0a"

#define AOS_VERSION "3.2.1"
#define AOS_VERSION "3.3.0"
#define AOS_VER "aliyun-sdk-c/" AOS_VERSION

#define AOS_HTTP_PREFIX "http://"
#define AOS_HTTPS_PREFIX "https://"
#define AOS_RTMP_PREFIX "rtmp://"

#define AOS_TEMP_FILE_SUFFIX ".tmp"

#define AOS_FALSE 0
#define AOS_TRUE 1

Expand Down
3 changes: 3 additions & 0 deletions oss_c_sdk/aos_http_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ aos_http_request_options_t *aos_http_request_options_create(aos_pool_t *p)
options->dns_cache_timeout = AOS_DNS_CACHE_TIMOUT;
options->max_memory_size = AOS_MAX_MEMORY_SIZE;
options->enable_crc = AOS_TRUE;
options->proxy_auth = NULL;
options->proxy_host = NULL;

return options;
}
Expand All @@ -102,6 +104,7 @@ aos_http_controller_t *aos_http_controller_create(aos_pool_t *p, int owner)
return NULL;
}
}

ctl = (aos_http_controller_t *)aos_pcalloc(p, sizeof(aos_http_controller_ex_t));
ctl->pool = p;
ctl->owner = owner;
Expand Down
1 change: 1 addition & 0 deletions oss_c_sdk/aos_http_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIBAOS_HTTP_IO_H

#include "aos_transport.h"
#include "oss_define.h"

AOS_CPP_START

Expand Down
8 changes: 8 additions & 0 deletions oss_c_sdk/aos_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ static APR_INLINE int aos_is_space_or_cntrl(char c)
return c <= ' ';
}

static APR_INLINE int aos_is_null_string(aos_string_t *str)
{
if (str == NULL || str->data == NULL || str->len == 0) {
return AOS_TRUE;
}
return AOS_FALSE;
}

void aos_strip_space(aos_string_t *str);
void aos_trip_space_and_cntrl(aos_string_t *str);
void aos_unquote_str(aos_string_t *str);
Expand Down
11 changes: 11 additions & 0 deletions oss_c_sdk/aos_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ int aos_curl_transport_setup(aos_curl_http_transport_t *t)
aos_init_curl_headers(t);
curl_easy_setopt_safe(CURLOPT_HTTPHEADER, t->headers);

if (t->controller->options->proxy_host != NULL) {
// proxy
curl_easy_setopt_safe(CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_easy_setopt_safe(CURLOPT_PROXY, t->controller->options->proxy_host);
// authorize
if (t->controller->options->proxy_auth != NULL) {
curl_easy_setopt_safe(CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_easy_setopt_safe(CURLOPT_PROXYUSERPWD, t->controller->options->proxy_auth);
}
}

if (NULL == t->req->signed_url) {
if (aos_init_curl_url(t) != AOSE_OK) {
return t->controller->error_code;
Expand Down
3 changes: 3 additions & 0 deletions oss_c_sdk/aos_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "aos_define.h"
#include "aos_buf.h"


AOS_CPP_START

typedef struct aos_http_request_s aos_http_request_t;
Expand Down Expand Up @@ -31,6 +32,8 @@ struct aos_http_request_options_s {
int connect_timeout;
int64_t max_memory_size;
int enable_crc;
char *proxy_host;
char *proxy_auth;
};

struct aos_http_transport_options_s {
Expand Down
2 changes: 2 additions & 0 deletions oss_c_sdk/aos_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ unsigned long long aos_strtoull(const char *nptr, char **endptr, int base)
}
}
if (neg && any > 0)
#pragma warning(disable : 4146)
acc = -acc;
#pragma warning(default : 4146)
if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr);
return (acc);
Expand Down
4 changes: 4 additions & 0 deletions oss_c_sdk/oss_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ typedef struct {
aos_string_t access_key_secret;
aos_string_t sts_token;
int is_cname;
aos_string_t proxy_host;
int proxy_port;
aos_string_t proxy_user;
aos_string_t proxy_passwd;
} oss_config_t;

typedef struct {
Expand Down
32 changes: 30 additions & 2 deletions oss_c_sdk/oss_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,18 @@ aos_status_t *oss_do_get_object_to_file(const oss_request_options_t *options,
aos_http_request_t *req = NULL;
aos_http_response_t *resp = NULL;
int res = AOSE_OK;
aos_string_t tmp_filename;

headers = aos_table_create_if_null(options, headers, 0);
params = aos_table_create_if_null(options, params, 0);

oss_get_temporary_file_name(options->pool, filename, &tmp_filename);

oss_init_object_request(options, bucket, object, HTTP_GET,
&req, params, headers, progress_callback, 0, &resp);

s = aos_status_create(options->pool);
res = oss_init_read_response_body_to_file(options->pool, filename, resp);
res = oss_init_read_response_body_to_file(options->pool, &tmp_filename, resp);
if (res != AOSE_OK) {
aos_file_error_status_set(s, res);
return s;
Expand All @@ -220,6 +223,8 @@ aos_status_t *oss_do_get_object_to_file(const oss_request_options_t *options,
oss_check_crc_consistent(resp->crc64, resp->headers, s);
}

oss_temp_file_rename(s, tmp_filename.data, filename->data, options->pool);

return s;
}

Expand Down Expand Up @@ -502,6 +507,10 @@ aos_status_t *oss_put_object_from_buffer_by_url(const oss_request_options_t *opt
s = oss_process_signed_request(options, req, resp);
oss_fill_read_response_header(resp, resp_headers);

if (is_enable_crc(options) && has_crc_in_response(resp)) {
oss_check_crc_consistent(req->crc64, resp->headers, s);
}

return s;
}

Expand Down Expand Up @@ -533,6 +542,10 @@ aos_status_t *oss_put_object_from_file_by_url(const oss_request_options_t *optio
s = oss_process_signed_request(options, req, resp);
oss_fill_read_response_header(resp, resp_headers);

if (is_enable_crc(options) && has_crc_in_response(resp)) {
oss_check_crc_consistent(req->crc64, resp->headers, s);
}

return s;
}

Expand All @@ -557,6 +570,11 @@ aos_status_t *oss_get_object_to_buffer_by_url(const oss_request_options_t *optio
oss_fill_read_response_body(resp, buffer);
oss_fill_read_response_header(resp, resp_headers);

if (is_enable_crc(options) && has_crc_in_response(resp) &&
!has_range_or_process_in_request(req)) {
oss_check_crc_consistent(resp->crc64, resp->headers, s);
}

return s;
}

Expand All @@ -571,11 +589,14 @@ aos_status_t *oss_get_object_to_file_by_url(const oss_request_options_t *options
aos_http_request_t *req = NULL;
aos_http_response_t *resp = NULL;
int res = AOSE_OK;
aos_string_t tmp_filename;

s = aos_status_create(options->pool);

headers = aos_table_create_if_null(options, headers, 0);
params = aos_table_create_if_null(options, params, 0);

oss_get_temporary_file_name(options->pool, filename, &tmp_filename);

oss_init_signed_url_request(options, signed_url, HTTP_GET,
&req, params, headers, &resp);
Expand All @@ -588,7 +609,14 @@ aos_status_t *oss_get_object_to_file_by_url(const oss_request_options_t *options

s = oss_process_signed_request(options, req, resp);
oss_fill_read_response_header(resp, resp_headers);


if (is_enable_crc(options) && has_crc_in_response(resp) &&
!has_range_or_process_in_request(req)) {
oss_check_crc_consistent(resp->crc64, resp->headers, s);
}

oss_temp_file_rename(s, tmp_filename.data, filename->data, options->pool);

return s;
}

Expand Down
47 changes: 47 additions & 0 deletions oss_c_sdk/oss_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ oss_config_t *oss_config_create(aos_pool_t *p)
return (oss_config_t *)aos_pcalloc(p, sizeof(oss_config_t));
}

void oss_config_resolve(aos_pool_t *pool, oss_config_t *config, aos_http_controller_t *ctl)
{
if(!aos_is_null_string(&config->proxy_host)) {
// proxy host:port
if (config->proxy_port == 0) {
ctl->options->proxy_host = apr_psprintf(pool, "%.*s", config->proxy_host.len, config->proxy_host.data);
} else {
ctl->options->proxy_host = apr_psprintf(pool, "%.*s:%d", config->proxy_host.len, config->proxy_host.data,
config->proxy_port);
}
// authorize user:passwd
if (!aos_is_null_string(&config->proxy_user) && !aos_is_null_string(&config->proxy_passwd)) {
ctl->options->proxy_auth = apr_psprintf(pool, "%.*s:%.*s", config->proxy_user.len,
config->proxy_user.data, config->proxy_passwd.len, config->proxy_passwd.data);
}
}
}

oss_request_options_t *oss_request_options_create(aos_pool_t *p)
{
int s;
Expand Down Expand Up @@ -825,3 +843,32 @@ int oss_check_crc_consistent(uint64_t crc, const apr_table_t *resp_headers, aos_
}
return res;
}

int oss_get_temporary_file_name(aos_pool_t *p, const aos_string_t *filename, aos_string_t *temp_file_name)
{
int len = filename->len + 1;
char *temp_file_name_ptr = NULL;

len += strlen(AOS_TEMP_FILE_SUFFIX);
temp_file_name_ptr = aos_pcalloc(p, len);

apr_snprintf(temp_file_name_ptr, len, "%.*s%s", filename->len, filename->data, AOS_TEMP_FILE_SUFFIX);
aos_str_set(temp_file_name, temp_file_name_ptr);

return len;
}

int oss_temp_file_rename(aos_status_t *s, const char *from_path, const char *to_path, apr_pool_t *pool)
{
int res = -1;

if (s != NULL) {
if (aos_status_is_ok(s)) {
res = apr_file_rename(from_path, to_path, pool);
} else {
res = apr_file_remove(from_path, pool);
}
}

return res;
}
9 changes: 9 additions & 0 deletions oss_c_sdk/oss_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const char *get_oss_acl_str(oss_acl_e oss_acl);
**/
oss_config_t *oss_config_create(aos_pool_t *p);

/**
* @brief evaluate config to curl
**/
void oss_config_resolve(aos_pool_t *pool, oss_config_t *config, aos_http_controller_t *ctl);

/**
* @brief create oss request options
* @return oss request options
Expand Down Expand Up @@ -265,6 +270,10 @@ int has_range_or_process_in_request(const aos_http_request_t *req) ;
**/
int oss_check_crc_consistent(uint64_t crc, const apr_table_t *resp_headers, aos_status_t *s);

int oss_get_temporary_file_name(aos_pool_t *p, const aos_string_t *filename, aos_string_t *temp_file_name);

int oss_temp_file_rename(aos_status_t *s, const char *from_path, const char *to_path, apr_pool_t *pool);

OSS_CPP_END

#endif
2 changes: 1 addition & 1 deletion oss_c_sdk_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ configure_file (
set(SAMPLE_SOURCE_FILES CuTest.c oss_test_util.c oss_config.c test_oss_bucket.c
test_oss_object.c test_oss_multipart.c test_oss_live.c test_oss_image.c
test_oss_progress.c test_oss_callback.c test_oss_crc.c cjson_utils.c cjson.c
test_aos.c test_all.c)
test_oss_proxy.c test_aos.c test_all.c)

# find_path(APR_INCLUDE_DIR apr-1/apr_time.h)
# find_path(APR_UTIL_INCLUDE_DIR apr/include/apr-1/apr_md5.h)
Expand Down
1 change: 1 addition & 0 deletions oss_c_sdk_test/oss_c_sdk_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ClCompile Include="test_oss_multipart.c" />
<ClCompile Include="test_oss_object.c" />
<ClCompile Include="test_oss_progress.c" />
<ClCompile Include="test_oss_proxy.c" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6373CC30-480D-4FC2-9EB5-2E9835DBB1C4}</ProjectGuid>
Expand Down
21 changes: 21 additions & 0 deletions oss_c_sdk_test/oss_test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ unsigned long get_file_size(const char *file_path)
return filesize;
}

char *decrypt(const char *encrypted_str, aos_pool_t *pool)
{
char *res_str = NULL;
int i = 0;

if (encrypted_str == NULL) {
return NULL;
}

res_str = (char *)aos_palloc(pool, strlen(encrypted_str) + 1);

while (*encrypted_str != '\0') {
res_str[i] = 0x6a ^ *encrypted_str;
encrypted_str++;
i++;
}
res_str[i] = '\0';

return res_str;
}

void progress_callback(int64_t consumed_bytes, int64_t total_bytes)
{
assert(total_bytes >= consumed_bytes);
Expand Down
2 changes: 2 additions & 0 deletions oss_c_sdk_test/oss_test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ char *gen_test_signed_url(const oss_request_options_t *options, const char *buck

unsigned long get_file_size(const char *file_path);

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

void percentage(int64_t consumed_bytes, int64_t total_bytes);
void progress_callback(int64_t consumed_bytes, int64_t total_bytes);

Expand Down
2 changes: 2 additions & 0 deletions oss_c_sdk_test/test_all.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern CuSuite *test_oss_util();
extern CuSuite *test_oss_xml();
extern CuSuite *test_oss_crc();
extern CuSuite *test_aos();
extern CuSuite *test_oss_proxy();

static const struct testlist {
const char *testname;
Expand All @@ -31,6 +32,7 @@ static const struct testlist {
{"test_oss_progress", test_oss_progress},
{"test_oss_callback", test_oss_callback},
{"test_oss_crc", test_oss_crc},
{"test_oss_proxy", test_oss_proxy},
{"test_aos", test_aos},
{"LastTest", NULL}
};
Expand Down
1 change: 1 addition & 0 deletions oss_c_sdk_test/test_oss_bucket.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ void test_list_object_with_delimiter(CuTest *tc)
params = oss_create_list_object_params(p);
params->max_ret = 5;
params->truncated = 0;
aos_str_set(&params->prefix, "oss_tmp");
aos_str_set(&params->delimiter, "/");
aos_str_set(&bucket, TEST_BUCKET_NAME);
s = oss_list_object(options, &bucket, params, &resp_headers);
Expand Down
Loading

0 comments on commit 0181077

Please sign in to comment.