summaryrefslogtreecommitdiff
path: root/FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/https/src/iot_https_client.c
blob: eb468674ca0863a213f1d1fafd61758c5168e7ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
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
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
/*
 * Amazon FreeRTOS HTTPS Client V1.1.0
 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://aws.amazon.com/freertos
 * http://www.FreeRTOS.org
 */

/**
 * @file iot_https_client.c
 * @brief Implementation of the user-facing functions of the Amazon FreeRTOS HTTPS Client library.
 */

/* The config header is always included first. */
#include "iot_config.h"

/* HTTPS Client library private includes. */
#include "private/iot_https_internal.h"

/*-----------------------------------------------------------*/

/**
 * @brief Partial HTTPS request first line.
 *
 * This is used for the calculation of the requestUserBufferMinimumSize.
 * The minimum path is "/" because we cannot know how long the application requested path is is going to be.
 * CONNECT is the longest string length HTTP method according to RFC 2616.
 */
#define HTTPS_PARTIAL_REQUEST_LINE                        HTTPS_CONNECT_METHOD " " HTTPS_EMPTY_PATH " " HTTPS_PROTOCOL_VERSION

/**
 * @brief The User-Agent header line string.
 *
 * This is of the form:
 * "User-Agent: <configured-user-agent>\r\n"
 * This is used for the calculation of the requestUserBufferMinimumSize.
 */
#define HTTPS_USER_AGENT_HEADER_LINE                      HTTPS_USER_AGENT_HEADER HTTPS_HEADER_FIELD_SEPARATOR IOT_HTTPS_USER_AGENT HTTPS_END_OF_HEADER_LINES_INDICATOR

/**
 * @brief The Host header line with the field only and not the value.
 *
 * This is of the form:
 * "Host: \r\n"
 * This is used for the calculation of the requestUserBufferMinimumSize. The Host value is not specified because we
 * cannot anticipate what server the client is making requests to.
 */
#define HTTPS_PARTIAL_HOST_HEADER_LINE                    HTTPS_HOST_HEADER HTTPS_HEADER_FIELD_SEPARATOR HTTPS_END_OF_HEADER_LINES_INDICATOR

/**
 * String constants for the Connection header and possible values.
 *
 * This is used for writing headers automatically during the sending of the HTTP request.
 * "Connection: keep-alive\r\n" is written automatically for a persistent connection.
 * "Connection: close\r\n" is written automatically for a non-persistent connection.
 */
#define HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE           HTTPS_CONNECTION_HEADER HTTPS_HEADER_FIELD_SEPARATOR HTTPS_CONNECTION_KEEP_ALIVE_HEADER_VALUE HTTPS_END_OF_HEADER_LINES_INDICATOR /**< @brief String literal for "Connection: keep-alive\r\n". */
#define HTTPS_CONNECTION_CLOSE_HEADER_LINE                HTTPS_CONNECTION_HEADER HTTPS_HEADER_FIELD_SEPARATOR HTTPS_CONNECTION_CLOSE_HEADER_VALUE HTTPS_END_OF_HEADER_LINES_INDICATOR      /**< @brief String literal for "Connection: close\r\n". */

/**
 * @brief The length of the "Connection: keep-alive\r\n" header.
 *
 * This is used for sizing a local buffer for the final headers to send that include the "Connection: keep-alive\r\n"
 * header line.
 *
 * This is used to initialize a local array for the final headers to send.
 */
#define HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE_LENGTH    ( 24 )

/**
 * Indicates for the http-parser parsing execution function to tell it to keep parsing or to stop parsing.
 *
 * A value of 0 means the parser should keep parsing if there is more unparsed length.
 * A value greater than 0 tells the parser to stop parsing.
 */
#define KEEP_PARSING                                      ( ( int ) 0 ) /**< @brief Indicator in the http-parser callback to keep parsing when the function returns. */
#define STOP_PARSING                                      ( ( int ) 1 ) /**< @brief Indicator in the http-parser callback to stop parsing when the function returns. */

/*-----------------------------------------------------------*/

/**
 * @brief Minimum size of the request user buffer.
 *
 * The request user buffer is configured in IotHttpsClientRequestInfo_t.userBuffer. This buffer stores the internal context
 * of the request and then the request headers right after. The minimum size for the buffer is the total size of the
 * internal request context, the HTTP formatted request line, the User-Agent header line, and the part of the Host
 * header line.
 */
const uint32_t requestUserBufferMinimumSize = sizeof( _httpsRequest_t ) +
                                              sizeof( HTTPS_PARTIAL_REQUEST_LINE ) +
                                              sizeof( HTTPS_USER_AGENT_HEADER_LINE ) +
                                              sizeof( HTTPS_PARTIAL_HOST_HEADER_LINE );

/**
 * @brief Minimum size of the response user buffer.
 *
 * The response user buffer is configured in IotHttpsClientRequestInfo_t.userBuffer. This buffer stores the internal context
 * of the response and then the response headers right after. This minimum size is calculated for the case if no bytes
 * from the HTTP response headers are to be stored.
 */
const uint32_t responseUserBufferMinimumSize = sizeof( _httpsResponse_t );

/**
 * @brief Minimum size of the connection user buffer.
 *
 * The connection user buffer is configured in IotHttpsConnectionInfo_t.userBuffer. This buffer stores the internal context of the
 * connection.
 */
const uint32_t connectionUserBufferMinimumSize = sizeof( _httpsConnection_t );

/*-----------------------------------------------------------*/

/**
 * @brief Callback from http-parser to indicate the start of the HTTP response message is reached.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_message_begin.
 */
static int _httpParserOnMessageBeginCallback( http_parser * pHttpParser );

/**
 * @brief Callback from http-parser to indicate it found the HTTP response status code.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 * @param[in] pLoc - Pointer to the HTTP response status code string in the response message buffer.
 * @param[in] length - The length of the HTTP response status code string.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_status.
 */
static int _httpParserOnStatusCallback( http_parser * pHttpParser,
                                        const char * pLoc,
                                        size_t length );

/**
 * @brief Callback from http-parser to indicate it found an HTTP response header field.
 *
 * If only part of the header field was returned here in this callback, then this callback will be invoked again the
 * next time the parser executes on the next part of the header field.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 * @param[in] pLoc - Pointer to the header field string in the response message buffer.
 * @param[in] length - The length of the header field.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_header_field.
 */
static int _httpParserOnHeaderFieldCallback( http_parser * pHttpParser,
                                             const char * pLoc,
                                             size_t length );

/**
 * @brief Callback from http-parser to indicate it found an HTTP response header value.
 *
 * This value corresponds to the field that was found in the _httpParserOnHeaderFieldCallback() called immediately
 * before this callback was called.
 *
 * If only part of the header value was returned here in this callback, then this callback will be invoked again the
 * next time the parser executes on the next part of the header value.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 * @param[in] pLoc - Pointer to the header value string in the response message buffer.
 * @param[in] length - The length of the header value.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_header_value.
 */
static int _httpParserOnHeaderValueCallback( http_parser * pHttpParser,
                                             const char * pLoc,
                                             size_t length );

/**
 * @brief Callback from http-parser to indicate it reached the end of the headers in the HTTP response message.
 *
 * The end of the headers is signalled in a HTTP response message by another "\r\n" after the final header line.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_headers_complete.
 */
static int _httpParserOnHeadersCompleteCallback( http_parser * pHttpParser );

/**
 * @brief Callback from http-parser to indicate it found HTTP response body.
 *
 * This callback will be invoked multiple times if the response body is of "Transfer-Encoding: chunked".
 * _httpParserOnChunkHeaderCallback() will be invoked first, then _httpParserOnBodyCallback(), then
 * _httpParserOnChunkCompleteCallback(), then repeated back to _httpParserOnChunkHeaderCallback() if there are more
 * "chunks".
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 * @param[in] pLoc - Pointer to the body string in the response message buffer.
 * @param[in] length - The length of the body found.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_body.
 */
static int _httpParserOnBodyCallback( http_parser * pHttpParser,
                                      const char * pLoc,
                                      size_t length );

/**
 * @brief Callback from http-parser to indicate it reached the end of the HTTP response message.
 *
 * The end of the message is signalled in a HTTP response message by another "\r\n" after the final header line, with no
 * entity body; or it is signaled by "\r\n" at the end of the entity body.
 *
 * For a Transfer-Encoding: chunked type of response message, the end of the message is signalled by a terminating
 * chunk header with length zero.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_message_complete.
 */
static int _httpParserOnMessageCompleteCallback( http_parser * pHttpParser );

/* This code prints debugging information and is, therefore, compiled only when
 * log level is set to IOT_LOG_DEBUG. */
#if ( LIBRARY_LOG_LEVEL == IOT_LOG_DEBUG )

/**
 * @brief Callback from http-parser to indicate it found an HTTP Transfer-Encoding: chunked header.
 *
 * Transfer-Encoding: chunked headers are embedded in the HTTP response entity body by a "\r\n" followed by the size of
 * the chunk followed by another "\r\n".
 *
 * If only part of the header field was returned here in this callback, then this callback will be invoked again the
 * next time the parser executes on the next part of the header field.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_chunk_header.
 */
    static int _httpParserOnChunkHeaderCallback( http_parser * pHttpParser );

/**
 * @brief Callback from http-parser to indicate it reached the end of an HTTP response message "chunk".
 *
 * A chunk is complete when the chunk header size is read fully in the body.
 *
 * See https://github.com/nodejs/http-parser for more information.
 *
 * @param[in] pHttpParser - http-parser state structure.
 *
 * @return 0 to tell http-parser to keep parsing.
 *         1 to tell http-parser that parsing should stop return from http_parser_execute with error HPE_CB_chunk_complete.
 */
    static int _httpParserOnChunkCompleteCallback( http_parser * pHttpParser );
#endif

/**
 * @brief Network receive callback for the HTTPS Client library.
 *
 * This function is called by the network layer whenever data is available for the HTTP library.
 *
 * @param[in] pNetworkConnection - The network connection with the HTTPS connection, passed by the network stack.
 * @param[in] pReceiveContext - A pointer to the HTTPS Client connection handle for which the packet was received.
 */
static void _networkReceiveCallback( void * pNetworkConnection,
                                     void * pReceiveContext );

/**
 * @brief Connects to HTTPS server and initializes the connection context.
 *
 * @param[out] pConnHandle - The out parameter to return handle representing the open connection.
 * @param[in] pConnInfo - The connection configuration.
 *
 * @return #IOT_HTTPS_OK if the connection context initialization was successful.
 *         #IOT_HTTPS_CONNECTION_ERROR if the connection failed.
 *         #IOT_HTTPS_INTERNAL_ERROR if the context initialization failed.
 */
static IotHttpsReturnCode_t _createHttpsConnection( IotHttpsConnectionHandle_t * pConnHandle,
                                                    IotHttpsConnectionInfo_t * pConnInfo );

/**
 * @brief Disconnects from the network.
 *
 * @param[in] pHttpsConnection - HTTPS connection handle.
 */
static void _networkDisconnect( _httpsConnection_t * pHttpsConnection );

/**
 * @brief Destroys the network connection.
 *
 * @param[in] pHttpsConnection - HTTPS connection handle.
 */
static void _networkDestroy( _httpsConnection_t * pHttpsConnection );

/**
 * @brief Add a header to the current HTTP request.
 *
 * The headers are stored in reqHandle->pHeaders.
 *
 * @param[in] pHttpsRequest - HTTP request context.
 * @param[in] pName - The name of the header to add.
 * @param[in] nameLen - The length of the header name string.
 * @param[in] pValue - The buffer containing the value string.
 * @param[in] valueLen - The length of the header value string.
 *
 * @return #IOT_HTTPS_OK if the header was added to the request successfully.
 *         #IOT_HTTPS_INSUFFICIENT_MEMORY if there was not enough room in the IotHttpsRequestHandle_t->pHeaders.
 */
static IotHttpsReturnCode_t _addHeader( _httpsRequest_t * pHttpsRequest,
                                        const char * pName,
                                        uint32_t nameLen,
                                        const char * pValue,
                                        uint32_t valueLen );

/**
 * @brief Send data on the network.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pBuf - The buffer containing the data to send.
 * @param[in] len - The length of the data to send.
 *
 * @return #IOT_HTTPS_OK if the data sent successfully.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error sending the data on the network.
 */
static IotHttpsReturnCode_t _networkSend( _httpsConnection_t * pHttpsConnection,
                                          uint8_t * pBuf,
                                          size_t len );

/**
 * @brief Receive data on the network.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pBuf - The buffer to receive the data into.
 * @param[in] bufLen - The length of the data to receive.
 * @param[in] numBytesRecv - The number of bytes read from the network.
 *
 * @return #IOT_HTTPS_OK if the data was received successfully.
 *         #IOT_HTTPS_NETWORK_ERROR if we timedout trying to receive data from the network.
 */
static IotHttpsReturnCode_t _networkRecv( _httpsConnection_t * pHttpsConnection,
                                          uint8_t * pBuf,
                                          size_t bufLen,
                                          size_t * numBytesRecv );

/**
 * @brief Send all of the HTTP request headers in the pHeadersBuf and the final Content-Length and Connection headers.
 *
 * All of the headers in headerbuf are sent first followed by the computed content length and persistent connection
 * indication.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pHeadersBuf - The buffer containing the request headers to send. This buffer must contain HTTP headers
 *            lines without the indicator for the the end of the HTTP headers.
 * @param[in] headersLength - The length of the request headers to send.
 * @param[in] isNonPersistent - Indicator of whether the connection is persistent or not.
 * @param[in] contentLength - The length of the request body used for automatically creating a "Content-Length" header.
 *
 * @return #IOT_HTTPS_OK if the headers were fully sent successfully.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _sendHttpsHeaders( _httpsConnection_t * pHttpsConnection,
                                               uint8_t * pHeadersBuf,
                                               uint32_t headersLength,
                                               bool isNonPersistent,
                                               uint32_t contentLength );

/**
 * @brief Send all of the HTTP request body in pBodyBuf.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pBodyBuf - Buffer of the request body to send.
 * @param[in] bodyLength - The length of the body to send.
 *
 * @return #IOT_HTTPS_OK if the body was fully sent successfully.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _sendHttpsBody( _httpsConnection_t * pHttpsConnection,
                                            uint8_t * pBodyBuf,
                                            uint32_t bodyLength );

/**
 * @brief Parse the HTTP response message in pBuf.
 *
 * @param[in] pHttpParserInfo - Pointer to the information containing the instance of the http-parser and the execution function.
 * @param[in] pBuf - The buffer containing the data to parse.
 * @param[in] len - The length of data to parse.
 *
 * @return #IOT_HTTPS_OK if the data was parsed successfully.
 *         #IOT_HTTPS_PARSING_ERROR if there was an error with parsing the data.
 */
static IotHttpsReturnCode_t _parseHttpsMessage( _httpParserInfo_t * pHttpParserInfo,
                                                char * pBuf,
                                                size_t len );

/**
 * @brief Receive any part of an HTTP response.
 *
 * This function is used for both receiving the body into the body buffer and receiving the header into the header
 * buffer.
 *
 * @param[in] pHttpsConnection - HTTP Connection context.
 * @param[in] pParser - Pointer to the instance of the http-parser.
 * @param[in] pCurrentParserState - The current state of what has been parsed in the HTTP response.
 * @param[in] finalParserState - The final state of the parser expected after this function finishes.
 * @param[in] currentBufferProcessingState - The current buffer that is the HTTPS message is being received into.
 * @param[in] pBufCur - Pointer to the next location to write data into the buffer pBuf. This is a double pointer to update the response context buffer pointers.
 * @param[in] pBufEnd - Pointer to the end of the buffer to receive the HTTP response into.
 *
 * @return #IOT_HTTPS_OK if we received the HTTP response message part successfully.
 *         #IOT_HTTPS_PARSING_ERROR if there was an error with parsing the data.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _receiveHttpsMessage( _httpsConnection_t * pHttpsConnection,
                                                  _httpParserInfo_t * pParser,
                                                  IotHttpsResponseParserState_t * pCurrentParserState,
                                                  IotHttpsResponseParserState_t finalParserState,
                                                  IotHttpsResponseBufferState_t currentBufferProcessingState,
                                                  uint8_t ** pBufCur,
                                                  uint8_t ** pBufEnd );

/**
 * @brief Receive the HTTP response headers.
 *
 * Receiving the response headers is always the first step in receiving the response, therefore the
 * pHttpsResponse->httpParserInfo will be initialized to a starting state when this function is called.
 *
 * This function also sets internal states to indicate that the header buffer is being processed now for a new response.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pHttpsResponse - HTTP response context.
 *
 * @return #IOT_HTTPS_OK if we received the HTTP headers successfully.
 *         #IOT_HTTPS_PARSING_ERROR if there was an error with parsing the header buffer.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _receiveHttpsHeaders( _httpsConnection_t * pHttpsConnection,
                                                  _httpsResponse_t * pHttpsResponse );

/**
 * @brief Receive the HTTP response body.
 *
 * Sets internal states to indicate that the the body buffer is being processed now for a new response.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pHttpsResponse - HTTP response context.
 *
 * @return #IOT_HTTPS_OK if we received the HTTP body successfully.
 *         #IOT_HTTPS_PARSING_ERROR if there was an error with parsing the body buffer.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _receiveHttpsBody( _httpsConnection_t * pHttpsConnection,
                                               _httpsResponse_t * pHttpsResponse );

/**
 * @brief Read the rest of any HTTP response that may be on the network.
 *
 * This reads the rest of any left over response data that might still be on the network buffers. We do not want this
 * data left over because it will spill into the header and body buffers of next response that we try to receive.
 *
 * If we performed a request without a body and the headers received exceeds the size of the
 * pHttpsResponse->pHeaders buffer, then we need to flush the network buffer.
 *
 * If the application configured the body buffer as null in IotHttpsResponseInfo_t.syncInfo.respData and the server
 * sends body in the response, but it exceeds the size of  pHttpsResponse->pHeaders buffer, then we need to flush the
 * network buffer.
 *
 * If the amount of body received on the network does not fit into a non-null IotHttpsResponseInfo_t.syncInfo.respData,
 * then we need to flush the network buffer.
 *
 * If an asynchronous request cancels in the middle of a response process, after already sending the request message,
 * then we need to flush the network buffer.
 *
 * @param[in] pHttpsConnection - HTTP connection context.
 * @param[in] pHttpsResponse - HTTP response context.
 *
 * @return #IOT_HTTPS_OK if we successfully flushed the network data.
 *         #IOT_HTTPS_PARSING_ERROR if there was an error with parsing the data.
 *         #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _flushHttpsNetworkData( _httpsConnection_t * pHttpsConnection,
                                                    _httpsResponse_t * pHttpsResponse );

/**
 * @brief Task pool job routine to send the HTTP request within the pUserContext.
 *
 * @param[in] pTaskPool Pointer to the system task pool.
 * @param[in] pJob Pointer the to the HTTP request sending job.
 * @param[in] pUserContext Pointer to an HTTP request, passed as an opaque context.
 */
static void _sendHttpsRequest( IotTaskPool_t pTaskPool,
                               IotTaskPoolJob_t pJob,
                               void * pUserContext );


/**
 * @brief Receive the HTTPS body specific to an asynchronous type of response.
 *
 * @param[in] pHttpsResponse - HTTP response context.
 *
 * @return  #IOT_HTTPS_OK - If the the response body was received with no issues.
 *          #IOT_HTTPS_RECEIVE_ABORT - If the request was cancelled by the Application
 *          #IOT_HTTPS_PARSING_ERROR - If there was an issue parsing the HTTP response body.
 *          #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _receiveHttpsBodyAsync( _httpsResponse_t * pHttpsResponse );

/**
 * @brief Receive the HTTPS body specific to a synchronous type of response.
 *
 * @param[in] pHttpsResponse - HTTP response context.
 *
 * @return  #IOT_HTTPS_OK - If the the response body was received with no issues.
 *          #IOT_HTTPS_MESSAGE_TOO_LARGE - If the body from the network is too large to fit into the configured body buffer.
 *          #IOT_HTTPS_PARSING_ERROR - If there was an issue parsing the HTTP response body.
 *          #IOT_HTTPS_NETWORK_ERROR if there was an error receiving the data on the network.
 */
static IotHttpsReturnCode_t _receiveHttpsBodySync( _httpsResponse_t * pHttpsResponse );

/**
 * @brief Schedule the task to send the the HTTP request.
 *
 * @param[in] pHttpsRequest - HTTP request context.
 *
 * @return  #IOT_HTTPS_OK - If the task to send the HTTP request was successfully scheduled.
 *          #IOT_HTTPS_INTERNAL_ERROR - If a taskpool job could not be created.
 *          #IOT_HTTPS_ASYNC_SCHEDULING_ERROR - If there was an error scheduling the job.
 */
IotHttpsReturnCode_t _scheduleHttpsRequestSend( _httpsRequest_t * pHttpsRequest );

/**
 * @brief Add the request to the connection's request queue.
 *
 * This will schedule a task if the request is first and only request in the queue.
 *
 * @param[in] pHttpsRequest - HTTP request context.
 *
 * @return  #IOT_HTTPS_OK - If the request was successfully added to the connection's request queue.
 *          #IOT_HTTPS_INTERNAL_ERROR - If a taskpool job could not be created.
 *          #IOT_HTTPS_ASYNC_SCHEDULING_ERROR - If there was an error scheduling the job.
 */
IotHttpsReturnCode_t _addRequestToConnectionReqQ( _httpsRequest_t * pHttpsRequest );

/**
 * @brief Cancel the HTTP request's processing.
 *
 * pHttpsRequest->cancelled will be checked and the request cancelled if specified so at the following intervals:
 *  - Before sending the HTTPS headers at the start of the scheduled sending of the HTTPS request.
 *  - After Sending the HTTPS headers.
 *  - After Sending the HTTPS body.
 *
 * @param[in] pHttpsRequest - HTTP request context.
 */
static void _cancelRequest( _httpsRequest_t * pHttpsRequest );

/**
 * @brief Cancel the HTTP response's processing.
 *
 * pHttpsResponse->cancelled will be checked and the response cancelled if specified so at the following intervals:
 *  - At the start of the network receive callback.
 *  - After receiving the HTTPS headers.
 *  - After Receiving the HTTPS body.
 *
 * @param[in] pHttpsResponse - HTTP response context.
 */
static void _cancelResponse( _httpsResponse_t * pHttpsResponse );

/**
 * @brief Initialize the input pHttpsResponse with pRespInfo.
 *
 * @param[in] pRespHandle - Non-null HTTP response context.
 * @param[in] pRespInfo - Response configuration information.
 * @param[in] pHttpsRequest - HTTP request to grab async information, persistence, and method from.
 */
static IotHttpsReturnCode_t _initializeResponse( IotHttpsResponseHandle_t * pRespHandle,
                                                 IotHttpsResponseInfo_t * pRespInfo,
                                                 _httpsRequest_t * pHttpsRequest );

/**
 * @brief Increment the pointer stored in pBufCur depending on the character found in there.
 *
 * This function increments the pHeadersCur pointer further if the message ended with a header line delimiter.
 *
 * @param[in] pBufCur - Pointer to the next location to write data into the buffer pBuf. This is a double pointer to update the response context buffer pointers.
 * @param[in] pBufEnd - Pointer to the end of the buffer to receive the HTTP response into.
 */
static void _incrementNextLocationToWriteBeyondParsed( uint8_t ** pBufCur,
                                                       uint8_t ** pBufEnd );

/**
 * @brief Send the HTTPS headers and body referenced in pHttpsRequest.
 *
 * Sends both the headers and body over the network.
 *
 * @param[in] pHttpsConnection - HTTPS connection context.
 * @param[in] pHttpsRequest - HTTPS request context.
 */
static IotHttpsReturnCode_t _sendHttpsHeadersAndBody( _httpsConnection_t * pHttpsConnection,
                                                      _httpsRequest_t * pHttpsRequest );

/*-----------------------------------------------------------*/

/**
 * @brief Definition of the http-parser settings.
 *
 * The http_parser_settings holds all of the callbacks invoked by the http-parser.
 */
static http_parser_settings _httpParserSettings = { 0 };

/*-----------------------------------------------------------*/

static int _httpParserOnMessageBeginCallback( http_parser * pHttpParser )
{
    int retVal = KEEP_PARSING;

    IotLogDebug( "Parser: Start of HTTPS Response message." );

    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );
    /* Set the state of the parser. The headers are at the start of the message always. */
    pHttpsResponse->parserState = PARSER_STATE_IN_HEADERS;
    return retVal;
}

/*-----------------------------------------------------------*/

static int _httpParserOnStatusCallback( http_parser * pHttpParser,
                                        const char * pLoc,
                                        size_t length )
{
    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );

    IotLogDebug( "Parser: Status %.*s retrieved from HTTPS response.", length, pLoc );

    /* Save the status code so it can be retrieved with IotHttpsClient_ReadResponseStatus(). */
    pHttpsResponse->status = ( uint16_t ) ( pHttpParser->status_code );

    /* If we are parsing the network data received in the header buffer then we
     * increment pHttpsResponse->pHeadersCur. The status line in the response is
     * part of the data stored in header buffer _httpResponse->pHeaders. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER )
    {
        /* pHeadersCur will never exceed the pHeadersEnd here because PROCESSING_STATE_FILLING_HEADER_BUFFER
         * indicates we are currently in the header buffer and the total size of the header buffer is passed
         * into http_parser_execute() as the maximum length to parse. */
        pHttpsResponse->pHeadersCur = ( uint8_t * ) ( pLoc += length );
    }

    return KEEP_PARSING;
}

/*-----------------------------------------------------------*/

static int _httpParserOnHeaderFieldCallback( http_parser * pHttpParser,
                                             const char * pLoc,
                                             size_t length )
{
    IotLogDebug( "Parser: HTTPS header field parsed %.*s", length, pLoc );

    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );

    /* If we are parsing the network data received in the header buffer then we can increment
     * pHttpsResponse->pHeadersCur. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER )
    {
        pHttpsResponse->pHeadersCur = ( uint8_t * ) ( pLoc += length );
    }

    /* If the IotHttpsClient_ReadHeader() was called, then we check for the header field of interest. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_SEARCHING_HEADER_BUFFER )
    {
        if( pHttpsResponse->readHeaderFieldLength != length )
        {
            pHttpsResponse->foundHeaderField = false;
        }
        else if( strncmp( pHttpsResponse->pReadHeaderField, pLoc, length ) == 0 )
        {
            pHttpsResponse->foundHeaderField = true;
        }
    }

    return KEEP_PARSING;
}

/*-----------------------------------------------------------*/

static int _httpParserOnHeaderValueCallback( http_parser * pHttpParser,
                                             const char * pLoc,
                                             size_t length )
{
    int retVal = KEEP_PARSING;

    IotLogDebug( "Parser: HTTPS header value parsed %.*s", length, pLoc );
    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );

    /* If we are parsing the network data received in the header buffer then we can increment
     * pHttpsResponse->pHeadersCur. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER )
    {
        pHttpsResponse->pHeadersCur = ( uint8_t * ) ( pLoc += length );
    }

    /* If the IotHttpsClient_ReadHeader() was called, then we check if we found the header field of interest. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_SEARCHING_HEADER_BUFFER )
    {
        if( pHttpsResponse->foundHeaderField )
        {
            pHttpsResponse->pReadHeaderValue = ( char * ) ( pLoc );
            pHttpsResponse->readHeaderValueLength = length;
            /* We found a header field so we don't want to keep parsing.*/
            retVal = STOP_PARSING;
        }
    }

    return retVal;
}

/*-----------------------------------------------------------*/

static int _httpParserOnHeadersCompleteCallback( http_parser * pHttpParser )
{
    IotLogDebug( "Parser: End of the headers reached." );

    int retVal = KEEP_PARSING;
    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );
    pHttpsResponse->parserState = PARSER_STATE_HEADERS_COMPLETE;

    /* If the IotHttpsClient_ReadHeader() was called, we return after finishing looking through all of the headers.
     * Returning a non-zero value exits the http parsing. */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_SEARCHING_HEADER_BUFFER )
    {
        retVal = STOP_PARSING;
    }

    /* When in this callback the pHeaderCur pointer is at the first "\r" in the last header line. HTTP/1.1
     * headers end with another "\r\n" at the end of the last line. This means we must increment
     * the headerCur pointer to the length of "\r\n\r\n". */
    if( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER )
    {
        pHttpsResponse->pHeadersCur += ( 2 * HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH );
    }

    /* This if-case is not incrementing any pHeaderCur pointers, so this case is safe to call when flushing the
     * network buffer. Flushing the network buffer needs the logic below to reach PARSER_STATE_BODY_COMPLETE if the
     * response is for a HEAD request. Before flushing the network buffer the bufferProcessingState is set to
     * PROCESSING_STATE_FINISHED so that other callback functions don't update header or body current pointers in the
     * response context. We don't want those pointers incremented because flushing the network uses a different buffer
     * to receive the rest of the response. */
    if( pHttpsResponse->bufferProcessingState <= PROCESSING_STATE_FINISHED )
    {
        /* For a HEAD method, there is no body expected in the response, so we return 1 to skip body parsing. */
        if( ( pHttpsResponse->method == IOT_HTTPS_METHOD_HEAD ) )
        {
            retVal = STOP_PARSING;

            /* Since the message is considered complete now for a HEAD response, then we set the parser state
             * to the completed state. */
            pHttpsResponse->parserState = PARSER_STATE_BODY_COMPLETE;
        }

        /* If this is NOT a HEAD method and there is body configured, but the server does not send a body in the
         * response, then the body buffer will be filled with the zeros from rest of the header buffer. http-parser
         * will invoke the on_body callback and consider the zeros following the headers as body. */

        /* If there is not body configured for a synchronous reponse, we do not stop the parser from continueing. */

        /* Skipping the body will cause the parser to invoke the _httpParserOnMessageComplete() callback. This is
         * not desired when there is actually HTTP response body sent by the server because this will set the parser
         * state to PARSER_STATE_BODY_COMPLETE. If this state is set then the rest of possible body will not be
         * flushed out. The network flush looks for the state being PARSER_STATE_BODY_COMPLETE to finish flushing. */
    }

    return retVal;
}

/*-----------------------------------------------------------*/

static int _httpParserOnBodyCallback( http_parser * pHttpParser,
                                      const char * pLoc,
                                      size_t length )
{
    IotLogDebug( "Parser: Reached the HTTPS message body. It is of length: %d", length );

    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );
    pHttpsResponse->parserState = PARSER_STATE_IN_BODY;

    /* If the header buffer is currently being processed, but HTTP response body was found, then for an asynchronous
     * request this if-case saves where the body is located. In the asynchronous case, the body buffer is not available
     * until the readReadyCallback is invoked, which happens after the headers are processed.  */
    if( ( pHttpsResponse->bufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER ) && ( pHttpsResponse->isAsync ) )
    {
        /* For an asynchronous response, the buffer to store the body will be available after the headers
         * are read first. We may receive part of the body in the header buffer. We will want to leave this here
         * and copy it over when the body buffer is available in the _readReadyCallback().
         */
        if( pHttpsResponse->pBodyInHeaderBuf == NULL )
        {
            pHttpsResponse->pBodyInHeaderBuf = ( uint8_t * ) ( pLoc );
            pHttpsResponse->pBodyCurInHeaderBuf = pHttpsResponse->pBodyInHeaderBuf;
        }

        /* If there is a chunk encoded body in the header buffer, we will want to overwrite the chunk headers with the
         * actual body. This is so that when the application calls IotHttpsClient_ReadResponseBody(), in the
         * readReadyCallback(), we can pass the body into the body buffer provided right away. */
        if( pHttpsResponse->pBodyCurInHeaderBuf != ( uint8_t * ) pLoc )
        {
            memcpy( pHttpsResponse->pBodyCurInHeaderBuf, pLoc, length );
        }

        pHttpsResponse->pBodyCurInHeaderBuf += length;
    }
    else if( pHttpsResponse->bufferProcessingState < PROCESSING_STATE_FINISHED )
    {
        /* Has the user provided a buffer and is it large enough to fit the body? The
         * case of body buffer not being large enough can happen if the body was received
         * in the header buffer and the body buffer can not fit in all the body. */
        if( ( pHttpsResponse->pBodyCur != NULL ) && ( pHttpsResponse->pBodyEnd - pHttpsResponse->pBodyCur > 0 ) )
        {
            /* There are two scenarios when we need to copy data around:
             * 1. Some or all of the response body may have been received in the header
             * buffer. If that is the case, we copy the response body received in the
             * header buffer to the user provided body buffer.
             * 2. When we receive chunked header, the actual body is separated in
             * multiple chunks which are preceeded by length. For example, a chunked
             * body may look like:
             *
             * 7\r\n
             * Mozilla\r\n
             * 9\r\n
             * Developer\r\n
             * 7\r\n
             * Network\r\n
             * 0\r\n
             * \r\n
             *
             * In this case, we want the parsed body buffer to contain actual body only
             * (MozillaDeveloperNetwork in the above example).
             */

            /* If the response body found by the parser (pLoc) is not equal to the
             * current writable location in the body buffer (_httpsResponse->pBodyCur),
             * it indicates that:
             * - Either the data is in the header buffer and needs to be copied into the
             * body buffer.
             * - Or it is a chunked response and the data needs to be moved up in the
             * body buffer. */
            if( ( pHttpsResponse->pBodyCur + length ) <= pHttpsResponse->pBodyEnd )
            {
                if( pHttpsResponse->pBodyCur != ( uint8_t * ) pLoc )
                {
                    memcpy( pHttpsResponse->pBodyCur, pLoc, length );
                }

                pHttpsResponse->pBodyCur += length;
            }
        }
    }

    return KEEP_PARSING;
}

/*-----------------------------------------------------------*/

static int _httpParserOnMessageCompleteCallback( http_parser * pHttpParser )
{
    IotLogDebug( "Parser: End of the HTTPS message reached." );

    _httpsResponse_t * pHttpsResponse = ( _httpsResponse_t * ) ( pHttpParser->data );
    pHttpsResponse->parserState = PARSER_STATE_BODY_COMPLETE;

    /* This callback is invoked when the complete HTTP response has been received.
     * We tell the parser to parse the whole body buffer as opposed to the size of
     * the response body. For example, if the size of the body buffer is 1000 but
     * the size of the actual body is 500, we tell the parser to parse the whole
     * buffer of length 1000. We do zero out the buffer in the beginning so that all
     * the buffer after the actual body contains zeros. We return greater than zero to stop parsing
     * since the end of the HTTP message has been reached. Any data beyond the end of the message is
     * ignored. */
    return STOP_PARSING;
}

/*-----------------------------------------------------------*/

/* This code prints debugging information and is, therefore, compiled only when
 * log level is set to IOT_LOG_DEBUG. */
#if ( LIBRARY_LOG_LEVEL == IOT_LOG_DEBUG )
    static int _httpParserOnChunkHeaderCallback( http_parser * pHttpParser )
    {
        ( void ) pHttpParser;
        IotLogDebug( "Parser: HTTPS message Chunked encoding header callback." );
        IotLogDebug( "Parser: HTTPS message Chunk size: %d", pHttpParser->content_length );
        return 0;
    }

/*-----------------------------------------------------------*/

    static int _httpParserOnChunkCompleteCallback( http_parser * pHttpParser )
    {
        ( void ) pHttpParser;
        IotLogDebug( "End of a HTTPS message Chunk complete callback." );
        return 0;
    }
#endif /* if ( LIBRARY_LOG_LEVEL == IOT_LOG_DEBUG ) */

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _receiveHttpsBodyAsync( _httpsResponse_t * pHttpsResponse )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    if( pHttpsResponse->pCallbacks->readReadyCallback )
    {
        /* If there is still more body that has not been passed back to the user, then this callback is invoked again. */
        do
        {
            IotLogDebug( "Invoking the readReadyCallback." );
            pHttpsResponse->pCallbacks->readReadyCallback( pHttpsResponse->pUserPrivData,
                                                           pHttpsResponse,
                                                           pHttpsResponse->bodyRxStatus,
                                                           pHttpsResponse->status );

            if( pHttpsResponse->cancelled == true )
            {
                IotLogDebug( "Cancelled HTTP response %d.", pHttpsResponse );
                status = IOT_HTTPS_RECEIVE_ABORT;

                /* We break out of the loop and do not goto clean up because we want to print debugging logs for
                 * the parser state and the networks status. */
                break;
            }
        } while( ( pHttpsResponse->parserState < PARSER_STATE_BODY_COMPLETE ) && ( pHttpsResponse->bodyRxStatus == IOT_HTTPS_OK ) );

        if( HTTPS_FAILED( pHttpsResponse->bodyRxStatus ) )
        {
            IotLogError( "Error receiving the HTTP response body for response %d. Error code: %d",
                         pHttpsResponse,
                         pHttpsResponse->bodyRxStatus );
            /* An error in the network or the parser takes precedence  */
            status = pHttpsResponse->bodyRxStatus;
        }

        if( pHttpsResponse->parserState < PARSER_STATE_BODY_COMPLETE )
        {
            IotLogDebug( "Did not receive all of the HTTP response body for response %d.",
                         pHttpsResponse );
        }
    }

    /* This GOTO cleanup is here for compiler warnings about using HTTPS_FUNCTION_EXIT_NO_CLEANUP() without a
     * corresponding goto. */
    HTTPS_GOTO_CLEANUP();
    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _receiveHttpsBodySync( _httpsResponse_t * pHttpsResponse )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );
    _httpsConnection_t * pHttpsConnection = pHttpsResponse->pHttpsConnection;

    /* The header buffer is now filled or the end of the headers has been reached already. If part of the response
     *  body was read from the network into the header buffer, then it was already copied to the body buffer in the
     *  _httpParserOnBodyCallback(). */
    if( pHttpsResponse->pBody != NULL )
    {
        /* If there is room left in the body buffer and we have not received the whole response body,
         * then try to receive more. */
        if( ( ( pHttpsResponse->pBodyEnd - pHttpsResponse->pBodyCur ) > 0 ) &&
            ( pHttpsResponse->parserState < PARSER_STATE_BODY_COMPLETE ) )
        {
            status = _receiveHttpsBody( pHttpsConnection,
                                        pHttpsResponse );

            if( HTTPS_FAILED( status ) )
            {
                IotLogError( "Error receiving the HTTPS response body for response %d. Error code: %d.",
                             pHttpsResponse,
                             status );
                HTTPS_GOTO_CLEANUP();
            }
        }
        else
        {
            IotLogDebug( "Received the maximum amount of HTTP body when filling the header buffer for response %d.",
                         pHttpsResponse );
        }

        /* If we don't reach the end of the HTTPS body in the parser, then we only received part of the body.
         *  The rest of body will be on the network socket. */
        if( HTTPS_SUCCEEDED( status ) && ( pHttpsResponse->parserState < PARSER_STATE_BODY_COMPLETE ) )
        {
            IotLogError( "HTTPS response body does not fit into application provided response buffer at location 0x%x "
                         "with length: %d",
                         pHttpsResponse->pBody,
                         pHttpsResponse->pBodyEnd - pHttpsResponse->pBody );
            HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_MESSAGE_TOO_LARGE );
        }
    }
    else
    {
        IotLogDebug( "No response body was configure for response %d.", pHttpsResponse );
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static void _networkReceiveCallback( void * pNetworkConnection,
                                     void * pReceiveContext )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    IotHttpsReturnCode_t flushStatus = IOT_HTTPS_OK;
    IotHttpsReturnCode_t disconnectStatus = IOT_HTTPS_OK;
    IotHttpsReturnCode_t scheduleStatus = IOT_HTTPS_OK;
    _httpsConnection_t * pHttpsConnection = ( _httpsConnection_t * ) pReceiveContext;
    _httpsResponse_t * pCurrentHttpsResponse = NULL;
    _httpsRequest_t * pNextHttpsRequest = NULL;
    IotLink_t * pQItem = NULL;
    bool fatalDisconnect = false;

    /* The network connection is already in the connection context. */
    ( void ) pNetworkConnection;

    /* Get the response from the response queue. */
    IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );
    pQItem = IotDeQueue_PeekHead( &( pHttpsConnection->respQ ) );
    IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

    /* If the receive callback is invoked and there is no response expected, then this a violation of the HTTP/1.1
     * protocol. */
    if( pQItem == NULL )
    {
        IotLogError( "Received data on the network, when no response was expected..." );
        fatalDisconnect = true;
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_NETWORK_ERROR );
    }

    /* Set the current HTTP response context to use. */
    pCurrentHttpsResponse = IotLink_Container( _httpsResponse_t, pQItem, link );

    /* If the receive callback has invoked, but the request associated with this response has not finished sending
     * to the server, then this is a violation of the HTTP/1.1 protocol.  */
    if( pCurrentHttpsResponse->reqFinishedSending == false )
    {
        IotLogError( "Received response data on the network when the request was not finished sending. This is unexpected." );
        fatalDisconnect = true;
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_NETWORK_ERROR );
    }

    /* If the current response was cancelled, then don't bother receiving the headers and body. */
    if( pCurrentHttpsResponse->cancelled )
    {
        IotLogDebug( "Response ID: %d was cancelled.", pCurrentHttpsResponse );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_RECEIVE_ABORT );
    }

    /* Reset the http-parser state to an initial state. This is done so that a new response can be parsed from the
     * beginning. */
    pCurrentHttpsResponse->parserState = PARSER_STATE_NONE;

    /* Receive the response from the network. */
    /* Receive the headers first. */
    status = _receiveHttpsHeaders( pHttpsConnection, pCurrentHttpsResponse );

    if( HTTPS_FAILED( status ) )
    {
        if( status == IOT_HTTPS_PARSING_ERROR )
        {
            /* There was an error parsing the HTTPS response body. This may be an indication of a server that does
             *  not adhere to protocol correctly. We should disconnect. */
            IotLogError( "Failed to parse the HTTPS headers for response %d, Error code: %d.",
                         pCurrentHttpsResponse,
                         status );
            fatalDisconnect = true;
        }
        else if( status == IOT_HTTPS_NETWORK_ERROR )
        {
            /* Given the function signature of IotNetworkInterface_t.receive, we can only receive 0 to the number of bytes
             * requested. Receiving less than the number of bytes requests is OK since we do not how much data is expected, so
             * we ask for the full size of the receive buffer. Therefore, the only error that can be returned from receiving
             * the headers or body is a timeout. We always disconnect from the network when there is a timeout because the
             * server may be slow to respond. If the server happens to send the response later at the same time another response
             * is waiting in the queue, then the workflow is corrupted. Pipelining is not current supported in this library. */
            IotLogError( "Network error receiving the HTTPS headers for response %d. Error code: %d",
                         pCurrentHttpsResponse,
                         status );
            fatalDisconnect = true;
        }
        else /* Any other error. */
        {
            IotLogError( "Failed to retrive the HTTPS body for response %d. Error code: %d", pCurrentHttpsResponse, status );
        }

        HTTPS_GOTO_CLEANUP();
    }

    /* Check if we received all of the headers into the header buffer. */
    if( pCurrentHttpsResponse->parserState < PARSER_STATE_HEADERS_COMPLETE )
    {
        IotLogDebug( "Headers received on the network did not all fit into the configured header buffer for response %d."
                     " The length of the headers buffer is: %d",
                     pCurrentHttpsResponse,
                     pCurrentHttpsResponse->pHeadersEnd - pCurrentHttpsResponse->pHeaders );
        /* It is not error if the headers did not all fit into the buffer. */
    }

    /* Receive the body. */
    if( pCurrentHttpsResponse->isAsync )
    {
        status = _receiveHttpsBodyAsync( pCurrentHttpsResponse );
    }
    else
    {
        /* Otherwise receive synchronously. */
        status = _receiveHttpsBodySync( pCurrentHttpsResponse );
    }

    if( HTTPS_FAILED( status ) )
    {
        if( status == IOT_HTTPS_RECEIVE_ABORT )
        {
            /* If the request was cancelled, this is logged, but does not close the connection. */
            IotLogDebug( "User cancelled during the async readReadyCallback() for response %d.",
                         pCurrentHttpsResponse );
        }
        else if( status == IOT_HTTPS_PARSING_ERROR )
        {
            /* There was an error parsing the HTTPS response body. This may be an indication of a server that does
             *  not adhere to protocol correctly. We should disconnect. */
            IotLogError( "Failed to parse the HTTPS body for response %d, Error code: %d.",
                         pCurrentHttpsResponse,
                         status );
            fatalDisconnect = true;
        }
        else if( status == IOT_HTTPS_NETWORK_ERROR )
        {
            /* We always disconnect for a network error because failure to receive the HTTPS body will result in a
             * corruption of the workflow. */
            IotLogError( "Network error receiving the HTTPS body for response %d. Error code: %d",
                         pCurrentHttpsResponse,
                         status );
            fatalDisconnect = true;
        }
        else /* Any other error. */
        {
            IotLogError( "Failed to retrive the HTTPS body for response %d. Error code: %d", pCurrentHttpsResponse, status );
        }

        HTTPS_GOTO_CLEANUP();
    }

    IOT_FUNCTION_CLEANUP_BEGIN();

    /* Disconnect and return in the event of an out-of-order response. If a response is received out of order
     * pCurrentHttpsResponse will be NULL because there will be no response in the connection's response queue.
     * If a response is received out of order that is an indication of a rogue server. */
    if( fatalDisconnect && !pCurrentHttpsResponse )
    {
        IotLogError( "An out-of-order response was received. The connection will be disconnected." );
        disconnectStatus = IotHttpsClient_Disconnect( pHttpsConnection );

        if( HTTPS_FAILED( disconnectStatus ) )
        {
            IotLogWarn( "Failed to disconnect after an out of order response. Error code: %d.", disconnectStatus );
        }

        /* In this case this routine returns immediately after to avoid further uses of pCurrentHttpsResponse. */
        return;
    }

    /* Report errors back to the application. */
    if( HTTPS_FAILED( status ) )
    {
        if( pCurrentHttpsResponse->isAsync && pCurrentHttpsResponse->pCallbacks->errorCallback )
        {
            pCurrentHttpsResponse->pCallbacks->errorCallback( pCurrentHttpsResponse->pUserPrivData, NULL, pCurrentHttpsResponse, status );
        }

        pCurrentHttpsResponse->syncStatus = status;
    }

    /* If this is not a persistent request, the server would have closed it after sending a response, but we
     * disconnect anyways. If we are disconnecting there is is no point in wasting time
     * flushing the network. If the network is being disconnected we also do not schedule any pending requests. */
    if( fatalDisconnect || pCurrentHttpsResponse->isNonPersistent )
    {
        IotLogDebug( "Disconnecting response %d.", pCurrentHttpsResponse );
        disconnectStatus = IotHttpsClient_Disconnect( pHttpsConnection );

        if( ( pCurrentHttpsResponse != NULL ) && pCurrentHttpsResponse->isAsync && pCurrentHttpsResponse->pCallbacks->connectionClosedCallback )
        {
            pCurrentHttpsResponse->pCallbacks->connectionClosedCallback( pCurrentHttpsResponse->pUserPrivData, pHttpsConnection, disconnectStatus );
        }

        if( HTTPS_FAILED( disconnectStatus ) )
        {
            IotLogWarn( "Failed to disconnect response %d. Error code: %d.", pCurrentHttpsResponse, disconnectStatus );
        }

        /* If we disconnect, we do not process anymore requests. */
    }
    else
    {
        /* Set the processing state of the buffer to finished for completeness. This is also to prevent the parsing of the flush
         * data from incrementing any pointer in the HTTP response context. */
        pCurrentHttpsResponse->bufferProcessingState = PROCESSING_STATE_FINISHED;

        /* Flush the socket of the rest of the data if there is data left from this response. We need to do this
         * so that for the next request on this connection, there is not left over response from this request in
         * the next response buffer.
         *
         * If a continuous stream of data is coming in from the connection, with an unknown end, we may not be able to
         * flush the network data. It may sit here forever. A continuous stream should be ingested with the async workflow.
         *
         * All network errors are ignore here because network read will have read the data from network buffer despite
         * errors. */
        flushStatus = _flushHttpsNetworkData( pHttpsConnection, pCurrentHttpsResponse );

        if( flushStatus == IOT_HTTPS_PARSING_ERROR )
        {
            IotLogWarn( "There an error parsing the network flush data. The network buffer might not be fully flushed." );
        }
        else if( flushStatus != IOT_HTTPS_OK )
        {
            IotLogDebug( "Network error when flushing the https network data: %d", flushStatus );
        }

        IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );
        /* Get the next request to process. */
        pQItem = IotDeQueue_PeekHead( &( pHttpsConnection->reqQ ) );
        IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

        /* If there is a next request to process, then create a taskpool job to send the request. */
        if( pQItem != NULL )
        {
            /* Set this next request to send. */
            pNextHttpsRequest = IotLink_Container( _httpsRequest_t, pQItem, link );

            if( pNextHttpsRequest->scheduled == false )
            {
                IotLogDebug( "Request %d is next in the queue. Now scheduling a task to send the request.", pNextHttpsRequest );
                scheduleStatus = _scheduleHttpsRequestSend( pNextHttpsRequest );

                /* If there was an error with scheduling the new task, then report it. */
                if( HTTPS_FAILED( scheduleStatus ) )
                {
                    IotLogError( "Error scheduling HTTPS request %d. Error code: %d", pNextHttpsRequest, scheduleStatus );

                    if( pNextHttpsRequest->isAsync && pNextHttpsRequest->pCallbacks->errorCallback )
                    {
                        pNextHttpsRequest->pCallbacks->errorCallback( pNextHttpsRequest->pUserPrivData, pNextHttpsRequest, NULL, scheduleStatus );
                    }
                    else
                    {
                        pNextHttpsRequest->pHttpsResponse->syncStatus = scheduleStatus;
                    }
                }
            }
        }
        else
        {
            IotLogDebug( "Network receive callback found the request queue empty. A network send task was not scheduled." );
        }
    }

    /* Dequeue response from the response queue now that it is finished. */
    IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );

    /* There could be a scenario where the request fails to send and the network server still responds,
     * In this case, the failed response will have been cancelled and removed from the queue. If the network
     * server still got a response, then the safest way to remove the current response is to remove it explicitly
     * from the queue instead of dequeuing the header of the queue which might not be the current response. */
    if( IotLink_IsLinked( &( pCurrentHttpsResponse->link ) ) )
    {
        IotDeQueue_Remove( &( pCurrentHttpsResponse->link ) );
    }

    IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

    /* The first if-case below notifies IotHttpsClient_SendSync() that the response is finished receiving. When
     * IotHttpsClient_SendSync() returns the user is allowed to modify the user buffer used for the response context.
     * In the asynchronous case, the responseCompleteCallback notifies the application that the user buffer used for the
     * response context can be modified. Posting to the respFinishedSem or calling the responseCompleteCallback MUST be
     * mutually exclusive by wrapping in an if/else. If these were separate if-cases, then there could be a context
     * switch in between where the application modifies the buffer causing the next if-case to be executed. */
    if( pCurrentHttpsResponse->isAsync == false )
    {
        IotSemaphore_Post( &( pCurrentHttpsResponse->respFinishedSem ) );
    }
    else if( pCurrentHttpsResponse->pCallbacks->responseCompleteCallback )
    {
        /* Signal to a synchronous reponse that the response is complete. */
        pCurrentHttpsResponse->pCallbacks->responseCompleteCallback( pCurrentHttpsResponse->pUserPrivData, pCurrentHttpsResponse, status, pCurrentHttpsResponse->status );
    }
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _createHttpsConnection( IotHttpsConnectionHandle_t * pConnHandle,
                                                    IotHttpsConnectionInfo_t * pConnInfo )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    IotNetworkError_t networkStatus = IOT_NETWORK_SUCCESS;

    /* The maximum string length of the ALPN protocols is configured in IOT_HTTPS_MAX_ALPN_PROTOCOLS_LENGTH.
     * The +1 is for the NULL terminator needed by IotNetworkCredentials_t.pAlpnProtos. */
    char pAlpnProtos[ IOT_HTTPS_MAX_ALPN_PROTOCOLS_LENGTH + 1 ] = { 0 };

    /* The maximum string length of the Server host name is configured in IOT_HTTPS_MAX_HOST_NAME_LENGTH.
     * This +1 is for the NULL terminator needed by IotNetworkServerInfo_t.pHostName. */
    char pHostName[ IOT_HTTPS_MAX_HOST_NAME_LENGTH + 1 ] = { 0 };
    bool connectionMutexCreated = false;
    struct IotNetworkServerInfo networkServerInfo = { 0 };
    struct IotNetworkCredentials networkCredentials = { 0 };
    _httpsConnection_t * pHttpsConnection = NULL;
    IotNetworkCredentials_t pNetworkCredentials = NULL;

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pConnInfo->userBuffer.pBuffer );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pConnInfo->pNetworkInterface );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pConnInfo->pAddress );
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( pConnInfo->addressLen > 0 );

    /* Make sure the connection context can fit in the user buffer. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( pConnInfo->userBuffer.bufferLen >= connectionUserBufferMinimumSize,
                                         IOT_HTTPS_INSUFFICIENT_MEMORY,
                                         "Buffer size is too small to initialize the connection context. User buffer size: %d, required minimum size; %d.",
                                         ( *pConnInfo ).userBuffer.bufferLen,
                                         connectionUserBufferMinimumSize );

    /* Make sure that the server address does not exceed the maximum permitted length. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( pConnInfo->addressLen <= IOT_HTTPS_MAX_HOST_NAME_LENGTH,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "IotHttpsConnectionInfo_t.addressLen has a host name length %d that exceeds maximum length %d.",
                                         pConnInfo->addressLen,
                                         IOT_HTTPS_MAX_HOST_NAME_LENGTH );

    /* Make sure that the ALPN protocols does not exceed the maximum permitted length. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( pConnInfo->alpnProtocolsLen <= IOT_HTTPS_MAX_ALPN_PROTOCOLS_LENGTH,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "IotHttpsConnectionInfo_t.alpnProtocolsLen of %d exceeds the configured maximum protocol length %d. See IOT_HTTPS_MAX_ALPN_PROTOCOLS_LENGTH for more information.",
                                         pConnInfo->alpnProtocolsLen,
                                         IOT_HTTPS_MAX_ALPN_PROTOCOLS_LENGTH );

    pHttpsConnection = ( _httpsConnection_t * ) ( pConnInfo->userBuffer.pBuffer );

    /* Start with the disconnected state. */
    pHttpsConnection->isConnected = false;

    /* Initialize disconnection state keeper. */
    pHttpsConnection->isDestroyed = false;

    /* Initialize the queue of responses and requests. */
    IotDeQueue_Create( &( pHttpsConnection->reqQ ) );
    IotDeQueue_Create( &( pHttpsConnection->respQ ) );

    /* This timeout is used to wait for a response on the connection as well as
     * for the timeout for the connect operation. */
    if( pConnInfo->timeout == 0 )
    {
        pHttpsConnection->timeout = IOT_HTTPS_RESPONSE_WAIT_MS;
    }
    else
    {
        pHttpsConnection->timeout = pConnInfo->timeout;
    }

    /* pNetworkInterface contains all the routines to be able to send/receive data on the network. */
    pHttpsConnection->pNetworkInterface = pConnInfo->pNetworkInterface;

    /* The address from the connection configuration information is copied to a local buffer because a NULL pointer
     * is required in IotNetworkServerInfo_t.pHostName. IotNetworkServerInfo_t contains the server information needed
     * by the network interface to create the connection. */
    memcpy( pHostName, pConnInfo->pAddress, pConnInfo->addressLen );
    pHostName[ pConnInfo->addressLen ] = '\0';
    /* Set it in the IOT network abstractions server information parameter. */
    networkServerInfo.pHostName = pHostName;
    networkServerInfo.port = pConnInfo->port;

    /* If this is TLS connection, then set the network credentials. */
    if( ( pConnInfo->flags & IOT_HTTPS_IS_NON_TLS_FLAG ) == 0 )
    {
        if( pConnInfo->flags & IOT_HTTPS_DISABLE_SNI )
        {
            networkCredentials.disableSni = true;
        }
        else
        {
            networkCredentials.disableSni = false;
        }

        if( pConnInfo->pAlpnProtocols != NULL )
        {
            /* The alpn protocol strings in IotNetworkCredentials_t require a NULL terminator, so the alpn protocol
             * string in the connection configuration information is copied to a local buffer to append the NULL
             * terminator. */
            memcpy( pAlpnProtos, pConnInfo->pAlpnProtocols, pConnInfo->alpnProtocolsLen );
            pAlpnProtos[ pConnInfo->alpnProtocolsLen ] = '\0';
            networkCredentials.pAlpnProtos = pAlpnProtos;
        }
        else
        {
            networkCredentials.pAlpnProtos = NULL;
        }

        /* If any of these are NULL a network error will result when trying to make the connection. Because there is
         * no invalid memory access resulting from these configurations being NULL, it is not check at the start
         * of the function. */
        networkCredentials.pRootCa = pConnInfo->pCaCert;
        networkCredentials.rootCaSize = pConnInfo->caCertLen;
        networkCredentials.pClientCert = pConnInfo->pClientCert;
        networkCredentials.clientCertSize = pConnInfo->clientCertLen;
        networkCredentials.pPrivateKey = pConnInfo->pPrivateKey;
        networkCredentials.privateKeySize = pConnInfo->privateKeyLen;

        pNetworkCredentials = &networkCredentials;
    }
    else
    {
        /* create() takes a NULL if there is no TLS configuration. */
        pNetworkCredentials = NULL;
    }

    /* create() will connect to the server specified in addition to creating other network layer
     *  specific resources. */
    networkStatus = pHttpsConnection->pNetworkInterface->create( &networkServerInfo,
                                                                 pNetworkCredentials,
                                                                 &( pHttpsConnection->pNetworkConnection ) );

    /* Check to see if the network connection succeeded. If it did not succeed,
     * then the output parameter pConnHandle will be used to return NULL and the
     * function returns an error. */
    if( networkStatus != IOT_NETWORK_SUCCESS )
    {
        IotLogError( "Failed to connect to the server at %.*s on port %d with error: %d",
                     pConnInfo->addressLen,
                     pConnInfo->pAddress,
                     pConnInfo->port,
                     networkStatus );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_CONNECTION_ERROR );
    }

    /* The connection succeeded so set the state to connected. */
    pHttpsConnection->isConnected = true;

    /* The receive callback is invoked by the network layer when data is ready
     * to be read from the network. */
    networkStatus = pHttpsConnection->pNetworkInterface->setReceiveCallback( pHttpsConnection->pNetworkConnection,
                                                                             _networkReceiveCallback,
                                                                             pHttpsConnection );

    if( networkStatus != IOT_NETWORK_SUCCESS )
    {
        IotLogError( "Failed to connect to set the HTTPS receive callback. " );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INTERNAL_ERROR );
    }

    /* Connection was successful, so create synchronization primitives. */

    connectionMutexCreated = IotMutex_Create( &( pHttpsConnection->connectionMutex ), false );

    if( !connectionMutexCreated )
    {
        IotLogError( "Failed to create an internal mutex." );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INTERNAL_ERROR );
    }

    /* Return the new connection information. */
    *pConnHandle = pHttpsConnection;

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    /* If we failed anywhere in the connection process, then destroy the semaphores created. */
    if( HTTPS_FAILED( status ) )
    {
        /* If there was a connect was successful, disconnect from the network.  */
        if( ( pHttpsConnection != NULL ) && ( pHttpsConnection->isConnected ) )
        {
            _networkDisconnect( pHttpsConnection );
            _networkDestroy( pHttpsConnection );
        }

        if( connectionMutexCreated )
        {
            IotMutex_Destroy( &( pHttpsConnection->connectionMutex ) );
        }

        /* Set the connection handle as NULL if everything failed. */
        *pConnHandle = NULL;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

static void _networkDisconnect( _httpsConnection_t * pHttpsConnection )
{
    IotNetworkError_t networkStatus = IOT_NETWORK_SUCCESS;

    networkStatus = pHttpsConnection->pNetworkInterface->close( pHttpsConnection->pNetworkConnection );

    if( networkStatus != IOT_NETWORK_SUCCESS )
    {
        IotLogWarn( "Failed to shutdown the socket with error code: %d", networkStatus );
    }
}

/*-----------------------------------------------------------*/

static void _networkDestroy( _httpsConnection_t * pHttpsConnection )
{
    IotNetworkError_t networkStatus = IOT_NETWORK_SUCCESS;

    networkStatus = pHttpsConnection->pNetworkInterface->destroy( pHttpsConnection->pNetworkConnection );

    if( networkStatus != IOT_NETWORK_SUCCESS )
    {
        IotLogWarn( "Failed to shutdown the socket with error code: %d", networkStatus );
    }
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _addHeader( _httpsRequest_t * pHttpsRequest,
                                        const char * pName,
                                        uint32_t nameLen,
                                        const char * pValue,
                                        uint32_t valueLen )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    int headerFieldSeparatorLen = HTTPS_HEADER_FIELD_SEPARATOR_LENGTH;
    uint32_t additionalLength = nameLen + headerFieldSeparatorLen + valueLen + HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;
    uint32_t possibleLastHeaderAdditionalLength = HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;

    /* Check if there is enough space to add the header field and value
     * (name:value\r\n). We need to add a "\r\n" at the end of headers. The use of
     * possibleLastHeaderAdditionalLength is to make sure that there is always
     * space for the last "\r\n". */
    if( ( additionalLength + possibleLastHeaderAdditionalLength ) > ( ( uint32_t ) ( pHttpsRequest->pHeadersEnd - pHttpsRequest->pHeadersCur ) ) )
    {
        IotLogError( "There is %d space left in the header buffer, but we want to add %d more of header.",
                     pHttpsRequest->pHeadersEnd - pHttpsRequest->pHeadersCur,
                     additionalLength + possibleLastHeaderAdditionalLength );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INSUFFICIENT_MEMORY );
    }

    memcpy( pHttpsRequest->pHeadersCur, pName, nameLen );
    pHttpsRequest->pHeadersCur += nameLen;
    memcpy( pHttpsRequest->pHeadersCur, HTTPS_HEADER_FIELD_SEPARATOR, headerFieldSeparatorLen );
    pHttpsRequest->pHeadersCur += headerFieldSeparatorLen;
    memcpy( pHttpsRequest->pHeadersCur, pValue, valueLen );
    pHttpsRequest->pHeadersCur += valueLen;
    memcpy( pHttpsRequest->pHeadersCur, HTTPS_END_OF_HEADER_LINES_INDICATOR, HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH );
    pHttpsRequest->pHeadersCur += HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;
    IotLogDebug( "Wrote header: \"%s: %.*s\r\n\". Space left in request user buffer: %d",
                 pName,
                 valueLen,
                 pValue,
                 pHttpsRequest->pHeadersEnd - pHttpsRequest->pHeadersCur );

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _networkSend( _httpsConnection_t * pHttpsConnection,
                                          uint8_t * pBuf,
                                          size_t len )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    size_t numBytesSent = 0;
    size_t numBytesSentTotal = 0;
    size_t sendLength = len;

    while( numBytesSentTotal < sendLength )
    {
        numBytesSent = pHttpsConnection->pNetworkInterface->send( pHttpsConnection->pNetworkConnection,
                                                                  &( pBuf[ numBytesSentTotal ] ),
                                                                  sendLength - numBytesSentTotal );

        /* pNetworkInterface->send returns 0 on error. */
        if( numBytesSent == 0 )
        {
            IotLogError( "Error in sending the HTTPS headers. Error code: %d", numBytesSent );
            break;
        }

        numBytesSentTotal += numBytesSent;
    }

    if( numBytesSentTotal != sendLength )
    {
        IotLogError( "Error sending data on the network. We sent %d but there were total %d.", numBytesSentTotal, sendLength );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_NETWORK_ERROR );
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _networkRecv( _httpsConnection_t * pHttpsConnection,
                                          uint8_t * pBuf,
                                          size_t bufLen,
                                          size_t * numBytesRecv )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    /* The HTTP server could send the header and the body in two separate TCP packets. If that is the case, then
     * receiveUpTo will return return the full headers first. Then on a second call, the body will be returned.
     * If the http parser receives just the headers despite the content length being greater than  */
    *numBytesRecv = pHttpsConnection->pNetworkInterface->receiveUpto( pHttpsConnection->pNetworkConnection,
                                                                      pBuf,
                                                                      bufLen );

    IotLogDebug( "The network interface receive returned %d.", numBytesRecv );

    /* We return IOT_HTTPS_NETWORK_ERROR only if we receive nothing. Receiving less
     * data than requested is okay because it is not known in advance how much data
     * we are going to receive and therefore we request for the available buffer
     * size. */
    if( *numBytesRecv == 0 )
    {
        IotLogError( "Error in receiving the HTTPS response message. Socket Error code %d", *numBytesRecv );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_NETWORK_ERROR );

        /* A network error is returned when zero is received because that would indicate that either there
        * was a network error or there was a timeout reading data. If there was timeout reading data, then
        * the server was too slow to respond. If the server is too slow to respond, then a network error must
        * be returned to trigger a connection close. The connection must close after the network error so
        * that the response from this request does not piggyback on the response from the next request. */
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _sendHttpsHeaders( _httpsConnection_t * pHttpsConnection,
                                               uint8_t * pHeadersBuf,
                                               uint32_t headersLength,
                                               bool isNonPersistent,
                                               uint32_t contentLength )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    const char * connectionHeader = NULL;
    int numWritten = 0;
    int connectionHeaderLen = 0;
    /* The Content-Length header of the form "Content-Length: N\r\n" with a NULL terminator for snprintf. */
    char contentLengthHeaderStr[ HTTPS_MAX_CONTENT_LENGTH_LINE_LENGTH + 1 ];

    /* The HTTP headers to send after the headers in pHeadersBuf are the Content-Length and the Connection type and
     * the final "\r\n" to indicate the end of the the header lines. Note that we are using
     * HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE_LENGTH because length of "Connection: keep-alive\r\n" is
     * more than "Connection: close\r\n". Creating a buffer of bigger size ensures that
     * both the connection type strings will fit in the buffer. */
    char finalHeaders[ HTTPS_MAX_CONTENT_LENGTH_LINE_LENGTH + HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE_LENGTH + HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH ] = { 0 };

    /* Send the headers passed into this function first. These headers are not terminated with a second set of "\r\n". */
    status = _networkSend( pHttpsConnection, pHeadersBuf, headersLength );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error sending the HTTPS headers in the request user buffer. Error code: %d", status );
        HTTPS_GOTO_CLEANUP();
    }

    /* If there is a Content-Length, then write that to the finalHeaders to send. */
    if( contentLength > 0 )
    {
        numWritten = snprintf( contentLengthHeaderStr,
                               sizeof( contentLengthHeaderStr ),
                               "%s: %u\r\n",
                               HTTPS_CONTENT_LENGTH_HEADER,
                               ( unsigned int ) contentLength );
    }

    if( ( numWritten < 0 ) || ( numWritten >= sizeof( contentLengthHeaderStr ) ) )
    {
        IotLogError( "Internal error in snprintf() in _sendHttpsHeaders(). Error code %d.", numWritten );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INTERNAL_ERROR );
    }

    /* snprintf() succeeded so copy that to the finalHeaders. */
    memcpy( finalHeaders, contentLengthHeaderStr, numWritten );

    /* Write the connection persistence type to the final headers. */
    if( isNonPersistent )
    {
        connectionHeader = HTTPS_CONNECTION_CLOSE_HEADER_LINE;
        connectionHeaderLen = FAST_MACRO_STRLEN( HTTPS_CONNECTION_CLOSE_HEADER_LINE );
    }
    else
    {
        connectionHeader = HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE;
        connectionHeaderLen = FAST_MACRO_STRLEN( HTTPS_CONNECTION_KEEP_ALIVE_HEADER_LINE );
    }

    memcpy( &finalHeaders[ numWritten ], connectionHeader, connectionHeaderLen );
    numWritten += connectionHeaderLen;
    memcpy( &finalHeaders[ numWritten ], HTTPS_END_OF_HEADER_LINES_INDICATOR, HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH );
    numWritten += HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;

    status = _networkSend( pHttpsConnection, ( uint8_t * ) finalHeaders, numWritten );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error sending final HTTPS Headers \r\n%s. Error code: %d", finalHeaders, status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _sendHttpsBody( _httpsConnection_t * pHttpsConnection,
                                            uint8_t * pBodyBuf,
                                            uint32_t bodyLength )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    status = _networkSend( pHttpsConnection, pBodyBuf, bodyLength );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error sending final HTTPS body at location 0x%x. Error code: %d", pBodyBuf, status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _parseHttpsMessage( _httpParserInfo_t * pHttpParserInfo,
                                                char * pBuf,
                                                size_t len )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    size_t parsedBytes = 0;
    const char * pHttpParserErrorDescription = NULL;
    http_parser * pHttpParser = &( pHttpParserInfo->responseParser );

    IotLogDebug( "Now parsing HTTP message buffer to process a response." );
    parsedBytes = pHttpParserInfo->parseFunc( pHttpParser, &_httpParserSettings, pBuf, len );
    IotLogDebug( "http-parser parsed %d bytes out of %d specified.", parsedBytes, len );

    /* If the parser fails with HPE_CLOSED_CONNECTION or HPE_INVALID_CONSTANT that simply means there
     * was data beyond the end of the message. We do not fail in this case because we give the whole
     * header buffer or body buffer to the parser even if it is only partly filled with data.
     * Errors <= HPE_CB_chunk_complete means that a non-zero number was returned from some callback.
     * A nonzero number is returned from some callbacks when we want to stop the parser early
     * for example - a HEAD request or the user explicitly asked to ignore the body by not
     * providing the body buffer. */
    if( ( pHttpParser->http_errno != 0 ) &&
        ( HTTP_PARSER_ERRNO( pHttpParser ) != HPE_CLOSED_CONNECTION ) &&
        ( HTTP_PARSER_ERRNO( pHttpParser ) != HPE_INVALID_CONSTANT ) &&
        ( HTTP_PARSER_ERRNO( pHttpParser ) > HPE_CB_chunk_complete ) )
    {
        pHttpParserErrorDescription = http_errno_description( HTTP_PARSER_ERRNO( pHttpParser ) );
        IotLogError( "http_parser failed on the http response with error: %s", pHttpParserErrorDescription );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_PARSING_ERROR );
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static void _incrementNextLocationToWriteBeyondParsed( uint8_t ** pBufCur,
                                                       uint8_t ** pBufEnd )
{
    /* There is an edge case where the final one or two character received in the header buffer is part of
     * the header field separator ": " or part of the header line end "\r\n" delimiters. When this
     * happens, pHeadersCur in the response will point not the end of the buffer, but to a character in
     * the delimiter. For example:
     * Let's say this is our current header buffer after receiving and parsing:
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: value1\r\n"]
     * pHeadersCur will point to \r because the http-parser does not invoke a callback on the
     * delimiters. Since no callback is invoked, pHeadersCur is not incremented. pHeadersEnd points to
     * the end of the header buffer which is the unwritable memory location right after the final '\n'.
     * Because pHeadersCur is less than pHeaderEnd we loop again and receive on the network causing the
     * buffer to look like this:
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: value1he"]
     * Which will cause an incorrect header1 value to be read if the application decides to read it with
     * IotHttpsClient_ReadHeader().
     *
     * If our header buffer looks like:
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: "]
     * then pHeaderCur will point to the colon.
     *
     * If our header buffer looks like:
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1:"]
     * then pHeaderCur will point to the colon.
     *
     * If our header buffer looks like
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: value1 "]
     * then http-parser will consider that space as part of value1.
     *
     * If our header buffer looks like
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: value1\r"]
     * then pHeaderCur will point to the carriage return.
     *
     * If our header buffer looks like
     * ["HTTP/1.1 200 OK\r\n\header0: value0\r\nheader1: value1\r\n"]
     * As explained in the example above, pHeaderCur will point to the carriage return.
     *
     * If we somehow receive a partial HTTP response message in our zeroed-out header buffer:
     * case 1: ["HTTP/1.1 200 OK\r\nheader0: value0\r\nheader1: value1\r\0\0\0\0\0\0\0"]
     * case 2: ["HTTP/1.1 200 OK\r\nheader0: value0\r\nheader1: value1\r\n\0\0\0\0\0\0"]
     * case 3: ["HTTP/1.1 200 OK\r\nheader0: value0\r\nheader1:\0\0\0\0\0\0\0\0\0\0\0"]
     * case 4: ["HTTP/1.1 200 OK\r\nheader0: value0\r\nheader1: \0\0\0\0\0\0\0\0\0\0\0"]
     * then parser may fail or append all of the NULL characters to a header field name or value. */
    while( *pBufCur < *pBufEnd )
    {
        if( **pBufCur == CARRIAGE_RETURN_CHARACTER )
        {
            ( *pBufCur )++;
        }
        else if( **pBufCur == NEWLINE_CHARACTER )
        {
            ( *pBufCur )++;
            break;
        }
        else if( **pBufCur == COLON_CHARACTER )
        {
            ( *pBufCur )++;
        }
        else if( ( **pBufCur == SPACE_CHARACTER ) && ( *( *pBufCur - 1 ) == COLON_CHARACTER ) )
        {
            ( *pBufCur )++;
            break;
        }
        else
        {
            break;
        }
    }
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _receiveHttpsMessage( _httpsConnection_t * pHttpsConnection,
                                                  _httpParserInfo_t * pHttpParserInfo,
                                                  IotHttpsResponseParserState_t * pCurrentParserState,
                                                  IotHttpsResponseParserState_t finalParserState,
                                                  IotHttpsResponseBufferState_t currentBufferProcessingState,
                                                  uint8_t ** pBufCur,
                                                  uint8_t ** pBufEnd )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    size_t numBytesRecv = 0;

    /* The final parser state is either the end of the header lines or the end of the entity body. This state is set in
     * the http-parser callbacks. */
    while( ( *pCurrentParserState < finalParserState ) && ( *pBufEnd - *pBufCur > 0 ) )
    {
        status = _networkRecv( pHttpsConnection,
                               *pBufCur,
                               *pBufEnd - *pBufCur,
                               &numBytesRecv );

        /* A network error in _networkRecv is returned only when we received zero bytes. In that case, there is
         * no point in parsing we return immediately with the network error. */
        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Network error receiving the HTTPS response headers. Error code: %d", status );
            break;
        }

        status = _parseHttpsMessage( pHttpParserInfo, ( char * ) ( *pBufCur ), numBytesRecv );

        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Failed to parse the message buffer with error: %d", pHttpParserInfo->responseParser.http_errno );
            break;
        }

        /* If the current buffer being filled is the header buffer, then \r\n header line separators should not get
         * overwritten on the next network read. See _incrementNextLocationToWriteBeyondParsed() for more
         * information. */
        if( currentBufferProcessingState == PROCESSING_STATE_FILLING_HEADER_BUFFER )
        {
            _incrementNextLocationToWriteBeyondParsed( pBufCur, pBufEnd );
        }

        /* The _httpsResponse->pHeadersCur pointer is updated in the http_parser callbacks. */
        IotLogDebug( "There is %d of space left in the buffer.", *pBufEnd - *pBufCur );
    }

    /* If we did not reach the end of the headers or body in the parser callbacks, then the buffer configured does not
     * fit all of that part of the HTTP message. */
    if( *pCurrentParserState < finalParserState )
    {
        IotLogDebug( "There are still more data on the network. It could not fit into the specified length %d.",
                     *pBufEnd - *pBufCur );
    }

    HTTPS_GOTO_CLEANUP();
    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _receiveHttpsHeaders( _httpsConnection_t * pHttpsConnection,
                                                  _httpsResponse_t * pHttpsResponse )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    pHttpsResponse->bufferProcessingState = PROCESSING_STATE_FILLING_HEADER_BUFFER;

    IotLogDebug( "Now attempting to receive the HTTP response headers into a buffer with length %d.",
                 pHttpsResponse->pHeadersEnd - pHttpsResponse->pHeadersCur );

    status = _receiveHttpsMessage( pHttpsConnection,
                                   &( pHttpsResponse->httpParserInfo ),
                                   &( pHttpsResponse->parserState ),
                                   PARSER_STATE_HEADERS_COMPLETE,
                                   PROCESSING_STATE_FILLING_HEADER_BUFFER,
                                   &( pHttpsResponse->pHeadersCur ),
                                   &( pHttpsResponse->pHeadersEnd ) );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error receiving the HTTP headers. Error code %d", status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

/* _receiveHttpsHeaders() must be called first before this function is called. */
static IotHttpsReturnCode_t _receiveHttpsBody( _httpsConnection_t * pHttpsConnection,
                                               _httpsResponse_t * pHttpsResponse )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    IotLogDebug( "Now attempting to receive the HTTP response body into a buffer with length %d.",
                 pHttpsResponse->pBodyEnd - pHttpsResponse->pBodyCur );

    pHttpsResponse->bufferProcessingState = PROCESSING_STATE_FILLING_BODY_BUFFER;

    status = _receiveHttpsMessage( pHttpsConnection,
                                   &( pHttpsResponse->httpParserInfo ),
                                   &( pHttpsResponse->parserState ),
                                   PARSER_STATE_BODY_COMPLETE,
                                   PROCESSING_STATE_FILLING_BODY_BUFFER,
                                   &( pHttpsResponse->pBodyCur ),
                                   &( pHttpsResponse->pBodyEnd ) );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error receiving the HTTP body. Error code %d", status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    IotLogDebug( "The remaining content length on the network is %d.",
                 pHttpsResponse->httpParserInfo.responseParser.content_length );

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _flushHttpsNetworkData( _httpsConnection_t * pHttpsConnection,
                                                    _httpsResponse_t * pHttpsResponse )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    static uint8_t flushBuffer[ IOT_HTTPS_MAX_FLUSH_BUFFER_SIZE ] = { 0 };
    const char * pHttpParserErrorDescription = NULL;
    IotHttpsReturnCode_t parserStatus = IOT_HTTPS_OK;
    IotHttpsReturnCode_t networkStatus = IOT_HTTPS_OK;
    size_t numBytesRecv = 0;

    /* Even if there is not body, the parser state will become body complete after the headers finish. */
    while( pHttpsResponse->parserState < PARSER_STATE_BODY_COMPLETE )
    {
        IotLogDebug( "Now clearing the rest of the response data on the socket. " );
        networkStatus = _networkRecv( pHttpsConnection, flushBuffer, IOT_HTTPS_MAX_FLUSH_BUFFER_SIZE, &numBytesRecv );

        /* Run this through the parser so that we can get the end of the HTTP message, instead of simply timing out the socket to stop.
         * If we relied on the socket timeout to stop reading the network socket, then the server may close the connection. */
        parserStatus = _parseHttpsMessage( &( pHttpsResponse->httpParserInfo ), ( char * ) flushBuffer, numBytesRecv );

        if( HTTPS_FAILED( parserStatus ) )
        {
            pHttpParserErrorDescription = http_errno_description( HTTP_PARSER_ERRNO( &pHttpsResponse->httpParserInfo.responseParser ) );
            IotLogError( "Network Flush: Failed to parse the response body buffer with error: %d, %s",
                         pHttpsResponse->httpParserInfo.responseParser.http_errno,
                         pHttpParserErrorDescription );
            break;
        }

        /* If there is a network error then we want to stop clearing out the buffer. */
        if( HTTPS_FAILED( networkStatus ) )
        {
            IotLogWarn( "Network Flush: Error receiving the rest of the HTTP response. Error code: %d",
                        networkStatus );
            break;
        }
    }

    /* All network errors except timeouts are returned. */
    if( HTTPS_FAILED( networkStatus ) )
    {
        status = networkStatus;
    }
    else
    {
        status = parserStatus;
    }

    HTTPS_GOTO_CLEANUP();

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _sendHttpsHeadersAndBody( _httpsConnection_t * pHttpsConnection,
                                                      _httpsRequest_t * pHttpsRequest )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    /* Send the HTTP headers. */
    status = _sendHttpsHeaders( pHttpsConnection,
                                pHttpsRequest->pHeaders,
                                pHttpsRequest->pHeadersCur - pHttpsRequest->pHeaders,
                                pHttpsRequest->isNonPersistent,
                                pHttpsRequest->bodyLength );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error sending the HTTPS headers with error code: %d", status );
        HTTPS_GOTO_CLEANUP();
    }

    if( ( pHttpsRequest->pBody != NULL ) && ( pHttpsRequest->bodyLength > 0 ) )
    {
        status = _sendHttpsBody( pHttpsConnection, pHttpsRequest->pBody, pHttpsRequest->bodyLength );

        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Error sending final HTTPS body. Return code: %d", status );
            HTTPS_GOTO_CLEANUP();
        }
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static void _sendHttpsRequest( IotTaskPool_t pTaskPool,
                               IotTaskPoolJob_t pJob,
                               void * pUserContext )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    _httpsRequest_t * pHttpsRequest = ( _httpsRequest_t * ) ( pUserContext );
    _httpsConnection_t * pHttpsConnection = pHttpsRequest->pHttpsConnection;
    _httpsResponse_t * pHttpsResponse = pHttpsRequest->pHttpsResponse;
    IotHttpsReturnCode_t disconnectStatus = IOT_HTTPS_OK;
    IotHttpsReturnCode_t scheduleStatus = IOT_HTTPS_OK;
    IotLink_t * pQItem = NULL;
    _httpsRequest_t * pNextHttpsRequest = NULL;

    ( void ) pTaskPool;
    ( void ) pJob;

    IotLogDebug( "Task with request ID: %d started.", pHttpsRequest );

    if( pHttpsRequest->cancelled == true )
    {
        IotLogDebug( "Request ID: %d was cancelled.", pHttpsRequest );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_SEND_ABORT );
    }

    /* To protect against out of order network data from a rouge server, signal that the request is
     * not finished sending. */
    pHttpsResponse->reqFinishedSending = false;

    /* Queue the response to expect from the network. */
    IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );
    IotDeQueue_EnqueueTail( &( pHttpsConnection->respQ ), &( pHttpsResponse->link ) );
    IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

    /* Get the headers from the application. For a synchronous request the application should have appended extra
     * headers before this point. */
    if( pHttpsRequest->isAsync && pHttpsRequest->pCallbacks->appendHeaderCallback )
    {
        pHttpsRequest->pCallbacks->appendHeaderCallback( pHttpsRequest->pUserPrivData, pHttpsRequest );
    }

    if( pHttpsRequest->cancelled == true )
    {
        IotLogDebug( "Request ID: %d was cancelled.", pHttpsRequest );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_SEND_ABORT );
    }

    /* Ask the user for data to write body to the network. We only ask the user once. This is so that
     * we can calculate the Content-Length to send.*/
    if( pHttpsRequest->isAsync && pHttpsRequest->pCallbacks->writeCallback )
    {
        /* If there is data, then a Content-Length header value will be provided and we send the headers
         * before that user data. */
        pHttpsRequest->pCallbacks->writeCallback( pHttpsRequest->pUserPrivData, pHttpsRequest );
    }

    if( HTTPS_FAILED( pHttpsRequest->bodyTxStatus ) )
    {
        IotLogError( "Failed to send the headers and body over the network during the writeCallback. Error code: %d.",
                     status );
        HTTPS_SET_AND_GOTO_CLEANUP( pHttpsRequest->bodyTxStatus );
    }

    if( pHttpsRequest->cancelled == true )
    {
        IotLogDebug( "Request ID: %d was cancelled.", pHttpsRequest );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_SEND_ABORT );
    }

    /* If this is a synchronous request then the header and body were configured beforehand. The header and body
     * are sent now. For an asynchronous request, the header and body are sent in IotHttpsClient_WriteRequestBody()
     * which is to be invoked in #IotHttpsClientCallbacks_t.writeCallback(). If the application never invokes
     * IotHttpsClient_WriteRequestBody(), then pHttpsRequest->pBody will be NULL. In this case we still want to
     * send whatever headers we have.  */
    if( ( pHttpsRequest->isAsync == false ) ||
        ( ( pHttpsRequest->isAsync ) && ( pHttpsRequest->pBody == NULL ) ) )
    {
        status = _sendHttpsHeadersAndBody( pHttpsConnection, pHttpsRequest );

        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Failed to send the headers and body on the network. Error code: %d", status );
            HTTPS_GOTO_CLEANUP();
        }
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    /* The request has finished sending. This indicates to the network receive callback that the request was
     * finished, so a response received on the network is valid. This also lets a possible application called
     * IotHttpsClient_Disconnect() know that the connection is not busy, so the connection can be destroyed. */
    pHttpsResponse->reqFinishedSending = true;

    if( HTTPS_FAILED( status ) )
    {
        /* If the headers or body failed to send, then there should be no response expected from the server. */
        /* Cancel the response incase there is a response from the server. */
        _cancelResponse( pHttpsResponse );
        IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );

        if( IotLink_IsLinked( &( pHttpsResponse->link ) ) )
        {
            IotDeQueue_Remove( &( pHttpsResponse->link ) );
        }

        IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

        /* Set the error status in the sync workflow. */
        pHttpsResponse->syncStatus = status;

        /* Return the error status or cancel status to the application for an asynchronous workflow. */
        if( pHttpsRequest->isAsync && pHttpsRequest->pCallbacks->errorCallback )
        {
            pHttpsRequest->pCallbacks->errorCallback( pHttpsRequest->pUserPrivData, pHttpsRequest, NULL, status );
        }

        /* We close the connection on all network errors. All network errors in receiving the response, close the
         * connection. For consistency in behavior, if there is a network error in send, the connection should also be
         * closed. */
        if( status == IOT_HTTPS_NETWORK_ERROR )
        {
            IotLogDebug( "Disconnecting request %d.", pHttpsRequest );
            disconnectStatus = IotHttpsClient_Disconnect( pHttpsConnection );

            if( pHttpsRequest->isAsync && pHttpsRequest->pCallbacks->connectionClosedCallback )
            {
                pHttpsRequest->pCallbacks->connectionClosedCallback( pHttpsRequest->pUserPrivData,
                                                                     pHttpsConnection,
                                                                     disconnectStatus );
            }

            if( HTTPS_FAILED( disconnectStatus ) )
            {
                IotLogWarn( "Failed to disconnect request %d. Error code: %d.", pHttpsRequest, disconnectStatus );
            }
        }
        else
        {
            /* Because this request failed, the network receive callback may never be invoked to schedule other possible
             * requests in the queue. In order to avoid requests never getting scheduled on a connected connection,
             * the first item in the queue is scheduled if it can be. */
            IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );

            /* Get the next item in the queue by removing this current (which is the first) and peeking at the head
             * again. */
            IotDeQueue_Remove( &( pHttpsRequest->link ) );
            pQItem = IotDeQueue_PeekHead( &( pHttpsConnection->reqQ ) );
            /* This current request is put back because it is removed again for all cases at the end of this routine. */
            IotDeQueue_EnqueueHead( &( pHttpsConnection->reqQ ), &( pHttpsRequest->link ) );
            IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

            if( pQItem != NULL )
            {
                /* Set this next request to send. */
                pNextHttpsRequest = IotLink_Container( _httpsRequest_t, pQItem, link );

                if( pNextHttpsRequest->scheduled == false )
                {
                    IotLogDebug( "Request %d is next in the queue. Now scheduling a task to send the request.", pNextHttpsRequest );
                    scheduleStatus = _scheduleHttpsRequestSend( pNextHttpsRequest );

                    /* If there was an error with scheduling the new task, then report it. */
                    if( HTTPS_FAILED( scheduleStatus ) )
                    {
                        IotLogError( "Error scheduling HTTPS request %d. Error code: %d", pNextHttpsRequest, scheduleStatus );

                        if( pNextHttpsRequest->isAsync && pNextHttpsRequest->pCallbacks->errorCallback )
                        {
                            pNextHttpsRequest->pCallbacks->errorCallback( pNextHttpsRequest->pUserPrivData, pNextHttpsRequest, NULL, scheduleStatus );
                        }
                        else
                        {
                            pNextHttpsRequest->pHttpsResponse->syncStatus = scheduleStatus;
                        }
                    }
                }
            }
        }

        /* Post to the response finished semaphore to unlock the application waiting on a synchronous request. */
        if( pHttpsRequest->isAsync == false )
        {
            IotSemaphore_Post( &( pHttpsResponse->respFinishedSem ) );
        }
        else if( pHttpsRequest->pCallbacks->responseCompleteCallback )
        {
            /* Call the response complete callback. We always call this even if we did not receive the response to
             * let the application know that the request has completed. */
            pHttpsRequest->pCallbacks->responseCompleteCallback( pHttpsRequest->pUserPrivData, NULL, status, 0 );
        }
    }

    IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );
    /* Now that the current request is finished, we dequeue the current request from the queue. */
    IotDeQueue_DequeueHead( &( pHttpsConnection->reqQ ) );
    IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

    /* This routine returns a void so there is no HTTPS_FUNCTION_CLEANUP_END();. */
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t _scheduleHttpsRequestSend( _httpsRequest_t * pHttpsRequest )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    IotTaskPoolError_t taskPoolStatus = IOT_TASKPOOL_SUCCESS;
    _httpsConnection_t * pHttpsConnection = pHttpsRequest->pHttpsConnection;

    /* Set the request to scheduled even if scheduling fails. */
    pHttpsRequest->scheduled = true;

    taskPoolStatus = IotTaskPool_CreateJob( _sendHttpsRequest,
                                            ( void * ) ( pHttpsRequest ),
                                            &( pHttpsConnection->taskPoolJobStorage ),
                                            &( pHttpsConnection->taskPoolJob ) );

    /* Creating a task pool job should never fail when parameters are valid. */
    if( taskPoolStatus != IOT_TASKPOOL_SUCCESS )
    {
        IotLogError( "Error creating a taskpool job for request servicing. Error code: %d", taskPoolStatus );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INTERNAL_ERROR );
    }

    taskPoolStatus = IotTaskPool_Schedule( IOT_SYSTEM_TASKPOOL, pHttpsConnection->taskPoolJob, 0 );

    if( taskPoolStatus != IOT_TASKPOOL_SUCCESS )
    {
        IotLogError( "Failed to schedule taskpool job. Error code: %d", taskPoolStatus );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_ASYNC_SCHEDULING_ERROR );
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t _addRequestToConnectionReqQ( _httpsRequest_t * pHttpsRequest )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    _httpsConnection_t * pHttpsConnection = pHttpsRequest->pHttpsConnection;
    bool scheduleRequest = false;

    /* Log information about the request*/
    IotLogDebug( "Now queueing request %d.", pHttpsRequest );

    if( pHttpsRequest->isNonPersistent )
    {
        IotLogDebug( "Request %d is non-persistent.", pHttpsRequest );
    }
    else
    {
        IotLogDebug( "Request %d is persistent. ", pHttpsRequest );
    }

    if( pHttpsRequest->isAsync )
    {
        IotLogDebug( " Request %d is asynchronous.", pHttpsRequest );
    }
    else
    {
        IotLogDebug( " Request %d is synchronous.", pHttpsRequest );
    }

    /* This is a new request and has not been scheduled if this routine is called. */
    pHttpsRequest->scheduled = false;

    /* Place the request into the queue. */
    IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );

    /* If there is an active response, scheduling the next request at the same time may corrupt the workflow. Part of
     * the next response for the next request may be present in the currently receiving response's buffers. To avoid
     * this, check if there are pending responses to determine if this request should be scheduled right away or not.
     *
     * If there are other requests in the queue, and there are responses in the queue, then the network receive callback
     * will handle scheduling the next requests (or is already scheduled and currently sending). */
    if( ( IotDeQueue_IsEmpty( &( pHttpsConnection->reqQ ) ) ) &&
        ( IotDeQueue_IsEmpty( &( pHttpsConnection->respQ ) ) ) )
    {
        scheduleRequest = true;
    }

    /* Place into the connection's request to have a taskpool worker schedule to serve it later. */
    IotDeQueue_EnqueueTail( &( pHttpsConnection->reqQ ), &( pHttpsRequest->link ) );
    IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

    if( scheduleRequest )
    {
        /* This routine schedules a task pool worker to send the request. If a worker is available immediately, then
         * the request is sent right away. */
        status = _scheduleHttpsRequestSend( pHttpsRequest );

        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Failed to schedule the request in the queue for request %d. Error code: %d", pHttpsRequest, status );

            /* If we fail to schedule the only request in the queue we should remove it. */
            IotMutex_Lock( &( pHttpsConnection->connectionMutex ) );
            IotDeQueue_Remove( &( pHttpsRequest->link ) );
            IotMutex_Unlock( &( pHttpsConnection->connectionMutex ) );

            HTTPS_GOTO_CLEANUP();
        }
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static void _cancelRequest( _httpsRequest_t * pHttpsRequest )
{
    pHttpsRequest->cancelled = true;
}

/*-----------------------------------------------------------*/

static void _cancelResponse( _httpsResponse_t * pHttpsResponse )
{
    pHttpsResponse->cancelled = true;
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_Init( void )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    /* This sets all member in the _httpParserSettings to zero. It does not return any errors. */
    http_parser_settings_init( &_httpParserSettings );

    /* Set the http-parser callbacks. */
    _httpParserSettings.on_message_begin = _httpParserOnMessageBeginCallback;
    _httpParserSettings.on_status = _httpParserOnStatusCallback;
    _httpParserSettings.on_header_field = _httpParserOnHeaderFieldCallback;
    _httpParserSettings.on_header_value = _httpParserOnHeaderValueCallback;
    _httpParserSettings.on_headers_complete = _httpParserOnHeadersCompleteCallback;
    _httpParserSettings.on_body = _httpParserOnBodyCallback;
    _httpParserSettings.on_message_complete = _httpParserOnMessageCompleteCallback;

/* This code prints debugging information and is, therefore, compiled only when
 * log level is set to IOT_LOG_DEBUG. */
    #if ( LIBRARY_LOG_LEVEL == IOT_LOG_DEBUG )
        _httpParserSettings.on_chunk_header = _httpParserOnChunkHeaderCallback;
        _httpParserSettings.on_chunk_complete = _httpParserOnChunkCompleteCallback;
    #endif
    HTTPS_GOTO_CLEANUP();
    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

static IotHttpsReturnCode_t _initializeResponse( IotHttpsResponseHandle_t * pRespHandle,
                                                 IotHttpsResponseInfo_t * pRespInfo,
                                                 _httpsRequest_t * pHttpsRequest )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    _httpsResponse_t * pHttpsResponse = NULL;

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pRespInfo->userBuffer.pBuffer );

    /* Check of the user buffer is large enough for the response context + default headers. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( pRespInfo->userBuffer.bufferLen >= responseUserBufferMinimumSize,
                                         IOT_HTTPS_INSUFFICIENT_MEMORY,
                                         "Buffer size is too small to initialize the response context. User buffer size: %d, required minimum size; %d.",
                                         pRespInfo->userBuffer.bufferLen,
                                         responseUserBufferMinimumSize );

    /* Initialize the corresponding response to this request. */
    pHttpsResponse = ( _httpsResponse_t * ) ( pRespInfo->userBuffer.pBuffer );

    /* Clear out the response user buffer. This is important because we
     * give the whole buffer to the parser as opposed to the actual content
     * length and rely on the parser to stop when a complete HTTP response
     * is found. To make sure that any data in the buffer which is not part
     * of the received HTTP response, does not get interpreted as part of
     * the HTTP repose, we zero out the buffer here. */
    memset( pRespInfo->userBuffer.pBuffer, 0, pRespInfo->userBuffer.bufferLen );

    pHttpsResponse->pHeaders = ( uint8_t * ) ( pHttpsResponse ) + sizeof( _httpsResponse_t );
    pHttpsResponse->pHeadersEnd = ( uint8_t * ) ( pHttpsResponse ) + pRespInfo->userBuffer.bufferLen;
    pHttpsResponse->pHeadersCur = pHttpsResponse->pHeaders;

    if( pHttpsRequest->isAsync )
    {
        pHttpsResponse->isAsync = true;

        /* For an asynchronous request the response body is provided by the application in the
         * IotHttpsCallbacks_t.readReadyCallback(). These pointers will be updated when IotHttpsClient_ReadResponseBody()
         * is invoked. */
        pHttpsResponse->pBody = NULL;
        pHttpsResponse->pBodyCur = NULL;
        pHttpsResponse->pBodyEnd = NULL;

        pHttpsResponse->pCallbacks = pHttpsRequest->pCallbacks;
        pHttpsResponse->pUserPrivData = pHttpsRequest->pUserPrivData;
    }
    else
    {
        pHttpsResponse->isAsync = false;
        /* The request body pointer is allowed to be NULL. u.pSyncInfo was checked for NULL earlier in this function. */
        pHttpsResponse->pBody = pRespInfo->pSyncInfo->pBody;
        pHttpsResponse->pBodyCur = pHttpsResponse->pBody;
        pHttpsResponse->pBodyEnd = pHttpsResponse->pBody + pRespInfo->pSyncInfo->bodyLen;

        /* Clear out the body bufffer. This is important because we give the
         * whole buffer to the parser as opposed to the actual content length and
         * rely on the parser to stop when a complete HTTP response is found. To
         * make sure that any data in the buffer which is not part of the received
         * HTTP response, does not get interpreted as part of the HTTP repose, we
         * zero out the buffer here. */
        memset( pRespInfo->pSyncInfo->pBody, 0, pRespInfo->pSyncInfo->bodyLen );
    }

    /* Reinitialize the parser and set the fill buffer state to empty. This does not return any errors. */
    http_parser_init( &( pHttpsResponse->httpParserInfo.responseParser ), HTTP_RESPONSE );
    http_parser_init( &( pHttpsResponse->httpParserInfo.readHeaderParser ), HTTP_RESPONSE );
    /* Set the third party http parser function. */
    pHttpsResponse->httpParserInfo.parseFunc = http_parser_execute;
    pHttpsResponse->httpParserInfo.readHeaderParser.data = ( void * ) ( pHttpsResponse );
    pHttpsResponse->httpParserInfo.responseParser.data = ( void * ) ( pHttpsResponse );

    pHttpsResponse->status = 0;
    pHttpsResponse->method = pHttpsRequest->method;
    pHttpsResponse->parserState = PARSER_STATE_NONE;
    pHttpsResponse->bufferProcessingState = PROCESSING_STATE_NONE;
    pHttpsResponse->pReadHeaderField = NULL;
    pHttpsResponse->readHeaderFieldLength = 0;
    pHttpsResponse->pReadHeaderValue = NULL;
    pHttpsResponse->readHeaderValueLength = 0;
    pHttpsResponse->foundHeaderField = 0;
    pHttpsResponse->pHttpsConnection = NULL;

    pHttpsResponse->pBodyInHeaderBuf = NULL;
    pHttpsResponse->pBodyCurInHeaderBuf = NULL;
    pHttpsResponse->bodyRxStatus = IOT_HTTPS_OK;
    pHttpsResponse->cancelled = false;
    pHttpsResponse->syncStatus = IOT_HTTPS_OK;
    /* There is no request associated with this response right now, so it is finished sending. */
    pHttpsResponse->reqFinishedSending = true;
    pHttpsResponse->isNonPersistent = pHttpsRequest->isNonPersistent;

    /* Set the response handle to return. */
    *pRespHandle = pHttpsResponse;

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    if( HTTPS_FAILED( status ) )
    {
        pRespHandle = NULL;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

void IotHttpsClient_Cleanup( void )
{
    /* There is nothing to clean up here as of now. */
}

/* --------------------------------------------------------- */

IotHttpsReturnCode_t IotHttpsClient_Connect( IotHttpsConnectionHandle_t * pConnHandle,
                                             IotHttpsConnectionInfo_t * pConnInfo )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    /* Check for NULL parameters in a public API. */
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pConnHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pConnInfo );

    /* If a valid connection handle is passed in. */
    if( *pConnHandle != NULL )
    {
        /* If the handle in a connected state, then we want to disconnect before reconnecting. The ONLY way to put the
         * handle is a disconnect state is to call IotHttpsClient_Disconnect(). */
        if( ( *pConnHandle )->isConnected )
        {
            status = IotHttpsClient_Disconnect( *pConnHandle );

            if( HTTPS_FAILED( status ) )
            {
                IotLogError( "Error disconnecting a connected *pConnHandle passed to IotHttpsClient_Connect().Error code %d", status );
                *pConnHandle = NULL;
                HTTPS_GOTO_CLEANUP();
            }
        }
    }

    /* Connect to the server now. Initialize all resources needed for the connection context as well here. */
    status = _createHttpsConnection( pConnHandle, pConnInfo );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error in IotHttpsClient_Connect(). Error code %d.", status );
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_Disconnect( IotHttpsConnectionHandle_t connHandle )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    _httpsRequest_t * pHttpsRequest = NULL;
    _httpsResponse_t * pHttpsResponse = NULL;
    IotLink_t * pRespItem = NULL;
    IotLink_t * pReqItem = NULL;

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( connHandle );

    /* If this routine is currently is progress by another thread, for instance the taskpool worker that received a
     * network error after sending, then return right away because connection resources are being used. */
    if( IotMutex_TryLock( &( connHandle->connectionMutex ) ) == false )
    {
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_BUSY );
    }

    /* Do not attempt to disconnect an already disconnected connection.
     * It can happen when a user calls this functions and we return IOT_HTTPS_BUSY. */
    if( connHandle->isConnected )
    {
        /* Mark the network as disconnected whether the disconnect passes or not. */
        connHandle->isConnected = false;
        _networkDisconnect( connHandle );
    }

    /* If there is a response in the connection's response queue and the associated request has not finished sending,
     * then we cannot destroy the connection until it finishes. */
    pRespItem = IotDeQueue_DequeueHead( &( connHandle->respQ ) );

    if( pRespItem != NULL )
    {
        pHttpsResponse = IotLink_Container( _httpsResponse_t, pRespItem, link );

        if( pHttpsResponse->reqFinishedSending == false )
        {
            IotLogError( "Connection is in use. Disconnected, but cannot destroy the connection." );
            status = IOT_HTTPS_BUSY;

            /* The request is busy, to as quickly as possible allow a successful retry call of this function we must
             * cancel the busy request which is the first in the queue. */
            pReqItem = IotDeQueue_PeekHead( &( connHandle->reqQ ) );

            if( pReqItem != NULL )
            {
                pHttpsRequest = IotLink_Container( _httpsRequest_t, pReqItem, link );
                _cancelRequest( pHttpsRequest );
            }

            /* We set the status as busy, but we do not goto the cleanup right away because we still want to remove
             * all pending requests. */
        }

        /* Delete all possible pending responses. (This is defensive.) */
        IotDeQueue_RemoveAll( &( connHandle->respQ ), NULL, 0 );

        /* Put the response that was dequeued back so that the application can call this function again to check later
         * that is exited and marked itself as finished sending.
         * If during the last check and this check reqFinishedSending gets set to true, that is OK because on the next
         * call to this routine, the disconnect will succeed. */
        if( pHttpsResponse->reqFinishedSending == false )
        {
            IotDeQueue_EnqueueHead( &( connHandle->respQ ), pRespItem );
        }
    }

    /* Remove all pending requests. If this routine is called from the application context and there is a
     * network receive callback in process, this routine will wait in _networkDestroy until that routine returns.
     * If this is routine is called from the network receive callback context, then the destroy happens after the
     * network receive callback context returns. */
    IotDeQueue_RemoveAll( &( connHandle->reqQ ), NULL, 0 );

    /* Do not attempt to destroy an already destroyed connection. This can happen when the user calls this function and
     * IOT_HTTPS_BUSY is returned. */
    if( HTTPS_SUCCEEDED( status ) )
    {
        if( connHandle->isDestroyed == false )
        {
            connHandle->isDestroyed = true;
            _networkDestroy( connHandle );
        }
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    /* This function is no longer in process, so disconnecting is no longer in process. This signals to the retry
     * on this function that it can proceed with the disconnecting activities. */
    if( connHandle != NULL )
    {
        IotMutex_Unlock( &( connHandle->connectionMutex ) );
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_InitializeRequest( IotHttpsRequestHandle_t * pReqHandle,
                                                       IotHttpsRequestInfo_t * pReqInfo )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    _httpsRequest_t * pHttpsRequest = NULL;
    size_t additionalLength = 0;
    size_t spaceLen = 1;
    char * pSpace = " ";
    size_t httpsMethodLen = 0;
    size_t httpsProtocolVersionLen = FAST_MACRO_STRLEN( HTTPS_PROTOCOL_VERSION );

    /* Check for NULL parameters in the public API. */
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqInfo );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqInfo->userBuffer.pBuffer );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqInfo->pHost );

    if( pReqInfo->isAsync )
    {
        HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqInfo->u.pAsyncInfo );
    }
    else
    {
        HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pReqInfo->u.pSyncInfo );
    }

    /* Check of the user buffer is large enough for the request context + default headers. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( pReqInfo->userBuffer.bufferLen >= requestUserBufferMinimumSize,
                                         IOT_HTTPS_INSUFFICIENT_MEMORY,
                                         "Buffer size is too small to initialize the request context. User buffer size: %d, required minimum size; %d.",
                                         pReqInfo->userBuffer.bufferLen,
                                         requestUserBufferMinimumSize );

    /* Set the request contet to the start of the userbuffer. */
    pHttpsRequest = ( _httpsRequest_t * ) ( pReqInfo->userBuffer.pBuffer );
    /* Clear out the user buffer. */
    memset( pReqInfo->userBuffer.pBuffer, 0, pReqInfo->userBuffer.bufferLen );

    /* Set the start of the headers to the end of the request context in the user buffer. */
    pHttpsRequest->pHeaders = ( uint8_t * ) pHttpsRequest + sizeof( _httpsRequest_t );
    pHttpsRequest->pHeadersEnd = ( uint8_t * ) pHttpsRequest + pReqInfo->userBuffer.bufferLen;
    pHttpsRequest->pHeadersCur = pHttpsRequest->pHeaders;

    /* Get the length of the HTTP method. */
    httpsMethodLen = strlen( _pHttpsMethodStrings[ pReqInfo->method ] );

    /* Add the request line to the header buffer. */
    additionalLength = httpsMethodLen +          \
                       spaceLen +                \
                       pReqInfo->pathLen +       \
                       spaceLen +                \
                       httpsProtocolVersionLen + \
                       HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;

    if( ( additionalLength + pHttpsRequest->pHeadersCur ) > ( pHttpsRequest->pHeadersEnd ) )
    {
        IotLogError( "Request line does not fit into the request user buffer: \"%s %.*s HTTP/1.1\\r\\n\" . ",
                     _pHttpsMethodStrings[ pReqInfo->method ],
                     pReqInfo->pathLen,
                     pReqInfo->pPath );
        IotLogError( "The length needed is %d and the space available is %d.", additionalLength, pHttpsRequest->pHeadersEnd - pHttpsRequest->pHeadersCur );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INSUFFICIENT_MEMORY );
    }

    /* Write "<METHOD> <PATH> HTTP/1.1\r\n" to the start of the header space. */
    memcpy( pHttpsRequest->pHeadersCur, _pHttpsMethodStrings[ pReqInfo->method ], httpsMethodLen );
    pHttpsRequest->pHeadersCur += httpsMethodLen;
    memcpy( pHttpsRequest->pHeadersCur, pSpace, spaceLen );
    pHttpsRequest->pHeadersCur += spaceLen;

    if( pReqInfo->pPath == NULL )
    {
        pReqInfo->pPath = HTTPS_EMPTY_PATH;
        pReqInfo->pathLen = FAST_MACRO_STRLEN( HTTPS_EMPTY_PATH );
    }

    memcpy( pHttpsRequest->pHeadersCur, pReqInfo->pPath, pReqInfo->pathLen );
    pHttpsRequest->pHeadersCur += pReqInfo->pathLen;
    memcpy( pHttpsRequest->pHeadersCur, pSpace, spaceLen );
    pHttpsRequest->pHeadersCur += spaceLen;
    memcpy( pHttpsRequest->pHeadersCur, HTTPS_PROTOCOL_VERSION, httpsProtocolVersionLen );
    pHttpsRequest->pHeadersCur += httpsProtocolVersionLen;
    memcpy( pHttpsRequest->pHeadersCur, HTTPS_END_OF_HEADER_LINES_INDICATOR, HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH );
    pHttpsRequest->pHeadersCur += HTTPS_END_OF_HEADER_LINES_INDICATOR_LENGTH;

    /* Add the User-Agent header. */
    status = _addHeader( pHttpsRequest, HTTPS_USER_AGENT_HEADER, FAST_MACRO_STRLEN( HTTPS_USER_AGENT_HEADER ), IOT_HTTPS_USER_AGENT, FAST_MACRO_STRLEN( IOT_HTTPS_USER_AGENT ) );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to write header to the request user buffer: \"User-Agent: %s\\r\\n\" . Error code: %d",
                     IOT_HTTPS_USER_AGENT,
                     status );
        HTTPS_GOTO_CLEANUP();
    }

    status = _addHeader( pHttpsRequest, HTTPS_HOST_HEADER, FAST_MACRO_STRLEN( HTTPS_HOST_HEADER ), pReqInfo->pHost, pReqInfo->hostLen );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to write \"Host: %.*s\\r\\n\" to the request user buffer. Error code: %d",
                     pReqInfo->hostLen,
                     pReqInfo->pHost,
                     status );
        HTTPS_GOTO_CLEANUP();
    }

    if( pReqInfo->isAsync )
    {
        pHttpsRequest->isAsync = true;
        /* If this is an asynchronous request then save the callbacks to use. */
        pHttpsRequest->pCallbacks = &( pReqInfo->u.pAsyncInfo->callbacks );
        pHttpsRequest->pUserPrivData = pReqInfo->u.pAsyncInfo->pPrivData;
        /* The body pointer and body length will be filled in when the application sends data in the writeCallback. */
        pHttpsRequest->pBody = NULL;
        pHttpsRequest->bodyLength = 0;
    }
    else
    {
        pHttpsRequest->isAsync = false;
        /* Set the HTTP request entity body. This is allowed to be NULL for no body like for a GET request. */
        pHttpsRequest->pBody = pReqInfo->u.pSyncInfo->pBody;
        pHttpsRequest->bodyLength = pReqInfo->u.pSyncInfo->bodyLen;
    }

    /* Save the method of this request. */
    pHttpsRequest->method = pReqInfo->method;
    /* Set the connection persistence flag for keeping the connection open after receiving a response. */
    pHttpsRequest->isNonPersistent = pReqInfo->isNonPersistent;
    /* Initialize the request cancellation. */
    pHttpsRequest->cancelled = false;
    /* Initialize the status of sending the body over the network in a possible asynchronous request. */
    pHttpsRequest->bodyTxStatus = IOT_HTTPS_OK;
    /* This is a new request and therefore not scheduled yet. */
    pHttpsRequest->scheduled = false;

    /* Set the request handle to return. */
    *pReqHandle = pHttpsRequest;

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    if( HTTPS_FAILED( status ) && ( pReqHandle != NULL ) )
    {
        /* Set the request handle to return to NULL, if we failed anywhere. */
        *pReqHandle = NULL;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_AddHeader( IotHttpsRequestHandle_t reqHandle,
                                               char * pName,
                                               uint32_t nameLen,
                                               char * pValue,
                                               uint32_t valueLen )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    /* Check for NULL pointer paramters. */
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pName );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pValue );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( reqHandle );

    /* Check for name long enough for header length calculation to overflow */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( nameLen <= ( UINT32_MAX >> 2 ),
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to generate headers with name length %d > %d. This is not allowed.",
                                         nameLen, UINT32_MAX >> 2 );

    /* Check for value long enough for header length calculation to overflow */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( valueLen <= ( UINT32_MAX >> 2 ),
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to generate headers with value length %d > %d. This is not allowed.",
                                         valueLen, UINT32_MAX >> 2 );

    /* Check for auto-generated header "Content-Length". This header is created and send automatically when right before
     * request body is sent on the network. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( strncmp( pName, HTTPS_CONTENT_LENGTH_HEADER, FAST_MACRO_STRLEN( HTTPS_CONTENT_LENGTH_HEADER ) ) != 0,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to add auto-generated header %s. This is not allowed.",
                                         HTTPS_CONTENT_LENGTH_HEADER );

    /* Check for auto-generated header "Connection". This header is created and send automatically when right before
     * request body is sent on the network. */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( strncmp( pName, HTTPS_CONNECTION_HEADER, FAST_MACRO_STRLEN( HTTPS_CONNECTION_HEADER ) ) != 0,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to add auto-generated header %s. This is not allowed.",
                                         HTTPS_CONNECTION_HEADER );

    /* Check for auto-generated header "Host". This header is created and placed into the header buffer space
     * in IotHttpsClient_InitializeRequest(). */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( strncmp( pName, HTTPS_HOST_HEADER, FAST_MACRO_STRLEN( HTTPS_HOST_HEADER ) ) != 0,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to add auto-generated header %s. This is not allowed.",
                                         HTTPS_HOST_HEADER );

    /* Check for auto-generated header "User-Agent". This header is created and placed into the header buffer space
     * in IotHttpsClient_InitializeRequest(). */
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( strncmp( pName, HTTPS_USER_AGENT_HEADER, FAST_MACRO_STRLEN( HTTPS_USER_AGENT_HEADER ) ) != 0,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "Attempting to add auto-generated header %s. This is not allowed.",
                                         HTTPS_USER_AGENT_HEADER );


    status = _addHeader( reqHandle, pName, nameLen, pValue, valueLen );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Error in IotHttpsClient_AddHeader(), error code %d.", status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_SendSync( IotHttpsConnectionHandle_t connHandle,
                                              IotHttpsRequestHandle_t reqHandle,
                                              IotHttpsResponseHandle_t * pRespHandle,
                                              IotHttpsResponseInfo_t * pRespInfo,
                                              uint32_t timeoutMs )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    bool respFinishedSemCreated = false;
    _httpsResponse_t * pHttpsResponse = NULL;

    /* Parameter checks. */
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( connHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( reqHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pRespHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pRespInfo );
    /* Stop the application from scheduling requests on a closed connection. */
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( connHandle->isConnected );

    /* If an asynchronous request/response is configured, that is invalid for this API. */
    if( reqHandle->isAsync )
    {
        IotLogError( "Called IotHttpsClient_SendSync on an asynchronous configured request." );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INVALID_PARAMETER );
    }

    /* Initialize the response handle to return. */
    status = _initializeResponse( pRespHandle, pRespInfo, reqHandle );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to initialize the response on the synchronous request %d.", reqHandle );
        HTTPS_GOTO_CLEANUP();
    }

    /* Set the internal response to use. */
    pHttpsResponse = *pRespHandle;

    /* The implicit connection passed and we need to the set the connection handle in the request and response. */
    reqHandle->pHttpsConnection = connHandle;
    pHttpsResponse->pHttpsConnection = connHandle;

    /* Create the semaphore used to wait on the response to finish being received. */
    respFinishedSemCreated = IotSemaphore_Create( &( pHttpsResponse->respFinishedSem ), 0 /* initialValue */, 1 /* maxValue */ );

    if( respFinishedSemCreated == false )
    {
        IotLogError( "Failed to create an internal semaphore." );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INTERNAL_ERROR );
    }

    /* Associate the response to the request so that we can schedule it to be received when the request gets scheduled to send. */
    reqHandle->pHttpsResponse = pHttpsResponse;

    /* Schedule this request to be sent by adding it to the connection's request queue. */
    status = _addRequestToConnectionReqQ( reqHandle );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to schedule the synchronous request. Error code: %d", status );
        HTTPS_GOTO_CLEANUP();
    }

    /* Wait for the request to finish. */
    if( timeoutMs == 0 )
    {
        IotSemaphore_Wait( &( pHttpsResponse->respFinishedSem ) );
    }
    else
    {
        if( IotSemaphore_TimedWait( &( pHttpsResponse->respFinishedSem ), timeoutMs ) == false )
        {
            IotLogError( "Timed out waiting for the synchronous request to finish. Timeout ms: %d", timeoutMs );
            _cancelRequest( reqHandle );
            HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_TIMEOUT_ERROR );
        }
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    if( respFinishedSemCreated )
    {
        IotSemaphore_Destroy( &( pHttpsResponse->respFinishedSem ) );
    }

    /* If the syncStatus is anything other than IOT_HTTPS_OK, then the request was scheduled. */
    if( ( pHttpsResponse != NULL ) && HTTPS_FAILED( pHttpsResponse->syncStatus ) )
    {
        status = pHttpsResponse->syncStatus;
    }

    if( HTTPS_FAILED( status ) )
    {
        if( pRespHandle != NULL )
        {
            *pRespHandle = NULL;
        }

        IotLogError( "IotHttpsClient_SendSync() failed." );
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_WriteRequestBody( IotHttpsRequestHandle_t reqHandle,
                                                      uint8_t * pBuf,
                                                      uint32_t len,
                                                      int isComplete )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( reqHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pBuf );

    /* This function is not valid for a synchronous response. Applications need to configure the request body in
     * IotHttpsRequestInfo_t.pSyncInfo_t.reqData before calling IotHttpsClient_SendSync(). */
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( reqHandle->isAsync );
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( isComplete == 1,
                                         IOT_HTTPS_NOT_SUPPORTED,
                                         "isComplete must be 1 in IotHttpsClient_WriteRequestBody() for the current version of the HTTPS Client library." );

    /* If the bodyLength is greater than 0, then we already called this function and we need to enforce that this
     * function must only be called once. We only call this function once so that we can calculate the Content-Length. */
    if( reqHandle->bodyLength > 0 )
    {
        IotLogError( "Error this function must be called once with the data needed to send. Variable length HTTP "
                     "request body is not supported in this library." );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_MESSAGE_FINISHED );
    }

    /* Set the pointer to the body and the length for the content-length calculation. */
    reqHandle->pBody = ( uint8_t * ) pBuf;
    reqHandle->bodyLength = len;

    /* We send the HTTPS headers and body in this function so that the application has the freedom to specify a body
     * that may be buffer on stack. */
    status = _sendHttpsHeadersAndBody( reqHandle->pHttpsConnection, reqHandle );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to send the headers and body. Error code %d.", status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    if( reqHandle != NULL )
    {
        reqHandle->bodyTxStatus = status;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_ReadResponseBody( IotHttpsResponseHandle_t respHandle,
                                                      uint8_t * pBuf,
                                                      uint32_t * pLen )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    uint32_t bodyLengthInHeaderBuf = 0;

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( respHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pBuf );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pLen );
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( respHandle->isAsync );

    /* Set the current body in the respHandle to use in _receiveHttpsBody(). _receiveHttpsBody is generic
     *  to both async and sync request/response handling. In the sync version the body is configured during
     *  initializing the request. In the async version the body is given in this function on the fly. */
    respHandle->pBody = pBuf;
    respHandle->pBodyCur = respHandle->pBody;
    respHandle->pBodyEnd = respHandle->pBodyCur + *pLen;

    /* When there is part of the body in the header pBuffer. We need to move that data to this body pBuffer
     * provided in this function. */
    bodyLengthInHeaderBuf = respHandle->pBodyCurInHeaderBuf - respHandle->pBodyInHeaderBuf;

    if( bodyLengthInHeaderBuf > 0 )
    {
        uint32_t copyLength = bodyLengthInHeaderBuf > *pLen ? *pLen : bodyLengthInHeaderBuf;
        memcpy( respHandle->pBodyCur, respHandle->pBodyInHeaderBuf, copyLength );
        respHandle->pBodyCur += copyLength;

        /* This function may be called multiple times until all of the body that may be present in the header buffer is
         * moved out. */
        respHandle->pBodyInHeaderBuf += copyLength;
    }

    /* If there is room in the body buffer just provided by the application and we have not completed the current
     * HTTP response message, then try to receive more body. */
    if( ( ( respHandle->pBodyEnd - respHandle->pBodyCur ) > 0 ) && ( respHandle->parserState < PARSER_STATE_BODY_COMPLETE ) )
    {
        status = _receiveHttpsBody( respHandle->pHttpsConnection, respHandle );

        if( HTTPS_FAILED( status ) )
        {
            IotLogError( "Failed to receive the HTTP response body on the network. Error code: %d.", status );
            HTTPS_GOTO_CLEANUP();
        }
    }

    *pLen = respHandle->pBodyCur - respHandle->pBody;

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    if( respHandle != NULL )
    {
        respHandle->bodyRxStatus = status;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_CancelRequestAsync( IotHttpsRequestHandle_t reqHandle )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( reqHandle );

    _cancelRequest( reqHandle );

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_CancelResponseAsync( IotHttpsResponseHandle_t respHandle )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( respHandle );

    _cancelResponse( respHandle );

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}


/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_SendAsync( IotHttpsConnectionHandle_t connHandle,
                                               IotHttpsRequestHandle_t reqHandle,
                                               IotHttpsResponseHandle_t * pRespHandle,
                                               IotHttpsResponseInfo_t * pRespInfo )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( connHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( reqHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pRespHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pRespInfo );
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( reqHandle->isAsync );
    /* Stop the application from scheduling requests on a closed connection. */
    HTTPS_ON_ARG_ERROR_GOTO_CLEANUP( connHandle->isConnected );

    /* Initialize the response handle to return. */
    status = _initializeResponse( pRespHandle, pRespInfo, reqHandle );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to initialize the response on the synchronous request %d.", reqHandle );
        HTTPS_GOTO_CLEANUP();
    }

    /* Set the connection handle in the request handle so that we can use it in the _writeRequestBody() callback. */
    reqHandle->pHttpsConnection = connHandle;

    /* Set the connection handle in the response handle sp that we can use it in the _readReadyCallback() callback. */
    ( *pRespHandle )->pHttpsConnection = connHandle;

    /* Associate the response to the request so that we can schedule it to be received when the request gets scheduled to send. */
    reqHandle->pHttpsResponse = *pRespHandle;

    /* Add the request to the connection's request queue. */
    status = _addRequestToConnectionReqQ( reqHandle );

    if( HTTPS_FAILED( status ) )
    {
        IotLogError( "Failed to add request %d to the connection's request queue. Error code: %d.", reqHandle, status );
        HTTPS_GOTO_CLEANUP();
    }

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_ReadResponseStatus( IotHttpsResponseHandle_t respHandle,
                                                        uint16_t * pStatus )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( respHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pStatus );

    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( respHandle->status != 0,
                                         IOT_HTTPS_NOT_FOUND,
                                         "The HTTP response status was not found in the HTTP response header buffer." );

    *pStatus = respHandle->status;

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_ReadHeader( IotHttpsResponseHandle_t respHandle,
                                                char * pName,
                                                uint32_t nameLen,
                                                char * pValue,
                                                uint32_t valueLen )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    const char * pHttpParserErrorDescription = NULL;
    IotHttpsResponseBufferState_t savedBufferState = PROCESSING_STATE_NONE;
    IotHttpsResponseParserState_t savedParserState = PARSER_STATE_NONE;
    size_t numParsed = 0;

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( respHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pName );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pValue );
    HTTPS_ON_ARG_ERROR_MSG_GOTO_CLEANUP( valueLen > 0,
                                         IOT_HTTPS_INVALID_PARAMETER,
                                         "pValue has insufficient space to store a value string (length is 0)" );

    /* The buffer processing state is changed to searching the header buffer in this function. The parser state is
    * changed in the response to wherever the parser is currently located in the response. If this function is called
    * in the middle of processing a response (for example in readReadyCallback() routine of an asynchronous response),
    * then parsing the response need to be able to start at the same place it was before calling this function. */
    savedBufferState = respHandle->bufferProcessingState;
    savedParserState = respHandle->parserState;

    /* The header search parameters in the response handle are used as context in the http-parser callbacks. During
     * the callback, pReadHeaderField is checked against the currently parsed header name. foundHeaderField is set to
     * true when the pReadHeaderField is found in a header field callback. The bufferProcessingState tells the callback
     * to skip the logic pertaining to when the response is being parsed for the first time. pReadHeaderValue will store
     * the header value found. readHeaderValueLength will store the length of the header value found from within the
     * response headers. */
    respHandle->pReadHeaderField = pName;
    respHandle->readHeaderFieldLength = nameLen;
    respHandle->foundHeaderField = false;
    respHandle->bufferProcessingState = PROCESSING_STATE_SEARCHING_HEADER_BUFFER;
    respHandle->pReadHeaderValue = NULL;
    respHandle->readHeaderValueLength = 0;

    /* Start over the HTTP parser so that it will parser from the beginning of the message. */
    http_parser_init( &( respHandle->httpParserInfo.readHeaderParser ), HTTP_RESPONSE );

    IotLogDebug( "Now parsing HTTP Message buffer to read a header." );
    numParsed = respHandle->httpParserInfo.parseFunc( &( respHandle->httpParserInfo.readHeaderParser ), &_httpParserSettings, ( char * ) ( respHandle->pHeaders ), respHandle->pHeadersCur - respHandle->pHeaders );
    IotLogDebug( "Parsed %d characters in IotHttpsClient_ReadHeader().", numParsed );

    /* There shouldn't be any errors parsing the response body given that the handle is from a validly
     * received response, so this check is defensive. If there were errors parsing the original response headers, then
     * the response handle would have been invalidated and the connection closed. */
    if( ( respHandle->httpParserInfo.readHeaderParser.http_errno != 0 ) &&
        ( HTTP_PARSER_ERRNO( &( respHandle->httpParserInfo.readHeaderParser ) ) > HPE_CB_chunk_complete ) )
    {
        pHttpParserErrorDescription = http_errno_description( HTTP_PARSER_ERRNO( &( respHandle->httpParserInfo.readHeaderParser ) ) );
        IotLogError( "http_parser failed on the http response with error: %s", pHttpParserErrorDescription );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_PARSING_ERROR );
    }

    /* Not only do we need an indication that the header field was found, but also that the value was found as well.
     * The value is found when it is non-NULL. The case where the header field is found, but the value is not found
     * occurs when there are incomplete headers stored in the header buffer. The header buffer could end with a header
     * field name. */
    if( respHandle->foundHeaderField && ( respHandle->pReadHeaderValue != NULL ) )
    {
        /* The len of the pValue buffer must account for the NULL terminator. */
        if( respHandle->readHeaderValueLength > ( valueLen - 1 ) )
        {
            IotLogError( "IotHttpsClient_ReadHeader(): The length of the pValue buffer specified is less than the actual length of the pValue. " );
            HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_INSUFFICIENT_MEMORY );
        }
        else
        {
            memcpy( pValue, respHandle->pReadHeaderValue, respHandle->readHeaderValueLength );
            pValue[ respHandle->readHeaderValueLength ] = '\0';
        }
    }
    else
    {
        IotLogWarn( "IotHttpsClient_ReadHeader(): The header field %s was not found.", pName );
        HTTPS_SET_AND_GOTO_CLEANUP( IOT_HTTPS_NOT_FOUND );
    }

    HTTPS_FUNCTION_CLEANUP_BEGIN();

    /* Always restore the state back to what it was before entering this function. */
    if( respHandle != NULL )
    {
        respHandle->bufferProcessingState = savedBufferState;
        respHandle->parserState = savedParserState;
    }

    HTTPS_FUNCTION_CLEANUP_END();
}

/*-----------------------------------------------------------*/

IotHttpsReturnCode_t IotHttpsClient_ReadContentLength( IotHttpsResponseHandle_t respHandle,
                                                       uint32_t * pContentLength )
{
    HTTPS_FUNCTION_ENTRY( IOT_HTTPS_OK );

    const int CONTENT_LENGTH_NUMBERIC_BASE = 10;
    char pContentLengthStr[ HTTPS_MAX_CONTENT_LENGTH_LINE_LENGTH ] = { 0 };

    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( respHandle );
    HTTPS_ON_NULL_ARG_GOTO_CLEANUP( pContentLength );

    /* If there is no content-length header or if we were not able to store it in the header buffer this will be
     * invalid. We do not use the content-length member of the http-parser state structure to get the content
     * length as this is a PRIVATE member. Because it is a PRIVATE member it can be any value. */
    status = IotHttpsClient_ReadHeader( respHandle, HTTPS_CONTENT_LENGTH_HEADER, FAST_MACRO_STRLEN( HTTPS_CONTENT_LENGTH_HEADER ), pContentLengthStr, HTTPS_MAX_CONTENT_LENGTH_LINE_LENGTH );

    if( HTTPS_FAILED( status ) )
    {
        *pContentLength = 0;
        IotLogError( "Could not read the Content-Length for the response." );
        HTTPS_GOTO_CLEANUP();
    }

    *pContentLength = strtoul( pContentLengthStr, NULL, CONTENT_LENGTH_NUMBERIC_BASE );

    HTTPS_FUNCTION_EXIT_NO_CLEANUP();
}

/*-----------------------------------------------------------*/

/* Provide access to internal functions and variables if testing. */
#if IOT_BUILD_TESTS == 1
    #include "iot_test_access_https_client.c"
#endif