2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "download-agent-http-msg-handler.h"
22 #include "download-agent-debug.h"
23 #include "download-agent-http-misc.h"
24 #include "download-agent-encoding.h"
26 // '.' and ';' are request from Vodafone
27 #define IS_TERMINATING_CHAR(c) ( ((c) == ';') || ((c) == '\0') || ((c) == 0x0d) || ((c) == 0x0a) || ((c) == 0x20) )
28 #define IS_TERMINATING_CHAR_EX(c) ( ((c) == '"') || ((c) == ';') || ((c) == '\0') || ((c) == 0x0d) || ((c) == 0x0a) || ((c) == 0x20) )
29 #define IS_URI_TERMINATING_CHAR(c) ( ((c) == '\0') || ((c) == 0x0d) || ((c) == 0x0a) || ((c) == 0x20) )
33 WITHOUT_PARSING_OPTION
36 static da_result_t __http_header_add_field(http_header_t **head,
37 const char *field, const char *value, enum parsing_type type);
38 static void __http_header_destroy_all_field(http_header_t **head);
39 static da_bool_t __get_http_header_for_field(
40 http_msg_response_t *http_msg_response, const char *in_field,
41 http_header_t **out_header);
42 static void __exchange_header_value(http_header_t *header,
43 const char *in_raw_value);
45 static http_header_options_t *__create_http_header_option(const char *field,
47 static void __http_header_destroy_all_option(http_header_options_t **head);
48 static da_bool_t __get_http_header_option_for_field(
49 http_header_options_t *header_option, const char *in_field,
52 static http_header_options_t *__parsing_N_create_option_str(char *org_str);
53 static http_header_options_t *__parsing_options(char *org_str);
54 static void __parsing_raw_value(http_header_t *http_header);
56 da_result_t http_msg_request_create(http_msg_request_t **http_msg_request)
58 http_msg_request_t *temp_http_msg_request = NULL;
60 DA_LOG_FUNC_START(HTTPManager);
62 temp_http_msg_request = (http_msg_request_t *)calloc(1,
63 sizeof(http_msg_request_t));
64 if (!temp_http_msg_request) {
65 *http_msg_request = NULL;
66 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
67 return DA_ERR_FAIL_TO_MEMALLOC;
70 temp_http_msg_request->http_method = NULL;
71 temp_http_msg_request->url = NULL;
72 temp_http_msg_request->head = NULL;
73 temp_http_msg_request->http_body = NULL;
75 *http_msg_request = temp_http_msg_request;
76 DA_LOG(HTTPManager, "http_msg_request: %x", (unsigned int)(*http_msg_request));
81 void http_msg_request_destroy(http_msg_request_t **http_msg_request)
83 http_msg_request_t *temp_http_msg_request = *http_msg_request;
85 DA_LOG_FUNC_START(HTTPManager);
87 if (temp_http_msg_request) {
88 if (temp_http_msg_request->http_method) {
89 free(temp_http_msg_request->http_method);
90 temp_http_msg_request->http_method = NULL;
93 if (temp_http_msg_request->url) {
94 free(temp_http_msg_request->url);
95 temp_http_msg_request->url = NULL;
98 if (temp_http_msg_request->http_body) {
99 free(temp_http_msg_request->http_body);
100 temp_http_msg_request->http_body = NULL;
103 __http_header_destroy_all_field(&(temp_http_msg_request->head));
105 free(temp_http_msg_request);
106 *http_msg_request = NULL;
111 da_result_t http_msg_request_set_method(http_msg_request_t *http_msg_request,
114 DA_LOG_FUNC_START(HTTPManager);
116 if (!http_msg_request || !method) {
117 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
118 return DA_ERR_INVALID_ARGUMENT;
121 // ToDo: check method is valid
123 http_msg_request->http_method = strdup(method);
125 DA_LOG(HTTPManager, "http method : %s", http_msg_request->http_method);
130 da_result_t http_msg_request_get_method(http_msg_request_t *http_msg_request,
133 // DA_LOG_FUNC_START(HTTPManager);
135 if (!http_msg_request) {
136 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
137 return DA_ERR_INVALID_ARGUMENT;
140 if (http_msg_request->http_method) {
141 *method = http_msg_request->http_method;
145 return DA_ERR_INVALID_ARGUMENT;
149 da_result_t http_msg_request_set_url(http_msg_request_t *http_msg_request,
152 DA_LOG_FUNC_START(HTTPManager);
154 if (!http_msg_request) {
155 DA_LOG_ERR(HTTPManager, "http_msg_request is NULL; DA_ERR_INVALID_ARGUMENT");
156 return DA_ERR_INVALID_ARGUMENT;
160 DA_LOG_ERR(HTTPManager, "url is NULL; DA_ERR_INVALID_ARGUMENT");
161 return DA_ERR_INVALID_URL;
164 http_msg_request->url = strdup(url);
166 DA_LOG(HTTPManager, "http url : %s", http_msg_request->url);
171 da_result_t http_msg_request_get_url(http_msg_request_t *http_msg_request,
174 // DA_LOG_FUNC_START(HTTPManager);
176 if (!http_msg_request) {
177 DA_LOG_ERR(HTTPManager, "http_msg_request is NULL; DA_ERR_INVALID_ARGUMENT");
178 return DA_ERR_INVALID_ARGUMENT;
181 if (http_msg_request->url) {
182 *url = http_msg_request->url;
186 return DA_ERR_INVALID_ARGUMENT;
190 da_result_t http_msg_request_set_body(http_msg_request_t *http_msg_request,
193 // DA_LOG_FUNC_START(HTTPManager);
195 if (!http_msg_request) {
196 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
197 return DA_ERR_INVALID_ARGUMENT;
203 http_msg_request->http_body = strdup(body);
205 DA_LOG(HTTPManager, "http body : %s", http_msg_request->http_body);
210 da_result_t http_msg_request_get_body(http_msg_request_t *http_msg_request,
213 // DA_LOG_FUNC_START(HTTPManager);
215 if (!http_msg_request) {
216 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
217 return DA_ERR_INVALID_ARGUMENT;
220 if (http_msg_request->http_body) {
221 *body = http_msg_request->http_body;
225 return DA_ERR_INVALID_ARGUMENT;
229 /* FIXME later : check to free filed and value after this API is called */
230 da_result_t http_msg_request_add_field(http_msg_request_t *http_msg_request,
231 const char *field, const char *value)
233 // DA_LOG_FUNC_START(HTTPManager);
235 if (!http_msg_request) {
236 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
237 return DA_ERR_INVALID_ARGUMENT;
240 return __http_header_add_field(&(http_msg_request->head), field, value, WITHOUT_PARSING_OPTION);
243 da_result_t http_msg_response_create(http_msg_response_t **http_msg_response)
245 http_msg_response_t *temp_http_msg_response = NULL;
247 DA_LOG_FUNC_START(HTTPManager);
249 temp_http_msg_response = (http_msg_response_t *)calloc(1,
250 sizeof(http_msg_response_t));
251 if (!temp_http_msg_response) {
252 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
253 return DA_ERR_FAIL_TO_MEMALLOC;
255 temp_http_msg_response->status_code = 0;
256 temp_http_msg_response->head = NULL;
258 *http_msg_response = temp_http_msg_response;
264 void http_msg_response_destroy(http_msg_response_t **http_msg_response)
266 http_msg_response_t *temp_http_msg_response = *http_msg_response;
268 DA_LOG_FUNC_START(HTTPManager);
269 if (temp_http_msg_response) {
270 __http_header_destroy_all_field(&(temp_http_msg_response->head));
272 free(temp_http_msg_response);
273 *http_msg_response = DA_NULL;
277 da_result_t http_msg_response_set_status_code(
278 http_msg_response_t *http_msg_response, int status_code)
280 // DA_LOG_FUNC_START(HTTPManager);
282 if (!http_msg_response) {
283 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
284 return DA_ERR_INVALID_ARGUMENT;
287 http_msg_response->status_code = status_code;
292 da_result_t http_msg_response_get_status_code(
293 http_msg_response_t *http_msg_response, int *status_code)
295 DA_LOG_FUNC_START(HTTPManager);
297 if (!http_msg_response) {
298 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
299 return DA_ERR_INVALID_ARGUMENT;
302 *status_code = http_msg_response->status_code;
307 da_result_t http_msg_response_add_field(http_msg_response_t *http_msg_response,
308 const char *field, const char *value)
310 // DA_LOG_FUNC_START(HTTPManager);
312 if (!http_msg_response) {
313 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
314 return DA_ERR_INVALID_ARGUMENT;
317 return __http_header_add_field(&(http_msg_response->head), field, value, WITH_PARSING_OPTION);
320 da_result_t __http_header_add_field(http_header_t **head,
321 const char *field, const char *value, enum parsing_type type)
323 http_header_t *pre = NULL;
324 http_header_t *cur = NULL;
326 // DA_LOG_FUNC_START(HTTPManager);
327 DA_LOG(HTTPManager, "[%s][%s]", field, value);
332 /* Replace default value with user wanted value
333 * Remove the value which is stored before and add a new value.
335 if (cur->field && cur->raw_value &&
336 strncmp(cur->field, field, strlen(field)) == 0) {
337 DA_LOG(HTTPManager, "Remove value for replacement [%s][%s]", cur->field, cur->raw_value);
342 if (cur->raw_value) {
343 free(cur->raw_value);
344 cur->raw_value= NULL;
350 cur = (http_header_t *)calloc(1, sizeof(http_header_t));
352 cur->field = strdup(field);
353 cur->raw_value = strdup(value);
357 if (type == WITHOUT_PARSING_OPTION) {
358 cur->value = strdup(value);
361 __parsing_raw_value(cur);
369 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
370 return DA_ERR_FAIL_TO_MEMALLOC;
376 void __http_header_destroy_all_field(http_header_t **head)
378 http_header_t *pre = NULL;
379 http_header_t *cur = NULL;
381 // DA_LOG_FUNC_START(HTTPManager);
387 DA_LOG_VERBOSE(HTTPManager, "field= %s", cur->field);
389 cur->field = DA_NULL;
394 cur->value = DA_NULL;
397 if (cur->raw_value) {
398 free(cur->raw_value);
399 cur->raw_value = DA_NULL;
402 __http_header_destroy_all_option(&(cur->options));
413 http_header_options_t *__create_http_header_option(const char *field,
416 http_header_options_t *option = NULL;
418 option = (http_header_options_t *)calloc(1,
419 sizeof(http_header_options_t));
422 option->field = strdup(field);
425 option->value = strdup(value);
433 void __http_header_destroy_all_option(http_header_options_t **head)
435 http_header_options_t *pre = NULL;
436 http_header_options_t *cur = NULL;
438 // DA_LOG_FUNC_START(HTTPManager);
444 DA_LOG_VERBOSE(HTTPManager, "field= %s", cur->field);
446 cur->field = DA_NULL;
451 cur->value = DA_NULL;
463 da_result_t http_msg_request_get_iter(http_msg_request_t *http_msg_request,
464 http_msg_iter_t *http_msg_iter)
466 DA_LOG_FUNC_START(HTTPManager);
468 if (!http_msg_request) {
469 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
470 return DA_ERR_INVALID_ARGUMENT;
473 *http_msg_iter = http_msg_request->head;
478 da_result_t http_msg_response_get_iter(http_msg_response_t *http_msg_response,
479 http_msg_iter_t *http_msg_iter)
481 // DA_LOG_FUNC_START(HTTPManager);
483 if (!http_msg_response) {
484 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
485 return DA_ERR_INVALID_ARGUMENT;
488 *http_msg_iter = http_msg_response->head;
489 // DA_LOG(HTTPManager, "retrieve iter = 0x%x", (unsigned int)http_msg_iter);
494 da_bool_t http_msg_get_field_with_iter(http_msg_iter_t *http_msg_iter,
495 char **out_field, char **out_value)
497 http_header_t *cur = *http_msg_iter;
499 // DA_LOG_FUNC_START(HTTPManager);
501 // DA_LOG(HTTPManager, "getting iter = 0x%x", (unsigned int)cur);
504 *out_field = cur->field;
505 *out_value = cur->value;
506 *http_msg_iter = cur->next;
510 // DA_LOG(HTTPManager, "end of iter");
515 da_bool_t http_msg_get_header_with_iter(http_msg_iter_t *http_msg_iter,
516 char **out_field, http_header_t **out_header)
518 http_header_t *cur = *http_msg_iter;
520 // DA_LOG_FUNC_START(HTTPManager);
522 // DA_LOG(HTTPManager, "getting iter = 0x%x", (unsigned int)cur);
525 *out_field = cur->field;
527 *http_msg_iter = cur->next;
531 // DA_LOG(HTTPManager, "end of iter");
536 http_header_options_t *__parsing_N_create_option_str(char *org_str)
538 char *option_field = NULL;
539 char *option_value = NULL;
540 int option_field_len = 0;
541 int option_value_len = 0;
543 char *org_pos = NULL;
546 char *working_str = NULL;
547 char *working_pos = NULL;
548 char *working_pos_field_start = NULL;
549 char *working_pos_value_start = NULL;
551 da_bool_t is_inside_quotation = DA_FALSE;
552 da_bool_t is_working_for_field = DA_TRUE;
554 http_header_options_t *option = NULL;
556 // DA_LOG_FUNC_START(HTTPManager);
561 org_str_len = strlen(org_str);
562 if (org_str_len <= 0)
565 working_str = (char *)calloc(1, org_str_len + 1);
570 working_pos_field_start = working_pos = working_str;
572 for (i = 0; i < org_str_len; i++) {
574 is_inside_quotation = !is_inside_quotation;
576 if (is_inside_quotation) {
577 // Leave anything including blank if it is inside of double quotation mark.
578 *working_pos = *org_pos;
579 is_working_for_field ? option_field_len++
580 : option_value_len++;
584 if (*org_pos == ' ') {
586 } else if (*org_pos == '=') {
587 if (is_working_for_field) {
588 is_working_for_field = DA_FALSE;
589 working_pos_value_start = working_pos;
594 *working_pos = *org_pos;
595 is_working_for_field ? option_field_len++
596 : option_value_len++;
603 if (option_field_len > 0 && working_pos_field_start) {
604 option_field = (char *)calloc(1, option_field_len + 1);
606 strncpy(option_field, working_pos_field_start,
610 if (option_value_len > 0 && working_pos_value_start) {
611 option_value = (char *)calloc(1, option_value_len + 1);
613 strncpy(option_value, working_pos_value_start,
619 working_pos = working_str = NULL;
622 DA_LOG(HTTPManager, "option_field = [%s], option_value = [%s]",
623 option_field, option_value);
625 if (option_field || option_value) {
626 option = __create_http_header_option(
627 option_field, option_value);
641 http_header_options_t *__parsing_options(char *org_str)
643 da_result_t ret = DA_RESULT_OK;
644 http_header_options_t *head = NULL;
645 http_header_options_t *pre = NULL;
646 http_header_options_t *cur = NULL;
648 int wanted_str_len = 0;
649 char *wanted_str = NULL;
650 char *wanted_str_start = NULL;
651 char *wanted_str_end = NULL;
652 char *cur_pos = NULL;
654 DA_LOG_FUNC_START(HTTPManager);
659 /* Do Not use strtok(). It's not thread safe. */
660 // DA_LOG_CRITICAL(HTTPManager, "org_str = %s", org_str);
665 wanted_str_start = cur_pos;
666 wanted_str_end = strchr(cur_pos, ';');
667 if (wanted_str_end) {
668 cur_pos = wanted_str_end + 1;
670 wanted_str_end = org_str + strlen(org_str);
674 wanted_str_len = wanted_str_end - wanted_str_start;
675 wanted_str = (char *)calloc(1, wanted_str_len + 1);
677 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
678 ret = DA_ERR_FAIL_TO_MEMALLOC;
681 strncpy(wanted_str, wanted_str_start, wanted_str_len);
683 // DA_LOG_CRITICAL(HTTPManager, "wanted_str = [%s]", wanted_str);
684 cur = __parsing_N_create_option_str(wanted_str);
697 if (ret != DA_RESULT_OK)
698 __http_header_destroy_all_option(&head);
703 void __parsing_raw_value(http_header_t *http_header_field)
705 char *raw_value = NULL;
706 char *option_str_start = NULL;
708 char *trimed_value = NULL;
709 int trimed_value_len = 0;
711 char *trimed_value_start = NULL;
712 char *trimed_value_end = NULL;
714 // DA_LOG_FUNC_START(HTTPManager);
716 raw_value = http_header_field->raw_value;
717 // DA_LOG_CRITICAL(HTTPManager, "raw_value = [%s]", raw_value);
722 trimed_value_start = raw_value;
724 trimed_value_end = strchr(raw_value, ';');
725 if (!trimed_value_end) {
727 http_header_field->value = strdup(raw_value);
728 http_header_field->options = NULL;
734 trimed_value_len = trimed_value_end - trimed_value_start;
736 trimed_value = (char *)calloc(1, trimed_value_len + 1);
738 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
741 strncpy(trimed_value, trimed_value_start, trimed_value_len);
742 http_header_field->value = trimed_value;
744 // for option parsing
745 option_str_start = trimed_value_end + 1;
747 http_header_field->options = __parsing_options(option_str_start);
750 http_header_options_t *cur = NULL;
752 cur = http_header_field->options;
754 DA_LOG(HTTPManager, "field = [%s], value = [%s]", cur->field, cur->value);
760 da_bool_t __get_http_header_option_for_field(
761 http_header_options_t *header_option, const char *in_field,
764 http_header_options_t *cur = NULL;
766 // DA_LOG_FUNC_START(HTTPManager);
768 if (!header_option) {
769 DA_LOG_ERR(HTTPManager, "input header_option is NULL.");
776 if (!strncmp(cur->field, in_field, strlen(cur->field)) &&
778 DA_LOG(HTTPManager, "[%s][%s]", cur->field, cur->value);
779 *out_value = cur->value;
790 da_bool_t __get_http_header_for_field(http_msg_response_t *http_msg_response,
791 const char *in_field, http_header_t **out_header)
793 http_msg_iter_t http_msg_iter;
794 http_header_t *header = NULL;
797 //DA_LOG_FUNC_START(HTTPManager);
799 http_msg_response_get_iter(http_msg_response, &http_msg_iter);
800 while (http_msg_get_header_with_iter(&http_msg_iter, &field, &header)) {
801 if (field && header && !strncmp(field, in_field, strlen(field))) {
802 DA_LOG_VERBOSE(HTTPManager, "[%s][%s]", field, header->value);
803 *out_header = header;
811 void __exchange_header_value(http_header_t *header, const char *in_raw_value)
813 DA_LOG_FUNC_START(HTTPManager);
815 if (!header || !in_raw_value)
818 __http_header_destroy_all_option(&(header->options));
822 header->value = DA_NULL;
825 if (header->raw_value)
826 free(header->raw_value);
827 header->raw_value = strdup(in_raw_value);
829 __parsing_raw_value(header);
832 da_bool_t http_msg_response_get_content_type(
833 http_msg_response_t *http_msg_response, char **out_type)
835 da_bool_t b_ret = DA_FALSE;
836 http_header_t *header = NULL;
838 DA_LOG_FUNC_START(HTTPManager);
840 b_ret = __get_http_header_for_field(http_msg_response, "Content-Type",
843 DA_LOG(HTTPManager, "no Content-Type");
848 *out_type = strdup(header->value);
853 void http_msg_response_set_content_type(http_msg_response_t *http_msg_response,
856 da_bool_t b_ret = DA_FALSE;
857 http_header_t *header = NULL;
859 DA_LOG_FUNC_START(HTTPManager);
861 if (!http_msg_response || !in_type)
864 b_ret = __get_http_header_for_field(http_msg_response, "Content-Type",
867 if (header->raw_value && (!strncmp(header->raw_value, in_type,
868 strlen(header->raw_value))))
871 DA_LOG(HTTPManager, "exchange Content-Type to [%s] from [%s]", in_type, header->value);
872 __exchange_header_value(header, in_type);
874 __http_header_add_field(&(http_msg_response->head),
875 "Content-Type", in_type, WITH_PARSING_OPTION);
879 da_bool_t http_msg_response_get_content_length(
880 http_msg_response_t *http_msg_response, unsigned long long *out_length)
882 da_bool_t b_ret = DA_FALSE;
883 http_header_t *header = NULL;
885 DA_LOG_FUNC_START(HTTPManager);
887 b_ret = __get_http_header_for_field(http_msg_response,
888 "Content-Length", &header);
890 DA_LOG(HTTPManager, "no Content-Length");
895 *out_length = atoll(header->value);
900 da_bool_t http_msg_response_get_content_disposition(
901 http_msg_response_t *http_msg_response, char **out_disposition,
902 char **out_file_name)
904 da_bool_t b_ret = DA_FALSE;
905 http_header_t *header = NULL;
906 char *file_name = NULL;
908 char *wanted_str = NULL;
909 char *wanted_str_start = NULL;
910 char *wanted_str_end = NULL;
911 char *decoded_str = NULL;
912 int wanted_str_len = 0;
914 DA_LOG_FUNC_START(HTTPManager);
916 b_ret = __get_http_header_for_field(http_msg_response,
917 "Content-Disposition", &header);
919 DA_LOG(HTTPManager, "no Content-Disposition");
924 *out_disposition = strdup(header->value);
929 b_ret = __get_http_header_option_for_field(header->options, "filename",
932 DA_LOG(HTTPManager, "no option");
936 // eliminate double quotation mark if it exists on derived value
937 wanted_str_start = strchr(file_name, '"');
938 if (!wanted_str_start) {
939 *out_file_name = strdup(file_name);
942 // DA_LOG(HTTPManager, "wanted_str_start = [%s]", wanted_str_start);
944 wanted_str_end = strchr(wanted_str_start, '"');
945 if (wanted_str_end) {
946 wanted_str_len = wanted_str_end - wanted_str_start;
947 wanted_str = (char*)calloc(1, wanted_str_len + 1);
949 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
952 strncpy(wanted_str, wanted_str_start, wanted_str_len);
954 b_ret = is_base64_encoded_word(wanted_str);
956 DA_LOG(HTTPManager, "It's base64 encoded-word string");
957 if (DA_RESULT_OK == decode_base64_encoded_str(
958 wanted_str, &decoded_str)) {
959 DA_LOG(HTTPManager, "base64 decoded str = [%s]", decoded_str);
961 wanted_str = decoded_str;
964 DA_LOG(HTTPManager, "Fail to base64 decode. Just use un-decoded string.");
967 DA_LOG(HTTPManager, "It's NOT base64 encoded-word string");
969 decode_url_encoded_str(wanted_str, &decoded_str);
970 /* If it is url encoded string */
972 DA_LOG(HTTPManager, "Url decoded str = [%s]", decoded_str);
974 wanted_str = decoded_str;
978 *out_file_name = wanted_str;
980 DA_LOG(HTTPManager, "out_file_name = [%s]", *out_file_name);
984 DA_LOG_ERR(HTTPManager, "Not matched \" !");
990 da_bool_t http_msg_response_get_ETag(http_msg_response_t *http_msg_response,
993 da_bool_t b_ret = DA_FALSE;
994 http_header_t *header = NULL;
996 DA_LOG_FUNC_START(HTTPManager);
998 b_ret = __get_http_header_for_field(http_msg_response, "ETag", &header);
1000 DA_LOG(HTTPManager, "no ETag");
1005 *out_value = strdup(header->value);
1010 da_bool_t http_msg_response_get_date(http_msg_response_t *http_msg_response,
1013 da_bool_t b_ret = DA_FALSE;
1014 http_header_t *header = NULL;
1016 DA_LOG_FUNC_START(HTTPManager);
1018 b_ret = __get_http_header_for_field(http_msg_response, "Date", &header);
1020 DA_LOG(HTTPManager, "no Date");
1025 *out_value = strdup(header->value);
1030 da_bool_t http_msg_response_get_location(http_msg_response_t *http_msg_response,
1033 da_bool_t b_ret = DA_FALSE;
1034 http_header_t *header = NULL;
1036 DA_LOG_FUNC_START(HTTPManager);
1038 b_ret = __get_http_header_for_field(http_msg_response, "Location", &header);
1040 DA_LOG(HTTPManager, "no Location");
1044 *out_value = strdup(header->value);
1049 da_result_t http_msg_response_get_boundary(
1050 http_msg_response_t *http_msg_response, char **out_val)
1052 da_result_t ret = DA_RESULT_OK;
1054 http_msg_iter_t http_msg_iter;
1057 char *boundary = NULL;
1059 DA_LOG_FUNC_START(HTTPManager);
1061 if (!http_msg_response) {
1062 DA_LOG_ERR(HTTPManager, "DA_ERR_INVALID_ARGUMENT");
1063 return DA_ERR_INVALID_ARGUMENT;
1066 http_msg_response_get_iter(http_msg_response, &http_msg_iter);
1067 while (http_msg_get_field_with_iter(&http_msg_iter, &field, &value)) {
1068 if ((field != DA_NULL) && (value != DA_NULL)) {
1069 if (!strncmp(field, "Content-Type",
1070 strlen("Content-Type"))) {
1071 char *org_str = NULL;
1072 char *boundary_str_start = NULL;
1073 char *boundary_value_start = NULL;
1074 char *boundary_value_end = NULL;
1075 int boundary_value_len = 0;
1080 = strstr(org_str, "boundary");
1081 if (boundary_str_start) {
1082 DA_LOG(HTTPManager, "boundary_str_start = %s", boundary_str_start);
1083 // this "Content-Type" value has "boundary" in it, so get the value
1084 boundary_value_start = strchr(
1085 boundary_str_start, '"');
1086 boundary_value_start += 1; // start without "
1088 boundary_value_end = strchr(
1089 boundary_value_start, '"');
1090 boundary_value_len = boundary_value_end
1091 - boundary_value_start;
1093 DA_LOG(HTTPManager, "boundary_value_start = %s", boundary_value_start);
1094 DA_LOG(HTTPManager, "boundary_value_end = %s", boundary_value_end);
1095 DA_LOG(HTTPManager, "boundary_value_len = %d", boundary_value_len);
1098 // no "boundary" field on this "Content-Type" value
1099 ret = DA_ERR_INVALID_ARGUMENT;
1104 boundary = (char *)calloc(1,
1105 boundary_value_len + 1);
1107 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
1108 ret = DA_ERR_FAIL_TO_MEMALLOC;
1112 strncpy(boundary, boundary_value_start,
1113 boundary_value_len);
1114 DA_LOG(HTTPManager, "[boundary][%s]", boundary);
1120 *out_val = boundary;
1126 char *get_http_response_header_raw(http_msg_response_t *http_msg_response)
1128 http_msg_iter_t http_msg_iter;
1129 http_header_t *header = NULL;
1131 char tmp_buf[1024*4] = {0,};
1132 char line_buf[1024] = {0,};
1136 DA_LOG_FUNC_START(HTTPManager);
1138 http_msg_response_get_iter(http_msg_response, &http_msg_iter);
1139 while (http_msg_get_header_with_iter(&http_msg_iter, &field, &header)) {
1140 if (field && header) {
1141 // FIXME later :: buffer length is more than total length. think about getting header's conent length from libsoup
1142 len = strlen(field) + strlen(header->value) + 2;
1143 snprintf(line_buf, len,"%s:%s", field, header->value);
1144 strncat(tmp_buf, line_buf, len);
1145 strcat(tmp_buf, "\n");
1148 if (strlen(tmp_buf) > 0) {
1149 buff = (char *)calloc(1, strlen(tmp_buf) + 1);
1150 if (buff == DA_NULL) {
1151 DA_LOG_ERR(HTTPManager, "DA_ERR_FAIL_TO_MEMALLOC");
1154 memcpy(buff, tmp_buf, strlen(tmp_buf));
1155 DA_LOG(HTTPManager, "\n---raw response header---\n%s\n------\n",buff);
1162 char *_stristr(const char *long_str, const char *find_str)
1165 int length_long = 0;
1166 int length_find = 0;
1167 char *ret_ptr = NULL;
1168 char *org_ptr = NULL;
1169 char *look_ptr = NULL;
1171 if (long_str == NULL || find_str == NULL) {
1172 DA_LOG_ERR(Default,"INVALID ARGUMENT");
1176 length_long = strlen(long_str);
1177 length_find = strlen(find_str);
1179 org_ptr = (char*)calloc(1, length_long + 1);
1181 if (org_ptr == NULL) {
1182 DA_LOG_ERR(Default,"INVALID ARGUMENT");
1186 look_ptr = (char*)calloc(1, length_find + 1);
1188 if (look_ptr == NULL) {
1189 DA_LOG_ERR(Default,"INVALID ARGUMENT");
1194 while (i < length_long) {
1195 if (isalpha(long_str[i]) != 0) {
1196 if (isupper(long_str[i]) != 0) {
1197 org_ptr[i] = long_str[i];
1199 org_ptr[i] = toupper(long_str[i]);
1202 org_ptr[i] = long_str[i];
1210 while (i < length_find) {
1211 if (isalpha(find_str[i]) != 0) {
1212 if (isupper(find_str[i]) != 0) {
1213 look_ptr[i] = find_str[i];
1215 look_ptr[i] = toupper(find_str[i]);
1218 look_ptr[i] = find_str[i];
1224 ret_ptr = strstr(org_ptr, look_ptr);
1231 i = ret_ptr - org_ptr;
1237 return (char*)(long_str + i);
1240 /* This is not used. But it can be needed if there is no http header parser at http library.*/
1241 da_bool_t extract_attribute_from_header(
1243 const char *szFindStr,
1247 char *pValuePos = NULL;
1251 int need_to_end_quataion_mark = 0;
1253 if (szHeadStr == DA_NULL || szFindStr == DA_NULL) {
1254 DA_LOG_ERR(Default,"INVALID ARGUMENT");
1258 if (strlen(szHeadStr) <= 0 || strlen(szFindStr) <= 0) {
1259 DA_LOG_ERR(Default,"INVALID ARGUMENT");;
1264 if (ppRtnValue == NULL) {
1268 pValuePos = _stristr(szHeadStr, (char*)szFindStr);
1269 if (pValuePos == NULL) {
1274 index = strlen(szFindStr);
1276 while (pValuePos[index] != ':' && pValuePos[index] != '=') {
1279 if (pValuePos[index] == '\0') {
1287 while (pValuePos[index] == ' ') {
1291 /* jump quatation mark */
1292 while (pValuePos[index] == '"') {
1293 need_to_end_quataion_mark = 1;
1299 /* Find the end of data. */
1300 if (0 == strcmp(szFindStr, "Location"))//terminate character list does not contain ';' in case of URI
1302 while (DA_FALSE == IS_URI_TERMINATING_CHAR(pValuePos[index])) {
1305 } else if (need_to_end_quataion_mark) {
1306 while (DA_FALSE == IS_TERMINATING_CHAR_EX(pValuePos[index])) {
1310 while (DA_FALSE == IS_TERMINATING_CHAR(pValuePos[index])) {
1315 strLen = index - startPos;
1318 DA_LOG_ERR(Default," strLen is < 1");
1322 *ppRtnValue = (char*)calloc(1, sizeof(char) * (strLen + 1));
1324 if (*ppRtnValue == NULL) {
1325 DA_LOG_ERR(Default," *ppRtnValue is NULL");
1329 strncpy(*ppRtnValue, pValuePos + startPos, strLen);
1330 *(*ppRtnValue + strLen) = '\0';