~drizzle-trunk/drizzle/development

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
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 *  Created by Barry Leslie on 10/02/09.
 *
 */
#include "CSConfig.h"
#include <inttypes.h>
#include <stdlib.h>

#include <curl/curl.h>

#include "CSGlobal.h"
#include "CSString.h"
#include "CSStrUtil.h"
#include "CSEncode.h"
#include "CSS3Protocol.h"
#include "CSXML.h"

#ifdef S3_UNIT_TEST
//#define SHOW_SIGNING
// Uncomment this line to trace network action during request. Very Usefull!!
#define DEBUG_CURL
#define DUMP_ERRORS
#endif

//#define DUMP_ERRORS
//#define SHOW_SIGNING

#define HEX_CHECKSUM_VALUE_SIZE (2 *CHECKSUM_VALUE_SIZE)

#define THROW_CURL_IF(v) { if (v) CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_curl_error);}

//-------------------------------
static const char *retryCodes[] = {
	"ExpiredToken",
	"InternalError",
	"OperationAborted",
	"RequestTimeout",
	"SlowDown",
	NULL
};

//======================================
static size_t receive_data(void *ptr, size_t size, size_t nmemb, void *stream);
static size_t receive_header(void *ptr, size_t size, size_t nmemb, void *stream);
static size_t send_callback(void *ptr, size_t size, size_t nmemb, void *stream);

class S3ProtocolCon : CSXMLBuffer, public CSObject {

	private:
	
	virtual bool openNode(char *path, char *value) {
		if (value && *value && (strcmp(path,"/error/code/") == 0)) {
			printf("S3 ERROR Code: %s\n", value);
			for (int i = 0; retryCodes[i] && !ms_retry; i++)
				ms_retry = (strcmp(value, retryCodes[i]) == 0);
				
			if (ms_retry && !strcmp("slowdown", value)) 
				ms_slowDown = true;
		} else if (value && *value && (strcmp(path,"/error/message/") == 0)) {
			printf("S3 ERROR MESSAGE: %s\n", value);
		}
		return true;
	}

	virtual bool closeNode(char *path) {
		(void)path;
		return true;
	}

	virtual bool addAttribute(char *path, char *name, char *value) {
		(void)path;
		(void)name;
		(void)value;
		return true;
	}
	
	//-------------------------------
	void parse_s3_error()
	{
		enter_();

		if (!ms_errorReply)
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "Missing HTTP reply: possible S3 connection failure.");

	#ifdef DUMP_ERRORS
		printf("ms_errorReply:\n===========\n%s\n===========\n", ms_errorReply->getCString());
	#endif
		
		if (!parseData(ms_errorReply->getCString(), ms_errorReply->length(), 0)){
			int		err;
			char	*msg;

			getError(&err, &msg);
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, msg);
		}
		
		exit_();
	}
	
	public:
	
	CSHTTPHeaders	ms_reply_headers;
	CSStringBuffer	ms_buffer; // A scratch buffer

	CURL			*ms_curl;	
	struct curl_slist	*ms_header_list;	// A curl list of headers to be sent with the next request.

	CSInputStream	*ms_inputStream;
	CSOutputStream	*ms_outputStream;
	
	CSMd5			ms_md5;
	char			ms_s3Checksum[HEX_CHECKSUM_VALUE_SIZE +1];
	bool			ms_calculate_md5;
	
	bool			ms_notFound; // True if the object could not be found
	bool			ms_retry; // True if the request failed with a retry error.
	bool			ms_slowDown;
	
	CSStringBuffer	*ms_errorReply;
	char			ms_curl_error[CURL_ERROR_SIZE];
	
	off64_t			ms_data_size;
	
	unsigned int	ms_replyStatus;
	bool			ms_throw_error;	// Gets set if an exception occurs in a callback.
	bool			ms_old_libcurl;
	char			*ms_safe_url;
	time_t			ms_last_modified;
	
	S3ProtocolCon():
		ms_curl(NULL),
		ms_header_list(NULL),
		ms_inputStream(NULL),
		ms_outputStream(NULL),
		ms_calculate_md5(false),
		ms_notFound(false),
		ms_retry(false),
		ms_slowDown(false),
		ms_errorReply(NULL),
		ms_data_size(0),
		ms_replyStatus(0),
		ms_throw_error(false),
		ms_old_libcurl(false),
		ms_safe_url(NULL),
		ms_last_modified(0)
	{
	
		ms_curl = curl_easy_init();
		if (!ms_curl)
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "curl_easy_init() failed.");

		curl_version_info_data *curl_ver = curl_version_info(CURLVERSION_NOW); 
		
		// libCurl versions prior to 7.17.0 did not make copies of strings passed into curl_easy_setopt()
		// If this version requirement is a problem I can do this myself, if I have to, I guess. :(
		if (curl_ver->version_num < 0X071700 ) {
			ms_old_libcurl = true;
			
			//char msg[200];
			//snprintf(msg, 200, "libcurl version %s is too old, require version 7.17.0 or newer.", curl_ver->version);
			//CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, msg);
		}
		
		if (curl_easy_setopt(ms_curl, CURLOPT_ERRORBUFFER, ms_curl_error))
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "curl_easy_setopt(CURLOPT_ERRORBUFFER) failed.");
		
#ifdef DEBUG_CURL
		curl_easy_setopt(ms_curl, CURLOPT_VERBOSE, 1L);
#endif		
		//THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_TCP_NODELAY, 1L));
	

		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_NOPROGRESS, 1L));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_WRITEFUNCTION, receive_data));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_READFUNCTION, send_callback));	
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HEADERFUNCTION, receive_header));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_WRITEDATA, this));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_READDATA, this));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_WRITEHEADER, this));
		

		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_FOLLOWLOCATION, 1L)); // Follow redirects.
	
	}
	
	~S3ProtocolCon()
	{
		if (ms_curl) 
			curl_easy_cleanup(ms_curl);			
		if (ms_header_list) 
			curl_slist_free_all(ms_header_list);			
		if (ms_inputStream)
			ms_inputStream->release();
		if (ms_outputStream)
			ms_outputStream->release();
		if (ms_errorReply)
			ms_errorReply->release();
			
		ms_reply_headers.clearHeaders();
		
		if (ms_safe_url)
			cs_free(ms_safe_url);
	}

	inline void check_reply_status() 
	{
		if (ms_replyStatus > 199 && ms_replyStatus < 300)
			return;
		
		
		
		switch (ms_replyStatus) {
			case 200:
			case 204:	// No Content
			//case 301: // Moved Permanently
			//case 307: // Temporary Redirect
				break;
			case 404:	// Not Found
			case 403:	// Forbidden (S3 object not found)
				ms_notFound = true;
				break;
			case 500:	
				ms_retry = true;
				break;
			default: {
				parse_s3_error();
				
				
				
				if (!ms_retry) {
					enter_();
					CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_errorReply->getCString());
					outer_();
				} else if (ms_slowDown) {
					enter_();
					CSException::logException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 slow down request.");
					self->sleep(10); // sleep for 1/100 second.
					outer_();
				}
			}
		}
		
	}

	
	inline void ms_reset()
	{
		// Remove any old headers
		if (ms_header_list) {
			curl_slist_free_all(ms_header_list);
			ms_header_list = NULL;
		}

		ms_reply_headers.clearHeaders();
		ms_replyStatus = 0;
		ms_throw_error = false;
		if (ms_errorReply)
			ms_errorReply->setLength(0);
			
		ms_s3Checksum[0] = 0;
		ms_notFound = false;
		ms_retry = false;
		
		if (ms_outputStream) {
			ms_outputStream->release();
			ms_outputStream = NULL;
		}
		if (ms_inputStream) {
			ms_inputStream->release();
			ms_inputStream = NULL;
		}
		
		if (ms_safe_url) {
			cs_free(ms_safe_url);
			ms_safe_url = NULL;
		}
	}
	
	inline void ms_setHeader(const char *header)
	{
		ms_header_list = curl_slist_append(ms_header_list, header);
		if (!ms_header_list) 
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "curl_slist_append() failed.");
	}


private:	
	inline const char *safe_url(const char *url)
	{
		if (ms_old_libcurl == false)
			return url;
			
		if (ms_safe_url) {
			cs_free(ms_safe_url);
			ms_safe_url = NULL;
		}
		ms_safe_url = cs_strdup(url);
		return ms_safe_url;
	}
	
public:	
	inline void ms_setURL(const char *url)
	{
		//printf("URL: \"%s\n", url);
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_URL, safe_url(url)));
	}
	
	inline void ms_execute_delete_request()
	{
		CURLcode rtc;
		
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HTTPHEADER, ms_header_list));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_CUSTOMREQUEST, "DELETE"));

		rtc = curl_easy_perform(ms_curl);
		
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_CUSTOMREQUEST, NULL)); // IMPORTANT: Reset this to it's default value

		if (rtc && !ms_throw_error)
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_curl_error);
			
		if (ms_throw_error) {
			enter_();
			throw_();
			outer_();
		}
		
		check_reply_status();
	}
	
	inline void ms_execute_copy_request()
	{
		CURLcode rtc;
		
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HTTPHEADER, ms_header_list));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_INFILESIZE_LARGE, 0));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_UPLOAD, 1L));
		
		rtc = curl_easy_perform(ms_curl);
		
		if (rtc && !ms_throw_error)
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_curl_error);
			
		if (ms_throw_error) {
			enter_();
			throw_();
			outer_();
		}
		
		check_reply_status();
	}
	
	inline void ms_execute_get_request(CSOutputStream *output)
	{
		enter_();
		
		if (output) {
			push_(output);
			THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HTTPGET, 1L));
		} else {
			THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_NOBODY, 1L));
		}
		
		// 
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_FILETIME, 1L));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HTTPHEADER, ms_header_list));
    // Ask curl to parse the Last-Modified header.  This is easier than
    // parsing it ourselves.

		ms_outputStream = output;	
		if (curl_easy_perform(ms_curl) && !ms_throw_error) {
			ms_outputStream = NULL;	
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_curl_error);
		}
		ms_outputStream = NULL;	
		if (output){
			release_(output);
		}
		
		if (ms_throw_error) 
			throw_();
		
		check_reply_status();
		curl_easy_getinfo(ms_curl, CURLINFO_FILETIME, &ms_last_modified);
		exit_();		
	}
	
	inline void ms_execute_put_request(CSInputStream *input, off64_t size)
	{
		enter_();
		
		push_(input);	
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_HTTPHEADER, ms_header_list));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_INFILESIZE_LARGE, size));
		THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_UPLOAD, 1L));
		//THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_POSTFIELDSIZE_LARGE, size));
		//THROW_CURL_IF(curl_easy_setopt(ms_curl, CURLOPT_POST, 1L));

		ms_md5.md5_init();
		
		ms_data_size = size;
		ms_inputStream = input;	
		if (curl_easy_perform(ms_curl) && !ms_throw_error) {
			ms_inputStream = NULL;	
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, ms_curl_error);
		}
		ms_inputStream = NULL;
		release_(input);	

			
		if (ms_throw_error)
			throw_();
		
		check_reply_status();
		
		if (ms_calculate_md5) {
			// If the data was not sent with an md5 checksum then verify
			// the server's md5 value with the one calculated during the send.
			char checksum[HEX_CHECKSUM_VALUE_SIZE +1];
			Md5Digest digest;
			
			ms_md5.md5_get_digest(&digest);
			cs_bin_to_hex(HEX_CHECKSUM_VALUE_SIZE, checksum, CHECKSUM_VALUE_SIZE, digest.val);
			checksum[HEX_CHECKSUM_VALUE_SIZE] = 0;
			
			cs_strToUpper(ms_s3Checksum);
			if (strcmp(checksum, ms_s3Checksum)) {
				// The request should be restarted in this case.
				ms_retry = true;
				CSException::logException(CS_CONTEXT, CS_ERR_CHECKSUM_ERROR, "Calculated checksum did not match S3 checksum");
			}
		}

		exit_();		
	}
	
};

//======================================




//======================================
CSS3Protocol::CSS3Protocol():
	s3_server(NULL),
	s3_public_key(NULL),
	s3_private_key(NULL),
	s3_maxRetries(5),
	s3_sleepTime(0)
{
	new_(s3_server, CSStringBuffer());
	s3_server->append("s3.amazonaws.com/");

	s3_public_key = CSString::newString("");
	s3_private_key = CSString::newString("");
	
}

//------------------
CSS3Protocol::~CSS3Protocol()
{
	if (s3_server)
		s3_server->release();
	
	if (s3_public_key)
		s3_public_key->release();
	
	if (s3_private_key)
		s3_private_key->release();
}
	
//------------------
CSString *CSS3Protocol::s3_getSignature(const char *verb, 
										const char *md5, 
										const char *content_type, 
										const char *date, 
										const char *bucket, 
										const char *key,
										CSString *headers 
									)
{
	CSStringBuffer *s3_buffer;
	enter_();
	if (headers)
		push_(headers);
	
	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);
	
	s3_buffer->setLength(0);
	s3_buffer->append(verb);	
	s3_buffer->append("\n");	
	if (md5) s3_buffer->append(md5);	
	s3_buffer->append("\n");	
	if (content_type) s3_buffer->append(content_type);	
	s3_buffer->append("\n");	
	s3_buffer->append(date);
	if (headers) { 
		// Note: headers are assumed to be in lower case, sorted, and containing no white space.
		s3_buffer->append("\n");	
		s3_buffer->append(headers->getCString());
	}
	s3_buffer->append("\n/");
	s3_buffer->append(bucket);
	s3_buffer->append("/");
	s3_buffer->append(key);
	
#ifdef SHOW_SIGNING
printf("signing:\n=================\n%s\n=================\n", 	s3_buffer->getCString());
if(0){
	const char *ptr = s3_buffer->getCString();
	while (*ptr) {
		printf("%x ", *ptr); ptr++;
	}
	printf("\n");
}
#endif

	CSString *sig = signature(s3_buffer->getCString(), s3_private_key->getCString());
	release_(s3_buffer);
	if (headers) 
		release_(headers);

	return_(sig);
}
//----------------------
// CURL callback functions:
////////////////////////////
//----------------------
//-----------------
static bool try_ReadStream(CSThread *self, S3ProtocolCon *con, unsigned char *ptr, size_t buffer_size, size_t *data_sent)
{
	volatile bool rtc = true;
	try_(a) {
		*data_sent = con->ms_inputStream->read((char*)ptr, buffer_size);
		if (*data_sent <= con->ms_data_size) {
			con->ms_data_size -= *data_sent;
			if (*data_sent)
				con->ms_md5.md5_append(ptr, *data_sent); // Calculating the checksum for the data sent.
		} else if (*data_sent > con->ms_data_size) 
			CSException::RecordException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "Blob larger than expected.");
		else if (con->ms_data_size && !*data_sent)
			CSException::RecordException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "Blob smaller than expected.");
		rtc = false;
	}
	
	catch_(a)
	cont_(a);
	return rtc;
}

//----------------------
static size_t send_callback(void *ptr, size_t objs, size_t obj_size, void *v_con)
{
	S3ProtocolCon *con = (S3ProtocolCon*) v_con;
	size_t data_sent, buffer_size = objs * obj_size;

	if (!con->ms_data_size)
		return 0;
		
	enter_();
	if (try_ReadStream(self, con, (unsigned char*)ptr, buffer_size, &data_sent)) {
		con->ms_throw_error = true;
		data_sent = (size_t)-1;
	}
	
	return_(data_sent);
}

//-----------------
static bool try_WriteStream(CSThread *self, S3ProtocolCon *con, char *ptr, size_t data_len)
{
	volatile bool rtc = true;
	try_(a) {
		if (con->ms_replyStatus >= 400) { // Collect the error reply.
			if (!con->ms_errorReply)
				con->ms_errorReply = new CSStringBuffer(50);		
			con->ms_errorReply->append(ptr, data_len);
		} else if (	con->ms_outputStream)
			con->ms_outputStream->write(ptr, data_len);
		rtc = false;
	}
	
	catch_(a)
	cont_(a);
	return rtc;
}

//----------------------
static size_t receive_data(void *vptr, size_t objs, size_t obj_size, void *v_con)
{
	S3ProtocolCon *con = (S3ProtocolCon*) v_con;
	size_t data_len = objs * obj_size;

	enter_();
	if (try_WriteStream(self, con, (char*)vptr, data_len)) {
		con->ms_throw_error = true;
		data_len = (size_t)-1;
	}

	return_(data_len);	
}

#define IS_REDIRECT(s) ((s >= 300) && (s < 400))
//----------------------
static bool try_addHeader(CSThread *self, S3ProtocolCon *con, char *name, uint32_t name_len, char *value, uint32_t value_len)
{
	try_(a) {
		con->ms_reply_headers.addHeader(name, name_len, value, value_len);
		return false;
	}
	
	catch_(a);
	cont_(a);
	return true;
}

//----------------------
static size_t receive_header(void *header, size_t objs, size_t obj_size, void *v_con)
{
	S3ProtocolCon *con = (S3ProtocolCon*) v_con;
	size_t size = objs * obj_size;
	char *end, *ptr = (char*) header, *name, *value = NULL;
	uint32_t name_len =0, value_len = 0;
	
//printf(	"receive_header: %s\n", ptr);
	end = ptr + size;
	if (*(end -2) == '\r' && *(end -1) == '\n')
		end -=2;
		
	while ((end != ptr) && (*ptr == ' ')) ptr++;
	if (end == ptr)
		return size;
	
	// Get the reply status.
	// Status 100 = Continue
	if (((!con->ms_replyStatus) || (con->ms_replyStatus == 100) || IS_REDIRECT(con->ms_replyStatus) ) 
			&& !strncasecmp(ptr, "HTTP", 4)
		) {
		char status[4];
		while ((end != ptr) && (*ptr != ' ')) ptr++; // skip HTTP stuff
		while ((end != ptr) && (*ptr == ' ')) ptr++; // find the start of eh status code.
		if (end == ptr)
			return size;
			
		if (end < (ptr +3)) // expecting a 3 digit status code.
			return size;
			
		memcpy(status, ptr, 3);
		status[3] = 0;
		
		con->ms_replyStatus = atoi(status);
	}
	
	name = ptr;
	while ((end != ptr) && (*ptr != ':')) ptr++;
	if (end == ptr)
		return size;
	name_len = ptr - name;
	
	ptr++; 
	while ((end != ptr) && (*ptr == ' ')) ptr++;
	if (end == ptr)
		return size;
	
	value = ptr;
	value_len = end - value;
	
	while (name[name_len-1] == ' ') name_len--;
	while (value[value_len-1] == ' ') value_len--;
	
	if (!strncasecmp(name, "ETag", 4)) {
		if (*value == '"') {
			value++; value_len -=2; // Strip quotation marks from checksum string.
		}
		if (value_len == HEX_CHECKSUM_VALUE_SIZE) {
			memcpy(con->ms_s3Checksum, value, value_len);
			con->ms_s3Checksum[value_len] = 0;
		}
	}
	
	enter_();
	if (try_addHeader(self, con, name, name_len, value, value_len)) {
		con->ms_throw_error = true;
		size = (size_t)-1;
	}
	return_(size);
}

//----------------------

#define SET_DATE_FROM_TIME(t, d) {strftime(d, sizeof(d), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));}
#define SET_DATE(d) {time_t t = time(NULL); SET_DATE_FROM_TIME(t, d);}

bool CSS3Protocol::s3_delete(const char *bucket, const char *key)
{
 	CSStringBuffer *s3_buffer;
	char date[64];
	CSString *signed_str;
	uint32_t retry_count = 0;
	S3ProtocolCon *con_data;

	enter_();

	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	new_(con_data, S3ProtocolCon());
	push_(con_data);

retry:
	// Clear old settings. 
	con_data->ms_reset();	
	
	SET_DATE(date);
 
	// Build the URL
	s3_buffer->setLength(0);
	s3_buffer->append("http://");
	s3_buffer->append(bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
	s3_buffer->append(key);

	con_data->ms_setURL(s3_buffer->getCString());
	
	// Add the 'DATE' header
	s3_buffer->setLength(0);
	s3_buffer->append("Date: ");	
	s3_buffer->append(date);
	con_data->ms_setHeader(s3_buffer->getCString());

	// Create the authentication signature and add the 'Authorization' header
	signed_str = s3_getSignature("DELETE", NULL, NULL, date, bucket, key);
	push_(signed_str);
	s3_buffer->setLength(0);
	s3_buffer->append("Authorization: AWS ");	
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append(":");	
	s3_buffer->append(signed_str->getCString());	
	release_(signed_str); signed_str = NULL;
	
	con_data->ms_setHeader(s3_buffer->getCString());
	
	con_data->ms_execute_delete_request();
	
	if (con_data->ms_retry) {
		if (retry_count == s3_maxRetries) {
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 operation aborted after max retries.");
		}
	//printf("RETRY: s3_delete()\n");
		retry_count++;
		self->sleep(s3_sleepTime);
		goto retry;
	}
	
	bool notFound = con_data->ms_notFound;
	release_(con_data);
	release_(s3_buffer);
	
	return_(!notFound);
}

//-------------------------------
void CSS3Protocol::s3_copy(const char *dest_server, const char *dest_bucket, const char *dest_key, const char *src_bucket, const char *src_key)
{
  	CSStringBuffer *s3_buffer;
	char date[64];
	CSString *signed_str;
	uint32_t retry_count = 0;
	S3ProtocolCon *con_data;

	enter_();

	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	new_(con_data, S3ProtocolCon());
	push_(con_data);
	
	if (!dest_server)
		dest_server = s3_server->getCString();

retry:
	// Clear old settings. 
	con_data->ms_reset();	
	
	SET_DATE(date);
 
	// Build the URL
	s3_buffer->setLength(0);
	s3_buffer->append("http://");
	s3_buffer->append(dest_bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
	s3_buffer->append(dest_key);

	con_data->ms_setURL(s3_buffer->getCString());
	
	// Add the destination location
	s3_buffer->setLength(0);
	s3_buffer->append("Host: ");	
	s3_buffer->append(dest_bucket);
	s3_buffer->append(".");	
	s3_buffer->append(dest_server);
	s3_buffer->setLength(s3_buffer->length() -1); // trim the '/'
	con_data->ms_setHeader(s3_buffer->getCString());
	
	// Add the source location
	s3_buffer->setLength(0);
	s3_buffer->append("x-amz-copy-source:");	
	s3_buffer->append(src_bucket);
	s3_buffer->append("/");
	s3_buffer->append(src_key);
	con_data->ms_setHeader(s3_buffer->getCString());
	
	// Create the authentication signature and add the 'Authorization' header
	signed_str = s3_getSignature("PUT", NULL, NULL, date, dest_bucket, dest_key, CSString::newString(s3_buffer->getCString()));
	push_(signed_str);

	// Add the 'DATE' header
	s3_buffer->setLength(0);
	s3_buffer->append("Date: ");	
	s3_buffer->append(date);
	con_data->ms_setHeader(s3_buffer->getCString());

	// Add the signature
	s3_buffer->setLength(0);
	s3_buffer->append("Authorization: AWS ");	
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append(":");	
	s3_buffer->append(signed_str->getCString());	
	release_(signed_str); signed_str = NULL;
	con_data->ms_setHeader(s3_buffer->getCString());
	
	con_data->ms_execute_copy_request();
	
	if (con_data->ms_notFound) {
		s3_buffer->setLength(0);
		s3_buffer->append("Cloud copy failed, object not found: ");
		s3_buffer->append(src_bucket);
		s3_buffer->append(" ");
		s3_buffer->append(src_key);
		CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, s3_buffer->getCString());
	}
	
	if (con_data->ms_retry) {
		if (retry_count == s3_maxRetries) {
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 operation aborted after max retries.");
		}
	//printf("RETRY: s3_copy()\n");
		retry_count++;
		self->sleep(s3_sleepTime);
		goto retry;
	}
	
	release_(con_data);
	release_(s3_buffer);
	
	exit_();
}


//-------------------------------
CSVector *CSS3Protocol::s3_receive(CSOutputStream *output, const char *bucket, const char *key, bool *found, S3RangePtr range, time_t *last_modified)
{
 	CSStringBuffer *s3_buffer;
    char date[64];
	CSString *signed_str;
	uint32_t retry_count = 0;
	S3ProtocolCon *con_data;
	CSVector *replyHeaders;
	CSString *range_header = NULL;
	const char *http_op;

	enter_();

	if (output) {
		push_(output);
		http_op = "GET";
	} else
		http_op = "HEAD";

	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	new_(con_data, S3ProtocolCon());
	push_(con_data);

retry:
	// Clear old settings. 
	con_data->ms_reset();	
	
	SET_DATE(date);
 
	// Build the URL
	s3_buffer->setLength(0);
	s3_buffer->append("http://");
	s3_buffer->append(bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
	s3_buffer->append(key);

	con_data->ms_setURL(s3_buffer->getCString());
	
	// Add the 'DATE' header
	s3_buffer->setLength(0);
	s3_buffer->append("Date: ");	
	s3_buffer->append(date);
	con_data->ms_setHeader(s3_buffer->getCString());

	if (range) {
		char buffer[80];
		snprintf(buffer, 80,"Range: bytes=%"PRIu64"-%"PRIu64, range->startByte, range->endByte);

		range_header = CSString::newString(buffer);
	}
	// Create the authentication signature and add the 'Authorization' header
	if (range_header)
		con_data->ms_setHeader(range_header->getCString());
	signed_str = s3_getSignature(http_op, NULL, NULL, date, bucket, key, NULL);
	push_(signed_str);
	s3_buffer->setLength(0);
	s3_buffer->append("Authorization: AWS ");	
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append(":");	
	s3_buffer->append(signed_str->getCString());	
	release_(signed_str); signed_str = NULL;
	con_data->ms_setHeader(s3_buffer->getCString());
	
	if (output) output->retain();
	con_data->ms_execute_get_request(output);
	
	if (con_data->ms_retry) {
		if (retry_count == s3_maxRetries) {
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 operation aborted after max retries.");
		}
	//printf("RETRY: s3_receive()\n");
		retry_count++;
		output->reset();
		self->sleep(s3_sleepTime);
		goto retry;
	}
	
	if (last_modified)
		*last_modified = con_data->ms_last_modified;
	*found = !con_data->ms_notFound;
	replyHeaders = con_data->ms_reply_headers.takeHeaders();
	release_(con_data);
	release_(s3_buffer);
	if (output)
		release_(output);
	
	return_(replyHeaders);
}

class S3ListParser : public CSXMLBuffer {

	CSVector *list;
	public:


	bool parseListData(const char *data, size_t len, CSVector *keys)
	{
		list = keys;
		return parseData(data, len, 0);
	}

	private:
	virtual bool openNode(char *path, char *value) {
		if (value && *value && (strcmp(path,"/listbucketresult/contents/key/") == 0))
			list->add(CSString::newString(value));
		return true;
	}

	virtual bool closeNode(char *path) {
		(void)path;
		return true;
	}

	virtual bool addAttribute(char *path, char *name, char *value) {
		(void)path;
		(void)name;
		(void)value;
		return true;
	}

};

//-------------------------------
static CSVector *parse_s3_list(CSMemoryOutputStream *output)
{
	S3ListParser s3ListParser;
	const char *data;
	CSVector *vector;
	size_t len;
	
	enter_();

	push_(output);
	
	new_(vector, CSVector(10));	
	push_(vector);	

	data = (const char *) output->getMemory(&len);
	if (!s3ListParser.parseListData(data, len, vector)) {
		int		err;
		char	*msg;

		s3ListParser.getError(&err, &msg);
		CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, msg);
	}

	pop_(vector);
	release_(output);
	return_(vector);
}


//-------------------------------
CSVector *CSS3Protocol::s3_list(const char *bucket, const char *key_prefix, uint32_t max)
{
 	CSStringBuffer *s3_buffer;
    char date[64];
	CSString *signed_str;
	CSMemoryOutputStream *output;
	uint32_t retry_count = 0;
	S3ProtocolCon *con_data;
	enter_();

	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	output = CSMemoryOutputStream::newStream(1024, 1024);
	push_(output);
	
	new_(con_data, S3ProtocolCon());
	push_(con_data);

retry:

	// Clear old settings. 
	con_data->ms_reset();	
	
	SET_DATE(date);
 
	// Build the URL
	s3_buffer->setLength(0);
	s3_buffer->append("http://");
	s3_buffer->append(bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
//s3_buffer->append("/");	
//s3_buffer->append(bucket);
	if (key_prefix) {
		s3_buffer->append("?prefix=");
		s3_buffer->append(key_prefix);
	}
	
	if (max) {
		if (key_prefix)
			s3_buffer->append("&max-keys=");
		else
			s3_buffer->append("?max-keys=");
		s3_buffer->append(max);
	}

	con_data->ms_setURL(s3_buffer->getCString());
	
	// Add the 'DATE' header
	s3_buffer->setLength(0);
	s3_buffer->append("Date: ");	
	s3_buffer->append(date);
	con_data->ms_setHeader(s3_buffer->getCString());

	// Create the authentication signature and add the 'Authorization' header
	signed_str = s3_getSignature("GET", NULL, NULL, date, bucket, "");
	push_(signed_str);
	s3_buffer->setLength(0);
	s3_buffer->append("Authorization: AWS ");	
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append(":");	
	s3_buffer->append(signed_str->getCString());	
	release_(signed_str); signed_str = NULL;
	con_data->ms_setHeader(s3_buffer->getCString());
	
	con_data->ms_execute_get_request(RETAIN(output));
	
	if (con_data->ms_retry) {
		if (retry_count == s3_maxRetries) {
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 operation aborted after max retries.");
		}
	//printf("RETRY: s3_list()\n");
		retry_count++;
		output->reset();
		self->sleep(s3_sleepTime);
		goto retry;
	}
	
	release_(con_data);
	pop_(output);
	release_(s3_buffer);
	return_(parse_s3_list(output));
}

//-------------------------------
CSString *CSS3Protocol::s3_getAuthorization(const char *bucket, const char *key, const char *content_type, uint32_t *s3AuthorizationTime)
{
    char date[64];
	CSString *signed_str;
	time_t sys_time;

	enter_();

	if (!content_type)
		content_type = "binary/octet-stream";
		
	sys_time = time(NULL);
	
	*s3AuthorizationTime = (uint32_t)sys_time;
	
	SET_DATE_FROM_TIME(sys_time, date);
	signed_str = s3_getSignature("PUT", NULL, content_type, date, bucket, key);
	return_(signed_str);
}

//-------------------------------
CSVector *CSS3Protocol::s3_send(CSInputStream *input, const char *bucket, const char *key, off64_t size, const char *content_type, Md5Digest *digest, const char *s3Authorization, time_t s3AuthorizationTime)
{
 	CSStringBuffer *s3_buffer;
    char date[64];
	CSString *signed_str;
	uint32_t retry_count = 0;
	S3ProtocolCon *con_data;
	CSVector *replyHeaders;
	char checksum[32], *md5 = NULL;

	enter_();
	push_(input);

	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	new_(con_data, S3ProtocolCon());
	push_(con_data);
		
	if (!content_type)
		content_type = "binary/octet-stream";
		
retry:

	// Clear old settings. 
	con_data->ms_reset();	
	
	if (s3Authorization) {
		SET_DATE_FROM_TIME(s3AuthorizationTime, date);
	} else {
		SET_DATE(date);
	}
	
	// Build the URL
	s3_buffer->setLength(0);
	s3_buffer->append("http://");
	s3_buffer->append(bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
	s3_buffer->append(key);

	con_data->ms_setURL(s3_buffer->getCString());
	
	// Add the 'DATE' header
	s3_buffer->setLength(0);
	s3_buffer->append("Date: ");	
	s3_buffer->append(date);
	con_data->ms_setHeader(s3_buffer->getCString());
	
	// Add the 'Content-Type' header
	s3_buffer->setLength(0);
	s3_buffer->append("Content-Type: ");	
	s3_buffer->append(content_type);
	con_data->ms_setHeader(s3_buffer->getCString());
		
	if (digest) {
		// Add the Md5 checksum header
		md5 = checksum;
		memset(checksum, 0, 32);
		base64Encode(digest->val, 16, checksum, 32);
		
		s3_buffer->setLength(0);
		s3_buffer->append("Content-MD5: ");	
		s3_buffer->append(checksum);
		con_data->ms_setHeader(s3_buffer->getCString());		
		con_data->ms_calculate_md5 = false;
	} else 
		con_data->ms_calculate_md5 = true;
	

	// Create the authentication signature and add the 'Authorization' header
	if (!s3Authorization)
		signed_str = s3_getSignature("PUT", md5, content_type, date, bucket, key);
	else
		signed_str = CSString::newString(s3Authorization);
	push_(signed_str);
	s3_buffer->setLength(0);
	s3_buffer->append("Authorization: AWS ");	
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append(":");	
	s3_buffer->append(signed_str->getCString());	
	release_(signed_str); signed_str = NULL;
	con_data->ms_setHeader(s3_buffer->getCString());
	
	con_data->ms_execute_put_request(RETAIN(input), size);
	
	if (con_data->ms_retry) {
		if (retry_count == s3_maxRetries) {
			CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, "S3 operation aborted after max retries.");
		}
	//printf("RETRY: s3_send()\n");
		retry_count++;
		input->reset();
		self->sleep(s3_sleepTime);
		goto retry;
	}
	
	replyHeaders = con_data->ms_reply_headers.takeHeaders();

	release_(con_data);
	release_(s3_buffer);
	release_(input);
	return_(replyHeaders);
}

//-------------------------------
CSString *CSS3Protocol::s3_getDataURL(const char *bucket, const char *key, uint32_t keep_alive)
{
 	CSStringBuffer *s3_buffer;
	char timeout[32];
	CSString *signed_str;
	enter_();
	
	new_(s3_buffer, CSStringBuffer());
	push_(s3_buffer);

	snprintf(timeout, 32, "%"PRId32"", ((uint32_t)time(NULL)) + keep_alive);
	
	signed_str = s3_getSignature("GET", NULL, NULL, timeout, bucket, key);
//printf("Unsafe: \"%s\"\n", signed_str->getCString());
	signed_str = urlEncode(signed_str); // Because the signature is in the URL it must be URL encoded.
//printf("  Safe: \"%s\"\n", signed_str->getCString());
	push_(signed_str);
	
	s3_buffer->setLength(0);	
	s3_buffer->append("http://");
	s3_buffer->append(bucket);
	s3_buffer->append(".");	
	s3_buffer->append(s3_server->getCString());
	s3_buffer->append(key);

	s3_buffer->append("?AWSAccessKeyId=");
	s3_buffer->append(s3_public_key->getCString());
	s3_buffer->append("&Expires=");
	s3_buffer->append(timeout);
	s3_buffer->append("&Signature=");
	s3_buffer->append(signed_str->getCString());
	
	release_(signed_str);
	
	pop_(s3_buffer);
	CSString *str = CSString::newString(s3_buffer);
	return_(str);	
}

//#define S3_UNIT_TEST
#ifdef S3_UNIT_TEST
static void show_help_info(const char *cmd)
{
	printf("Get authenticated query string:\n\t%s q <bucket> <object_key> <timeout>\n", cmd);
	printf("Delete object:\n\t%s d <bucket> <object_key>\n", cmd);
	printf("Delete all object with a given prefix:\n\t%s D <bucket> <object_prefix>\n", cmd);
	printf("Get object, data will be written to 'prottest.out':\n\t%s g <bucket> <object_key> <timeout>\n", cmd);
	printf("Get object header only:\n\t%s h <bucket> <object_key> <timeout>\n", cmd);
	printf("Put (Upload) an object:\n\t%s p <bucket> <object_key> <file>\n", cmd);
	printf("List objects in the bucket:\n\t%s l <bucket> [<object_prefix> [max_list_size]]\n", cmd);
	printf("Copy object:\n\t%s c <src_bucket> <src_object_key> <dst_bucket> <dst_object_key> \n", cmd);
	printf("Copy all object with a given prefix:\n\t%s C <src_bucket> <object_key_prefix> <dst_bucket> \n", cmd);
}

void dump_headers(CSVector *header_array)
{
	CSHTTPHeaders headers;
	
	headers.setHeaders(header_array);
	printf("Reply Headers:\n");
	printf("--------------\n");
	
	for (uint32_t i = 0; i < headers.numHeaders(); i++) {
		CSHeader *h = headers.getHeader(i);
		
		printf("%s : %s\n", h->getNameCString(), h->getValueCString());
		h->release();
	}
	printf("--------------\n");
	headers.clearHeaders();
}

int main(int argc, char **argv)
{
	CSThread *main_thread;
	const char *pub_key;
	const char *priv_key;
	const char *server;
	CSS3Protocol *prot = NULL;
	
	if (argc < 3) {
		show_help_info(argv[0]);
		return 0;
	}
	
	if (! CSThread::startUp()) {
		CSException::throwException(CS_CONTEXT, ENOMEM, "CSThread::startUp() failed.");
		return 1;
	}
	
	cs_init_memory();
	
	main_thread = new CSThread( NULL);
	CSThread::setSelf(main_thread);
	
	enter_();
	try_(a) {
	
		pub_key = getenv("S3_ACCESS_KEY_ID");
		priv_key = getenv("S3_SECRET_ACCESS_KEY");
		new_(prot, CSS3Protocol());
		push_(prot);
		
		server = getenv("S3_SERVER");
		if ((server == NULL) || (*server == 0))
			server = "s3.amazonaws.com/";
		prot->s3_setServer(server);
		prot->s3_setPublicKey(pub_key);
		prot->s3_setPrivateKey(priv_key);
		
		switch (argv[1][0]) {
			case 'q': // Get the query string
				if (argc == 5) {
					CSString *qstr = prot->s3_getDataURL(argv[2], argv[3], atoi(argv[4]));
					printf("To test call:\ncurl -L -D - \"%s\"\n", qstr->getCString());
					qstr->release();
				} else
					printf("Bad command: q <bucket> <object_key> <timeout>\n");
				
				break;
			case 'd': // Delete the object
				if (argc == 4) {
					printf("delete %s %s\n", argv[2], argv[3]);
					if (!prot->s3_delete(argv[2], argv[3]))
						printf("%s/%s could not be found.\n", argv[2], argv[3]);

				} else
					printf("Bad command: d <bucket> <object_key>\n");
				
				break;
			case 'D': // Delete  objects like
				if (argc == 4) {
					CSVector *list;
					CSString *key;
					
					list = prot->s3_list(argv[2], argv[3]);
					push_(list);
					while (key = (CSString*) list->take(0)) {
						printf("Deleting %s\n", key->getCString());
						prot->s3_delete(argv[2], key->getCString());
						key->release();
					}
					release_(list);
					
				} else
					printf("Bad command: D <bucket> <object_key_prefix>\n");
				
				break;
			case 'g':  // Get the object
				if ((argc == 4) || (argc == 6)) {
					CSFile *output;	
					CSVector *headers;
					bool found;				
					S3RangeRec *range_ptr = NULL, range = 	{0,0};		
					
					if (argc == 6) {
						range.startByte = atoi(argv[4]);
						range.endByte = atoi(argv[5]);
						range_ptr = &range;
					}
					
					output = CSFile::newFile("prottest.out");
					push_(output);
					output->open(CSFile::CREATE | CSFile::TRUNCATE);
					headers = prot->s3_receive(output->getOutputStream(), argv[2], argv[3], &found, range_ptr);
					if (!found)
						printf("%s/%s could not be found.\n", argv[2], argv[3]);
						
					dump_headers(headers);
						
					release_(output);
				} else
					printf("Bad command: g <bucket> <object_key>\n");
				
				break;
				
			case 'h':  // Get the object header
				if (argc == 4) {
					CSVector *headers;
					bool found;	
					S3RangeRec range = 	{0,0};		
					
					headers = prot->s3_receive(NULL, argv[2], argv[3], &found);
					if (!found)
						printf("%s/%s could not be found.\n", argv[2], argv[3]);
						
					dump_headers(headers);
						
				} else
					printf("Bad command: h <bucket> <object_key>\n");
				
				break;
				
			case 'p':  // Put (Upload) the object
				if (argc == 5) {
					CSFile *input;
					Md5Digest digest;
					CSVector *headers;
					
					input = CSFile::newFile(argv[4]);
					push_(input);
					input->open(CSFile::READONLY);
					input->md5Digest(&digest);
					headers = prot->s3_send(input->getInputStream(), argv[2], argv[3], input->myFilePath->getSize(), NULL, &digest);
					dump_headers(headers);
					release_(input);
				} else
					printf("Bad command: p <bucket> <object_key> <file> \n");
				
				break;
				
			case 'c':  // Copy the object
				if (argc == 6) {
					prot->s3_copy(NULL, argv[4], argv[5], argv[2], argv[3]);
				} else
					printf("Bad command: c <src_bucket> <src_object_key> <dst_bucket> <dst_object_key>\n");
				
				break;
				
			case 'C':  // Copy  objects like
				if (argc == 5) {
					CSVector *list;
					CSString *key;
					
					list = prot->s3_list(argv[2], argv[3]);
					push_(list);
					while (key = (CSString*) list->take(0)) {
						printf("Copying %s\n", key->getCString());
						prot->s3_copy(NULL, argv[4], key->getCString(), argv[2], key->getCString());
						key->release();
					}
					release_(list);
					
				} else
					printf("Bad command: C <src_bucket> <object_key_prefix> <dst_bucket>\n");
				
				break;
			case 'l':  // List the object
				if ((argc == 3) || (argc == 4) || (argc == 5)) {
					uint32_t max = 0;
					char *prefix = NULL;
					CSVector *list;
					CSString *key;
					
					if (argc > 3) {
						prefix = argv[3];
						if (!strlen(prefix))
							prefix = NULL;
					}
					
					if (argc == 5) 
						max = atol(argv[4]);
						
					list = prot->s3_list(argv[2], prefix, max);
					push_(list);
					while (key = (CSString*) list->take(0)) {
						printf("%s\n", key->getCString());
						key->release();
					}
					release_(list);
					
				} else
					printf("Bad command: l <bucket> [<object_prefix> [max_list_size]] \n");
				
				break;
			default:
				printf("Unknown command.\n");
				show_help_info(argv[0]);
		}
		
		release_(prot);
	}
	
	catch_(a);		
	self->logException();
	
	cont_(a);
		
	outer_()
	main_thread->release();
	cs_exit_memory();
	CSThread::shutDown();
	return 0;
}

#endif