summaryrefslogtreecommitdiff
path: root/agent/download-agent-http-mgr.c
blob: 3a2feb97e988833dfb1a2781d7348948f4f52328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#include "download-agent-dl-info.h"
#include "download-agent-file.h"
#include "download-agent-mime-util.h"
#include "download-agent-client-mgr.h"
#include "download-agent-http-msg-handler.h"
#include "download-agent-plugin-conf.h"
#include "download-agent-plugin-drm.h"
#include "download-agent-plugin-libcurl.h"

void __http_update_cb(http_raw_data_t *data, void *user_param);

#define CONVERT_STR(NAME) (#NAME)

static const char *__get_state_str(http_state_t state)
{
	char *str = NULL;
	switch(state) {
	case HTTP_STATE_READY_TO_DOWNLOAD:
		str = CONVERT_STR(HTTP_STATE_READY_TO_DOWNLOAD);
		break;
	case HTTP_STATE_REDIRECTED:
		str = CONVERT_STR(HTTP_STATE_REDIRECTED);
		break;
	case HTTP_STATE_DOWNLOAD_REQUESTED:
		str = CONVERT_STR(HTTP_STATE_DOWNLOAD_REQUESTED);
		break;
	case HTTP_STATE_DOWNLOAD_STARTED:
		str = CONVERT_STR(HTTP_STATE_DOWNLOAD_STARTED);
		break;
	case HTTP_STATE_DOWNLOADING:
		str = CONVERT_STR(HTTP_STATE_DOWNLOADING);
		break;
	case HTTP_STATE_DOWNLOAD_FINISH:
		str = CONVERT_STR(HTTP_STATE_DOWNLOAD_FINISH);
		break;
	case HTTP_STATE_REQUEST_CANCEL:
		str = CONVERT_STR(HTTP_STATE_REQUEST_CANCEL);
		break;
	case HTTP_STATE_REQUEST_PAUSE:
		str = CONVERT_STR(HTTP_STATE_REQUEST_PAUSE);
		break;
	case HTTP_STATE_REQUEST_RESUME:
		str = CONVERT_STR(HTTP_STATE_REQUEST_RESUME);
		break;
	case HTTP_STATE_CANCELED:
		str = CONVERT_STR(HTTP_STATE_CANCELED);
		break;
	case HTTP_STATE_FAILED:
		str = CONVERT_STR(HTTP_STATE_FAILED);
		break;
	case HTTP_STATE_PAUSED:
		str = CONVERT_STR(HTTP_STATE_PAUSED);
		break;
	case HTTP_STATE_RESUMED:
		str = CONVERT_STR(HTTP_STATE_RESUMED);
		break;
	case HTTP_STATE_ABORTED:
		str = CONVERT_STR(HTTP_STATE_ABORTED);
		break;
	case HTTP_STATE_WAIT_FOR_NET_ERR:
		str = CONVERT_STR(HTTP_STATE_WAIT_FOR_NET_ERR);
		break;
	default:
		str = "Unknown State";
		break;
	}
	return str;
}

void __init_http_info(http_info_t *http_info)
{
	DA_LOGV("");

	http_info->state = HTTP_STATE_READY_TO_DOWNLOAD;
	http_info->update_cb = __http_update_cb;
	DA_MUTEX_INIT(&(http_info->mutex_state), DA_NULL);
	DA_MUTEX_INIT(&(http_info->mutex_http), DA_NULL);
	DA_COND_INIT(&(http_info->cond_http), DA_NULL);
}

void __parsing_user_request_header(char *user_request_header,
		char **out_field, char **out_value)
{
	int len = 0;
	char *pos = NULL;
	char *temp_pos = NULL;
	char *field = NULL;
	char *value = NULL;

	DA_LOGV("");

	if (!user_request_header) {
		DA_LOGE("NULL CHECK!: user_request_header");
		goto ERR;
	}

	pos = strchr(user_request_header, ':');
	if (!pos) {
		DA_LOGE("Fail to parse");
		goto ERR;
	}
	temp_pos = (char *)user_request_header;
	while (*temp_pos)
	{
		if (temp_pos == pos || *temp_pos == ' ') {
			len =  temp_pos - user_request_header;
			break;
		}
		temp_pos++;
	}
	if (len < 1) {
		DA_LOGE("Wrong field name");
		goto ERR;
	}
	field = (char *)calloc(1, len + 1);
	if (!field) {
		DA_LOGE("Fail to calloc");
		goto ERR;
	}
	strncpy(field, user_request_header, len);
	pos++;
	while (*pos)
	{
		if (*pos != ' ')
			break;
		pos++;
	}
	len = strlen(pos) + 1;
	value = (char *)calloc(1, len + 1);
	if (!value) {
		DA_LOGE("Fail to calloc");
		goto ERR;
	}
	strncpy(value, pos, len);
	*out_field = field;
	*out_value = value;
	DA_SECURE_LOGD("field[%s], value[%s]", field, value);

	return;
ERR:
	if (field) {
		free(field);
		field = NULL;
	}
	return;
}


da_ret_t __set_http_request_hdr(req_info_t *req_info, http_info_t *http_info, file_info_t *file_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_msg_request_t *http_msg_request = NULL;
	char *user_agent = NULL;
	int count = 0;

	DA_LOGV("");

	NULL_CHECK_RET(req_info);
	NULL_CHECK_RET(http_info);
	NULL_CHECK_RET(file_info);
	NULL_CHECK_RET_OPT(req_info->url, DA_ERR_INVALID_URL);
	count = req_info->req_header_count;

	ret = http_msg_request_create(&http_msg_request);
	if (ret != DA_RESULT_OK)
		goto ERR;

	ret = http_msg_request_set_url(http_msg_request, req_info->url);
	if (ret != DA_RESULT_OK)
		goto ERR;

	ret = get_user_agent_string(&user_agent);
	if (user_agent && ret == DA_RESULT_OK)
		http_msg_request_add_field(http_msg_request,
				HTTP_FIELD_UAGENT, user_agent);


	http_msg_request_add_field(http_msg_request,
			HTTP_FIELD_ACCEPT_LANGUAGE, "en");
	http_msg_request_add_field(http_msg_request,
			HTTP_FIELD_ACCEPT_CHARSET, "utf-8");

	if (req_info->req_header && count > 0) {
		int i = 0;
		for (i = 0; i < count; i++) {
			char *field = NULL;
			char *value = NULL;
			__parsing_user_request_header(req_info->req_header[i],
					&field, &value);
			if (field && value) {
				http_msg_request_add_field(http_msg_request, field, value);
				if (field) {
					free(field);
					field = NULL;
				}
				if (value) {
					free(value);
					value= NULL;
				}
			} else {
				if (field) {
					free(field);
					field = NULL;
				}
				if (value) {
					free(value);
					value= NULL;
				}
				DA_LOGE("Fail to parse user request header");
			}
		}
	}
	if (req_info->etag) {
		char buff[64] = {0,};
		da_size_t size = 0;
		http_msg_request_add_field(http_msg_request,
				HTTP_FIELD_IF_RANGE, req_info->etag);
		get_file_size(req_info->temp_file_path, &size);
#ifdef _RAF_SUPPORT
		file_info->file_size_of_temp_file = size;
#endif
		snprintf(buff, sizeof(buff)-1, "bytes=%llu-", size);
		http_msg_request_add_field(http_msg_request,
				HTTP_FIELD_RANGE, buff);
	}

	http_info->http_msg_request = http_msg_request;
	free(user_agent);
	return ret;
ERR:
	if (http_msg_request)
		http_msg_request_destroy(&http_msg_request);

	return ret;

}

da_ret_t __create_http_resume_hdr(req_info_t *req_info, http_info_t *http_info,
		file_info_t *file_info)
{
	da_ret_t ret = DA_RESULT_OK;
	da_bool_t b_ret = DA_FALSE;
	char *value = NULL;
	char temp_size_str[32] = { 0, };
	char *etag_from_response = NULL;
	char *date_from_response = NULL;
	http_msg_response_t *first_response = NULL;
	http_msg_request_t *resume_request = NULL;
	http_msg_request_t *old_request = NULL;

	DA_LOGV("");

	first_response = http_info->http_msg_response;
	if (first_response) {
		b_ret = http_msg_response_get_ETag(first_response, &value);
		if (b_ret) {
			etag_from_response = value;
			value = NULL;
			DA_SECURE_LOGD("[ETag][%s]", etag_from_response);
		}
		b_ret = http_msg_response_get_date(first_response, &value);
		if (b_ret) {
			date_from_response = value;
			value = NULL;
			DA_LOGV("[Date][%s]", date_from_response);
		}
		DA_SECURE_LOGD("downloaded_size[%u]", file_info->bytes_written_to_file);
		snprintf(temp_size_str, sizeof(temp_size_str), "bytes=%llu-",
				file_info->bytes_written_to_file);
		DA_SECURE_LOGD("size str[%s]", temp_size_str);
		free(first_response);
		http_info->http_msg_response = DA_NULL;
	}
	old_request = http_info->http_msg_request;
	free(old_request);
	http_info->http_msg_request = DA_NULL;

	ret = __set_http_request_hdr(req_info, http_info, file_info);
	if (ret != DA_RESULT_OK)
		goto ERR;

	resume_request = http_info->http_msg_request;
	if (etag_from_response) {
		http_msg_request_add_field(resume_request, HTTP_FIELD_IF_RANGE,
				etag_from_response);
	} else {
		if (date_from_response) {
			http_msg_request_add_field(resume_request,
					HTTP_FIELD_IF_RANGE, date_from_response);
		}
	}

	if (strlen(temp_size_str) > 0)
		http_msg_request_add_field(resume_request, HTTP_FIELD_RANGE,
				temp_size_str);

ERR:
	if (etag_from_response) {
		free(etag_from_response);
		etag_from_response = NULL;
	}
	if (date_from_response) {
		free(date_from_response);
		date_from_response = NULL;
	}
	return ret;
}


da_ret_t __start_transaction(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info;
	DA_LOGV("");

	if (!da_info) {
		DA_LOGE("NULL CHECK!: da_info");
		return DA_ERR_INVALID_ARGUMENT;
	}
	http_info = da_info->http_info;
	if (!http_info) {
		DA_LOGE("NULL CHECK!: http_info");
		return DA_ERR_INVALID_ARGUMENT;
	}
	http_info->http_method = HTTP_METHOD_GET;
	http_info->proxy_addr = get_proxy_address();

	ret = PI_http_start(da_info);

	return ret;
}

da_ret_t __start_resume_transaction(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info = DA_NULL;
	file_info_t *file_info = DA_NULL;
	req_info_t *req_info = DA_NULL;

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	file_info = da_info->file_info;
	NULL_CHECK_RET(file_info);
	req_info = da_info->req_info;
	NULL_CHECK_RET(req_info);

	ret = __create_http_resume_hdr(req_info, http_info,
			file_info);
	if (ret != DA_RESULT_OK)
		return ret;

	reset_http_info_for_resume(http_info);
	if (file_info->file_path) {
		req_info->temp_file_path = strdup(file_info->file_path);
	} else {
		DA_LOGE("file_path cannot be NULL in resume case");
		return DA_ERR_INVALID_ARGUMENT;
	}
	ret = __start_transaction(da_info);
	return ret;
}


da_ret_t __start_new_transaction(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;

	if (!da_info) {
		DA_LOGE("NULL CHECK!: da_info");
		return DA_ERR_INVALID_ARGUMENT;
	}

	ret = __set_http_request_hdr(da_info->req_info, da_info->http_info, da_info->file_info);
	if (ret != DA_RESULT_OK)
		return ret;

	ret = __start_transaction(da_info);
	return ret;
}

int __check_wait_for_auto_retry(http_info_t *http_info)
{
	da_ret_t ret = DA_RESULT_OK;
	struct timespec ts;
	struct timeval tp;
	NULL_CHECK_RET_OPT(http_info, 0);
	gettimeofday(&tp, NULL);
	ts.tv_sec = tp.tv_sec + DA_MAX_TIME_OUT;
	ts.tv_nsec = tp.tv_usec * 1000;
	DA_LOGI("Network Fail case, wait for a while");

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_info->state = HTTP_STATE_WAIT_FOR_NET_ERR;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_MUTEX_LOCK(&(http_info->mutex_http));
	ret = pthread_cond_timedwait(&(http_info->cond_http),
			&(http_info->mutex_http), &ts);
	DA_MUTEX_UNLOCK(&(http_info->mutex_http));
	if (ret == ETIMEDOUT) {
		DA_LOGI("Waiting is done by timeout");
	} else if (ret != 0) {
		DA_LOGE("fail to pthread_cond_waittime[%d][%s]",ret, strerror(ret));
	} else {
		DA_LOGI("Waiting is done by control");
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		DA_LOGI("http_state[%s]", __get_state_str(http_info->state));
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		return 1;
	}

	return 0;
}

// In download thread
da_ret_t request_http_download(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	req_info_t *req_info = DA_NULL;
	http_info_t *http_info = DA_NULL;
	http_state_t http_state = 0;
	da_bool_t need_wait = DA_TRUE;

	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	req_info = da_info->req_info;
	NULL_CHECK_RET(req_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	__init_http_info(http_info);

	do {
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_state = http_info->state;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_LOGD("http_state[%s][%d]",__get_state_str(http_info->state), da_info->da_id);
		switch (http_state) {
		case HTTP_STATE_READY_TO_DOWNLOAD:
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_DOWNLOAD_REQUESTED;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			ret = __start_new_transaction(da_info);
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_state = http_info->state;
			DA_LOGD("http_state[%s][%d]",__get_state_str(http_info->state), da_info->da_id);
			http_info->error_code = ret;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			if (ret == DA_ERR_NETWORK_FAIL && http_state != HTTP_STATE_PAUSED) {
				DA_LOGE("Network failed");
				if (__check_wait_for_auto_retry(http_info) == 1)
					need_wait = DA_TRUE;
				else
					need_wait = DA_FALSE;
			}
			break;
		case HTTP_STATE_REDIRECTED:
		case HTTP_STATE_DOWNLOAD_REQUESTED:
		case HTTP_STATE_DOWNLOAD_STARTED:
		case HTTP_STATE_DOWNLOADING:
		case HTTP_STATE_REQUEST_PAUSE:
			DA_LOGE("Cannot enter here:[%s][id]",
					__get_state_str(http_info->state), da_info->da_id);
			break;
		case HTTP_STATE_REQUEST_CANCEL:
			break;
		case HTTP_STATE_REQUEST_RESUME:
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_READY_TO_DOWNLOAD;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			need_wait = DA_TRUE;
			break;
		case HTTP_STATE_CANCELED:
			need_wait = DA_FALSE;
			ret = DA_RESULT_USER_CANCELED;
			break;
		case HTTP_STATE_PAUSED:
			DA_LOGD("error_code[%d]", http_info->error_code);
			send_client_paused_info(da_info);
			DA_LOGD("Waiting thread for paused state");
			DA_MUTEX_LOCK(&(http_info->mutex_http));
			pthread_cond_wait(&(http_info->cond_http),&(http_info->mutex_http));
			DA_MUTEX_UNLOCK(&(http_info->mutex_http));
			DA_LOGD("Wake up thread due to resume");
			break;
		case HTTP_STATE_RESUMED:
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_DOWNLOAD_REQUESTED;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			ret = __start_resume_transaction(da_info);
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_state = http_info->state;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			if (ret == DA_ERR_NETWORK_FAIL && http_state != HTTP_STATE_PAUSED) {
				DA_LOGE("Network failed");
				if (__check_wait_for_auto_retry(http_info) == 1)
					need_wait = DA_TRUE;
				else
					need_wait = DA_FALSE;
			}
			break;
		case HTTP_STATE_DOWNLOAD_FINISH:
			need_wait = DA_FALSE;
			if (ret == DA_RESULT_OK)
				ret = check_drm_convert(da_info->file_info);
			break;
		case HTTP_STATE_FAILED:
			if (ret == DA_ERR_NETWORK_FAIL) {
				if (__check_wait_for_auto_retry(http_info) == 1)
					need_wait = DA_TRUE;
				else
					need_wait = DA_FALSE;
			} else {
				need_wait = DA_FALSE;
			}
			break;
		case HTTP_STATE_ABORTED:
			need_wait = DA_FALSE;
			break;
		default:
			break;
		}
	} while (need_wait == DA_TRUE);
	DA_LOGD("Final http_state[%s][%d] err[%d]",__get_state_str(http_info->state), da_info->da_id, ret);
	if (http_info->state != HTTP_STATE_PAUSED)
		send_client_finished_info(da_info ,ret);
	DA_LOGI("=== Exiting http_download ret[%d] ===", ret);
	return ret;
}

da_ret_t __disconnect_transaction(http_info_t *http_info)
{
	da_ret_t ret = DA_RESULT_OK;
	DA_LOGD("");
	ret = PI_http_disconnect(http_info);
	return ret;
}

da_ret_t __handle_event_abort(http_info_t *http_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_state_t state = 0;

	DA_LOGD("");

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGV("http_state[%s]", __get_state_str(state));

	switch (state) {
	case HTTP_STATE_READY_TO_DOWNLOAD:
	case HTTP_STATE_REDIRECTED:
	case HTTP_STATE_DOWNLOAD_REQUESTED:
	case HTTP_STATE_DOWNLOAD_STARTED:
	case HTTP_STATE_DOWNLOADING:
	case HTTP_STATE_REQUEST_CANCEL:
	case HTTP_STATE_REQUEST_PAUSE:
	case HTTP_STATE_REQUEST_RESUME:
	case HTTP_STATE_CANCELED:
	case HTTP_STATE_PAUSED:
	case HTTP_STATE_RESUMED:
	case HTTP_STATE_ABORTED:
	case HTTP_STATE_WAIT_FOR_NET_ERR:
		/* IF the network session is terminated due to some error,
		 * the state can be aborted.(data aborted case) */
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_ABORTED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		__disconnect_transaction(http_info);
		break;
	case HTTP_STATE_DOWNLOAD_FINISH:
		break;
	default:
		DA_LOGE("Cannot enter here");
		break;
	}
	return ret;
}

da_ret_t __check_enough_memory(http_info_t *http_info, char *user_install_path)
{
	da_ret_t ret = DA_RESULT_OK;
	da_size_t cont_len = 0;
	char *dir_path = DA_DEFAULT_INSTALL_PATH_FOR_PHONE;

	DA_LOGV("");
	NULL_CHECK_RET(http_info);
	cont_len = http_info->content_len_from_header;
	if (cont_len > 0) {
		if (user_install_path)
			dir_path = user_install_path;
		ret = get_available_memory(dir_path, cont_len);
	}
	return ret;
}

da_ret_t request_to_abort_http_download(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	ret = __handle_event_abort(da_info->http_info);
	return ret;
}

da_ret_t request_to_cancel_http_download(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info = DA_NULL;
	http_state_t http_state = 0;
	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGD("http_state[%s]", __get_state_str(http_state));
	switch (http_state) {
	case HTTP_STATE_READY_TO_DOWNLOAD:
		ret = PI_http_cancel(http_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_CANCELED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		if (da_info->thread_id > 0) {
			if (pthread_cancel(da_info->thread_id) != 0) {
				DA_LOGE("Fail to cancel thread id[%d] err[%s]",
						da_info->thread_id, strerror(errno));
			} else {
				DA_LOGI("====Exit thread with cancel:da_id[%d]===",
						da_info->da_id);
			}
		}
		break;
	case HTTP_STATE_WAIT_FOR_NET_ERR:
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_CANCELED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_MUTEX_LOCK(&(http_info->mutex_http));
		DA_COND_SIGNAL(&(http_info->cond_http));
		DA_MUTEX_UNLOCK(&(http_info->mutex_http));
		break;
	case HTTP_STATE_PAUSED:
		reset_http_info(http_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_CANCELED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_DOWNLOAD_REQUESTED:
	case HTTP_STATE_DOWNLOAD_STARTED:
	case HTTP_STATE_DOWNLOADING:
	case HTTP_STATE_REQUEST_RESUME:
	case HTTP_STATE_RESUMED:
		ret = PI_http_cancel(http_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_REQUEST_CANCEL;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_DOWNLOAD_FINISH:
		ret = DA_ERR_INVALID_STATE;
		break;
	case HTTP_STATE_REQUEST_CANCEL:
		DA_LOGV("cancel is already in progress... ");
		ret = DA_ERR_INVALID_STATE;
		break;
	default:
		ret = DA_ERR_INVALID_STATE;
		DA_LOGE("Cannot enter here");
		break;
	}
	return ret;
}

da_ret_t request_to_suspend_http_download(da_info_t *da_info)
{

	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info = DA_NULL;
	http_state_t http_state = 0;

	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGD("http_state[%s]", __get_state_str(http_state));

	switch (http_state) {
	case HTTP_STATE_PAUSED:
	case HTTP_STATE_REQUEST_PAUSE:
		DA_LOGI("Already paused. http_state[%s]", __get_state_str(http_state));
		ret = DA_ERR_ALREADY_SUSPENDED;
		break;
	case HTTP_STATE_READY_TO_DOWNLOAD:
		DA_LOGE("Download has not been started yet");
		ret = DA_ERR_INVALID_STATE;
		break;
	case HTTP_STATE_WAIT_FOR_NET_ERR:
		DA_LOGD("error_code[%d]", http_info->error_code);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_PAUSED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_MUTEX_LOCK(&(http_info->mutex_http));
		DA_COND_SIGNAL(&(http_info->cond_http));
		DA_MUTEX_UNLOCK(&(http_info->mutex_http));
		break;
	default:
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_REQUEST_PAUSE;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_LOGD("error_code[%d]", http_info->error_code);
		if (http_info->error_code != DA_ERR_NETWORK_FAIL)
			ret = PI_http_pause(http_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_PAUSED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));

		break;
	}
	return ret;
}

da_ret_t request_to_resume_http_download(da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info = DA_NULL;
	http_state_t http_state = 0;
	int retry_count = 0;

	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGD("http_state[%s]", __get_state_str(http_state));

	switch (http_state) {
	case HTTP_STATE_PAUSED:
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_RESUMED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_LOGD("Wake up thread for paused state");
		DA_MUTEX_LOCK(&(http_info->mutex_http));
		DA_COND_SIGNAL(&(http_info->cond_http));
		DA_MUTEX_UNLOCK(&(http_info->mutex_http));
		DA_LOGD("error_code[%d]", http_info->error_code);
		if (http_info->error_code != DA_ERR_NETWORK_FAIL) {
			ret = PI_http_unpause(http_info);
			if (ret != DA_RESULT_OK)
				PI_http_cancel(http_info);
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_DOWNLOADING;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));

		}
		break;
	case HTTP_STATE_REQUEST_PAUSE:
		DA_LOGD("Waiting to handle pause request");
		do {
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_state = http_info->state;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
			if (http_state == HTTP_STATE_PAUSED) {
				DA_LOGD("Change to paused state");
				ret = PI_http_unpause(http_info);
				break;
			}
			retry_count++;
		} while(retry_count < 10000);
		if (ret != DA_RESULT_OK || retry_count >= 10000)
			PI_http_cancel(http_info);
		break;
	case HTTP_STATE_RESUMED:
		ret = DA_ERR_ALREADY_RESUMED;
		break;
	default:
		DA_LOGE("Fail to resume. Invalid state check. http_state[%s]",
				__get_state_str(http_state));
		ret = DA_ERR_INVALID_STATE;
		// If resume is failed due to invalid state, the previous pause should be canceled.
		PI_http_cancel(http_info);
		break;
	}
	return ret;
}

da_ret_t __check_resume_download_is_available(
		req_info_t *req_info, http_info_t *http_info, file_info_t *file_info)
{
	da_ret_t ret = DA_RESULT_OK;
	da_bool_t b_ret = DA_FALSE;
	char *origin_ETag = NULL;
	char *new_ETag = NULL;
	da_size_t remained_content_len = 0;
	char *value = NULL;
	da_size_t size = 0;
	char *temp_file_path = DA_NULL;
	char *dir_path = DA_DEFAULT_INSTALL_PATH_FOR_PHONE;

	DA_LOGV("");

	origin_ETag = req_info->etag;

	b_ret = http_msg_response_get_content_length(http_info->http_msg_response,
			&size);
	if (b_ret) {
		remained_content_len = size;
		size = 0;
		DA_SECURE_LOGD("remained_content_len[%llu]", remained_content_len);
	}

	b_ret = http_msg_response_get_ETag(http_info->http_msg_response, &value);
	if (b_ret) {
		new_ETag = value;
		value = NULL;
		DA_SECURE_LOGD("new ETag[%s]", new_ETag);
	} else {
		goto ERR;
	}

	if (origin_ETag && new_ETag &&
			0 != strncmp(origin_ETag, new_ETag, strlen(new_ETag))) {
		DA_LOGE("ETag is not identical! revoke!");
		/* FIXME Later : Need to detail error exception handling */
		ret = DA_ERR_NETWORK_FAIL;
		/*ret = DA_ERR_MISMATCH_HTTP_HEADER; */
		goto ERR;
	}

	if (remained_content_len > 0) {
		if (req_info->install_path)
				dir_path = req_info->install_path;
		ret = get_available_memory(dir_path, remained_content_len);
		if (ret != DA_RESULT_OK)
			goto ERR;
	}

	if (!http_info->content_type_from_header) {
		b_ret = http_msg_response_get_content_type(http_info->http_msg_response,
				&value);
		if (b_ret) {
			http_info->content_type_from_header = value;
			value = NULL;
			DA_SECURE_LOGD("Content-Type[%s]",
				http_info->content_type_from_header);
		}
	}
	temp_file_path = req_info->temp_file_path;
	if (!temp_file_path) {
		DA_LOGE("Temporary file path cannot be NULL");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}
	get_file_size(temp_file_path, &size);
	http_info->content_len_from_header = remained_content_len + size;
	DA_SECURE_LOGD("Content-Length[%llu]", http_info->content_len_from_header);
ERR:
	if (new_ETag) {
		free(new_ETag);
		new_ETag = DA_NULL;
	}
	return ret;
}


da_ret_t __check_content_type_is_matched(http_info_t *http_info)
{
	da_ret_t ret = DA_RESULT_OK;
	char *content_type_from_server = DA_NULL;

	DA_LOGV("");

	content_type_from_server = http_info->content_type_from_header;
	if (content_type_from_server == DA_NULL) {
		DA_LOGV("http header has no Content-Type field, no need to compare");
		return DA_RESULT_OK;
	}
	return ret;
}

da_ret_t __handle_http_status_code(http_info_t *http_info,
		file_info_t *file_info, req_info_t *req_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_state_t http_state = 0;
	http_msg_response_t *http_msg_response = DA_NULL;
	char *location = DA_NULL;
	char *if_range_str = DA_NULL;
	char *range_str = DA_NULL;
	int http_status = 0;

	NULL_CHECK_RET(http_info);
	NULL_CHECK_RET(file_info);
	NULL_CHECK_RET(req_info);
	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGD("http_state[%s]", __get_state_str(http_state));
	http_msg_response = http_info->http_msg_response;
	NULL_CHECK_RET(http_msg_response);
	http_status = http_msg_response->status_code;
	switch (http_status) {
	case 200:
	case 201:
	case 202:
	case 203:
// Although expecting 206, 200 response is received. Remove temporary file and reset file info
		if (http_info->http_msg_request &&
				http_msg_request_get_if_range(http_info->http_msg_request, &if_range_str) == DA_TRUE &&
				http_msg_request_get_range(http_info->http_msg_request, &range_str) == DA_TRUE) {
			DA_LOGI("Server do not support if-range option");
			clean_paused_file(file_info);
		}
		free(if_range_str);
		free(range_str);
		if (http_state == HTTP_STATE_REQUEST_RESUME)
			clean_paused_file(file_info);
		ret = __check_content_type_is_matched(http_info);
		if (ret != DA_RESULT_OK)
			goto ERR;
		ret = __check_enough_memory(http_info, req_info->install_path);
		if (ret != DA_RESULT_OK)
			goto ERR;
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state =  HTTP_STATE_DOWNLOAD_STARTED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;

	case 206:
		DA_LOGV("HTTP Status is %d - Partial download for resume!", http_status);
		/* The resume can be started with start API.
		 * So the state should be not HTTP_STATE_RESUME_REQUESTED but HTTP_STATE_DOWNLOAD_REQUESTED*/
		if (http_state == HTTP_STATE_DOWNLOAD_REQUESTED) {
			ret = __check_resume_download_is_available(req_info, http_info, file_info);
			if (ret != DA_RESULT_OK)
				goto ERR;
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_DOWNLOAD_STARTED;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));

		} else if (http_state == HTTP_STATE_REQUEST_RESUME) {
			///FIXME later : how get previous response header
			///ret = __check_this_partial_download_is_available(http_info,
				///	previous_ http_msg_response);
			//if (ret != DA_RESULT_OK)
				//goto ERR;
			DA_MUTEX_LOCK(&(http_info->mutex_state));
			http_info->state = HTTP_STATE_RESUMED;
			DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		} else {
			DA_LOGE("This download is not resumed, revoke");
			ret = DA_ERR_INVALID_STATE;
			goto ERR;
		}
		break;

	case 300:
	case 301:
	case 302:
	case 303:
	case 305:
	case 306:
	case 307:
		DA_LOGV("HTTP Status is %d - redirection!",http_status);
		if (http_msg_response_get_location(http_msg_response, &location)) {
			DA_SECURE_LOGD("location  = %s\n", location);
			http_info->location_url = location;
			DA_LOGI("[TEST] location_url[%p]",http_info->location_url);
		}
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_REDIRECTED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		http_msg_response_destroy(&http_msg_response);
		http_info->http_msg_response = DA_NULL;
		break;

	case 100:
	case 101:
	case 102:
	case 204:
	case 304:
		DA_LOGV("HTTP Status is %d - 204 server got the request, \
				but no content to reply back, \
				304 means not modified!", http_status);
		ret = DA_ERR_SERVER_RESPOND_BUT_SEND_NO_CONTENT;
		break;

	case 416: // Requested range not satisfiable
	case 503:
	case 504:
	default:
///		GET_REQUEST_HTTP_RESULT(request_info)
///			= DA_ERR_UNREACHABLE_SERVER;
		DA_LOGI("set internal error code : DA_ERR_UNREACHABLE_SERVER");
		break;
	}

ERR:
	return ret;
}

da_ret_t __check_before_downloading(da_info_t *da_info, http_state_t state)
{
	da_ret_t ret = DA_RESULT_OK;
	http_info_t *http_info = DA_NULL;
	req_info_t *req_info = DA_NULL;
	file_info_t *file_info = DA_NULL;
	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	req_info = da_info->req_info;
	NULL_CHECK_RET(req_info);
	file_info = da_info->file_info;
	NULL_CHECK_RET(file_info);
	DA_LOGD("state:%s",__get_state_str(state));
	// resume case
	if (req_info->temp_file_path && file_info->bytes_written_to_file > 0) {
		ret = start_file_append(file_info);
	} else if (state == HTTP_STATE_DOWNLOAD_STARTED) {
		ret = start_file_writing(da_info);
	} else {
		DA_LOGE("Cannot enter here!");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}
	if (DA_RESULT_OK != ret)
		goto ERR;

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_info->state = HTTP_STATE_DOWNLOADING;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));

	ret = send_client_update_dl_info(da_info);
ERR:
	return ret;
}

da_ret_t __handle_event_http_header(http_raw_data_t *raw_data, da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_state_t http_state = 0;
	http_info_t *http_info = DA_NULL;
	file_info_t *file_info = DA_NULL;
	req_info_t *req_info = DA_NULL;
	http_msg_response_t *http_msg_response = DA_NULL;
	da_size_t size = 0;
	char *mime_type = DA_NULL;
	char *etag = DA_NULL;
	char *file_name = DA_NULL;

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	file_info = da_info->file_info;
	NULL_CHECK_RET(file_info);
	req_info = da_info->req_info;
	NULL_CHECK_RET(req_info);
	NULL_CHECK_RET(raw_data);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGV("http_state[%s]", __get_state_str(http_state));
	http_msg_response = http_info->http_msg_response;
	switch (http_state) {
	case HTTP_STATE_DOWNLOAD_REQUESTED:
	case HTTP_STATE_REQUEST_PAUSE:
	case HTTP_STATE_REQUEST_RESUME:
	case HTTP_STATE_REDIRECTED:
		http_msg_response_get_content_length(http_msg_response, &size);
		http_info->content_len_from_header = size;
		http_msg_response_get_content_type(http_msg_response, &mime_type);
		http_info->content_type_from_header = mime_type;
		if (mime_type)
			file_info->mime_type = strdup(mime_type);
		http_msg_response_get_ETag(http_msg_response, &etag);
		http_info->etag_from_header = etag;
		http_msg_response_get_content_disposition(
				http_msg_response, DA_NULL, &file_name);
		http_info->file_name_from_header = file_name;
		ret = __handle_http_status_code(http_info, file_info, req_info);
		if (ret != DA_RESULT_OK) {
			DA_LOGE("Fail to handle http status code");
			goto ERR;
		}
#ifdef _RAF_SUPPORT
		char *val = NULL;
		http_msg_response_get_RAF_mode(http_msg_response, &val);
		if (!val) {
			DA_LOGE("Fail to raf mode value from response header");
		} else {
			DA_LOGI("[RAF] val[%s:%s]", HTTP_FIELD_RAF_MODE, val);
			if (strncmp(val, "yes", strlen("yes")) == 0) {
				DA_MUTEX_LOCK(&(http_info->mutex_state));
				http_info->state = HTTP_STATE_DOWNLOAD_STARTED;
				DA_MUTEX_UNLOCK(&(http_info->mutex_state));
				ret = __check_before_downloading(da_info, http_info->state);
				if (ret != DA_RESULT_OK) {
					free(val);
					goto ERR;
				}
				http_info->is_raf_mode_confirmed = DA_TRUE;
				ret = PI_http_set_file_name_to_curl(http_info->http_msg, file_info->file_path);
				if (ret != DA_RESULT_OK) {
					DA_LOGE("Fail to set file name to curl");
					free(val);
					goto ERR;
				}
			}
			free(val);
		}
#endif
		break;
	case HTTP_STATE_REQUEST_CANCEL:
		DA_LOGV("Cancel is in progress.. http_state[%s]",
				__get_state_str(http_state));
		break;

	default:
		DA_LOGE("http_state[%s]", __get_state_str(http_state));
		goto ERR;
	}

ERR:
	if (ret != DA_RESULT_OK) {
		DA_LOGE("Request to cancel due to error[%d]", ret);
		PI_http_cancel(http_info);
		http_info->error_code = ret;
		discard_download(file_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_FAILED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	}
	free(raw_data);
	return ret;
}

da_ret_t __handle_event_http_packet(http_raw_data_t *raw_data, da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_state_t http_state = 0;
	http_info_t *http_info = DA_NULL;
	file_info_t *file_info = DA_NULL;
	time_t t;
	struct tm *lc_time;
	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	file_info = da_info->file_info;
	NULL_CHECK_RET(file_info);
	NULL_CHECK_RET(raw_data);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));

	switch (http_state) {
	case HTTP_STATE_DOWNLOAD_STARTED:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_FALSE) {
#endif
		ret = __check_before_downloading(da_info, http_state);
		if (ret != DA_RESULT_OK)
			goto ERR;
		ret = file_write_ongoing(file_info,
				raw_data->body, raw_data->body_len);
		if (ret != DA_RESULT_OK)
			goto ERR;
#ifdef _RAF_SUPPORT
		} else {
			file_info->bytes_written_to_file =
					raw_data->received_len + file_info->file_size_of_temp_file;
			file_info->is_updated = DA_TRUE;
		}
#endif
		ret = send_client_update_progress_info(da_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_state = HTTP_STATE_DOWNLOADING;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_RESUMED:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_FALSE) {
#endif
		__check_before_downloading(da_info, http_state);
		ret = file_write_ongoing(file_info,
				raw_data->body, raw_data->body_len);
		if (ret != DA_RESULT_OK)
			goto ERR;
#ifdef _RAF_SUPPORT
		} else {
			file_info->bytes_written_to_file =
					raw_data->received_len + file_info->file_size_of_temp_file;
			file_info->is_updated = DA_TRUE;
		}
#endif
		ret = send_client_update_progress_info(da_info);
		break;
	case HTTP_STATE_REDIRECTED:
		DA_LOGV("http_state[%s]", __get_state_str(http_state));
		break;
	case HTTP_STATE_DOWNLOADING:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_FALSE) {
#endif
		/* Should this function before updating download info
		 * Because it extract mime type at once only if first download updating at client */
		ret = file_write_ongoing(file_info,
				raw_data->body, raw_data->body_len);
		if (ret != DA_RESULT_OK)
			goto ERR;
#ifdef _RAF_SUPPORT
		} else {
			file_info->bytes_written_to_file =
					raw_data->received_len + file_info->file_size_of_temp_file;
			file_info->is_updated = DA_TRUE;
		}
#endif
		// send event every 1 second.
		if ((t = time(DA_NULL)) > 0) {
			if ((lc_time = localtime(&t)) != DA_NULL) {
				if (da_info->update_time != lc_time->tm_sec) {
					da_info->update_time = lc_time->tm_sec;
					ret = send_client_update_progress_info(da_info);
				}
			} else {
				DA_LOGE("Fail to call localtime[%s]",strerror(errno));
				ret = send_client_update_progress_info(da_info);
			}
		} else {
			DA_LOGE("Fail to call time[%s]",strerror(errno));
			ret = send_client_update_progress_info(da_info);
		}
		break;
	case HTTP_STATE_REQUEST_PAUSE:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_FALSE) {
#endif
		DA_LOGV("http_state[%s]", __get_state_str(http_state));
		ret = file_write_ongoing(file_info,
				raw_data->body, raw_data->body_len);
		if (ret != DA_RESULT_OK)
			goto ERR;
#ifdef _RAF_SUPPORT
		} else {
			file_info->bytes_written_to_file =
					raw_data->received_len + file_info->file_size_of_temp_file;
			file_info->is_updated = DA_TRUE;
		}
#endif

		break;
	default:
		DA_LOGE("Do nothing! http_state is in case[%s]",
				__get_state_str(http_state));
		goto ERR;
	}
ERR:
	if (ret != DA_RESULT_OK) {
		DA_LOGE("Request to cancel due to error[%d]", ret);
		PI_http_cancel(http_info);
		http_info->error_code = ret;
		discard_download(da_info->file_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_FAILED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	}
	if (raw_data->body)
		free(raw_data->body);
	free(raw_data);
	return ret;
}

da_ret_t __check_file_size_with_header_content_size(file_info_t *file_info)
{
	da_ret_t ret = DA_RESULT_OK;
	unsigned long long tmp_file_size = 0;

	DA_LOGV("");

	if (file_info->file_size > 0) {

#ifdef _ENABLE_OMA_DRM
		if (is_content_drm_dm(file_info->mime_type)) {
			/* FIXME Later : How can get the file size of DRM file. */
			return ret;
		}
#endif

		get_file_size(file_info->file_path, &tmp_file_size);

		if (tmp_file_size != file_info->file_size) {
			DA_SECURE_LOGE("Real file size[%llu], MISMATCH CONTENT SIZE",
					tmp_file_size);
			ret = DA_ERR_MISMATCH_CONTENT_SIZE;
		}
	}
	return ret;
}

da_ret_t __handle_event_http_final(http_raw_data_t *raw_data, da_info_t *da_info)
{
	da_ret_t ret = DA_RESULT_OK;
	http_state_t http_state = 0;
	http_info_t *http_info = DA_NULL;
	file_info_t *file_info = DA_NULL;

	DA_LOGV("");

	NULL_CHECK_RET(da_info);
	http_info = da_info->http_info;
	NULL_CHECK_RET(http_info);
	file_info = da_info->file_info;
	NULL_CHECK_RET(file_info);
	NULL_CHECK_RET(raw_data);

	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	DA_LOGD("http_state[%s]", __get_state_str(http_state));

	switch (http_state) {
	case HTTP_STATE_REDIRECTED:
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_READY_TO_DOWNLOAD;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_DOWNLOAD_REQUESTED:
		DA_LOGV("case HTTP_STATE_DOWNLOAD_REQUESTED");
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_DOWNLOAD_FINISH;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_DOWNLOADING:
		DA_LOGD("case HTTP_STATE_DOWNLOADING");
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_TRUE) {
			ret = file_write_complete_for_raf(file_info);
		} else {
			ret = file_write_complete(file_info);
		}
#else
		ret = file_write_complete(file_info);
#endif
		if (ret != DA_RESULT_OK) {
			discard_download(file_info);
			goto ERR;
		}
		ret = __check_file_size_with_header_content_size(file_info);
		if(ret != DA_RESULT_OK) {
			discard_download(file_info) ;
			goto ERR;
		}
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_DOWNLOAD_FINISH;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		ret = send_client_update_progress_info(da_info);
		break;
	case HTTP_STATE_REQUEST_PAUSE:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_TRUE) {
			if (file_info->file_handle)
				ret = file_write_complete_for_raf(file_info);
		} else {
			ret = file_write_complete(file_info);
		}
#else
		if (file_info->file_handle) {
			ret = file_write_complete(file_info);
//			send_client_update_progress_info(da_info);
		}
#endif
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_PAUSED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		DA_LOGV("Server Notification code is set to NULL");
		break;
	case HTTP_STATE_ABORTED:
	case HTTP_STATE_CANCELED:
		discard_download(file_info);
		break;
	case HTTP_STATE_REQUEST_CANCEL:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_TRUE) {
			ret = file_write_complete_for_raf(file_info);
		} else {
			ret = file_write_complete(file_info);
		}
#else
		ret = file_write_complete(file_info);
#endif
		if (ret != DA_RESULT_OK)
			goto ERR;
		discard_download(file_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_CANCELED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	case HTTP_STATE_PAUSED:
		DA_LOGD("Remain paused stated");
		break;
	default:
#ifdef _RAF_SUPPORT
		if (http_info->is_raf_mode_confirmed == DA_TRUE) {
			ret = file_write_complete_for_raf(file_info);
		} else {
			ret = file_write_complete(file_info);
		}
#else
		ret = file_write_complete(file_info);
#endif
		if (ret != DA_RESULT_OK)
			goto ERR;
		discard_download(file_info);
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_FAILED;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
		break;
	}

ERR:
	/* When file complete is failed */
	if (DA_RESULT_OK != ret) {
		DA_MUTEX_LOCK(&(http_info->mutex_state));
		http_info->state = HTTP_STATE_DOWNLOAD_FINISH;
		DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	}
	if (raw_data->body)
		free(raw_data->body);
	free(raw_data);
	return ret;
}

void __http_update_cb(http_raw_data_t *data, void *user_param)
{
	http_raw_data_t *raw_data = DA_NULL;
	da_info_t *da_info = DA_NULL;
	if (!data || !user_param) {
		DA_LOGE("NULL CHECK!: data, user_param");
		return;
	}
	DA_LOGV("");
	raw_data = data;
	da_info = (da_info_t *)user_param;

	switch(data->type) {
	case HTTP_EVENT_GOT_HEADER:
		__handle_event_http_header(raw_data, da_info);
		break;
	case HTTP_EVENT_GOT_PACKET:
		__handle_event_http_packet(raw_data, da_info);
		break;
	case HTTP_EVENT_FINAL:
		__handle_event_http_final(raw_data, da_info);
		break;
/*
	case HTTP_EVENT_ABORT:
		ret = __handle_event_http_abort(raw_data, da_info);
		break;
*/
	}
}

da_bool_t is_stopped_state(da_info_t *da_info)
{
	http_info_t *http_info = DA_NULL;
	http_state_t http_state;
	NULL_CHECK_RET_OPT(da_info, DA_FALSE);
	http_info = da_info->http_info;
	NULL_CHECK_RET_OPT(http_info, DA_FALSE);
	DA_MUTEX_LOCK(&(http_info->mutex_state));
	http_state = http_info->state;
	DA_MUTEX_UNLOCK(&(http_info->mutex_state));
	switch (http_state) {
	case HTTP_STATE_REQUEST_CANCEL:
	case HTTP_STATE_CANCELED:
	case HTTP_STATE_FAILED:
	case HTTP_STATE_ABORTED:
	//case HTTP_STATE_REQUEST_PAUSE:
	//case HTTP_STATE_REQUEST_RESUME:
	//case HTTP_STATE_WAIT_FOR_NET_ERR:
		return DA_TRUE;
	default:
		return DA_FALSE;
	}
	return DA_FALSE;
}