File: ghttpPost.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (1643 lines) | stat: -rw-r--r-- 39,931 bytes parent folder | download | duplicates (2)
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
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
/*
GameSpy GHTTP SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

#include "ghttpPost.h"
#include "ghttpMain.h"
#include "ghttpConnection.h"
#include "ghttpCommon.h"

#include "../common/gsCrypt.h"
#include "../common/gsSSL.h"
#include "../common/gsXML.h"


// The border between parts in a file send.
///////////////////////////////////////////
#define GHI_MULTIPART_BOUNDARY          "Qr4G823s23d---<<><><<<>--7d118e0536"
#define GHI_MULTIPART_BOUNDARY_BASE     "--" GHI_MULTIPART_BOUNDARY
#define GHI_MULTIPART_BOUNDARY_FIRST    GHI_MULTIPART_BOUNDARY_BASE CRLF
#define GHI_MULTIPART_BOUNDARY_NORMAL   CRLF GHI_MULTIPART_BOUNDARY_BASE CRLF
#define GHI_MULTIPART_BOUNDARY_END      CRLF GHI_MULTIPART_BOUNDARY_BASE "--" CRLF

#define GHI_LEGAL_URLENCODED_CHARS      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_@-.*"
#define GHI_DIGITS                      "0123456789ABCDEF"

// DIME header settings
    // first byte is a combination of VERSION + first/last/chunked
#define GHI_DIME_VERSION         (0x1<<3) // 5th bit (from the left)
#define GHI_DIMEFLAG_FIRSTRECORD (1<<2)
#define GHI_DIMEFLAG_LASTRECORD  (1<<1)
#define GHI_DIMEFLAG_CHUNKED     (1<<0)
    // second byte is combination of TYPE_T and reserved (4bits = 0)
#define GHI_DIMETYPE_T_UNCHANGED (0x0 << 4)
#define GHI_DIMETYPE_T_MEDIA     (0x1 << 4)
#define GHI_DIMETYPE_T_URI       (0x2 << 4)
#define GHI_DIMETYPE_T_UNKNOWN   (0x3 << 4)
#define GHI_DIMETYPE_T_EMPTY     (0x4 << 4) // lengths must be set to 0

//#define GHI_DIME_SOAPID "gsi:soap"
#define GHI_DIME_SOAPID "cid:id0"
#define GHI_DIME_SOAPTYPE "http://schemas.xmlsoap.org/soap/envelope/"

typedef struct GSIDimeHeader
{
	gsi_u8 mVersionAndFlags;
	gsi_u8 mTypeT;
	gsi_u16 mOptionsLength;
	gsi_u16 mIdLength;
	gsi_u16 mTypeLength;
	gsi_u32 mDataLength;
	// gsi_u8 mOptions[mOptionsLength];
	// gsi_u8 mId[mIdLength];
	// gsi_u8 mType[mTypeLength];
	// gsi_u8 mData[mDataLength];
} GHIDimeHeader;

// POST TYPES.
//////////////
typedef enum
{
	GHIString,      // A regular string.
	GHIFileDisk,    // A file from disk.
	GHIFileMemory,  // A file from memory.
	GHIXmlData      // XML Soap. (long string)
} GHIPostDataType;

// POST OBJECT.
///////////////
typedef struct GHIPost
{
	DArray data;
	ghttpPostCallback callback;
	void * param;
	GHTTPBool hasFiles;
	GHTTPBool hasSoap;
	GHTTPBool useDIME;
	GHTTPBool autoFree;
} GHIPost;

// POST DATA.
/////////////
typedef struct GHIPostStringData
{
	char * string;
	int len;
	GHTTPBool invalidChars;
	int extendedChars;
} GHIPostStringData;

typedef struct GHIPostFileDiskData
{
	char * filename;
	char * reportFilename;
	char * contentType;
} GHIPostFileDiskData;

typedef struct GHIPostFileMemoryData
{
	const char * buffer;
	int len;
	char * reportFilename;
	char * contentType;
} GHIPostFileMemoryData;

typedef struct GHIPostXmlData
{
	GSXmlStreamWriter xml;
} GHIPostXmlData;

typedef struct GHIPostData
{
	GHIPostDataType type;
	char * name;
	union
	{
		GHIPostStringData string;
		GHIPostFileDiskData fileDisk;
		GHIPostFileMemoryData fileMemory;
		GHIPostXmlData xml;
	} data;
} GHIPostData;

// POST STATE.
//////////////
//typedef struct GHIPostStringState
//{
//} GHIPostStringState;

typedef struct GHIPostFileDiskState
{
	FILE * file;
	long len;
} GHIPostFileDiskState;

//typedef struct GHIPostFileMemoryState
//{
//} GHIPostFileMemoryState;


//typedef struct GHIPostSoapState
//{
//} GHIPostSoapState;

typedef struct GHIPostState
{
	GHIPostData * data;
	int pos;
	union
	{
		//GHIPostStringState string;
		GHIPostFileDiskState fileDisk;
		//GHIPostFileMemoryState fileMemory;
		//GHIPostSoapState soap;
	} state;
} GHIPostState;

// FUNCTIONS.
/////////////
static void ghiPostDataFree
(
	void * elem
)
{
	GHIPostData * data = (GHIPostData *)elem;

	// Free the name.
	/////////////////
	if (data->type != GHIXmlData)
		gsifree(data->name);

	// Free based on type.
	//////////////////////
	if(data->type == GHIString)
	{
		// Free the string string.
		/////////////////////////
		gsifree(data->data.string.string);
	}
	else if(data->type == GHIFileDisk)
	{
		// Free the strings.
		////////////////////
		gsifree(data->data.fileDisk.filename);
		gsifree(data->data.fileDisk.reportFilename);
		gsifree(data->data.fileDisk.contentType);
	}
	else if(data->type == GHIFileMemory)
	{
		// Free the strings.
		////////////////////
		gsifree(data->data.fileMemory.reportFilename);
		gsifree(data->data.fileMemory.contentType);
	}
	else if(data->type == GHIXmlData)
	{
		gsXmlFreeWriter(data->data.xml.xml);
	}
	else
	{
		// The type didn't match any known types.
		/////////////////////////////////////////
		assert(0);
	}
}

GHTTPPost ghiNewPost
(
	void
)
{
	GHIPost * post;

	// Allocate the post object.
	////////////////////////////
	post = (GHIPost *)gsimalloc(sizeof(GHIPost));
	if(!post)
		return NULL;

	// Initialize it.
	/////////////////
	memset(post, 0, sizeof(GHIPost));
	post->autoFree = GHTTPTrue;

	// Create the array of data objects.
	////////////////////////////////////
	post->data = ArrayNew(sizeof(GHIPostData), 0, ghiPostDataFree);
	if(!post->data)
	{
		gsifree(post);
		return NULL;
	}

	return (GHTTPPost)post;
}

void ghiPostSetAutoFree
(
	GHTTPPost _post,
	GHTTPBool autoFree
)
{
	GHIPost * post = (GHIPost *)_post;

	post->autoFree = autoFree;
}

GHTTPBool ghiIsPostAutoFree
(
	GHTTPPost _post
)
{
	GHIPost * post = (GHIPost *)_post;

	return post->autoFree;
}

void ghiFreePost
(
	GHTTPPost _post
)
{
	GHIPost * post = (GHIPost *)_post;

	// Free the array of data objects.
	//////////////////////////////////
	ArrayFree(post->data);

	// Free the post object.
	////////////////////////
	gsifree(post);
}

GHTTPBool ghiPostAddString
(
	GHTTPPost _post,
	const char * name,
	const char * string
)
{
	GHIPost * post = (GHIPost *)_post;
	GHIPostData data;
	int len;
	int rcode;

	// Copy the strings.
	////////////////////
	name = goastrdup(name);
	string = goastrdup(string);
	if(!name || !string)
	{
		gsifree((char *)name);
		gsifree((char *)string);
		return GHTTPFalse;
	}

	// Set the data.
	////////////////
	memset(&data, 0, sizeof(GHIPostData));
	data.type = GHIString;
	data.name = (char *)name;
	data.data.string.string = (char *)string;
	len = (int)strlen(string);
	data.data.string.len = len;
	data.data.string.invalidChars = GHTTPFalse;

	// Are there any invalid characters?
	////////////////////////////////////
	rcode = (int)strspn(string, GHI_LEGAL_URLENCODED_CHARS);
	if(rcode != len)
	{
		int i;
		int count = 0;

		data.data.string.invalidChars = GHTTPTrue;

		// Count the number, not including spaces.
		//////////////////////////////////////////
		for(i = 0 ; string[i] ; i++)
			if(!strchr(GHI_LEGAL_URLENCODED_CHARS, string[i]) && (string[i] != ' '))
				count++;

		data.data.string.extendedChars = count;
	}

	// Add it.
	//////////
	ArrayAppend(post->data, &data);

	return GHTTPTrue;
}

GHTTPBool ghiPostAddFileFromDisk
(
	GHTTPPost _post,
	const char * name,
	const char * filename,
	const char * reportFilename,
	const char * contentType
)
{
	GHIPost * post = (GHIPost *)_post;
	GHIPostData data;

	// Copy the strings.
	////////////////////
	name = goastrdup(name);
	filename = goastrdup(filename);
	reportFilename = goastrdup(reportFilename);
	contentType = goastrdup(contentType);
	if(!name || !filename || !reportFilename || !contentType)
	{
		gsifree((char *)name);
		gsifree((char *)filename);
		gsifree((char *)reportFilename);
		gsifree((char *)contentType);
		return GHTTPFalse;
	}

	// Set the data.
	////////////////
	memset(&data, 0, sizeof(GHIPostData));
	data.type = GHIFileDisk;
	data.name = (char *)name;
	data.data.fileDisk.filename = (char *)filename;
	data.data.fileDisk.reportFilename = (char *)reportFilename;
	data.data.fileDisk.contentType = (char *)contentType;

	// Add it.
	//////////
	ArrayAppend(post->data, &data);

	// We have files.
	/////////////////
	post->hasFiles = GHTTPTrue;

	// if we have both soap and a file we MUST use DIME
	if (post->hasSoap == GHTTPTrue)
		post->useDIME = GHTTPTrue;

	return GHTTPTrue;
}

GHTTPBool ghiPostAddFileFromMemory
(
	GHTTPPost _post,
	const char * name,
	const char * buffer,
	int bufferLen,
	const char * reportFilename,
	const char * contentType
)
{
	GHIPost * post = (GHIPost *)_post;
	GHIPostData data;

	// Copy the strings.
	////////////////////
	name = goastrdup(name);
	reportFilename = goastrdup(reportFilename);
	contentType = goastrdup(contentType);
	if(!name || !reportFilename || !contentType)
	{
		gsifree((char *)name);
		gsifree((char *)reportFilename);
		gsifree((char *)contentType);
		return GHTTPFalse;
	}

	// Set it.
	//////////
	memset(&data, 0, sizeof(GHIPostData));
	data.type = GHIFileMemory;
	data.name = (char *)name;
	data.data.fileMemory.buffer = (char *)buffer;
	data.data.fileMemory.len = bufferLen;
	data.data.fileMemory.reportFilename = (char *)reportFilename;
	data.data.fileMemory.contentType = (char *)contentType;

	// Add it.
	//////////
	ArrayAppend(post->data, &data);

	// We have a file.
	//////////////////
	post->hasFiles = GHTTPTrue;

	// if we have both soap and a file we MUST use DIME
	if (post->hasSoap == GHTTPTrue)
		post->useDIME = GHTTPTrue;

	return GHTTPTrue;
}

GHTTPBool ghiPostAddXml
(
	GHTTPPost _post,
	GSXmlStreamWriter xml
)
{
	GHIPostData data;
	//unsigned int rcode = 0;

	GHIPost * post = (GHIPost *)_post;

	data.type = GHIXmlData;
	data.data.xml.xml = xml;
	ArrayAppend(post->data, &data);
	post->hasSoap = GHTTPTrue;

	// if we have both soap and a file we MUST use DIME
	if (post->hasFiles == GHTTPTrue)
		post->useDIME = GHTTPTrue;

	return GHTTPTrue;
}

void ghiPostSetCallback
(
	GHTTPPost _post,
	ghttpPostCallback callback,
	void * param
)
{
	GHIPost * post = (GHIPost *)_post;

	// Set the callback and param.
	//////////////////////////////
	post->callback = callback;
	post->param = param;
}

const char * ghiPostGetContentType
(
	struct GHIConnection * connection
)
{
	GHIPost * post = connection->post;

	assert(post);
	if(!post)
		return "";

	// Report content-type based on if we are sending files or not.
	///////////////////////////////////////////////////////////////
	if(post->useDIME)
		return ("application/dime");
	else if (post->hasFiles)
	{
		GS_ASSERT(!post->hasSoap);
		return ("multipart/form-data; boundary=" GHI_MULTIPART_BOUNDARY);
	}
	else if (post->hasSoap)
	{
		GS_ASSERT(!post->hasFiles);
		return ("text/xml");
	}
	else
	{
		GS_ASSERT(!post->hasSoap);
		GS_ASSERT(!post->hasFiles);
		return "application/x-www-form-urlencoded";
	}
}

static int ghiPostGetNoFilesContentLength
(
	struct GHIConnection * connection
)
{
	GHIPost * post = connection->post;
	GHIPostData * data;
	int i;
	int num;
	int total = 0;
	int foundSoapAlready = 0;

	num = ArrayLength(post->data);
	if(!num)
		return 0;

	for(i = 0 ; i < num ; i++)
	{
		data = (GHIPostData *)ArrayNth(post->data, i);

		GS_ASSERT(data->type == GHIString || data->type == GHIXmlData);

		if (data->type == GHIString)
		{
			total += (int)strlen(data->name);
			total += data->data.string.len;
			total += (data->data.string.extendedChars * 2);
			total++;  // '='
		}
		else if (data->type == GHIXmlData)
		{
			GS_ASSERT(foundSoapAlready == 0); // only support one soap object per request
			foundSoapAlready = 1;
			total += gsXmlWriterGetDataLength(data->data.xml.xml);
		}
	}

	total += (num - 1);  // '&'

	GSI_UNUSED(foundSoapAlready);
	return total;
}

static int ghiPostGetHasFilesContentLength
(
	struct GHIConnection * connection
)
{
	GHIPost * post = connection->post;
	GHIPostData * data;
	int i;
	int num;
	int total = 0;
	int foundSoapAlready = 0;
	static int boundaryLen;
	static int stringBaseLen;
	static int fileBaseLen;
	static int endLen;
	static int xmlBaseLen;
	
	if(!boundaryLen)
	{
		if (post->useDIME)
		{
			GS_ASSERT(post->hasSoap);
			GS_ASSERT(post->hasFiles);
			boundaryLen = sizeof(GHIDimeHeader);
			stringBaseLen = boundaryLen;
			fileBaseLen = boundaryLen;
			xmlBaseLen = boundaryLen;
			endLen = 0;
		}
		else
		{
			GS_ASSERT(!post->hasSoap);
			boundaryLen = (int)strlen(GHI_MULTIPART_BOUNDARY_BASE);
			stringBaseLen = (boundaryLen + 47);  // + name + string
			fileBaseLen = (boundaryLen + 76);  // + name + filename + content-type + file
			xmlBaseLen = 0; // no boundaries for text/xml type soap
			endLen = (boundaryLen + 4);
		}
	}

	num = ArrayLength(post->data);

	for(i = 0 ; i < num ; i++)
	{
		data = (GHIPostData *)ArrayNth(post->data, i);

		if(data->type == GHIString)
		{
			total += stringBaseLen;
			total += (int)strlen(data->name);
			total += data->data.string.len;
		}
		else if(data->type == GHIFileDisk)
		{
			GHIPostState * state;

			total += fileBaseLen;
			total += (int)strlen(data->name);
			total += (int)strlen(data->data.fileDisk.contentType);
			state = (GHIPostState *)ArrayNth(connection->postingState.states, i);
			assert(state);
			total += (int)state->state.fileDisk.len;

			if (!post->useDIME)
				total += (int)strlen(data->data.fileDisk.reportFilename);

			if (post->useDIME)
			{
				// have to include padding bytes!
				int padBytes = 0;

				padBytes = 4-(int)strlen(data->name)%4;
				if (padBytes != 4)
					total += padBytes;
				padBytes = 4-(int)strlen(data->data.fileDisk.contentType)%4;
				if (padBytes != 4)
					total += padBytes;
				padBytes = 4-(int)state->state.fileDisk.len%4;
				if (padBytes != 4)
					total += padBytes;
			}
		}
		else if(data->type == GHIFileMemory)
		{
			total += fileBaseLen;
			total += (int)strlen(data->name);
			total += (int)strlen(data->data.fileMemory.contentType);
			total += data->data.fileMemory.len;

			if (!post->useDIME)
				total += (int)strlen(data->data.fileMemory.reportFilename);

			if (post->useDIME)
			{
				// have to include padding bytes!
				int padBytes = 0;

				padBytes = 4-(int)strlen(data->name)%4;
				if (padBytes != 4)
					total += padBytes;
				padBytes = 4-(int)strlen(data->data.fileMemory.contentType)%4;
				if (padBytes != 4)
					total += padBytes;
				padBytes = 4-(int)data->data.fileMemory.len%4;
				if (padBytes != 4)
					total += padBytes;
			}
		}
		else if(data->type == GHIXmlData)
		{
			int padBytes = 0;

			GS_ASSERT(foundSoapAlready == 0); // only one soap envelope per request
			GS_ASSERT(post->useDIME); // soap+file = use DIME
			foundSoapAlready = 1;
			total += xmlBaseLen;
			total += gsXmlWriterGetDataLength(data->data.xml.xml);

			// have to include padding bytes!
			padBytes = 4-(int)gsXmlWriterGetDataLength(data->data.xml.xml)%4;
			if (padBytes != 4)
				total += padBytes;
			total += (int)strlen(GHI_DIME_SOAPID);
			padBytes = 4-(int)strlen(GHI_DIME_SOAPID)%4;
			if (padBytes != 4)
				total += padBytes;
			total += (int)strlen(GHI_DIME_SOAPTYPE);
			padBytes = 4-(int)strlen(GHI_DIME_SOAPTYPE)%4;
			if (padBytes != 4)
				total += padBytes;
		}
		else
		{
			assert(0);
			return 0;
		}
	}

	// Add the end.
	///////////////
	total += endLen;

	GSI_UNUSED(foundSoapAlready);
	return total;
}

static int ghiPostGetContentLength
(
	struct GHIConnection * connection
)
{
	GHIPost * post = connection->post;

	assert(post);
	if(!post)
		return 0;

	if(post->hasFiles)
		return ghiPostGetHasFilesContentLength(connection);

	return ghiPostGetNoFilesContentLength(connection);
}

static GHTTPBool ghiPostStateInit
(
	GHIPostState * state
)
{
	GHIPostDataType type;

	// The type.
	////////////
	type = state->data->type;

	// Set the position to sending header.
	//////////////////////////////////////
	state->pos = -1;

	// Init based on type.
	//////////////////////
	if(type == GHIString)
	{
	}
	else if(type == GHIFileDisk)
	{
		// Open the file.
		/////////////////
#ifndef NOFILE
		state->state.fileDisk.file = fopen(state->data->data.fileDisk.filename, "rb");
#endif
		if(!state->state.fileDisk.file)
			return GHTTPFalse;

		// Get the file length.
		///////////////////////
		if(fseek(state->state.fileDisk.file, 0, SEEK_END) != 0)
			return GHTTPFalse;
		state->state.fileDisk.len = ftell(state->state.fileDisk.file);
		if(state->state.fileDisk.len == EOF)
			return GHTTPFalse;
		rewind(state->state.fileDisk.file);
	}
	else if(type == GHIFileMemory)
	{
	}
	else if(type == GHIXmlData)
	{
	}
	else
	{
		// The type didn't match any known types.
		/////////////////////////////////////////
		assert(0);

		return GHTTPFalse;
	}

	return GHTTPTrue;
}

static void ghiPostStateCleanup
(
	GHIPostState * state
)
{
	GHIPostDataType type;

	// The type.
	////////////
	type = state->data->type;

	// Init based on type.
	//////////////////////
	if(type == GHIString)
	{
	}
	else if(type == GHIFileDisk)
	{
		if(state->state.fileDisk.file)
			fclose(state->state.fileDisk.file);
		state->state.fileDisk.file = NULL;
	}
	else if(type == GHIFileMemory)
	{
	}
	else if(type == GHIXmlData)
	{
	}
	else
	{
		// The type didn't match any known types.
		/////////////////////////////////////////
		assert(0);
	}
}

GHTTPBool ghiPostInitState
(
	struct GHIConnection * connection
)
{
	int i;
	int len;
	GHIPostData * data;
	GHIPostState state;
	GHIPostState * pState;

	assert(connection->post);
	if(!connection->post)
		return GHTTPFalse;

	// Create an array for the states.
	//////////////////////////////////
	connection->postingState.index = 0;
	connection->postingState.bytesPosted = 0;
	connection->postingState.totalBytes = 0;
	connection->postingState.completed = GHTTPFalse;
	connection->postingState.callback = connection->post->callback;
	connection->postingState.param = connection->post->param;
	len = ArrayLength(connection->post->data);
	connection->postingState.states = ArrayNew(sizeof(GHIPostState), len, NULL);
	if(!connection->postingState.states)
		return GHTTPFalse;

	// Setup all the states.
	////////////////////////
	for(i = 0 ; i < len ; i++)
	{
		// Get the data object for this index.
		//////////////////////////////////////
		data = (GHIPostData *)ArrayNth(connection->post->data, i);

		// Initialize the state's members.
		//////////////////////////////////
		memset(&state, 0, sizeof(GHIPostState));
		state.data = data;

		// Call the init function.
		//////////////////////////
		if(!ghiPostStateInit(&state))
		{
			// We need to cleanup everything we just initialized.
			/////////////////////////////////////////////////////
			for(i-- ; i >= 0 ; i--)
			{
				pState = (GHIPostState *)ArrayNth(connection->postingState.states, i);
				ghiPostStateCleanup(pState);
			}

			// Free the array.
			//////////////////
			ArrayFree(connection->postingState.states);
			connection->postingState.states = NULL;

			return GHTTPFalse;
		}

		// Add it to the array.
		///////////////////////
		ArrayAppend(connection->postingState.states, &state);
	}

	// If this asserts, there aren't the same number of state objects as data objects.
	// There should be a 1-to-1 mapping between data and states.
	//////////////////////////////////////////////////////////////////////////////////
	assert(ArrayLength(connection->post->data) == ArrayLength(connection->postingState.states));

	// Get the total number of bytes.
	/////////////////////////////////
	connection->postingState.totalBytes = ghiPostGetContentLength(connection);

	// Wait for continue before posting.
	//	  -- Enabled for Soap messages only
	//    -- Disabled for all other content because many web servers do not support it
	//////////////////////////////////////////////////////
	if (connection->post->hasSoap == GHTTPTrue)
		connection->postingState.waitPostContinue = GHTTPTrue;
	else
		connection->postingState.waitPostContinue = GHTTPFalse;

	return GHTTPTrue;
}

void ghiPostCleanupState
(
	struct GHIConnection * connection
)
{
	int i;
	int len;
	GHIPostState * state;

	// Loop through and call the cleanup function.
	//////////////////////////////////////////////
	if(connection->postingState.states)
	{
		len = ArrayLength(connection->postingState.states);
		for(i = 0 ; i < len ; i++)
		{
			state = (GHIPostState *)ArrayNth(connection->postingState.states, i);
			ghiPostStateCleanup(state);
		}

		// Free the array.
		//////////////////
		ArrayFree(connection->postingState.states);
		connection->postingState.states = NULL;
	}

	// Free the post.
	/////////////////
	if(connection->post && connection->post->autoFree)
	{
		ghiFreePost(connection->post);
		connection->post = NULL;
	}
}

static GHIPostingResult ghiPostStringStateDoPosting
(
	GHIPostState * state,
	GHIConnection * connection
)
{
	//GHTTPBool result;
	
	assert(state->pos >= 0);

	// Is this an empty string?
	///////////////////////////
	if(state->data->data.string.len == 0)
		return GHIPostingDone;

	assert(state->pos < state->data->data.string.len);

	// If we're doing a simple post, we need to fix invalid characters.
	//   - only applies to simple posts
	///////////////////////////////////////////////////////////////////
	if(!connection->post->hasFiles && !connection->post->hasSoap && state->data->data.string.invalidChars)
	{
		int i;
		int c;
		const char * string = state->data->data.string.string;
		char hex[4] = "%00";
		GHIBuffer *writeBuffer;

		// When encrypting, we need space for two copies
		if (connection->encryptor.mEngine == GHTTPEncryptionEngine_None)
			writeBuffer = &connection->sendBuffer; 
		else
			writeBuffer = &connection->encodeBuffer;

		// This could probably be done a lot better.
		////////////////////////////////////////////
		for(i = 0 ; (c = string[i]) != 0 ; i++)
		{
			if(strchr(GHI_LEGAL_URLENCODED_CHARS, c))
			{
				// Legal.
				/////////
				//result = ghiAppendCharToBuffer(writeBuffer, c);
				ghiAppendCharToBuffer(writeBuffer, c);
			}
			else if(c == ' ')
			{
				// Space.
				/////////
				//result = ghiAppendCharToBuffer(writeBuffer, '+');
				ghiAppendCharToBuffer(writeBuffer, '+');
			}
			else
			{
				// To hex.
				//////////
				assert((c / 16) < 16);
				hex[1] = GHI_DIGITS[c / 16];
				hex[2] = GHI_DIGITS[c % 16];
				//result = ghiAppendDataToBuffer(writeBuffer, hex, 3);
				ghiAppendDataToBuffer(writeBuffer, hex, 3);
			}
		}
	}
	else
	{
		// copy the string as-is, encrypting if necessary
		GHITrySendResult result = ghiTrySendThenBuffer(connection, 
			state->data->data.string.string, state->data->data.string.len);
		if (result == GHITrySendError)
			return GHIPostingError;
		else
			return GHIPostingDone;
	}

	// Send the URL fixed string
	////////////////////////////
	if (connection->encryptor.mEngine == GHTTPEncryptionEngine_None)
	{
		// The URL fixed string was written to the send buffer, so send it!
		if (!ghiSendBufferedData(connection))
			return GHIPostingError;

		if (connection->sendBuffer.pos == connection->sendBuffer.len)
			ghiResetBuffer(&connection->sendBuffer);
		return GHIPostingDone;
	}
	else
	{
		// SSL data is in the "to be encrypted" buffer, so wait until
		// we have the full MIME form before encrypting (for efficiency)
		return GHIPostingDone;
	}
}

static GHIPostingResult ghiPostXmlStateDoPosting
(
	GHIPostState * state,
	GHIConnection * connection
)
{
	GSXmlStreamWriter xml = state->data->data.xml.xml;
	char pad[3] = { '\0', '\0', '\0' };
	int padlen = 0;
	
	// make sure state is valid
	GS_ASSERT(state->pos >= 0);
	GS_ASSERT(connection->post != NULL);

	// when using a DIME, we have to pad to multiple of 4
	if (connection->post->useDIME)
	{
		padlen = 4-(gsXmlWriterGetDataLength(xml)%4);
		if (padlen == 4)
			padlen = 0;
	}

	if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None &&
		connection->encryptor.mEncryptOnBuffer == GHTTPTrue)
	{
		// Copy to encode buffer before encrypting
		GS_ASSERT(connection->encodeBuffer.len >= 0); // there must be a header for this soap data!
		if (!ghiAppendDataToBuffer(&connection->encodeBuffer, gsXmlWriterGetData(xml), gsXmlWriterGetDataLength(xml)) ||
			!ghiAppendDataToBuffer(&connection->encodeBuffer, pad, padlen) ||
			!ghiEncryptDataToBuffer(&connection->sendBuffer, connection->encodeBuffer.data, connection->encodeBuffer.len)
			)
		{
			return GHIPostingError;
		}

		// Clear out our temporary buffer
		ghiResetBuffer(&connection->encodeBuffer);

		// Send what we can now
		if (GHTTPFalse == ghiSendBufferedData(connection))
			return GHIPostingError;

		// is there more to send?
		if (connection->sendBuffer.pos == connection->sendBuffer.len)
			ghiResetBuffer(&connection->sendBuffer);

		return GHIPostingDone;
	}
	else
	{
		GHITrySendResult result;

		// plain text - send immediately
		result = ghiTrySendThenBuffer(connection, gsXmlWriterGetData(xml), gsXmlWriterGetDataLength(xml));
		if (result == GHITrySendError)
			return GHIPostingError;
		result = ghiTrySendThenBuffer(connection, pad, padlen);
		if (result == GHITrySendError)
			return GHIPostingError;
		return GHIPostingDone;
	}
}

static GHIPostingResult ghiPostFileDiskStateDoPosting
(
	GHIPostState * state,
	GHIConnection * connection
)
{
	char buffer[4096];
	int len;
	GHITrySendResult result;

	assert(state->pos >= 0);
	assert(state->pos < state->state.fileDisk.len);
	assert(state->pos == (int)ftell(state->state.fileDisk.file));

	// Loop while data is being sent.
	/////////////////////////////////
	do
	{
		// Read some data from the file.
		////////////////////////////////
		len = (int)fread(buffer, 1, sizeof(buffer), state->state.fileDisk.file);
		if(len <= 0)
		{
			connection->completed = GHTTPTrue;
			connection->result = GHTTPFileReadFailed;
			return GHIPostingError;
		}

		// Update our position.
		///////////////////////
		state->pos += len;

		// Check for too much.
		//////////////////////
		if(state->pos > state->state.fileDisk.len)
		{
			connection->completed = GHTTPTrue;
			connection->result = GHTTPFileReadFailed;
			return GHIPostingError;
		}

		// Send.
		////////
		result = ghiTrySendThenBuffer(connection, buffer, len);
		if(result == GHITrySendError)
			return GHIPostingError;

		// Check if we've handled everything.
		/////////////////////////////////////
		if(state->pos == state->state.fileDisk.len)
		{
			// when using a DIME, we have to pad to multiple of 4
			if (connection->post->useDIME)
			{
				char pad[3] = { '\0', '\0', '\0' };
				int padlen = 4-state->state.fileDisk.len%4;
				if (padlen != 4 && padlen > 0)
				{
					if (GHITrySendError == ghiTrySendThenBuffer(connection, pad, padlen))
						return GHIPostingError;
				}
			}
			return GHIPostingDone;
		}
	}
	while(result == GHITrySendSent);

	return GHIPostingPosting;
}

static GHIPostingResult ghiPostFileMemoryStateDoPosting
(
	GHIPostState * state,
	GHIConnection * connection
)
{
	int rcode;
	int len;

	assert(state->pos >= 0);

	// Is this an empty file?
	/////////////////////////
	if(state->data->data.fileMemory.len == 0)
		return GHIPostingDone;

	assert(state->pos < state->data->data.fileMemory.len);

	// Send what we can.
	////////////////////
	if (connection->encryptor.mEngine == GHTTPEncryptionEngine_None)
	{
		// Plain text: Send directly from memory
		do
		{
			len = (state->data->data.fileMemory.len - state->pos);
			rcode = ghiDoSend(connection, state->data->data.fileMemory.buffer + state->pos, len);
			if(gsiSocketIsError(rcode))
				return GHIPostingError;

			// Update the pos.
			//////////////////
			state->pos += rcode;

			// Did we send it all?
			//////////////////////
			if(state->data->data.fileMemory.len == state->pos)
			{
				// when using a DIME, we have to pad to multiple of 4
				if (connection->post->useDIME)
				{
					char pad[3] = { '\0', '\0', '\0' };
					int padlen = 4-state->data->data.fileMemory.len%4;
					if (padlen != 4 && padlen > 0)
					{
						if (GHITrySendError == ghiTrySendThenBuffer(connection, pad, padlen))
							return GHIPostingError;
					}
				}
				return GHIPostingDone;
			}
		}
		while(rcode);
		return GHIPostingPosting; // (rcode == 0) ?
	}
	else
	{
		// Encrypted: can't avoid the copy due to encryption+MAC
		GHITrySendResult result;
		do 
		{
			len = (state->data->data.fileMemory.len - state->pos);
			len = min(len, GS_SSL_MAX_CONTENTLENGTH);
			result = ghiTrySendThenBuffer(connection, state->data->data.fileMemory.buffer + state->pos, len);
			if (result == GHITrySendError)
				return GHIPostingError;
			
			// Update the pos.
			//////////////////
			state->pos += len;

			// Did we send it all?
			//////////////////////
			if(state->data->data.fileMemory.len == state->pos)
			{
				// when using a DIME, we have to pad to multiple of 4
				if (connection->post->useDIME)
				{
					char pad[3] = { '\0', '\0', '\0' };
					int padlen = 4-state->data->data.fileMemory.len%4;
					if (padlen != 4 && padlen > 0)
					{
						if (GHITrySendError == ghiTrySendThenBuffer(connection, pad, padlen))
							return GHIPostingError;
					}
				}
				return GHIPostingDone;
			}
		} 
		while(result == GHITrySendSent);
		return GHIPostingPosting;
	}
}

static GHIPostingResult ghiPostStateDoPosting
(
	GHIPostState * state,
	GHIConnection * connection,
	GHTTPBool first,
	GHTTPBool last
)
{
	int len = 0;
	GHITrySendResult result;

	// Check for sending the header.
	////////////////////////////////
	if(state->pos == -1)
	{
		char buffer[2048];
		
		// Bump up the position so we only send the header once.
		////////////////////////////////////////////////////////
		state->pos = 0;

		// Check if this is a simple post.
		//////////////////////////////////
		if(!connection->post->hasFiles && !connection->post->hasSoap)
		{
			// Simple post only supports strings.
			/////////////////////////////////////
			assert(state->data->type == GHIString);

			// Format the header.
			/////////////////////
			if(first)
				sprintf(buffer, "%s=", state->data->name);
			else
				sprintf(buffer, "&%s=", state->data->name);
		}
		else
		{
			// Format the header based on string or file.
			/////////////////////////////////////////////
			if(state->data->type == GHIString)
			{
				sprintf(buffer,
					"%s"
					"Content-Disposition: form-data; "
					"name=\"%s\"" CRLF
					CRLF,
					first?GHI_MULTIPART_BOUNDARY_FIRST:GHI_MULTIPART_BOUNDARY_NORMAL,
					state->data->name);
			}
			else if(state->data->type == GHIXmlData)
			{
				if (connection->post->useDIME)
				{
					// use DIME header
					//    Copy from a temp struct to circumvent alignment issues
					int writePos = 0;
					int padBytes = 0;
					GHIDimeHeader header;

					header.mVersionAndFlags = GHI_DIME_VERSION;
					if (first)
						header.mVersionAndFlags |= GHI_DIMEFLAG_FIRSTRECORD;
					if (last)
						header.mVersionAndFlags |= GHI_DIMEFLAG_LASTRECORD;
					header.mTypeT = GHI_DIMETYPE_T_URI;
					header.mOptionsLength = 0;
					header.mIdLength = htons((short)strlen(GHI_DIME_SOAPID));
					header.mTypeLength = htons((short)strlen(GHI_DIME_SOAPTYPE));
					header.mDataLength = htonl(gsXmlWriterGetDataLength(state->data->data.xml.xml));

					memcpy(&buffer[writePos], &header, sizeof(GHIDimeHeader));
					writePos += sizeof(GHIDimeHeader);

					// id
					strcpy(&buffer[writePos], GHI_DIME_SOAPID);
					writePos += strlen(GHI_DIME_SOAPID);
					padBytes = (int)(4-strlen(GHI_DIME_SOAPID)%4);
					if (padBytes != 4)
					{
						while(padBytes-- > 0)
							buffer[writePos++] = '\0';
					}

					// type
					strcpy(&buffer[writePos], GHI_DIME_SOAPTYPE);
					writePos += strlen(GHI_DIME_SOAPTYPE);
					padBytes = (int)(4-strlen(GHI_DIME_SOAPTYPE)%4);
					if (padBytes != 4)
					{
						while(padBytes-- > 0)
							buffer[writePos++] = '\0';
					}

					len = writePos;
				}
				else
					buffer[0] = '\0';
			}
			else if((state->data->type == GHIFileDisk) || (state->data->type == GHIFileMemory))
			{
				const char * filename;
				const char * contentType;
				int filelen;

				if(state->data->type == GHIFileDisk)
				{
					filelen = state->state.fileDisk.len;
					filename = state->data->data.fileDisk.reportFilename;
					contentType = state->data->data.fileDisk.contentType;
				}
				else
				{
					filelen = state->data->data.fileMemory.len;
					filename = state->data->data.fileMemory.reportFilename;
					contentType = state->data->data.fileMemory.contentType;
				}

				if (connection->post->useDIME)
				{
					// use DIME header
					//    Copy from a temp struct to circumvent alignment issues
					int writePos = 0;
					int padBytes = 0;
					GHIDimeHeader header;

					header.mVersionAndFlags = GHI_DIME_VERSION;
					if (first)
						header.mVersionAndFlags |= GHI_DIMEFLAG_FIRSTRECORD;
					if (last)
						header.mVersionAndFlags |= GHI_DIMEFLAG_LASTRECORD;
					header.mTypeT = GHI_DIMETYPE_T_MEDIA;
					header.mOptionsLength = 0;
					header.mIdLength = htons((short)strlen(state->data->name));
					header.mTypeLength = htons((short)strlen(contentType));
					header.mDataLength = htonl(filelen);

					memcpy(&buffer[writePos], &header, sizeof(GHIDimeHeader));
					writePos += sizeof(GHIDimeHeader);

					// id
					strcpy(&buffer[writePos], state->data->name);
					writePos += strlen(state->data->name);
					padBytes = (int)(4-strlen(state->data->name)%4);
					if (padBytes != 4)
					{
						while(padBytes-- > 0)
							buffer[writePos++] = '\0';
					}

					// type
					strcpy(&buffer[writePos], contentType);
					writePos += strlen(contentType);
					padBytes = (int)(4-strlen(contentType)%4);
					if (padBytes != 4)
					{
						while(padBytes-- > 0)
							buffer[writePos++] = '\0';
					}

					len = writePos;
				}
				else
				{
					// use MIME header
					sprintf(buffer,
						"%s"
						"Content-Disposition: form-data; "
						"name=\"%s\"; "
						"filename=\"%s\"" CRLF
						"Content-Type: %s" CRLF CRLF,
						first?GHI_MULTIPART_BOUNDARY_FIRST:GHI_MULTIPART_BOUNDARY_NORMAL,
						state->data->name,
						filename,
						contentType);
				}
			}
			else
			{
				assert(0);
			}
		}

		// SSL: encrypt and send
		if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None &&
			connection->encryptor.mEncryptOnBuffer == GHTTPTrue)
		{
			if (len == 0)
				len = (int)strlen(buffer);
			if (GHTTPFalse == ghiEncryptDataToBuffer(&connection->sendBuffer, buffer, len))
				return GHIPostingError;
			if (GHTTPFalse == ghiSendBufferedData(connection))
				return GHIPostingError;

			// any data remaining?
			if (connection->sendBuffer.pos < connection->sendBuffer.len)
				return GHIPostingPosting;

			// We sent everything, reset the send buffer to conserve space
			ghiResetBuffer(&connection->sendBuffer);
		}
		// If sending plain text, send right away
		else
		{
			// Try sending. (the one-time header)
			/////////////////////////////////////
			if (len == 0)
				len = (int)strlen(buffer);
			result = ghiTrySendThenBuffer(connection, buffer, len);
			if(result == GHITrySendError)
				return GHIPostingError;

			// If it was buffered, don't try anymore.
			/////////////////////////////////////////
			if(result == GHITrySendBuffered)
				return GHIPostingPosting;

			// We sent everything, reset the send buffer to conserve space
			ghiResetBuffer(&connection->sendBuffer);
		}
	}

	// Post based on type.
	//////////////////////
	if(state->data->type == GHIString)
		return ghiPostStringStateDoPosting(state, connection);

	if(state->data->type == GHIXmlData)
		return ghiPostXmlStateDoPosting(state, connection);

	if(state->data->type == GHIFileDisk)
		return ghiPostFileDiskStateDoPosting(state, connection);

	assert(state->data->type == GHIFileMemory);
	return ghiPostFileMemoryStateDoPosting(state, connection);
}

GHIPostingResult ghiPostDoPosting
(
	struct GHIConnection * connection
)
{
	GHIPostingResult postingResult;
	GHITrySendResult trySendResult;
	GHIPostingState * postingState;
	GHIPostState * postState;
	int len;

	assert(connection);
	assert(connection->post);
	assert(connection->postingState.states);
	assert(ArrayLength(connection->post->data) == ArrayLength(connection->postingState.states));
	assert(connection->postingState.index >= 0);
	assert(connection->postingState.index <= ArrayLength(connection->postingState.states));

	// Cache some stuff.
	////////////////////
	postingState = &connection->postingState;
	len = ArrayLength(postingState->states);

	// Check for buffered data.
	///////////////////////////
	if(connection->sendBuffer.pos < connection->sendBuffer.len)
	{
		// Send the buffered data.
		//////////////////////////
		if(!ghiSendBufferedData(connection))
			return GHIPostingError;

		// Check if we couldn't send it all.
		////////////////////////////////////
		if(connection->sendBuffer.pos < connection->sendBuffer.len)
			return GHIPostingPosting;

		// We sent it all, so reset the buffer.
		///////////////////////////////////////
		ghiResetBuffer(&connection->sendBuffer);

		// If uploading a DIME attachment, wait for HTTP continue.
		//////////////////////////////////////////////////////////
		if (connection->postingState.waitPostContinue)
			return GHIPostingWaitForContinue;

		// Was that all that's left?
		////////////////////////////
		if(connection->postingState.index == len)
			return GHIPostingDone;
	}

	// When posting soap and DIME attachments, we should terminate the
	// header and wait for a response.  This will either be a continue or
	// a server error.
	if (connection->postingState.waitPostContinue)
	{
		if (connection->post->hasFiles || connection->post->hasSoap)
		{
			// terminate the header and wait for a response
		  	GS_ASSERT(connection->encodeBuffer.len == 0);
			trySendResult = ghiTrySendThenBuffer(connection, CRLF, (int)strlen(CRLF));
			if(trySendResult == GHITrySendError)
				return GHIPostingError;
			else if (trySendResult == GHITrySendBuffered)
				return GHIPostingPosting;
			else
			{
				if (connection->postingState.waitPostContinue == GHTTPTrue)
					return GHIPostingWaitForContinue;
				//else
				//	fall through
			}
		}
		else
		{
			// simple posts don't have to wait
			connection->postingState.waitPostContinue = GHTTPFalse;
			// fall through
		}
	}

	// Loop while there's data to upload.
	/////////////////////////////////////
	while(postingState->index < len)
	{
		// Get the current data state.
		//////////////////////////////
		postState = (GHIPostState *)ArrayNth(postingState->states, postingState->index);
		assert(postState);

		// Upload the current data.
		///////////////////////////
		postingResult = ghiPostStateDoPosting(postState, connection, 
			(postingState->index == 0)?GHTTPTrue:GHTTPFalse,
			(postingState->index == (ArrayLength(postingState->states)-1))?GHTTPTrue:GHTTPFalse);

		// Check for error.
		///////////////////
		if(postingResult == GHIPostingError)
		{
			// Make sure we already set the error stuff.
			////////////////////////////////////////////
			assert(connection->completed && connection->result);

			return GHIPostingError;
		}

		// Check for still posting.
		///////////////////////////
		if(postingResult == GHIPostingPosting)
			return GHIPostingPosting;

		// One more done.
		/////////////////
		postingState->index++;
	}

	// Encrypt and send anything left in the encode buffer
	//   -- for example, when posting string data we don't encrypt until we have the entire string (for efficiency only)
	if (connection->encryptor.mEngine != GHTTPEncryptionEngine_None)
	{
		if (connection->encodeBuffer.len > 0)
		{
			GS_ASSERT(connection->encodeBuffer.pos == 0); // if you hit this, it means you forgot the clear the buffer
			if (GHTTPFalse == ghiEncryptDataToBuffer(&connection->sendBuffer, 
						connection->encodeBuffer.data, connection->encodeBuffer.len))
			{
				return GHIPostingError;
			}
			ghiResetBuffer(&connection->encodeBuffer);
		}
	}

	// Send or buffer the end marker.
	/////////////////////////////////
	if(connection->post->hasFiles && !connection->post->useDIME)
	{
		GS_ASSERT(!connection->post->hasSoap);

		// send MIME boundary end
		trySendResult = ghiTrySendThenBuffer(connection, GHI_MULTIPART_BOUNDARY_END, (int)strlen(GHI_MULTIPART_BOUNDARY_END));
		if(trySendResult == GHITrySendError)
			return GHIPostingError;
	}

	// We're not done if there's stuff in the buffer.
	/////////////////////////////////////////////////
	if(connection->sendBuffer.pos < connection->sendBuffer.len)
		return GHIPostingPosting;

	return GHIPostingDone;
}