summaryrefslogtreecommitdiffstats
path: root/httprl.module
blob: a13bc0a4740f6494fd750f3b274ddee8fdd999ff (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
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
<?php
/**
 * @file
 * HTTP Parallel Request Library module.
 */

/**
 * Default value
 */
define('HTTPRL_BACKGROUND_CALLBACK', TRUE);

/**
 * Default maximum number of seconds a single request call may take.
 */
define('HTTPRL_TIMEOUT', 30.0);

/**
 * Default maximum number of seconds the DNS portion of a request may take.
 */
define('HTTPRL_DNS_TIMEOUT', 5.0);

/**
 * Default maximum number of seconds establishing the TCP connection of a
 * request may take.
 */
define('HTTPRL_CONNECT_TIMEOUT', 5.0);

/**
 * Default maximum number of seconds a connection may take to download the first
 * byte.
 */
define('HTTPRL_TTFB_TIMEOUT', 20.0);

/**
 * Default maximum number of seconds a function call may take.
 */
define('HTTPRL_GLOBAL_TIMEOUT', 120.0);

/**
 * Error code indicating that the request made by httprl_request() exceeded
 * the maximum allowed redirects without reaching the final target.
 */
define('HTTPRL_REQUEST_ALLOWED_REDIRECTS_EXHAUSTED', -2);

/**
 * Error code indicating that the call to fwrite() failed.
 */
define('HTTPRL_REQUEST_FWRITE_FAIL', -3);

/**
 * Error code indicating that all requests made by httprl_send_request
 * exceeded the specified timeout.
 */
define('HTTPRL_FUNCTION_TIMEOUT', -4);

/**
 * Error code indicating that this request made by stream_select() couldn't
 * open a read and/or write to any stream after a minimum of ~10 seconds.
 */
define('HTTPRL_STREAM_SELECT_TIMEOUT', -5);

/**
 * parse_url() was unable to parse the given url.
 */
define('HTTPRL_URL_PARSE_ERROR', -1001);

/**
 * Given URL is missing a schema (http, https, feed).
 */
define('HTTPRL_URL_MISSING_SCHEMA', -1002);

/**
 * Invalid schema. Only http, feed, and https allowed currently.
 */
define('HTTPRL_URL_INVALID_SCHEMA', -1003);

/**
 * An error occurred before the system connect() call. This is most likely due
 * to a problem initializing the stream.
 */
define('HTTPRL_ERROR_INITIALIZING_STREAM', -1004);

/**
 * Error code indicating that software caused the connection to be aborted.
 *
 * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx
 */
define('HTTPRL_REQUEST_ABORTED', -10053);

/**
 * Error code indicating that the connection was forcibly closed by the remote
 * host.
 *
 * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx
 */
define('HTTPRL_CONNECTION_RESET', -10054);

/**
 * Error code indicating that the request exceeded the specified timeout.
 *
 * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx
 */
define('HTTPRL_REQUEST_TIMEOUT', -10060);

/**
 * Error code indicating that the endpoint server has refused or dropped the
 * connection.
 *
 * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx
 */
define('HTTPRL_CONNECTION_REFUSED', -10061);

/**
 * Error code indicating that the host is unknown or can not be found.
 *
 * @see http://msdn.microsoft.com/en-us/library/aa924071.aspx
 */
define('HTTPRL_HOST_NOT_FOUND', -11001);

/**
 * HTTP encapsulation boundary string.
 */
define('HTTPRL_MULTIPART_BOUNDARY', '---------------------------' . str_replace('.', '', microtime(TRUE)));

/**
 * Max length of a string inside of httprl_pr(). Default is 256KB.
 */
define('HTTPRL_PR_MAX_STRING_LENGTH', 262144);

/**
 * Run httprl_url_inbound_alter().
 */
define('HTTPRL_URL_INBOUND_ALTER', TRUE);

/**
 * What to set the server schema to when doing a self request.
 */
define('HTTPRL_SERVER_SCHEMA', 0);

/**
 * Wait X ms after finishing a non blocking request.
 */
define('HTTPRL_NON_BLOCKING_FCLOSE_DELAY', 0);

/**
 * Default port value; blank, so do not set one.
 */
define('HTTPRL_SERVER_PORT', '');

/**
 * Implements hook_url_inbound_alter().
 */
function httprl_url_inbound_alter(&$path, $original_path, $path_language) {
  // Do nothing if this has been disabled.
  if (!variable_get('httprl_url_inbound_alter', HTTPRL_URL_INBOUND_ALTER)) {
    return;
  }

  // If requested path was for an async callback but now it is something else
  // switch is back to the requested path.
  $request_path = request_path();
  if ($path != $request_path && strpos($request_path, 'httprl_async_function_callback') !== FALSE) {
    $path = $request_path;
  }
}

/**
 * Implements hook_menu().
 */
function httprl_menu() {
  $items = array();

  // Admin page.
  if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
    $config_url = 'admin/config/development/httprl';
  }
  else {
    $config_url = 'admin/settings/httprl';
  }
  $items[$config_url] = array(
    'title' => 'HTTPRL',
    'description' => 'Configure HTTPRL settings.',
    'access arguments' => array('administer site configuration'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('httprl_admin_settings_form'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'httprl.admin.inc',
  );

  // Async Function Callback.
  $items['httprl_async_function_callback'] = array(
    'title' => 'HTTPRL',
    'page callback' => 'httprl_async_page',
    'access callback' => TRUE,
    'description' => 'URL for async function workers.',
    'type' => MENU_CALLBACK,
    'file' => 'httprl.async.inc',
  );

  return $items;
}

/**
 * Implements hook_cron().
 *
 * This hook should be ran about once an hour to once every 5 minutes.
 */
function httprl_cron() {
  // Let expiration times vary by 5 minutes.
  $fuzz_factor = 300;

  // Remove expired locks from the semaphore database table.
  if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
    db_delete('semaphore')
      ->condition('value', 'httprl')
      ->condition('expire', REQUEST_TIME - $fuzz_factor, '<')
      ->execute();
  }
  else {
    db_query("DELETE FROM {semaphore} WHERE value = 'httprl' AND expire < %f", time() - $fuzz_factor);
  }

  // Let expiration times vary by 60 minutes.
  $fuzz_factor = 3600;

  // Remove expired locks from the semaphore database table.
  if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
    db_delete('semaphore')
      ->condition('name', db_like('httprl_') . '%', 'LIKE')
      ->condition('expire', REQUEST_TIME - $fuzz_factor, '<')
      ->execute();
  }
  else {
    db_query("DELETE FROM {semaphore} WHERE name LIKE 'httprl_%' AND expire < %f", time() - $fuzz_factor);
  }
}

/**
 * Queue and send off http request.
 *
 * @see drupal_http_request()
 *
 * This is a flexible and powerful HTTP client implementation. Correctly
 * handles GET, POST, PUT or any other HTTP requests.
 *
 * @param string $url
 *   A string containing a fully qualified URI.
 * @param array $options
 *   (optional) An array of options.
 *
 * @return object
 *   The request object.
 */
function httprl_override_core($url, $options = array()) {
  // Clear out older requests if needed.
  httprl_send_request();
  // Queue up the request.
  httprl_request($url, $options);
  // Execute request.
  $response = httprl_send_request();
  // Send back results.
  return is_array($response) && is_string($url) && array_key_exists($url, $response) ? $response[$url] : is_array($response) ? array_pop($response) : $response;
}

/**
 * Helper function to build an URL for asynchronous requests to self.
 *
 * @param int $level
 *   How deep to go when setting the base path.
 * @param int $hostname_mode
 *   Force a hostname type.
 *
 * @return string
 *   URL pointing to this server.
 */
function _httprl_build_drupal_root($level = 0, $hostname_mode = 0) {
  static $webroot;
  $root_path = '/';
  if ($level > 0) {
    // Work backwards from this file till we find drupal's index.php.
    if (!isset($webroot)) {
      $webroot = str_replace('\\', '/', dirname(__FILE__));
      while (!empty($webroot)) {
        if (file_exists($webroot . '/index.php') && strpos(file_get_contents($webroot . '/index.php'), 'menu_execute_active_handler();') !== FALSE) {
          break;
        }
        $new_webroot = str_replace('\\', '/', dirname($webroot));
        if ($new_webroot == $webroot) {
          $webroot = str_replace('\\', '/', getcwd());
          break;
        }
        $webroot = $new_webroot;
      }
    }

    $root_path = '';
    $webroot_array = explode('/', $webroot);
    while ($level > 0 && count($webroot_array) != 0) {
      $level--;
      $root_path = array_pop($webroot_array) . '/' . $root_path;
    }
    $root_path = '/' . $root_path;
    $root_path = str_replace('//', '/', $root_path);
  }
  else {
    if (!empty($GLOBALS['base_path'])) {
      $root_path = $GLOBALS['base_path'];
    }
  }

  // Server auth.
  $auth = '';
  if (module_exists('shield')) {
    $auth = variable_get('shield_user', '') . ':' . variable_get('shield_pass', '') . '@';
  }
  elseif ((isset($_SERVER['AUTH_TYPE']) && $_SERVER['AUTH_TYPE'] == 'Basic')
      || (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION']))
  ) {
    $auth = $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] . '@';
  }
  // Use static for gethostbyname lookup.
  static $dns_cache;

  $hostname = httprl_get_hostname();
  // Get Host.
  $ip = httprl_variable_get('httprl_server_addr', FALSE);
  if ($ip == -1 || $hostname_mode == 1) {
    $ip = $hostname;
    // If the host is bad don't use it.
    if ((is_callable('drupal_is_cli') && drupal_is_cli()) || (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)))) {
      if (!isset($dns_cache[$hostname])) {
        $dns_cache[$hostname] = gethostbyname($hostname);
      }
      if ($dns_cache[$hostname] == $hostname) {
        $ip = '';
      }
    }
  }
  if (empty($ip) || $hostname_mode == 2) {
    $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
    // Check for IPv6. If IPv6 convert to IPv4 if possible.
    if (strpos($ip, ':') !== FALSE) {
      if ($_SERVER['SERVER_ADDR'] == '::1') {
        $ip = "127.0.0.1";
      }
      elseif (preg_match('/^::\d+.\d+.\d+.\d+$/', $ip)) {
        $ip = substr($ip, 2);
      }
      elseif (!empty($hostname)) {
        // Last option is to use the IP from the host name.
        if (!isset($dns_cache[$hostname])) {
          $dns_cache[$hostname] = gethostbyname($hostname);
        }
        $ip = $dns_cache[$hostname];
        if ($dns_cache[$hostname] == $hostname) {
          $ip = '';
        }
      }
    }
  }
  if ($hostname_mode == 3) {
    $ip = httprl_variable_get('httprl_server_hostname', FALSE);
  }
  if (empty($ip) || $hostname_mode == 4) {
    $ip = '127.0.0.1';
  }

  // Port.
  $port = httprl_variable_get('httprl_server_port', HTTPRL_SERVER_PORT);
  // Add the port separator if necessary.
  if (!empty($port)) {
    $port = ':' . $port;
  }
  elseif ( isset($_SERVER['SERVER_PORT'])
        && is_numeric($_SERVER['SERVER_PORT'])
        && $_SERVER['SERVER_PORT'] != 80
        && $_SERVER['SERVER_PORT'] != 443
  ) {
    $port = ':' . $_SERVER['SERVER_PORT'];
  }

  // URL schema http or https.
  $schema_var = httprl_variable_get('httprl_server_schema', HTTPRL_SERVER_SCHEMA);
  if ($schema_var == 0) {
    $schema = httprl_get_server_schema() . '://';
  }
  elseif ($schema_var == 1) {
    $schema = 'http://';
  }
  elseif ($schema_var == 2) {
    $schema = 'https://';
  }

  // Special handling if clean urls are disabled.
  if (!variable_get('clean_url', 0)) {
    $path_parts = @parse_url('http://example.com/' . $path);
    if (!empty($path_parts)) {
      $path_parts_query = array();
      if (isset($path_parts['query'])) {
        parse_str($path_parts['query'], $path_parts_query);
      }
      $path_parts_query['q'] = ltrim($path_parts['path'], '/');
      $path = '?' . http_build_query($path_parts_query, '', '&');
    }
  }

  return $schema . $auth . $ip . $port . $root_path;
}

/**
 * Helper function to build an URL for asynchronous requests to self.
 *
 * @param string $path
 *   Path to a URI excluding everything to the left and including the base path.
 * @param bool $detect_schema
 *   If TRUE it will see if this request is https; if so, it will set the full
 *   url to be https as well.
 * @param bool $reset
 *   If TRUE the $drupal_root static will be reset.
 *
 * @return string
 *   The URL that points to this Drupal install.
 */
function httprl_build_url_self($path = '', $detect_schema = FALSE,  $reset = FALSE) {
  static $drupal_root;
  if (!isset($drupal_root) || $reset) {
    $drupal_root = _httprl_build_drupal_root();

    // If ran from the command line, the drupal root might be in a subdir. Test to
    // make sure we have the right directory.
    if ((is_callable('drupal_is_cli') && drupal_is_cli()) || (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)))) {
      $level = 0;
      $found = FALSE;
      $mode = 0;
      while (!$found) {
        // Trick due to not knowing the subdir.
        // http://stackoverflow.com/questions/8361355/get-apache-document-root-from-command-line-execution-no-browser/8415235#8415235
        // Issue http request and get the headers.
        $override_function = httprl_variable_get('drupal_http_request_function', FALSE);
        $GLOBALS['conf']['drupal_http_request_function'] = FALSE;
        $test_request = drupal_http_request($drupal_root . 'httprl_async_function_callback');
        $GLOBALS['conf']['drupal_http_request_function'] = $override_function;

        // Look for the X-HTTPRL header.
        $headers = isset($test_request->headers) ? $test_request->headers : array();
        if (!empty($headers)) {
          foreach ($headers as $key => $value) {
            if (stripos($key, 'X-HTTPRL') !== FALSE || stripos($value, 'X-HTTPRL') !== FALSE) {
              $found = TRUE;
              break;
            }
          }
        }
        // If the header is not found, adjust sub dir and try again.
        if (!$found) {
          $level++;
          $new_drupal_root = _httprl_build_drupal_root($level, $mode);
          if ($new_drupal_root == $drupal_root) {
            // If the header is not found, adjust hostname and try again.
            $mode++;
            $level = 0;
            if ($mode > 4) {
              // Use no subdirectories if nothing worked.
              $drupal_root = _httprl_build_drupal_root();
              break;
            }
          }
          $drupal_root = $new_drupal_root;
        }
      }
    }
  }

  return $drupal_root . $path;
}

/**
 * Run parse_url and handle any errors.
 *
 * @param string $url
 *   String containing the URL to be parsed by parse_url().
 * @param object &$result
 *   Result object; used only for error handling in this function.
 *
 * @return array
 *   Array from parse_url().
 */
function httprl_parse_url($url, &$result) {
  // Parse the URL and make sure we can handle the schema.
  $uri = @parse_url($url);

  // If the t function is not available use httprl_pr.
  $t = function_exists('t') ? 't' : 'httprl_pr';

  if (empty($uri)) {
    // Set error code for failed request.
    $result->error = $t('Unable to parse URL.');
    $result->code = HTTPRL_URL_PARSE_ERROR;
  }
  elseif (!isset($uri['scheme'])) {
    // Set error code for failed request.
    $result->error = $t('Missing schema.');
    $result->code = HTTPRL_URL_MISSING_SCHEMA;
  }

  return $uri;
}

/**
 * Set the default options in the $options array.
 *
 * @param array &$options
 *   Array containing options.
 */
function httprl_set_default_options(array &$options) {
  global $base_root;

  // Merge the default options.
  $options += array(
    'headers' => array(),
    'method' => 'GET',
    'data' => NULL,
    'max_redirects' => 3,
    'timeout' => httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT),
    'dns_timeout' => httprl_variable_get('httprl_dns_timeout', HTTPRL_DNS_TIMEOUT),
    'connect_timeout' => httprl_variable_get('httprl_connect_timeout', HTTPRL_CONNECT_TIMEOUT),
    'ttfb_timeout' => httprl_variable_get('httprl_ttfb_timeout', HTTPRL_TTFB_TIMEOUT),
    'context' => NULL,
    'secure_socket_transport' => 'ssl',
    'blocking' => TRUE,
    'version' => '1.0',
    'referrer' => FALSE,
    'domain_connections' => 2,
    'global_connections' => 128,
    'global_timeout' => httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
    'chunk_size_read' => 32768,
    'chunk_size_write' => 1024,
    'async_connect' => TRUE,
    'ping_db' => 20,
    'url_encoding' => array(),
  );

  // Adjust Time To First Byte Timeout if timeout is large and ttfb is default.
  if ($options['timeout'] > httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT) && $options['ttfb_timeout'] == httprl_variable_get('httprl_ttfb_timeout', HTTPRL_TTFB_TIMEOUT)) {
    $options['ttfb_timeout'] = $options['timeout'] - max(1, httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT) - httprl_variable_get('httprl_ttfb_timeout', HTTPRL_TTFB_TIMEOUT));
  }
  // Adjust Global Timeout if timeout is large and global_timeout is default.
  if ($options['timeout'] > httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT) && $options['global_timeout'] == httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT)) {
    $options['global_timeout'] = $options['timeout'] + max(1, httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT) - httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT));
  }

  // Merge the default headers.
  // Set user agent to drupal.
  // Set connection to closed to prevent keep-alive from causing a timeout.
  $options['headers'] += array(
    'User-Agent' => 'Drupal (+http://drupal.org/)',
    'Connection' => 'close',
  );

  // Defaults for url encoding the url.
  // Use %20 for spaces in the path and in the query string.
  $options['url_encoding'] += array(
    'space' => array(),
  );
  $options['url_encoding']['space'] += array(
    'path' => '%20',
    'query' => '%20',
  );

  // Set referrer to current page.
  if (!isset($options['headers']['Referer']) && !empty($options['referrer'])) {
    if (function_exists('request_uri')) {
      $options['headers']['Referer'] = $base_root . request_uri();
    }
  }

  // stream_socket_client() requires timeout to be a float.
  $options['timeout'] = (float) $options['timeout'];
}

/**
 * If server uses a proxy, change the request to utilize said proxy.
 *
 * @param array &$uri
 *   Array from parse_url().
 * @param array &$options
 *   Array containing options.
 * @param string $url
 *   String containing the URL.
 *
 * @return string
 *   String containing the proxy servers host name if one is to be used.
 */
function httprl_setup_proxy(&$uri, &$options, $url) {
  // Proxy setup.
  $proxy_server = httprl_variable_get('proxy_server', '');
  // Use a proxy if one is defined and the host is not on the excluded list.
  if ($proxy_server && _httprl_use_proxy($uri['host'])) {
    // Set the scheme so we open a socket to the proxy server.
    $uri['scheme'] = 'proxy';
    // Set the path to be the full URL.
    $uri['path'] = $url;
    // Since the full URL is passed as the path, we won't use the parsed query.
    unset($uri['query']);

    // Add in username and password to Proxy-Authorization header if needed.
    if ($proxy_username = httprl_variable_get('proxy_username', '')) {
      $proxy_password = httprl_variable_get('proxy_password', '');
      $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . ':' . $proxy_password);
    }
    // Some proxies reject requests with any User-Agent headers, while others
    // require a specific one.
    $proxy_user_agent = httprl_variable_get('proxy_user_agent', '');
    // The default value matches neither condition.
    if (is_null($proxy_user_agent)) {
      unset($options['headers']['User-Agent']);
    }
    elseif ($proxy_user_agent) {
      $options['headers']['User-Agent'] = $proxy_user_agent;
    }
  }
  return $proxy_server;
}

/**
 * Create the TCP/SSL socket connection string.
 *
 * @param array $uri
 *   Array from parse_url().
 * @param array &$options
 *   Array containing options.
 * @param string $proxy_server
 *   String containing the proxy servers host name if one is to be used.
 * @param object &$result
 *   Result object; used only for error handling in this function.
 *
 * @return string
 *   String containing the TCP/SSL socket connection URI.
 */
function httprl_set_socket($uri, &$options, $proxy_server, &$result) {
  $socket = '';
  switch ($uri['scheme']) {
    case 'proxy':
      // Make the socket connection to a proxy server.
      $socket = 'tcp://' . $proxy_server . ':' . httprl_variable_get('proxy_port', 8080);
      // The Host header still needs to match the real request.
      $options['headers']['Host'] = $uri['host'];
      $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
      break;

    case 'http':
    case 'feed':
      $port = isset($uri['port']) ? $uri['port'] : 80;
      $socket = 'tcp://' . $uri['host'] . ':' . $port;
      // RFC 2616: "non-standard ports MUST, default ports MAY be included".
      // We don't add the standard port to prevent from breaking rewrite rules
      // checking the host that do not take into account the port number.
      if (empty($options['headers']['Host'])) {
        $options['headers']['Host'] = $uri['host'];
      }
      if ($port != 80) {
        $options['headers']['Host'] .= ':' . $port;
      }
      break;

    case 'https':
      // Note: Only works when PHP is compiled with OpenSSL support.
      $port = isset($uri['port']) ? $uri['port'] : 443;
      $socket = $options['secure_socket_transport'] . '://' . $uri['host'] . ':' . $port;
      if (empty($options['headers']['Host'])) {
        $options['headers']['Host'] = $uri['host'];
      }
      if ($port != 443) {
        $options['headers']['Host'] .= ':' . $port;
      }

      // Disable SNI support as this causes issues with old versions of OpenSSL.
      // By default httprl doesn't validate the SSL certificate, so this is OK.
      if (empty($options['context'])) {
        $drupal_ssl_context_options = variable_get('drupal_ssl_context_options', array('verify_peer' => TRUE));
        // Affected versions of openssl are 1.0.0i to 1.0.1b.
        if (!defined('OPENSSL_VERSION_NUMBER') || (OPENSSL_VERSION_NUMBER >= 0x1000009f && OPENSSL_VERSION_NUMBER <= 0x1000102f)) {
          $drupal_ssl_context_options += array('SNI_enabled' => FALSE);
        }
        $options['context'] = stream_context_create(array('ssl' => $drupal_ssl_context_options));
      }
      break;

    default:
      // If the t function is not available use httprl_pr.
      $t = function_exists('t') ? 't' : 'httprl_pr';

      $result->error = $t('Invalid schema @scheme.', array('@scheme' => $uri['scheme']));
      $result->code = HTTPRL_URL_INVALID_SCHEMA;

  }

  return $socket;
}

/**
 * Select which connect flags to use in stream_socket_client().
 *
 * @param array &$options
 *   Array containing options.
 * @param array $uri
 *   Array from parse_url().
 *
 * @return int
 *   STREAM_CLIENT_CONNECT or STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT.
 */
function httprl_set_connection_flag(&$options, $uri) {
  $flags = STREAM_CLIENT_CONNECT;

  // Set connection flag.
  if ($options['async_connect']) {
    // Workaround for PHP bug with STREAM_CLIENT_ASYNC_CONNECT and SSL
    // https://bugs.php.net/bug.php?id=48182 - Fixed in PHP 5.2.11 and 5.3.1
    if ($uri['scheme'] == 'https' && (version_compare(PHP_VERSION, '5.2.11', '<') || version_compare(PHP_VERSION, '5.3.0', '='))) {
      $options['async_connect'] = FALSE;
    }
    else {
      $flags = STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT;
    }
  }
  return $flags;
}

/**
 * If data is being sent out in this request, handle it correctly.
 *
 * If $options['data'] is not a string, convert it to a string using
 * http_build_query(). Set the Content-Length header correctly. Set the
 * Content-Type to application/x-www-form-urlencoded if not already set and
 * using method is POST.
 *
 * @todo
 *   Proper mime support.
 *
 * @param array &$options
 *   Array containing options.
 */
function httprl_handle_data(&$options) {
  // Encode data if not already done.
  if (isset($options['data']) && !is_string($options['data'])) {
    // Record raw data before it gets processed.
    $options['data-input'] = $options['data'];

    if (!empty($options['headers']['Content-Type']) && strpos($options['headers']['Content-Type'], 'multipart/related') === 0 && !empty($options['data'])) {
      // Trim semicolon from Content-Type header if needed.
      $options['headers']['Content-Type'] = trim($options['headers']['Content-Type']);
      if (substr_compare($options['headers']['Content-Type'], ';', -1, 1) === 0) {
        $options['headers']['Content-Type'] = substr($options['headers']['Content-Type'], -1);
      }
      // Add in boundary.
      $options['headers']['Content-Type'] .= '; boundary=' . HTTPRL_MULTIPART_BOUNDARY;

      $data_stream = '';
      foreach ($options['data'] as $part) {
        $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "\r\n";
        foreach ($part['headers'] as $key => $value) {
          $data_stream .= $key . ': ' . $value . "\r\n";
        }
        $data_stream .= "\r\n";
        if (isset($part['file'])) {
          $data_stream .= file_get_contents($part['file']) . "\r\n";
        }
        elseif (isset($part['string'])) {
          $data_stream .= $part['string'] . "\r\n";
        }
      }

      // Signal end of request (note the trailing "--").
      $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "--\r\n";
      $options['data'] = $data_stream;
    }
    // No files passed in, url-encode the data.
    elseif (empty($options['data']['files']) || !is_array($options['data']['files'])) {
      $options['data'] = http_build_query($options['data'], '', '&');

      // Set the Content-Type to application/x-www-form-urlencoded if the data
      // is not empty, the Content-Type is not set, and the method is POST or
      // PUT.
      if (!empty($options['data']) && !isset($options['headers']['Content-Type']) && ($options['method'] == 'POST' || $options['method'] == 'PUT')) {
        $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
      }
    }
    else {
      $data_stream = '';
      // Add files to the request.
      foreach ($options['data']['files'] as $field_name => $info) {
        $multi_field = '[]';
        // Convert $info into an array if it's a string.
        // This makes for one code path (the foreach loop).
        if (is_string($info)) {
          $multi_field = '';
          $temp = $info;
          unset($info);
          $info[] = $temp;
        }
        foreach ($info as $fullpath) {
          // Strip '@' from the start of the path (cURL requirement).
          if (substr($fullpath, 0, 1) == "@") {
            $fullpath = substr($fullpath, 1);
          }
          $filename = basename($fullpath);
          // TODO: mime detection.
          $mimetype = 'application/octet-stream';

          // Build the data-stream for this file.
          $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "\r\n";
          $data_stream .= 'Content-Disposition: form-data; name="files[' . $field_name . ']' . $multi_field . '"; filename="' . $filename . "\"\r\n";
          $data_stream .= 'Content-Transfer-Encoding: binary' . "\r\n";
          $data_stream .= 'Content-Type: ' . $mimetype . "\r\n\r\n";
          $data_stream .= file_get_contents($fullpath) . "\r\n";
        }
      }
      // Remove files from the data array as they have already been added.
      $data_array = $options['data'];
      unset($data_array['files']);
      // Add fields to the request too: $_POST['foo'] = 'bar'.
      httprl_multipart_encoder($data_stream, $data_array);

      // Signal end of request (note the trailing "--").
      $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "--\r\n";
      $options['data'] = $data_stream;

      // Set the Content-Type to multipart/form-data if the data is not empty,
      // the Content-Type is not set, and the method is POST or PUT.
      if (!empty($options['data']) && !isset($options['headers']['Content-Type']) && ($options['method'] == 'POST' || $options['method'] == 'PUT')) {
        $options['headers']['Content-Type'] = 'multipart/form-data; boundary=' . HTTPRL_MULTIPART_BOUNDARY;
      }
    }
  }

  // Only add Content-Length if we actually have any content or if it is a POST
  // or PUT request. Some non-standard servers get confused by Content-Length in
  // at least HEAD/GET requests, and Squid always requires Content-Length in
  // POST/PUT requests.
  if (strlen($options['data']) > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
    $options['headers']['Content-Length'] = httprl_strlen($options['data']);
  }
}

/**
 * Multipart encode a data array.
 *
 * PHP has http_build_query() which will url-encode data. There is no built in
 * function to multipart encode data thus we have this function below.
 *
 * @param string &$data_stream
 *   Appended with all multi-part headers.
 * @param array $data_array
 *   Array of data in key => value pairs.
 * @param array $prepend
 *   (optional) key => values pairs to prepend to $data_array.
 */
function httprl_multipart_encoder(&$data_stream, $data_array, $prepend = array()) {
  foreach ($data_array as $key => $value) {
    $key_array = $prepend;
    $key_array[] = $key;
    if (is_array($value)) {
      httprl_multipart_encoder($data_stream, $value, $key_array);
    }
    elseif (is_scalar($value)) {
      $key_string = array_shift($key_array);
      if (!empty($key_array)) {
        $key_string .= '[' . implode('][', $key_array) . ']';
      }
      $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "\r\n";
      $data_stream .= 'Content-Disposition: form-data; name="' . $key_string . "\"\r\n\r\n";
      $data_stream .= $value . "\r\n";
    }
  }
}

/**
 * Set the Authorization header if a user is set in the URI.
 *
 * @param array $uri
 *   Array from parse_url().
 * @param array &$options
 *   Array containing options.
 */
function httprl_basic_auth($uri, &$options) {
  // If the server URL has a user then attempt to use basic authentication.
  if (isset($uri['user'])) {
    $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . ':' . (isset($uri['pass']) ? $uri['pass'] : ''));
  }
}

/**
 * Build the request string.
 *
 * This string is what gets sent to the server once a connection has been made.
 *
 * @param array $uri
 *   Array from parse_url().
 * @param array $options
 *   Array containing options.
 *
 * @return string
 *   String containing the data that will be written to the server.
 */
function httprl_build_request_string($uri, $options) {
  // Construct the path to act on.
  // Enocde any spaces to be %20.
  $path = isset($uri['path']) ? str_replace(' ', $options['url_encoding']['space']['path'], $uri['path']) : '/';
  if (isset($uri['query'])) {
    // Enocde any spaces in the query string to be %20.
    $path .= '?' . str_replace(' ', $options['url_encoding']['space']['query'], $uri['query']);
  }

  // Assemble the request together. HTTP version requires to be a float.
  $request = $options['method'] . ' ' . $path . ' HTTP/' . sprintf("%.1F", $options['version']) . "\r\n";
  foreach ($options['headers'] as $name => $value) {
    $request .= $name . ': ' . trim($value) . "\r\n";
  }

  return $request . "\r\n" . $options['data'];
}


/**
 * Read the error number & string and give a nice looking error in the output.
 *
 * This is a flexible and powerful HTTP client implementation. Correctly
 * handles GET, POST, PUT or any other HTTP requests.
 *
 * @param int $errno
 *   Error number from stream_socket_client().
 * @param string $errstr
 *   Error string from stream_socket_client().
 * @param object $result
 *   An object for httprl_send_request.
 */
function httprl_stream_connection_error_formatter($errno, $errstr, &$result) {
  // If the t function is not available use httprl_pr.
  $t = function_exists('t') ? 't' : 'httprl_pr';
  if (function_exists('t')) {
    // Make sure drupal_convert_to_utf8() is available.
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      require_once DRUPAL_ROOT . '/includes/unicode.inc';
    }
    else {
      require_once './includes/unicode.inc';
    }

    // Convert error message to utf-8. Using ISO-8859-1 (Latin-1) as source
    // encoding could be wrong; it is a simple workaround :)
    $errstr = trim(drupal_convert_to_utf8($errstr, 'ISO-8859-1'));
  }

  if (!$errno) {
    // If $errno is 0, it is an indication that the error occurred
    // before the connect() call.
    if (empty($errstr)) {
      // If the error string is empty as well, this is most likely due to a
      // problem initializing the stream.
      $result->code = HTTPRL_ERROR_INITIALIZING_STREAM;
      $result->error = $t('Error initializing socket @socket.', array('@socket' => $result->socket));
    }
    elseif (stripos($errstr, 'network_getaddresses: getaddrinfo failed:') !== FALSE) {
      // Host not found. No such host is known. The name is not an official host
      // name or alias.
      $result->code = HTTPRL_HOST_NOT_FOUND;
      $result->error = $errstr;
    }
  }
  elseif ($errno == 110) {
    // 110 means Connection timed out. This should be HTTPRL_REQUEST_TIMEOUT.
    $result->code = HTTPRL_REQUEST_TIMEOUT;
    $result->error = !empty($errstr) ? $errstr : $t('Connection timed out. TCP.');
  }
  else {
    // When a network error occurs, we use a negative number so it does not
    // clash with the HTTP status codes.
    $result->code = (int) - $errno;
    $result->error = !empty($errstr) ? $errstr : $t('Error opening socket @socket.', array('@socket' => $result->socket));
  }
}

/**
 * Use stream_socket_client() to create a connection to the server.
 *
 * @param object $result
 *   A object to hold the result values.
 */
function httprl_establish_stream_connection(&$result) {
  // Record start time.
  $start_time = microtime(TRUE);
  $result->fp = FALSE;

  // Try to make a connection, 3 max tries in loop.
  $count = 0;
  while (!$result->fp && $count < 3) {
    // Try the connection again not using async if in https mode.
    if ($count > 0) {
      if ($result->flags === STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT && $result->uri['scheme'] == 'https') {
        $result->flags = STREAM_CLIENT_CONNECT;
        $result->options['async_connect'] = FALSE;
      }
      else {
        // Break out of while loop if we can't connect.
        break;
      }
    }

    // Set the DNS timeout.
    $timeout = $result->options['dns_timeout'];
    // If not using async_connect then add connect_timeout to timeout.
    if (!$result->options['async_connect']) {
      $timeout += $result->options['connect_timeout'];
    }

    // Open the connection.
    if (empty($result->options['context'])) {
      $result->fp = @stream_socket_client($result->socket, $errno, $errstr, $timeout, $result->flags);
    }
    else {
      // Create a stream with context. Context allows for the verification of
      // a SSL certificate.
      $result->fp = @stream_socket_client($result->socket, $errno, $errstr, $timeout, $result->flags, $result->options['context']);
    }
    $count++;
  }

  // Make sure the stream opened properly. This check doesn't work if
  // async_connect is used, so only check it if async_connect is FALSE. Making
  // sure that stream_socket_get_name returns a "TRUE" value.
  if (   $result->fp
      && !$result->options['async_connect']
      && !stream_socket_get_name($result->fp, TRUE)
        ) {
    $errno = HTTPRL_CONNECTION_REFUSED;
    $errstr = 'Connection refused. No connection could be made because the target machine actively refused it.';
    $result->fp = FALSE;
  }

  // Report any errors or set the stream to non blocking mode.
  if (!$result->fp) {
    httprl_stream_connection_error_formatter($errno, $errstr, $result);
  }
  else {
    stream_set_blocking($result->fp, 0);
  }

  // Record end time.
  $end_time = microtime(TRUE);
  $extra = 0;
  if (isset($result->options['internal_states']['running_time'])) {
    $extra = $result->options['internal_states']['running_time'];
    unset($result->options['internal_states']['running_time']);
  }
  $result->running_time = $end_time - $start_time + $extra;
}

/**
 * Queue up a HTTP request in httprl_send_request.
 *
 * @see drupal_http_request()
 *
 * This is a flexible and powerful HTTP client implementation. Correctly
 * handles GET, POST, PUT or any other HTTP requests.
 *
 * @param string|array $urls
 *   A string or an array containing a fully qualified URI(s).
 * @param array $options
 *   (optional) An array that can have one or more of the following elements:
 *   - headers: An array containing request headers to send as name/value pairs.
 *     Some of the more useful headers:
 *     - For POST: 'Content-Type' => 'application/x-www-form-urlencoded',
 *     - Limit number of bytes server sends back: 'Range' => 'bytes=0-1024',
 *     - Compression: 'Accept-Encoding' => 'gzip, deflate',
 *     - Let server know where request came from: 'Referer' => 'example.com',
 *     - Content-Types that are acceptable: 'Accept' => 'text/plain',
 *     - Send Cookies: 'Cookie' => 'key1=value1; key2=value2;',
 *     - Skip the cache: 'Cache-Control' => 'no-cache',
 *     - Skip the cache: 'Pragma' => 'no-cache',
 *     List of headers: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
 *   - method: A string containing the request method. Defaults to 'GET'.
 *   - data: A string containing the request body, formatted as
 *     'param=value&param=value&...'. Defaults to NULL.
 *   - max_redirects: An integer representing how many times a redirect
 *     may be followed. Defaults to 3.
 *   - timeout: A float representing the maximum number of seconds a connection
 *     may take. The default is 30 seconds. If a timeout occurs, the error code
 *     is set to the HTTPRL_REQUEST_ABORTED constant.
 *   - dns_timeout: A float representing the maximum number of seconds a DNS
 *     lookup request may take. The default is 5 seconds. If a timeout occurs,
 *     the error code is set to the HTTPRL_HOST_NOT_FOUND constant.
 *   - connect_timeout: A float representing the maximum number of seconds
 *     establishing the TCP connection may take. The default is 5 seconds. If a
 *     timeout occurs, the error code is set to the HTTPRL_REQUEST_TIMEOUT
 *     constant.
 *   - ttfb_timeout: A float representing the maximum number of seconds a
 *     connection may take to download the first byte. The default is 20
 *     seconds. If a timeout occurs, the error code is set to the
 *     HTTPRL_REQUEST_ABORTED constant.
 *   - context: A context resource created with stream_context_create().
 *   - secure_socket_transport: The transport to use when making secure
 *     requests over HTTPS; see http://php.net/manual/en/transports.inet.php
 *     for more information. The value should be 'ssl', 'sslv2', 'sslv3' or
 *     'tls'. Defaults to 'ssl', which will work for HTTPS requests to most
 *     remote servers.
 *   - blocking: set to FALSE to make this not care about the returned data.
 *   - version: HTTP Version 1.0 or 1.1. Default is 1.0 for a good reason.
 *   - referrer: TRUE - send current page; FALSE - do not send current
 *     page. Default is FALSE.
 *   - domain_connections: Maximum number of simultaneous connections to a given
 *     domain name. Default is 8.
 *   - global_connections: Maximum number of simultaneous connections that can
 *     be open on the server. Default is 128.
 *   - global_timeout: A float representing the maximum number of seconds the
 *     function call may take. If a timeout occurs,the error code is set to the
 *     HTTPRL_FUNCTION_TIMEOUT constant. Default is 120 seconds.
 *   - chunk_size_write: max size of what will be written in fwrite().
 *   - chunk_size_read: max size of what will be read from fread().
 *   - async_connect: default is TRUE. FALSE may give more info on errors but is
 *     generally slower.
 *   - callback: Array where the first value is an array of options; the result
 *     is passed to the callback function as the first argument, the other
 *     options passed in this array are passed in after the result. The options
 *     array needs to contain the function name and the target variable for the
 *     result of the function.
 *   - background_callback: Array where the first value is an array of options;
 *     the result is passed to the callback function as the first argument, the
 *     other options passed in this array are passed in after the result. The
 *     options array needs to contain the function name. If the return or
 *     printed keys are not defined this function will run in non blocking mode
 *     and the parent will not be able to get the result; if the return or
 *     printed keys defined then this function will run in blocking mode and the
 *     returned and printed data as well as any variables passed by reference
 *     will be available to the parent.
 *   - alter_all_streams_function: Function name. This function runs at the end
 *     of httprl_post_processing() so that one can alter the $responses and
 *     $output variables inside of httprl_send_request. Defined function
 *     should have the following parameters:
 *       ($id, &$responses).
 *   - stall_fread: TRUE or FALSE. If true once all fwrites have been done
 *     httprl_send_request() will return. You will need to call
 *     httprl_send_request() a second time to read the responses back.
 *   - ping_db: After X amount of time, ping the DB with a simple query in order
 *     to keep the connection alive. Default is every 20 seconds. Set to 0 to
 *     disable.
 *
 * @return array
 *   Array where key is the URL and the value is the return value from
 *   httprl_send_request.
 */
function httprl_request($urls, array $options = array()) {
  // See if a full bootstrap has been done.
  $full_bootstrap = httprl_drupal_full_bootstrap();

  // Transform string to an array.
  if (!is_array($urls)) {
    $temp = &$urls;
    unset($urls);
    $urls = array(&$temp);
    unset($temp);
  }

  if ($full_bootstrap) {
    // Allow other modules to alter things before we get started.
    // Run hook_pre_httprl_request_alter().
    $data = array($urls, $options);
    drupal_alter('pre_httprl_request', $data);
    list($urls, $options) = $data;
  }

  $connections = array();
  $return = array();
  // Set things up; but do not perform any IO.
  foreach ($urls as &$url) {
    $result = new stdClass();
    $result->url = &$url;
    $result->status = 'Connecting.';
    $result->code = 0;
    $result->chunk_size = 1024;
    $result->data = '';

    // Copy Options.
    $these_options = $options;

    // Setup the default options.
    httprl_set_default_options($these_options);

    // Parse the given URL and skip if an error occurred.
    $uri = httprl_parse_url($url, $result);
    if (isset($result->error)) {
      // Put all variables into an array for easy alterations.
      $connections[] = array(NULL, NULL, $uri, $url, $these_options, $result, NULL);
      $return[$url] = FALSE;
      // Stop processing this request as we have encountered an error.
      continue;
    }

    // Set the proxy server if one is required.
    $proxy_server = httprl_setup_proxy($uri, $these_options, $url);
    // Create the socket string and skip if an error occurred.
    $socket = httprl_set_socket($uri, $these_options, $proxy_server, $result, $return, $url);
    if (isset($result->error)) {
      // Put all variables into an array for easy alterations.
      $connections[] = array($socket, NULL, $uri, $url, $these_options, $result, NULL);
      $return[$url] = FALSE;
      // Stop processing this request as we have encountered an error.
      continue;
    }

    // Use a sync or async connection.
    $flags = httprl_set_connection_flag($these_options, $uri);
    // Set basic authorization header if needed.
    httprl_basic_auth($uri, $these_options);
    // If any data is given, do the right things to this request so it works.
    httprl_handle_data($these_options);
    // Build the request string.
    $response = httprl_build_request_string($uri, $these_options);

    // Put all variables into an array for easy alterations.
    $connections[] = array($socket, $flags, $uri, $url, $these_options, $result, $response);
    $return[$url] = TRUE;
  }

  $results = array();
  foreach ($connections as $connection) {
    list($socket, $flags, $uri, $url, $options, $result, $response) = $connection;
    $result->request = $response;
    $result->options = $options;
    $result->socket = $socket;
    $result->flags = $flags;
    $result->uri = $uri;
    $result->running_time = 0;
    $results[] = $result;
  }

  if ($full_bootstrap) {
    // Allow other programs to alter the connections before they are made.
    // run hook_httprl_request_alter().
    drupal_alter('httprl_request', $results);
  }

  if (httprl_send_request($results)) {
    return $return;
  }
  else {
    return FALSE;
  }
}

/**
 * Perform many HTTP requests.
 *
 * @see drupal_http_request()
 *
 * This is a flexible and powerful HTTP client implementation. Correctly
 * handles GET, POST, PUT or any other HTTP requests.
 *
 * @param $results
 *   (optional) Array of results.
 *
 * @return bool
 *   TRUE if function worked as planed.
 */
function httprl_send_request($results = NULL) {
  static $responses = array();
  static $counter = 0;
  static $output = array();
  static $static_stall_freads = FALSE;

  if (!is_null($results)) {
    if (empty($results)) {
      return FALSE;
    }
    // Put the connection information into the responses array.
    foreach ($results as $result) {
      $responses[$counter] = $result;
      $counter++;
    }
    return TRUE;
  }

  // Exit if there is nothing to do.
  if (empty($responses)) {
    return FALSE;
  }

  // If the t function is not available use httprl_pr.
  $t = function_exists('t') ? 't' : 'httprl_pr';

  // Remove errors from responses array and set the global timeout.
  $global_timeout = 1;
  $global_connection_limit = 1;
  $stall_freads = FALSE;
  foreach ($responses as $id => &$result) {
    if (!empty($result->error)) {
      $result->status = 'Connection not made.';
      // Do post processing on the stream.
      httprl_post_processing($id, $responses, $output);
      continue;
    }

    if (!empty($result->cached)) {
      // Used the cached data.
      $output[$result->cached->url] = $result->cached;
      unset($responses[$id]);
      continue;
    }

    // Get connection limits.
    $global_connection_limit = max($global_connection_limit, $result->options['global_connections']);
    if (!isset($domain_connection_limit[$result->options['headers']['Host']])) {
      $domain_connection_limit[$result->options['headers']['Host']] = max(1, $result->options['domain_connections']);
    }
    else {
      $domain_connection_limit[$result->options['headers']['Host']] = max($domain_connection_limit[$result->options['headers']['Host']], $result->options['domain_connections']);
    }

    // Set global timeout.
    $global_timeout = max($global_timeout, $result->options['global_timeout']);

    // Issue fwrite, return. Run fread later on in the script.
    if (!empty($result->options['stall_fread']) && !$static_stall_freads) {
      $static_stall_freads = TRUE;
      $stall_freads = TRUE;
    }
  }

  // Record start time.
  $start_time_this_run = $start_time_global = microtime(TRUE);
  // Record the number of db pings done.
  $ping_db_counts = array();
  $full_bootstrap = httprl_drupal_full_bootstrap();

  // Run the loop as long as we have a stream to read/write to.
  $stream_select_timeout = 1;
  $stream_write_count = 0;

  while (!empty($responses)) {

    // Initialize connection limits.
    $this_run = array();
    $global_connection_count = 0;
    $domain_connection_count = array();
    $restart_timers = FALSE;

    // Get time.
    $now = microtime(TRUE);

    // Calculate times.
    $elapsed_time = $now - $start_time_this_run;
    $start_time_this_run = $now;
    $global_time = $global_timeout - ($start_time_this_run - $start_time_global);

    // See if the DB needs to be pinged.
    $rounded_time = floor($elapsed_time);
    if (   $full_bootstrap
        && !empty($result->options['ping_db'])
        && $rounded_time >= $result->options['ping_db']
        && $rounded_time % $result->options['ping_db'] == 0
        && empty($ping_db_counts[$rounded_time])
          ) {
      $empty_array = array();
      system_get_files_database($empty_array, 'ping_db');
      $ping_db_counts[$rounded_time] = 1;
    }

    // Inspect each stream, checking for timeouts and connection limits.
    foreach ($responses as $id => &$result) {
      // See if function timed out.
      if ($global_time <= 0) {
        // Function timed out & the request is not done.
        if ($result->status == 'Connecting.') {
          $result->error = $t('Function timed out. TCP.');
          // If stream is not done writing, then remove one from the write count.
          if (isset($result->fp)) {
            $stream_write_count--;
          }
        }
        elseif ($result->status == 'Writing to server.') {
          $result->error = $t('Function timed out. Write.');
          // If stream is not done writing, then remove one from the write count.
          if (isset($result->fp)) {
            $stream_write_count--;
          }
        }
        else {
          $result->error = $t('Function timed out. Read');
        }
        $result->code = HTTPRL_FUNCTION_TIMEOUT;
        $result->status = 'Done.';

        // Do post processing on the stream and close it.
        httprl_post_processing($id, $responses, $output, $global_time);
        continue;
      }
      // Do not calculate local timeout if a file pointer doesn't exist.
      if (isset($result->fp)) {
        // Add the elapsed time to this stream.
        $result->running_time += $elapsed_time;
        // Calculate how much time is left of the original timeout value.
        $timeout = $result->options['timeout'] - $result->running_time;

        // Connection was dropped or connection timed out.
        if ($timeout <= 0) {
          $result->error = $t('Connection timed out.');
          // Stream timed out & the request is not done.
          if ($result->status == 'Writing to server.') {
            $result->error .= ' ' . $t('Write.');
            // If stream is not done writing, then remove one from the write count.
            $stream_write_count--;
          }
          else {
            $result->error .= ' ' . $t('Read.');
          }
          $result->code = HTTPRL_REQUEST_TIMEOUT;
          $result->status = 'Done.';

          // Do post processing on the stream.
          httprl_post_processing($id, $responses, $output, $timeout);
          continue;
        }

        // Connection was dropped or connection timed out.
        if ($result->status == 'Connecting.' && $result->running_time > $result->options['connect_timeout']) {
          $socket_name = stream_socket_get_name($result->fp, TRUE);
          if (empty($socket_name) || $result->running_time > ($result->options['connect_timeout'] * 1.5)) {
            $result->error = $t('Connection timed out.');
            // Stream timed out & the request is not done.
            if ($result->status == 'Connecting.') {
              $result->error .= ' ' . $t('TCP Connect Timeout.');
              // If stream is not done writing, then remove one from the write count.
              $stream_write_count--;
            }
            $result->code = HTTPRL_REQUEST_TIMEOUT;
            $result->status = 'Done.';

            // Do post processing on the stream.
            httprl_post_processing($id, $responses, $output, $timeout);
            continue;
          }
        }

        if (!isset($responses[$id]->time_to_first_byte) && $result->running_time > $result->options['ttfb_timeout']) {
          $result->error = $t('Connection timed out. Time to First Byte Timeout.');
          $result->code = HTTPRL_REQUEST_ABORTED;
          $result->status = 'Done.';

          // Do post processing on the stream.
          httprl_post_processing($id, $responses, $output, $timeout);
          continue;
        }
      }

      // Connection was handled elsewhere.
      if (!isset($result->fp) && $result->status != 'Connecting.') {
        // Do post processing on the stream.
        httprl_post_processing($id, $responses, $output);
        continue;
      }

      // Set the connection limits for this run.
      // Get the host name.
      $host = $result->options['headers']['Host'];
      // Set the domain connection limit if none has been defined yet.
      if (!isset($domain_connection_limit[$host])) {
        $domain_connection_limit[$host] = max(1, $result->options['domain_connections']);
      }
      // Count up the number of connections.
      $global_connection_count++;
      if (empty($domain_connection_count[$host])) {
        $domain_connection_count[$host] = 1;
      }
      else {
        $domain_connection_count[$host]++;
      }
      // If the conditions are correct, let the stream be ran in this loop.
      if ($global_connection_limit >= $global_connection_count && $domain_connection_limit[$host] >= $domain_connection_count[$host]) {
        // Establish a new connection.
        if (!isset($result->fp) && $result->status == 'Connecting.') {
          // Establish a connection to the server.
          httprl_establish_stream_connection($result);

          // Reset timer.
          $restart_timers = TRUE;

          // Get lock if needed.
          if (!empty($result->options['lock_name'])) {
            httprl_acquire_lock($result);
          }

          // If connection can not be established bail out here.
          if (!$result->fp) {
            // Do post processing on the stream.
            httprl_post_processing($id, $responses, $output);
            $domain_connection_count[$host]--;
            $global_connection_count--;
            continue;
          }
          $stream_write_count++;

        }
        if (!empty($result->fp)) {
          $this_run[$id] = $result->fp;
        }
      }
    }

    // All streams removed; exit loop.
    if (empty($responses)) {
      break;
    }
    // Restart timers.
    if ($restart_timers) {
      $start_time_this_run = microtime(TRUE);
    }
    // No streams selected; restart loop from the top.
    if (empty($this_run)) {
      continue;
    }

    // Set the read and write vars to the streams var.
    $read = $write = $this_run;
    $except = array();
    // Do some voodoo and open all streams at once. Wait 25ms for streams to
    // respond.
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
      // If on windows, use error suppression http://drupal.org/node/1869026
      $n = @stream_select($read, $write, $except, $stream_select_timeout, 25000);
    }
    else {
      $n = stream_select($read, $write, $except, $stream_select_timeout, 25000);
    }
    $stream_select_timeout = 0;

    // An error occurred with the streams. Remove bad ones.
    if ($n === FALSE) {
      $merged = array_unique(array_merge($read, $write, $except));
      foreach ($merged as $m) {
        $id = array_search($m, $this_run);
        @fclose($m);
        if ($id !== FALSE && isset($responses[$id])) {
          watchdog('httprl', 'The following url had a stream_select error and had to be terminated: %info', array('%info' => $responses[$id]->url), WATCHDOG_ERROR);
          unset($responses[$id]);
        }
      }
    }

    // We have some streams to read/write to.
    $rw_done = FALSE;
    if (!empty($n)) {
      if (!empty($read) && is_array($read)) {
        // Readable sockets either have data for us, or are failed connection
        // attempts.
        foreach ($read as $r) {
          $id = array_search($r, $this_run);
          // Make sure ID is in the streams.
          if ($id === FALSE) {
            @fclose($r);
            continue;
          }
          // Close the stream if ID not found in $responses.
          if (!array_key_exists($id, $responses)) {
            @fclose($r);
            continue;
          }
          // Do not read from the non blocking sockets.
          if (empty($responses[$id]->options['blocking'])) {
            // Do post processing on the stream and close it.
            httprl_post_processing($id, $responses, $output);
            continue;
          }

          // Read socket.
          $chunk = fread($r, $responses[$id]->chunk_size);
          if (strlen($chunk) > 0) {
            $rw_done = TRUE;
            if (!isset($responses[$id]->time_to_first_byte)) {
              // Calculate Time to First Byte.
              $responses[$id]->time_to_first_byte = $result->running_time + microtime(TRUE) - $start_time_this_run;
            }
          }
          $responses[$id]->data .= $chunk;

          // Process the headers if we have some data.
          if (!empty($responses[$id]->data) && empty($responses[$id]->headers) &&
              (    strpos($responses[$id]->data, "\r\n\r\n")
                || strpos($responses[$id]->data, "\n\n")
                || strpos($responses[$id]->data, "\r\r")
                  )
                    ) {
            // See if the headers are in the data stream.
            httprl_parse_data($responses[$id]);
            if (!empty($responses[$id]->headers)) {
              // Stream was a redirect, kill & close this connection; redirect is
              // being followed now.
              if (!empty($responses[$id]->options['internal_states']['kill'])) {
                fclose($r);
                unset($responses[$id]);
                continue;
              }

              // Now that we have the headers, increase the chunk size.
              $responses[$id]->chunk_size = $responses[$id]->options['chunk_size_read'];

              // If a range header is set, 200 was returned, method is GET,
              // calculate how many bytes need to be downloaded.
              if (   !empty($responses[$id]->options['headers']['Range'])
                  && $responses[$id]->code == 200
                  && $responses[$id]->options['method'] == 'GET'
                    ) {
                $responses[$id]->ranges = httprl_get_ranges($responses[$id]->options['headers']['Range']);
                $responses[$id]->options['max_data_size'] = httprl_get_last_byte_from_range($responses[$id]->ranges);
              }
            }
          }

          // Close the connection if Transfer-Encoding & Content-Encoding are not
          // used, a Range request was made and the currently downloaded data size
          // is larger than the Range request.
          if (   !empty($responses[$id]->options['max_data_size'])
              && is_numeric($responses[$id]->options['max_data_size'])
              && (!isset($result->headers['transfer-encoding']) || $result->headers['transfer-encoding'] != 'chunked')
              && (!isset($result->headers['content-encoding']) || ($result->headers['content-encoding'] != 'gzip' && $result->headers['content-encoding'] != 'deflate'))
              && $responses[$id]->options['max_data_size'] < httprl_strlen($responses[$id]->data)
                ) {
            $responses[$id]->status = 'Done.';

            // Do post processing on the stream.
            httprl_post_processing($id, $responses, $output);
            continue;
          }

          // Get stream data.
          $info = stream_get_meta_data($r);
          $alive = !$info['eof'] && !feof($r) && !$info['timed_out'] && strlen($chunk);
          if (!$alive && $responses[$id]->status !== 'Request sent, waiting for response.') {
            if ($responses[$id]->status == 'Connecting.') {
              $responses[$id]->error = $t('Connection refused by destination. TCP.');
              $responses[$id]->code = HTTPRL_CONNECTION_REFUSED;
            }
            if ($responses[$id]->status == 'Writing to server.') {
              $responses[$id]->error = $t('Connection refused by destination. Write.');
              $responses[$id]->code = HTTPRL_CONNECTION_REFUSED;
            }
            $responses[$id]->status = 'Done.';

            // Do post processing on the stream.
            httprl_post_processing($id, $responses, $output);
            continue;
          }
          else {
            $responses[$id]->status = 'Reading data';
          }
        }
      }

      // Write to each stream if it is available.
      if ($stream_write_count > 0 && !empty($write) && is_array($write)) {
        foreach ($write as $w) {
          $id = array_search($w, $this_run);
          // Make sure ID is in the streams & status is for writing.
          if ($id === FALSE || empty($responses[$id]->status) || ($responses[$id]->status != 'Connecting.' && $responses[$id]->status != 'Writing to server.')) {
            continue;
          }

          // Keep track of how many bytes are sent.
          if (!isset($responses[$id]->bytes_sent)) {
            $responses[$id]->bytes_sent = 0;
          }

          // Have twice the number of bytes available for fwrite.
          $data_to_send = substr($responses[$id]->request, $responses[$id]->bytes_sent, 2 * $responses[$id]->options['chunk_size_write']);
          // Calculate the number of bytes we need to write to the stream.
          $len = httprl_strlen($data_to_send);
          if ($len > 0) {
            // Write to the stream.
            $bytes = fwrite($w, $data_to_send, min($responses[$id]->options['chunk_size_write'], $len));
          }
          else {
            // Nothing to write.
            $bytes = $len;
          }

          // See if we are done with writing.
          if ($bytes === FALSE) {
            // fwrite failed.
            $responses[$id]->error = $t('fwrite() failed.');
            $responses[$id]->code = HTTPRL_REQUEST_FWRITE_FAIL;
            $responses[$id]->status = 'Done.';
            $stream_write_count--;

            // Do post processing on the stream.
            httprl_post_processing($id, $responses, $output);
            continue;
          }
          elseif ($bytes >= $len) {
            // fwrite is done.
            $stream_write_count--;

            // If this is a non blocking request then close the connection and
            // destroy the stream.
            if (empty($responses[$id]->options['blocking'])) {
              $responses[$id]->status = 'Non-Blocking request sent out. Not waiting for the response.';
              // Do post processing on the stream.
              httprl_post_processing($id, $responses, $output);
              continue;
            }
            else {
              // All data has been written to the socket. We are read only from
              // here on out.
              $responses[$id]->status = "Request sent, waiting for response.";
            }

            // Record how many bytes were sent.
            $responses[$id]->bytes_sent += $bytes;
            $rw_done = TRUE;
          }
          else {
            // Change status to 'Writing to server.'
            if ($responses[$id]->status = 'Connecting.') {
              $responses[$id]->status = 'Writing to server.';
            }
            // There is more data to write to this socket. Cut what was sent
            // across the stream and resend whats left next time in the loop.
            $responses[$id]->bytes_sent += $bytes;
            $rw_done = TRUE;
          }
        }
      }
      elseif ($stall_freads) {
        return;
      }
    }
    if (!$rw_done) {
      // Wait 5ms for data buffers.
      usleep(5000);
    }
  }

  // Copy output.
  $return = $output;

  // Free memory/reset static variables.
  $responses = array();
  $counter = 0;
  $output = array();
  $static_stall_freads = FALSE;

  return $return;
}

/**
 * Extract the header and meta data from the http data stream.
 *
 * @see drupal_http_request()
 *
 * @todo Send cookies in the redirect request if domain/path match.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_parse_data(&$result) {
  // If in non blocking mode, skip.
  if (empty($result->options['blocking'])) {
    return;
  }

  // If the headers are already parsed, skip.
  if (!empty($result->headers)) {
    return;
  }

  // If the t function is not available use httprl_pr.
  $t = function_exists('t') ? 't' : 'httprl_pr';

  // Parse response headers from the response body.
  // Be tolerant of malformed HTTP responses that separate header and body with
  // \n\n or \r\r instead of \r\n\r\n.
  $response = $result->data;
  list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
  $response = preg_split("/\r\n|\n|\r/", $response);

  // Parse the response status line.
  $protocol_code_array = explode(' ', trim(array_shift($response)), 3);
  $result->protocol = $protocol_code_array[0];
  $code = (int) $protocol_code_array[1];
  // If the response does not include a description, don't try to process it.
  $result->status_message = isset($protocol_code_array[2]) ? $protocol_code_array[2] : '';
  unset($protocol_code_array);

  $result->headers = array();

  // Parse the response headers.
  $cookie_primary_counter = 0;
  while ($line = trim(array_shift($response))) {
    list($name, $value) = explode(':', $line, 2);
    $name = strtolower($name);

    // Parse cookies before they get added to the header.
    if ($name == 'set-cookie') {
      // Extract the key value pairs for this cookie.
      foreach (explode(';', $value) as $cookie_name_value) {
        $temp = explode('=', trim($cookie_name_value));
        $cookie_key = trim($temp[0]);
        $cookie_value = isset($temp[1]) ? trim($temp[1]) : '';
        unset($temp);
        // The cookie name-value pair always comes first (RFC 2109 4.2.2).
        if (!isset($result->cookies[$cookie_primary_counter])) {
          $result->cookies[$cookie_primary_counter] = array(
            'name' => $cookie_key,
            'value' => $cookie_value,
          );
        }
        // Extract the rest of the attribute-value pairs.
        else {
          $result->cookies[$cookie_primary_counter] += array(
            $cookie_key => $cookie_value,
          );
        }
      }
      $cookie_primary_counter++;
    }

    // Add key value pairs to the header; including cookies.
    if (isset($result->headers[$name]) && $name == 'set-cookie') {
      // RFC 2109: the Set-Cookie response header comprises the token Set-
      // Cookie:, followed by a comma-separated list of one or more cookies.
      $result->headers[$name] .= ',' . trim($value);
    }
    else {
      $result->headers[$name] = trim($value);
    }
  }

  $responses = array(
    100 => 'Continue',
    101 => 'Switching Protocols',
    200 => 'OK',
    201 => 'Created',
    202 => 'Accepted',
    203 => 'Non-Authoritative Information',
    204 => 'No Content',
    205 => 'Reset Content',
    206 => 'Partial Content',
    300 => 'Multiple Choices',
    301 => 'Moved Permanently',
    302 => 'Found',
    303 => 'See Other',
    304 => 'Not Modified',
    305 => 'Use Proxy',
    307 => 'Temporary Redirect',
    400 => 'Bad Request',
    401 => 'Unauthorized',
    402 => 'Payment Required',
    403 => 'Forbidden',
    404 => 'Not Found',
    405 => 'Method Not Allowed',
    406 => 'Not Acceptable',
    407 => 'Proxy Authentication Required',
    408 => 'Request Time-out',
    409 => 'Conflict',
    410 => 'Gone',
    411 => 'Length Required',
    412 => 'Precondition Failed',
    413 => 'Request Entity Too Large',
    414 => 'Request-URI Too Large',
    415 => 'Unsupported Media Type',
    416 => 'Requested range not satisfiable',
    417 => 'Expectation Failed',
    500 => 'Internal Server Error',
    501 => 'Not Implemented',
    502 => 'Bad Gateway',
    503 => 'Service Unavailable',
    504 => 'Gateway Time-out',
    505 => 'HTTP Version not supported',
  );
  // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  // base code in their class.
  if (!isset($responses[$code])) {
    $code = floor($code / 100) * 100;
  }
  $result->code = $code;

  switch ($code) {
    case 200: // OK
    case 201: // Created
    case 202: // Accepted
    case 206: // Partial Content
    case 304: // Not modified
      break;

    case 301: // Moved permanently
    case 302: // Moved temporarily
    case 307: // Moved temporarily
      $location = @parse_url($result->headers['location']);

      // If location isn't fully qualified URL (as per W3 RFC2616), build one.
      if (empty($location['scheme']) || empty($location['host'])) {
        $hostname = httprl_get_hostname();

        // Get the important parts from the original request.
        $original_location = @parse_url($result->url);
        // Assume request is to self if none of this was setup correctly.
        $location['scheme'] = !empty($location['scheme']) ? $location['scheme'] : $original_location['scheme'];
        $location['host'] = !empty($location['host']) ? $location['host'] : (!empty($original_location['host']) ? $original_location['host'] : $hostname);
        $location['port'] = !empty($location['port']) ? $location['port'] : (!empty($original_location['port']) ? $original_location['port'] : '');
        $location = httprl_glue_url($location);
      }
      else {
        $location = $result->headers['location'];
      }

      // Set internal redirect states.
      $result->options['internal_states']['redirect_code_array'][] = $code;
      $result->options['internal_states']['redirect_url_array'][] = $location;
      if (!isset($result->options['internal_states']['original_url'])) {
        $result->options['internal_states']['original_url'] = $result->url;
      }

      // Error out if we hit the max redirect.
      if ($result->options['max_redirects'] <= 0) {
        $result->code = HTTPRL_REQUEST_ALLOWED_REDIRECTS_EXHAUSTED;
        $result->error = $t('Maximum allowed redirects exhausted.');
      }
      else {
        // Redirect to the new location.
        // TODO: Send cookies in the redirect request if domain/path match.
        $result->options['max_redirects']--;
        if (isset($result->options['headers']['Referer'])) {
          $result->options['headers']['Referer'] = $result->url;
        }
        // Remove the host from the header.
        unset($result->options['headers']['Host']);

        // Pass along running time.
        $result->options['internal_states']['running_time'] = $result->running_time;

        // Send new request.
        httprl_request($location, $result->options);
        // Kill this request.
        $result->options['internal_states']['kill'] = TRUE;
      }

      break;

    default:
      $result->error = $result->status_message;
  }
}

/**
 * Parse a range header into start and end byte ranges.
 *
 * @param string $input
 *   String in the form of bytes=0-1024 or bytes=0-1024,2048-4096
 *
 * @return array
 *   Keyed arrays containing start and end values for the byte ranges.
 *   Empty array if the string can not be parsed.
 */
function httprl_get_ranges($input) {
  $ranges = array();
  // Make sure the input string matches the correct format.
  $string = preg_match('/^bytes=((\d*-\d*,? ?)+)$/', $input, $matches) ? $matches[1] : FALSE;
  if (!empty($string)) {
    // Handle multiple ranges.
    foreach (explode(',', $string) as $range) {
      // Get the start and end byte values for this range.
      $values = explode('-', $range);
      if (count($values) != 2) {
        return FALSE;
      }
      $ranges[] = array('start' => $values[0], 'end' => $values[1]);
    }
  }
  return $ranges;
}

/**
 * Given an array of ranges, get the last byte we need to download.
 *
 * @param array $ranges
 *   Multi dimensional array
 *
 * @return int|null
 *   NULL: Get all values; int: last byte to download.
 */
function httprl_get_last_byte_from_range($ranges) {
  $max = 0;
  if (empty($ranges)) {
    return NULL;
  }
  foreach ($ranges as $range) {
    if (!is_numeric($range['start']) || !is_numeric($range['end'])) {
      return NULL;
    }
    $max = max($range['end'] + 1, $max);
  }
  return $max;
}

/**
 * Run post processing on the request if we are done reading.
 *
 * Decode transfer-encoding and content-encoding.
 * Reconstruct the internal redirect arrays.
 *
 * @result object
 *   An object from httprl_send_request.
 */
function httprl_post_processing($id, &$responses, &$output, $time_left = NULL) {
  // Create the result reference.
  $result = &$responses[$id];

  // Close file.
  if (isset($result->fp)) {
    if (empty($responses[$id]->options['blocking'])) {
      $ms_delay = httprl_variable_get('httprl_non_blocking_fclose_delay', HTTPRL_NON_BLOCKING_FCLOSE_DELAY);
      if (!empty($ms_delay)) {
        // Wait X ms before closing a non blocking connection.
        usleep($ms_delay * 1000);
      }
    }
    @fclose($result->fp);
  }

  // Set timeout.
  if (is_null($time_left)) {
    $time_left = $result->options['timeout'] - $result->running_time;
  }
  $result->options['timeout'] = $time_left;

  // Assemble redirects.
  httprl_reconstruct_redirects($result);

  // Decode chunked transfer-encoding and gzip/deflate content-encoding.
  httprl_decode_data($result);

  // If this is a background callback request, extract the data and return.
  if (isset($result->options['internal_states']) && array_key_exists('background_function_return', $result->options['internal_states']) && isset($result->headers['content-type']) && strpos($result->headers['content-type'], 'application/x-www-form-urlencoded') !== FALSE) {
    httprl_extract_background_callback_data($result);
    unset($responses[$id]);
    return;
  }

  // See if a full bootstrap has been done.
  $full_bootstrap = httprl_drupal_full_bootstrap();

  // Allow a user defined function to alter all $responses.
  if ($full_bootstrap && !empty($result->options['alter_all_streams_function']) && function_exists($result->options['alter_all_streams_function'])) {
    $result->options['alter_all_streams_function']($id, $responses);
  }
  unset($responses[$id]);

  // Allow other modules to alter the result.
  if ($full_bootstrap) {
    // Call hook_httprl_post_processing_alter().
    drupal_alter('httprl_post_processing', $result);
  }

  // Run callback so other modules can do stuff in the event loop.
  if (   $full_bootstrap
      && !empty($result->options['callback'])
      && is_array($result->options['callback'])
      && !empty($result->options['callback'][0])
      && is_array($result->options['callback'][0])
      && !empty($result->options['callback'][0]['function'])
        ) {
    httprl_run_callback($result);
  }

  // Run background_callback.
  if (   !empty($result->options['background_callback'])
      && is_array($result->options['background_callback'])
      && !empty($result->options['background_callback'][0])
      && is_array($result->options['background_callback'][0])
      && !empty($result->options['background_callback'][0]['function'])
        ) {
    $call_is_queued = httprl_queue_background_callback($result->options['background_callback'], $result);
    if (is_null($call_is_queued)) {
      watchdog('httprl', 'Background callback attempted but it is disabled. Going to use a normal callback');
      unset($result->options['callback']);
      $result->options['callback'] = $result->options['background_callback'];
      unset($result->options['background_callback']);
      httprl_run_callback($result);
    }
  }

  // Copy the result to the output array.
  if (isset($result->url)) {
    $output[$result->url] = $result;
  }

}

/**
 * Extract background callback data.
 *
 * Set the return and printed values & any pass by reference values from a
 * background callback operation.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_extract_background_callback_data(&$result) {
  // Extract data from string.
  $data = array();
  parse_str($result->data, $data);
  // Follow rfc4648 for base64url
  // @see http://tools.ietf.org/html/rfc4648#page-7
  $serialized_string = trim(base64_decode(strtr(current($data), array('-' => '+', '_' => '/'))));
  $data = @unserialize($serialized_string);
  if ($data !== 'b:0;' && $data === FALSE) {
    return;
  }

  // Set return and printed values.
  if (isset($data['return'])) {
    $result->options['internal_states']['background_function_return'] = $data['return'];
  }
  if (isset($data['printed'])) {
    $result->options['internal_states']['background_function_printed'] = $data['printed'];
  }

  // Set any pass by reference values.
  if (isset($data['args'])) {
    httprl_recursive_array_reference_extract($result->options['internal_states']['background_function_args'], $data['args']);
  }
}

/**
 * Replace data in place so pass by reference sill works.
 *
 * @param array $array
 *   An array containing the references if any.
 * @param array $data
 *   An array that has the new values to copy into $array.
 * @param int $depth
 *   Only go 10 levels deep. Prevent infinite loops.
 */
function httprl_recursive_array_reference_extract(&$array, $data, $depth = 0) {
  $depth++;
  foreach ($array as $key => &$value) {
    if (isset($data[$key])) {
      if (is_array($data[$key]) && is_array($value) && $depth < 10) {
        $value = httprl_recursive_array_reference_extract($value, $data[$key], $depth);
      }
      else {
        $value = $data[$key];
      }
    }
    else {
      $value = NULL;
    }
  }
  // Copy new keys into the data structure.
  foreach ($data as $key => $value) {
    if (isset($array[$key])) {
      continue;
    }
    $array[$key] = $value;
  }
}

/**
 * Run callback.
 *
 * Will run the given callback returning values and what might have been
 * printed by that function, as well as respecting any pass by reference values.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_run_callback(&$result) {
  // Get options.
  $callback_options = $result->options['callback'][0];
  // Merge in values by reference.
  $result->options['callback'][0] = &$result;

  // Capture anything printed out.
  ob_start();
  // Call function.
  $callback_options['return'] = call_user_func_array($callback_options['function'], $result->options['callback']);
  if (array_key_exists('printed', $callback_options)) {
    // Return printed output if requested to do so.
    $callback_options['printed'] = ob_get_contents();
  }
  ob_end_clean();

  // Add options back into the callback array.
  if (isset($result->options['callback'])) {
    array_unshift($result->options['callback'], $callback_options);
  }
}

/**
 * Run callback in the background.
 *
 * Will run the given callback returning values and what might have been
 * printed by that function, as well as respecting any pass by reference values.
 *
 * @param array $args
 *   An array of arguments, first key value pair is used to control the
 *   callback function. The rest of the key value pairs will be arguments for
 *   the callback function.
 * @param object $result
 *   (optional) An object from httprl_send_request. If this is set, this will
 *   be the first argument of the function.
 */
function httprl_queue_background_callback(&$args, &$result = NULL) {
  // Use a counter to prevent key collisions in httprl_send_request.
  static $counter;
  if (!isset($counter)) {
    $counter = 0;
  }
  $counter++;

  if (!httprl_is_background_callback_capable()) {
    return NULL;
  }

  // Get URL to call function in background.
  if (empty($callback_options['url'])) {
    $url = httprl_build_url_self('httprl_async_function_callback?count=' . $counter);
  }
  else {
    $url = $callback_options['url'];
  }

  // Get options.
  $callback_options = $args[0];

  if (is_null($result)) {
    array_shift($args);
  }
  else {
    // Merge in this request by reference.
    $args[0] = &$result;
  }

  // Set blocking mode.
  if (isset($callback_options['return']) || isset($callback_options['printed'])) {
    $mode = TRUE;
  }
  else {
    $mode = FALSE;
  }
  // Make sure some array keys exist.
  if (!isset($callback_options['return'])) {
    $callback_options['return'] = '';
  }
  if (!isset($callback_options['function'])) {
    $callback_options['function'] = '';
  }

  // Get the maximum amount of time this could take.
  $times = array(
    httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT),
    httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
  );
  if (isset($callback_options['options']['timeout'])) {
    $times[] = $callback_options['options']['timeout'];
  }
  if (isset($callback_options['options']['global_timeout'])) {
    $times[] = $callback_options['options']['global_timeout'];
  }

  // Create lock name for this run.
  $available = FALSE;
  $lock_counter = 0;
  while (!$available && $lock_counter < 20) {
    // 512 bits = 64 bytes.
    if (function_exists('drupal_random_bytes')) {
      $name = 'httprl_' . hash('sha512', drupal_random_bytes(64));
    }
    elseif (function_exists('openssl_random_pseudo_bytes')) {
      $name = 'httprl_' . hash('sha512', openssl_random_pseudo_bytes(64));
    }
    else {
      $name = 'httprl_' . hash('sha512', mt_rand() . microtime(TRUE) . serialize($_SERVER));
    }
    $available = lock_may_be_available($name);
    $lock_counter++;
  }
  $callback_options['options']['lock_name'] = $name;

  // Create data array and options for request.
  $options = array(
    'data' => array(
      'master_key' => hash('sha512', httprl_drupal_get_private_key()),
      'temp_key' => $name,
      'mode' => $mode,
      'php_timeout' => max($times),
      'function' => $callback_options['function'],
      'context' => isset($callback_options['context']) ? $callback_options['context'] : array(),
      // Follow rfc4648 for base64url
      // @see http://tools.ietf.org/html/rfc4648#page-7
      'args' => strtr(base64_encode(serialize($args)), array('+' => '-', '/' => '_')),
    ),
    'internal_states' => array(
      'background_function_return' => &$callback_options['return'],
      'background_function_args' => &$args,
    ),
    'blocking' => $mode,
    'method' => 'POST',
  );
  if (isset($callback_options['printed'])) {
    $options['internal_states']['background_function_printed'] = &$callback_options['printed'];
  }
  if (isset($callback_options['options']) && is_array($callback_options['options'])) {
    $options += $callback_options['options'];
  }

  // Set Host header.
  if (empty($options['headers']['Host'])) {
    $hostname = httprl_get_hostname();
    if (!empty($hostname)) {
      $options['headers']['Host'] = $hostname;
    }
  }

  // Set Session header if requested to.
  if (!empty($callback_options['context']['session']) && !empty($_COOKIE[session_name()])) {
    if (!isset($options['headers']['Cookie'])) {
      $options['headers']['Cookie'] = '';
    }
    $options['headers']['Cookie'] = session_name() . '=' . $_COOKIE[session_name()] . ';';
  }

  // Send Request.
  return httprl_request($url, $options);
}

/**
 * Get a lock so background calls work.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_acquire_lock(&$result) {
  if (empty($result->options['lock_name'])) {
    return FALSE;
  }

  // Get the maximum amount of time this could take.
  $times = array(
    httprl_variable_get('httprl_timeout', HTTPRL_TIMEOUT),
    httprl_variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
  );
  if (isset($result->options['timeout'])) {
    $times[] = $result->options['timeout'];
  }
  if (isset($result->options['global_timeout'])) {
    $times[] = $result->options['global_timeout'];
  }

  $time = max($times);
  $name = $result->options['lock_name'];
  return httprl_acquire_headless_lock($name, $time);
}

/**
 * Get a floating lock so background calls work.
 *
 * @param string $name
 *   Name of the lock to use.
 * @param int $time
 *   How long the lock will last.
 */
function httprl_acquire_headless_lock($name, $time = 60) {
  // Acquire lock for this run.
  $locked = FALSE;
  $lock_counter = 0;
  while (!$locked && $lock_counter < 3) {
    // Set lock to maximum amount of time.
    $locked = lock_acquire($name, $time);
    $lock_counter++;
  }
  if (!$locked) {
    return FALSE;
  }

  // Make sure lock exists after this process is dead.
  // Remove from the global locks variable.
  unset($GLOBALS['locks'][$name]);

  // Remove the lock_id reference in the database.
  $lock_inc = httprl_variable_get('lock_inc', './includes/lock.inc');
  if ($lock_inc === './includes/lock.inc') {
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      db_update('semaphore')
        ->fields(array('value' => 'httprl'))
        ->condition('name', $name)
        ->condition('value', _lock_id())
        ->execute();
    }
    else {
      db_query("UPDATE {semaphore} SET value = '%s' WHERE name = '%s' AND value = '%s'", 'httprl', $name, _lock_id());
    }
  }
  elseif (strpos($lock_inc, '/apdqc/apdqc.lock.inc') !== FALSE) {
    lock_change_lock_id($name, _lock_id(), 'httprl');
  }
  return TRUE;
}

/**
 * Will decode chunked transfer-encoding and gzip/deflate content-encoding.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_decode_data(&$result) {
  if (isset($result->headers['transfer-encoding']) && $result->headers['transfer-encoding'] == 'chunked') {
    $stream_position = 0;
    $output = '';
    $data = $result->data;
    while ($stream_position < httprl_strlen($data)) {
      // Get the number of bytes to read for this chunk.
      $rawnum = substr($data, $stream_position, strpos(substr($data, $stream_position), "\r\n") + 2);
      $num = hexdec(trim($rawnum));
      // Get the position to read from.
      $stream_position += httprl_strlen($rawnum);
      // Extract the chunk.
      $chunk = substr($data, $stream_position, $num);
      // Decompress if compressed.
      if (isset($result->headers['content-encoding'])) {
        if ($result->headers['content-encoding'] === 'gzip') {
          $chunk = @gzinflate(substr($chunk, 10));
        }
        elseif ($result->headers['content-encoding'] === 'deflate') {
          $chunk = @gzinflate($chunk);
        }
        elseif ($result->headers['content-encoding'] === 'br' && is_callable('brotli_uncompress')) {
          $chunk = @brotli_uncompress($chunk);
        }
        if ($chunk === FALSE) {
          break;
        }
      }
      // Glue the chunks together.
      $output .= $chunk;
      $stream_position += httprl_strlen($chunk);
    }
    if ($chunk !== FALSE) {
      $result->data = $output;
    }
  }
  // Decompress if compressed.
  elseif (isset($result->headers['content-encoding'])) {
    if ($result->headers['content-encoding'] === 'gzip') {
      $chunk = @gzinflate(substr($result->data, 10));
    }
    elseif ($result->headers['content-encoding'] === 'deflate') {
      $chunk = @gzinflate($result->data);
    }
    elseif ($result->headers['content-encoding'] === 'br' && is_callable('brotli_uncompress')) {
      $chunk = @brotli_uncompress($result->data);
    }
    if (isset($chunk) && $chunk !== FALSE) {
      $result->data = $chunk;
    }
  }

  // Cut up response for one sided Range requests.
  if (array_key_exists('max_data_size', $result->options)) {
    $result->code = 206;

    // Make the data conform to the range request.
    $new_data = array();
    foreach ($result->ranges as $range) {
      // Get only the last X number of bytes.
      if (!is_numeric($range['start'])) {
        $new_data[] = substr($result->data, -$range['end']);
      }
      // Get all but the first X number of bytes.
      elseif (!is_numeric($range['end'])) {
        $new_data[] = substr($result->data, $range['start']);
      }
      else {
        $new_data[] = substr($result->data, $range['start'], ($range['end'] + 1) - $range['start']);
      }
    }
    $result->data = implode('', $new_data);

    // Fix content-length for fake 206s.
    if (isset($result->headers['content-length'])) {
      $result->headers['content-length'] = httprl_strlen($result->data);
    }
  }

  // Reassemble multipart/byteranges response.
  if (isset($result->headers['content-type']) && strpos($result->headers['content-type'], 'multipart/byteranges; boundary=') !== FALSE) {
    // Get boundary string.
    $boundary = "\r\n--" . substr($result->headers['content-type'], 31);
    $datas = explode($boundary, $result->data);
    $result->data = '';
    foreach ($datas as $data) {
      $split = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2);
      if (count($split) < 2) {
        continue;
      }

      // Separate the data from the headers.
      list($response, $data) = $split;
      $response = array_filter(preg_split("/\r\n|\n|\r/", $response));

      // Parse the response headers.
      while ($line = trim(array_shift($response))) {
        list($name, $value) = explode(':', $line, 2);
        $name = strtolower($name);

        // Add key value pairs to the header.
        if ($name != 'content-range') {
          $result->headers[$name] = trim($value);
        }
      }
      $result->data .= $data;
    }
    // Fix content-length for multipart/byteranges.
    if (isset($result->headers['content-length'])) {
      $result->headers['content-length'] = httprl_strlen($result->data);
    }
  }
}

/**
 * Reconstruct the internal redirect arrays.
 *
 * @param object $result
 *   An object from httprl_send_request.
 */
function httprl_reconstruct_redirects(&$result) {
  // Return if original_url is not set.
  if (empty($result->options['internal_states']['original_url'])) {
    return;
  }
  // Set the original url.
  $result->url = $result->options['internal_states']['original_url'];

  // Set the redirect code.
  $result->redirect_code_array = $result->options['internal_states']['redirect_code_array'];
  $result->redirect_code = array_pop($result->options['internal_states']['redirect_code_array']);

  // Set the redirect url.
  $result->redirect_url_array = $result->options['internal_states']['redirect_url_array'];
  $result->redirect_url = array_pop($result->options['internal_states']['redirect_url_array']);

  // Cleanup.
  unset($result->options['internal_states']['original_url'], $result->options['internal_states']['redirect_code_array'], $result->options['internal_states']['redirect_url_array']);
  if (empty($result->options['internal_states'])) {
    unset($result->options['internal_states']);
  }
}

/**
 * Output text, close connection, continue processing in the background.
 *
 * @param string $output
 *   Text to output to open connection.
 * @param bool $wait
 *   Wait 1 second?
 * @param string $content_type
 *   Content type header.
 * @param int $length
 *   Content length.
 *
 * @return bool
 *   Returns TRUE if operation worked, FALSE if it failed.
 */
function httprl_background_processing($output, $wait = TRUE, $content_type = "text/html; charset=utf-8", $length = 0) {
  // Can't do background processing if headers are already sent.
  if (headers_sent()) {
    return FALSE;
  }

  // Prime php for background operations.
  // Remove any output buffers.
  @ob_end_clean();
  $loop = 0;
  while (ob_get_level() && $loop < 25) {
    @ob_end_clean();
    $loop++;
  }

  // Ignore user aborts.
  ignore_user_abort(TRUE);

  // Output headers & data.
  ob_start();
  header("HTTP/1.0 200 OK");
  header("Content-type: " . $content_type);
  header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  header("Cache-Control: no-cache");
  header("Cache-Control: must-revalidate");
  header("Connection: close");
  header('Etag: "' . microtime(TRUE) . '"');
  print ($output);
  $size = ob_get_length();
  header("Content-Length: " . $size);
  @ob_end_flush();
  @ob_flush();
  @flush();

  if (function_exists('fastcgi_finish_request')) {
    fastcgi_finish_request();
  }

  // Wait for 1 second.
  if ($wait) {
    sleep(1);
  }

  // Text returned and connection closed.
  // Do background processing. Time taken after should not effect page load times.
  return TRUE;
}

/**
 * Get the length of a string in bytes.
 *
 * @param string $string
 *   get string length
 */
function httprl_strlen($string) {
  static $mb_strlen;
  if (!isset($mb_strlen)) {
    $mb_strlen = function_exists('mb_strlen');
  }
  if ($mb_strlen) {
    return mb_strlen($string, '8bit');
  }
  else {
    return strlen($string);
  }
}

/**
 * Alt to http_build_url().
 *
 * @see http://php.net/parse-url#85963
 *
 * @param array $parsed
 *   array from parse_url()
 *
 * @return string
 *   URI is returned.
 */
function httprl_glue_url($parsed) {
  if (!is_array($parsed)) {
    return FALSE;
  }

  $uri = isset($parsed['scheme']) ? $parsed['scheme'] . ':' . ((strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : '';
  $uri .= isset($parsed['user']) ? $parsed['user'] . (isset($parsed['pass']) ? ':' . $parsed['pass'] : '') . '@' : '';
  $uri .= isset($parsed['host']) ? $parsed['host'] : '';
  $uri .= !empty($parsed['port']) ? ':' . $parsed['port'] : '';

  if (isset($parsed['path'])) {
    $uri .= (substr($parsed['path'], 0, 1) == '/') ? $parsed['path'] : ((!empty($uri) ? '/' : '') . $parsed['path']);
  }

  $uri .= isset($parsed['query']) ? '?' . $parsed['query'] : '';
  $uri .= isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';

  return $uri;
}

/**
 * Return the server schema (http or https).
 *
 * @return string
 *   http OR https.
 */
function httprl_get_server_schema() {
  return (   (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
          || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
          || (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] == 'on')
            ) ? 'https' : 'http';
}

/**
 * Send out a fast 403 and exit.
 */
function httprl_fast403($msg = '') {
  global $base_path;

  // Set headers.
  if (!headers_sent()) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
    header('X-HTTPRL: Forbidden.');
  }

  // Print simple 403 page.
  print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
  print '<html>';
  print '<head><title>403 Forbidden</title></head>';
  print '<body><h1>Forbidden</h1>';
  print '<p>You are not authorized to access this page.</p>';
  print '<p><a href="' . $base_path . '">Home</a></p>';
  print '<!-- httprl_fast403 ' . $msg . ' -->';
  print '</body></html>';

  // Exit Script.
  httprl_call_exit();
}

/**
 * Release a lock previously acquired by lock_acquire().
 *
 * This will release the named lock.
 *
 * @param string $name
 *   The name of the lock.
 */
function httprl_lock_release($name) {
  $lock_inc = httprl_variable_get('lock_inc', './includes/lock.inc');
  // Core.
  if ($lock_inc === './includes/lock.inc') {
    unset($GLOBALS['locks'][$name]);
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      db_delete('semaphore')
        ->condition('name', $name)
        ->execute();
    }
    else {
      db_query("DELETE FROM {semaphore} WHERE name = '%s'", $name);
    }
  }
  // Memcache storage module.
  elseif (strpos($lock_inc, '/memcache_storage/includes/lock.inc') !== FALSE) {
    // We unset unconditionally since caller assumes lock is released anyway.
    unset($GLOBALS['locks'][$name]);

    // Remove current lock from memcached pool.
    if (MemcacheStorageAPI::get($name, 'semaphore')) {
      MemcacheStorageAPI::delete($name, 'semaphore');
    }
  }
  // Memcache Integration module.
  elseif (strpos($lock_inc, '/memcache/memcache-lock.inc') !== FALSE) {
    // We unset unconditionally since caller assumes lock is released anyway.
    unset($GLOBALS['locks'][$name]);

    if (dmemcache_get($name, 'semaphore')) {
      dmemcache_delete($name, 'semaphore');
    }
  }
  elseif (strpos($lock_inc, '/apdqc/apdqc.lock.inc') !== FALSE) {
    lock_release_fuzzy($name);
  }
  else {
    lock_release($name);
  }
}


/**
 * If $data is bool or strlen = 0 use var_export. Recursively go deeper.
 *
 * @param mixed $data
 *   Data In.
 * @param int $level
 *   (optional) At what level of the array/object are we at.
 *
 * @return mixed
 *   $data
 */
function httprl_print_empty(&$data, $level = 0) {
  $level++;
  if ($level < 5) {
    if (is_object($data)) {
      $new_object = new stdClass();
      $new_object->__original_class_name__ = get_class($data);
      foreach ($data as $key => $values) {
        $new_object->{$key} = httprl_print_empty($values, $level);
      }
      $data = $new_object;
    }
    elseif (is_array($data)) {
      foreach ($data as &$values) {
        $values = httprl_print_empty($values, $level);
      }
    }
    elseif (is_bool($data) || strlen((string) $data) == 0) {
      $data = strtoupper(var_export($data, TRUE));
    }
    elseif (is_string($data) && strlen($data) > HTTPRL_PR_MAX_STRING_LENGTH) {
      // Do not try to print a string longer than 256KB.
      // Some browsers have issues with very long documents.
      $data = substr($data, 0, HTTPRL_PR_MAX_STRING_LENGTH);
    }
  }
  return $data;
}

/**
 * Pretty print data.
 *
 * @param string $input
 *   Data In.
 *
 * @return string
 *   Human readable HTML version of the data.
 */
function httprl_pr($input) {
  $output_plain_text = FALSE;
  if (strpos(implode("\n", array_map('strtolower', headers_list())), 'content-type: text/plain') !== FALSE || (function_exists('drupal_is_cli') && drupal_is_cli())) {
    $output_plain_text = TRUE;
  }

  $old_setting = ini_set('mbstring.substitute_character', '"none"');

  // Get extra arguments passed in.
  $input = func_get_args();

  // If bool or strlen = 0 use var_export on that variable.
  $data = httprl_print_empty($input);

  // Merge into base array if only one argument passed in.
  if (count($data) == 1) {
    $data = array_pop($data);
  }

  // Print_r the input.
  $output = print_r($data, TRUE);

  // Remove non UTF-8 Characters.
  $encoded = FALSE;
  if (function_exists('mb_convert_encoding')) {
    $track_errors = ini_set('track_errors', '1');
    $php_errormsg = '';
    $translated = @mb_convert_encoding($output, 'UTF-8', 'auto');
    if (empty($php_errormsg)) {
      $encoded = TRUE;
    }
    $php_errormsg = '';
    ini_set('track_errors', $track_errors);
  }
  if (!$encoded) {
    $translated = @iconv('utf-8', 'utf-8//TRANSLIT//IGNORE', $output);
  }

  // Convert html entities.
  $options = ENT_QUOTES;
  if (defined('ENT_SUBSTITUTE')) {
    $options = ENT_QUOTES | ENT_SUBSTITUTE;
  }
  elseif (defined('ENT_IGNORE')) {
    $options = ENT_QUOTES | ENT_IGNORE;
  }
  $translated = htmlentities($translated, $options, 'UTF-8');

  // Make sure the UTF-8 translation didn't kill the output.
  $original_size = strlen($output);
  $translated_size = strlen($translated);
  $ratio = 0;
  if ($original_size != 0) {
    $ratio = ($original_size - $translated_size) / $original_size;
  }

  // Decide to use the original output or the translated one.
  if (!empty($translated_size) && !empty($ratio) && $ratio < 0.5 && !$output_plain_text) {
    $html_output = TRUE;
    $output = $translated;
  }
  else {
    if (!$output_plain_text) {
      $output = str_replace(array('<', '>'), array('&lt;', '&gt;'), $output);
    }
    $output = "<pre>\n" . $output . "\n</pre>";
  }

  // Remove extra new lines.
  $output = array_filter(explode("\n", $output), 'strlen');

  // Whitespace compression.
  foreach ($output as $key => $value) {
    if (str_replace('    ', '', $value) == "(") {
      $output[$key - 1] .= ' (';
      unset($output[$key]);
    }
  }

  // Replace whitespace with html markup.
  $output = implode("\n", $output);
  if (!empty($html_output)) {
    $output = str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br($output)) . '<br />';
  }
  else {
    $output .= "\n";
  }

  ini_set('mbstring.substitute_character', $old_setting);
  return $output;
}

/**
 * Helper function for determining hosts excluded from needing a proxy.
 *
 * @return bool
 *   TRUE if a proxy should be used for this host.
 */

function _httprl_use_proxy($host) {
  // To add a specific domain name in the proxy exception so that all the URL related
  // to this domain can bypass the proxy e.g. we add drupal.org in exception list then
  // it can bypass api.drupal.org, groups.drupal.org so even we can get matching pattern.
  $proxy_exceptions = httprl_variable_get('proxy_exceptions', array('localhost', '127\.0\.0\.1'));
  $use_proxy = TRUE;
  if (is_array($proxy_exceptions) && count($proxy_exceptions)) {
    foreach ($proxy_exceptions as $exception) {
      $match = preg_match("/{$exception}/is", $host);
      if ($match) {
        $use_proxy = FALSE;
        break;
      }
    }
  }
  return $use_proxy;
}

/**
 * Returns a persistent variable.
 *
 * This version will read directly from the database if value is not in global
 * $conf variable.
 *
 * Case-sensitivity of the variable_* functions depends on the database
 * collation used. To avoid problems, always use lower case for persistent
 * variable names.
 *
 * @param string $name
 *   The name of the variable to return.
 * @param mixed $default
 *   The default value to use if this variable has never been set.
 * @return mixed
 *   The value of the variable.
 *
 * @see variable_del()
 * @see variable_set()
 */
function httprl_variable_get($name, $default = NULL) {
  // Try global configuration variable first.
  global $conf;
  if (isset($conf[$name])) {
    return $conf[$name];
  }

  // Try database next if not at a full bootstrap level.
  if (function_exists('db_query') && !httprl_drupal_full_bootstrap()) {
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable} WHERE name = :name', array(':name' => $name))->fetchAllKeyed());

      // Use the default if need be.
      return isset($variables[$name]) ? $variables[$name] : $default;
    }
    else {
      $result = db_query("SELECT value FROM {variable} WHERE name = '%s'", $name);
      if (!empty($result)) {
        $result = db_result($result);
        if (!empty($result)) {
          $value = @unserialize($result);
        }
      }

      // Use the default if need be.
      return isset($value) ? $value : $default;
    }
  }
  else {
    // Return default if database is not available or if at a full bootstrap.
    return $default;
  }
}

/**
 * Run multiple functions or methods independently or chained.
 *
 * Example for running a Drupal 6 Database query.
 * @code
 * // Run 2 queries and get it's result.
 * $max = db_result(db_query('SELECT MAX(wid) FROM {watchdog}'));
 * $min = db_result(db_query('SELECT MIN(wid) FROM {watchdog}'));
 * echo $max . ' ' . $min;
 *
 * // Doing the same thing as above but with a set of arrays.
 * $max = '';
 * $min = '';
 * $args = array(
 *   array(
 *     'type' => 'function',
 *     'call' => 'db_query',
 *     'args' => array('SELECT MAX(wid) FROM {watchdog}'),
 *   ),
 *   array(
 *     'type' => 'function',
 *     'call' => 'db_result',
 *     'args' => array('last' => NULL),
 *     'return' => &$max,
 *   ),
 *   array(
 *     'type' => 'function',
 *     'call' => 'db_query',
 *     'args' => array('SELECT MIN(wid) FROM {watchdog}'),
 *   ),
 *   array(
 *     'type' => 'function',
 *     'call' => 'db_result',
 *     'args' => array('last' => NULL),
 *     'return' => &$min,
 *   ),
 * );
 * httprl_run_array($args);
 * echo $max . ' ' . $min;
 * @endcode
 *
 * Example for running a Drupal 7 Database query.
 * @code
 * // Run a query and get it's result.
 * $min = db_select('watchdog', 'w')
 *  ->fields('w', array('wid'))
 *  ->orderBy('wid', 'DESC')
 *  ->range(999, 1)
 *  ->execute()
 *  ->fetchField();
 * echo $min;
 *
 * // Doing the same thing as above but with a set of arrays.
 * $min = '';
 * $args = array(
 *   array(
 *     'type' => 'function',
 *     'call' => 'db_select',
 *     'args' => array('watchdog', 'w',),
 *    ),
 *   array(
 *     'type' => 'method',
 *     'call' => 'fields',
 *     'args' => array('w', array('wid')),
 *   ),
 *   array(
 *     'type' => 'method',
 *     'call' => 'orderBy',
 *     'args' => array('wid', 'DESC'),
 *   ),
 *   array(
 *     'type' => 'method',
 *     'call' => 'range',
 *     'args' => array(999, 1),
 *   ),
 *   array(
 *     'type' => 'method',
 *     'call' => 'execute',
 *     'args' => array(),
 *   ),
 *   array(
 *     'type' => 'method',
 *     'call' => 'fetchField',
 *     'args' => array(),
 *     'return' => &$min,
 *   ),
 * );
 * httprl_run_array($args);
 * echo $min;
 * @endcode
 *
 * @param array $array
 *   2 dimensional array
 *   array(
 *     array(
 *       'type' => function or method
 *       'call' => function name or name of object method
 *       'args' => array(
 *          List of arguments to pass in. If you set the key to last, the return
 *          value of the last thing ran will be put in this place.
 *          'last' => NULL
 *        ),
 *       'return' => what was returned from this call.
 *       'printed' => what was printed from this call.
 *       'error' => any errors that might have occurred.
 *       'last' => set the last variable to anything.
 *     )
 *   )
 */
function httprl_run_array(&$array) {
  $last = NULL;
  foreach ($array as &$data) {
    // Skip if no type is set.
    if (!isset($data['type'])) {
      continue;
    }

    // Set the last variable if so desired.
    if (isset($data['last'])) {
      $last = $data['last'];
    }

    // Replace the last key with the last thing that has been returned.
    if (isset($data['args']) && array_key_exists('last', $data['args'])) {
      $data['args']['last'] = $last;
      $data['args'] = array_values($data['args']);
    }

    // Capture output if requested.
    if (array_key_exists('printed', $data)) {
      ob_start();
    }

    // Pass by reference trick for call_user_func_array().
    $args = array();
    if (isset($data['args']) && is_array($data['args'])) {
      foreach ($data['args'] as &$arg) {
        $args[] = &$arg;
      }
    }

    // Start to capture errors.
    $track_errors = ini_set('track_errors', '1');
    $php_errormsg = '';

    // Call a function or a method.
    switch ($data['type']) {
      case 'function':
        if (function_exists($data['call'])) {
          $last = call_user_func_array($data['call'], $args);
        }
        else {
          $php_errormsg = 'Recoverable Fatal error: Call to undefined function ' . $data['call'] . '()';
        }
        break;

      case 'method':
        if (method_exists($last, $data['call'])) {
          $last = call_user_func_array(array($last, $data['call']), $args);
        }
        else {
          $php_errormsg = 'Recoverable Fatal error: Call to undefined method ' . get_class($last) . '::' . $data['call'] . '()';
        }
        break;

    }

    // Set any errors if any where thrown.
    if (!empty($php_errormsg)) {
      $data['error'] = $php_errormsg;
      ini_set('track_errors', $track_errors);
      watchdog('httprl', 'Error thrown in httprl_run_array(). <br /> @error', array('@error' => $php_errormsg),  WATCHDOG_ERROR);
    }

    // End capture.
    if (array_key_exists('printed', $data)) {
      $data['printed'] = ob_get_contents();
      ob_end_clean();
    }

    // Set what was returned from each call.
    if (array_key_exists('return', $data)) {
      $data['return'] = $last;
    }
  }

  return array('args' => array($array));
}

/**
 * Run a single function.
 *
 * @param string $function
 *   Name of function to run.
 * @param array $input_args
 *   list of arguments to pass along to the function.
 */
function httprl_run_function($function, &$input_args) {
  // Pass by reference trick for call_user_func_array().
  $args = array();
  foreach ($input_args as &$arg) {
    $args[] = &$arg;
  }

  // Capture anything printed out.
  ob_start();

  // Start to capture errors.
  $track_errors = ini_set('track_errors', '1');
  $php_errormsg = '';

  // Run function.
  $return = NULL;
  // Do not let an exception cause issues.
  try {
    if (function_exists($function)) {
      $return = call_user_func_array($function, $args);
    }
    else {
      $php_errormsg = 'Recoverable Fatal error: Call to undefined function ' . $function . '()';
    }
  }
  catch (Exception $e) {
    $php_errormsg = $e;
  }

  $printed = ob_get_contents();
  ob_end_clean();

  // Create data array.
  $data = array('return' => $return, 'args' => $args, 'printed' => $printed);

  // Set any errors if any where thrown.
  if (!empty($php_errormsg)) {
    $data['error'] = $php_errormsg;
    ini_set('track_errors', $track_errors);
    watchdog('httprl', 'Error thrown in httprl_run_function(). <br /> @error', array('@error' => $php_errormsg), WATCHDOG_ERROR);
  }

  return $data;
}

/**
 * Implements hook_boot().
 */
function httprl_boot() {
  global $base_root;
  $full_url = $base_root . request_uri();

  // Return if this is not a httprl_async_function_callback request.
  if (   strpos($full_url, '/httprl_async_function_callback') === FALSE
      || $_SERVER['REQUEST_METHOD'] !== 'POST'
      || empty($_POST['master_key'])
      || empty($_POST['temp_key'])
      || strpos($_POST['temp_key'], 'httprl_') !== 0
      || !empty($_POST['function'])
        ) {
    return NULL;
  }

  // Load httprl.async.inc.
  if (defined('DRUPAL_ROOT')) {
    require_once DRUPAL_ROOT . '/' . dirname(drupal_get_filename('module', 'httprl')) . '/httprl.async.inc';
  }
  else {
    require_once './' . dirname(drupal_get_filename('module', 'httprl')) . '/httprl.async.inc';
  }
  httprl_async_page();
}

/**
 * Gets the private key variable.
 *
 * @return string
 *   The private key.
 */
function httprl_drupal_get_private_key() {
  $full_bootstrap = httprl_drupal_full_bootstrap();

  $private_key = $full_bootstrap ? drupal_get_private_key() : httprl_variable_get('drupal_private_key', 0);
  return $private_key;
}

/**
 * Performs end-of-request tasks and/or call exit directly.
 */
function httprl_call_exit() {
  if (defined('VERSION') && substr(VERSION, 0, 1) >= 7 && drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) {
    // Do not write the session.
    drupal_save_session(FALSE);
    drupal_exit();
  }
  else {
    session_save_session(FALSE);
    exit;
  }
}

/**
 * Sees if Drupal has been fully booted.
 *
 * @return Bool
 *   TRUE if DRUPAL_BOOTSTRAP_FULL.
 *   FALSE if not DRUPAL_BOOTSTRAP_FULL.
 */
function httprl_drupal_full_bootstrap() {
  static $full_bootstrap;
  if (!isset($full_bootstrap)) {
    // See if a full bootstrap has been done given the Drupal version.
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      $level = drupal_bootstrap();
      $full_bootstrap = ($level == DRUPAL_BOOTSTRAP_FULL) ? TRUE : FALSE;
    }
    else {
      $full_bootstrap = isset($GLOBALS['multibyte']) ? TRUE : FALSE;
    }
  }
  return $full_bootstrap;
}


/**
 * Sees if httprl can run a background callback.
 *
 * @return Bool
 *   TRUE or FALSE.
 */
function httprl_is_background_callback_capable() {
  // Check if site is offline.
  if ((defined('VERSION') && substr(VERSION, 0, 1) >= 7 && httprl_variable_get('maintenance_mode', 0)) || httprl_variable_get('site_offline', 0)) {
    return FALSE;
  }
  // Check that the httprl_background_callback variable is enabled.
  if (!httprl_variable_get('httprl_background_callback', HTTPRL_BACKGROUND_CALLBACK)) {
    return FALSE;
  }
  // Check that the callback in menu works.
  if (   httprl_drupal_full_bootstrap()
      && function_exists('menu_get_item')
      &&  !menu_get_item('httprl_async_function_callback')
        ) {
    return FALSE;
  }

  // All checks passed.
  return TRUE;
}

/**
 * Sets the global user to the given user ID.
 *
 * @param int $uid
 *   Integer specifying the user ID to load.
 */
function httprl_set_user($uid) {
  $account = user_load($uid);
  if (!empty($account)) {
    $GLOBALS['user'] = $account;
    if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) {
      drupal_save_session(FALSE);
    }
    else {
      session_save_session(FALSE);
    }
    return TRUE;
  }
}

/**
 * Sets the global $_GET['q'] parameter.
 *
 * @param string $q
 *   Internal URL.
 */
function httprl_set_q($q) {
  $_GET['q'] = $q;
}

/**
 * Queue Callback to run In a New Process.
 *
 * @see call_user_func_array()
 *
 * @param callback
 *   The callable to be called.
 * @param param_arr
 *   The parameters to be passed to the callback, as an indexed array.
 * @param $return
 *   Set to TRUE if you want something returned.
 * @param $httprl_options
 *   Options to pass along to httprl_queue_background_callback.
 * @return
 *   Reference to the return variable OR NULL if $return is FALSE.
 */
function &httprl_qcinp($callback, $param_arr = array(), $return = TRUE, $httprl_options = array()) {
  $return_var = NULL;

  // Setup callback options array.
  $callback_options[0]['function'] = $callback;
  if ($return) {
    $return_var = '';
    $callback_options[0]['return'] = &$return_var;
  }
  if (isset($httprl_options['context'])) {
    $callback_options[0]['context'] = $httprl_options['context'];
    unset($httprl_options['context']);
  }
  $callback_options[0]['options'] = $httprl_options;

  // Include function arguments.
  $callback_options = array_merge($callback_options, $param_arr);

  // Queue up the request.
  httprl_queue_background_callback($callback_options);

  return $return_var;
}

/**
 * Given an array of data, use multiple processes to crunch it.
 *
 * Similar to Map Reduce.
 *
 * @see http://php.net/array-chunk#63394
 *
 * @param $callback
 *   The function to run
 * @param $data
 *   The data to process.
 * @return
 *   Array of returned results.
 */
function httprl_batch_callback($callback, $data, $options = array()) {
  // Set defaults.
  $results = array();
  $unified_result = NULL;
  $number_of_items = count($data);
  $options += array(
    'max_batchsize' => 30,
    'threads' => 3,
    'timeout' => 120,
    'multiple_helper' => FALSE,
  );

  // Shrink batchsize to evenly distribute workload if needed.
  if ($number_of_items < $options['max_batchsize']*$options['threads']) {
    $options['max_batchsize'] = ceil($number_of_items/$options['threads']);
  }

  // Chunk the data.
  $data = array_chunk($data, $options['max_batchsize'], TRUE);

  // Convert options to httprl_queue_background_callback options.
  unset($options['max_batchsize']);
  $options['domain_connections'] = $options['threads'];
  unset($options['threads']);
  $multiple = $options['multiple_helper'];
  unset($options['multiple_helper']);

  // Queue up the processes.
  if ($multiple) {
    foreach ($data as $key => $values) {
      $results[$key] = &httprl_qcinp('httprl_run_multiple', array($callback, $values), TRUE, $options);
    }
  }
  else {
    foreach ($data as $key => $values) {
      $results[$key] = &httprl_qcinp($callback, array($values), TRUE, $options);
    }
  }

  // Execute in parallel.
  httprl_send_request();

  // Try to merge the results into one.
  $unified = TRUE;
  $is_assoc = TRUE;
  foreach ($results as $key => $value) {
    if (is_null($unified_result)) {
      $unified_result = $results[$key];
    }
    elseif (is_string($results[$key]) && is_string($unified_result)) {
      $unified_result .= $results[$key];
    }
    elseif (is_array($results[$key]) && is_array($unified_result)) {
      if ($is_assoc && httprl_is_array_assoc($results[$key]) && httprl_is_array_assoc($unified_result)) {
        $unified_result = httprl_lossless_assoc_array_merge($unified_result, $results[$key]);
      }
      else {
        $is_assoc = FALSE;
        $unified_result += $results[$key];
      }
    }
    else {
      $unified = FALSE;
      break;
    }
  }

  // Return results.
  if ($unified) {
    return $unified_result;
  }
  else {
    return $results;
  }
}

/**
 * Given an array return TRUE if all keys are numeric.
 *
 * @see http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-sequential/2444661#2444661
 *
 * @param $array
 *   The data to process.
 * @return
 *   TRUE or FALSE.
 */
function httprl_is_array_assoc($array) {
  return ctype_digit(implode('', array_keys($array)));
}

/**
 * Merge multiple associative arrays into one.
 *
 * @see http://stackoverflow.com/questions/2148694/how-to-combine-2-associative-arrays-in-php-such-that-we-do-not-overwrite-any-dup/2152054#2152054
 *
 * @param ...
 *   Arrays to merge.
 * @return
 *   Merged array.
 */
function httprl_lossless_assoc_array_merge() {
  $arrays = func_get_args();
  $data = array();
  foreach ($arrays as $a) {
    foreach ($a as $k => $v) {
      if (isset($data[$k])) {
        $data[] = $v;
      }
      else {
        $data[$k] = $v;
      }
    }
  }
  return $data;
}

/**
 * Run array of data through callback.
 *
 * @param $data
 *   The data to process.
 * @param $callback
 *   The function to run
 * @return
 *   Array of results.
 */
function httprl_run_multiple($callback, $data) {
  $results = array();
  foreach ($data as $key => $values) {
    $results[$key] = call_user_func_array($callback, array($values));
  }
  return $results;
}

/**
 * Run the callback with the given params in the background.
 *
 * @param string $callback
 *   The function to run
 * @param array $param_arr
 *   The arguments to pass along to the function.
 */
function httprl_call_user_func_array_async($callback, array $param_arr) {
  if (!httprl_is_background_callback_capable()) {
    call_user_func_array($callback, $param_arr);
  }
  else {
    // Setup callback options array; call $callback in the background.
    $callback_options = array_merge(array(array('function' => $callback)), $param_arr);
    // Queue up the request.
    httprl_queue_background_callback($callback_options);
    // Execute request.
    httprl_send_request();
  }
}

/**
 * Cache a function; regenerate return value in the background.
 *
 * @param callable $callback
 *   The callable to be called.
 * @param array $param_arr
 *   The parameters to be passed to the callback, as an indexed array.
 * @param array $options
 *   An associative array with the keys cache_lifetime_min, cache_lifetime_max,
 *   bin, lock_timeout, and return_null_cache_miss.
 *
 * @return mixed
 *   Array of results.
 */
function httprl_call_user_func_array_cache($callback, array $param_arr = array(), array $options = array()) {
  $options += array(
    // How long to wait until regenerating this cached value.
    'cache_lifetime_min' => 0,
    // How long the cache can still be used; 3600 = 1 hour.
    'cache_lifetime_max' => 3600,
    // What cache bin to use.
    'bin' => 'cache',
    // Lock timeout.
    'lock_timeout' => 30.0,
    // Return NULL if cache miss.
    'return_null_cache_miss' => FALSE,
  );
  // The cache id for this call.
  if (is_string($callback)) {
    $cid = __FUNCTION__ . ':' . $callback . ':' . drupal_hash_base64(serialize(array($param_arr)));
  }
  else {
    $cid = __FUNCTION__ . ':' . drupal_hash_base64(serialize(array($callback, $param_arr)));
  }

  $cache = cache_get($cid, $options['bin']);
  // Don't use the cache if it's not there or the cached item is older than
  // the max cache lifetime.
  if (empty($options['return_null_cache_miss']) && (!isset($cache->data) || $cache->created < REQUEST_TIME - $options['cache_lifetime_max'])) {
    if (httprl_acquire_headless_lock($cid . ':run_function', $options['lock_timeout'])) {
      $cache = new stdClass();
      // Run the callback.
      $cache->data = call_user_func_array($callback, $param_arr);
      // Save the cached data.
      cache_set($cid, $cache->data, $options['bin'], $options['cache_lifetime_max'] + time());
      // Release lock.
      httprl_lock_release($cid . ':run_function');
    }
    else {
      lock_wait($cid . ':run_function', $options['lock_timeout']);
      $cache = cache_get($cid);
      if (!isset($cache->data)) {
        $cache = new stdClass();
        // Run the callback.
        $cache->data = call_user_func_array($callback, $param_arr);
        // Save the cached data.
        cache_set($cid, $cache->data, $options['bin'], $options['cache_lifetime_max'] + time());
      }
    }
    // Release the lock if the cache lifetime is 0.
    if ($options['cache_lifetime_max'] == 0) {
      httprl_lock_release($cid);
    }
  }
  else {
    // Regenerate if cache is older than min cache lifetime and no one else is
    // doing the same thing.
    if (empty($cache) || $cache->created < REQUEST_TIME - $options['cache_lifetime_min']
      && lock_may_be_available($cid . ':run_function')
      && httprl_acquire_headless_lock($cid, $options['lock_timeout'])
    ) {
      // Get all function arguments.
      $args = array($callback, $param_arr, $options);
      // Call this function again in another process with max lifetime set to
      // zero.
      _httprl_run_functions_once_on_shutdown_array_cache($args);
    }
  }
  return isset($cache->data) ? $cache->data : NULL;
}

/**
 * Cache function helper. Combine duplicates & run them in a shutdown function.
 *
 * @param callable $args
 *   The callable to be called.
 */
function _httprl_run_functions_once_on_shutdown_array_cache($args = array()) {
  $run_list = &drupal_static('_httprl_run_functions_once_on_shutdown_array_cache', array());
  if (!empty($args)) {
    if (empty($run_list)) {
      // Register a shutdown function is this is the first time being ran.
      register_shutdown_function('_httprl_run_functions_once_on_shutdown_array_cache');
    }
    $run_list[] = $args;
  }
  else {
    unset($args);
    $will_run = array();
    $already_ran = array();
    foreach ($run_list as $args) {
      if (empty($args)) {
        continue;
      }
      if (is_string($args[0])) {
        $cid = __FUNCTION__ . ':' . $args[0] . ':' . drupal_hash_base64(serialize(array($args[1])));
      }
      else {
        $cid = __FUNCTION__ . ':' . drupal_hash_base64(serialize(array($args[0], $args[1])));
      }
      if (empty($already_ran[$cid])) {
        $already_ran[$cid] = TRUE;
        $will_run[] = $args;
      }
    }
    if (!empty($will_run)) {
      httprl_call_user_func_array_async('_httprl_run_cache_functions', array($will_run));
    }
  }
}

/**
 * Cache function helper. Runs the array through the cache function.
 *
 * @param callable $functions_to_cache
 *   The callable to be called.
 */
function _httprl_run_cache_functions($functions_to_cache) {
  foreach ($functions_to_cache as $args) {
    // Set max to zero so this gets regenerated in the background.
    // Also do not skip on a cache miss.
    $args[2]['cache_lifetime_max'] = 0;
    $args[2]['return_null_cache_miss'] = FALSE;
    call_user_func_array('httprl_call_user_func_array_cache', $args);
  }
}

/**
 * Get hostname of this server.

 * @return string
 *   hostname or empty string; does not contain the port number.
 */
function httprl_get_hostname() {
  if (!empty($_SERVER['HTTP_HOST'])) {
    // If HTTP_HOST contains the port, use SERVER_NAME.
    if (strpos($_SERVER['HTTP_HOST'], ':') !== FALSE) {
      return $_SERVER['SERVER_NAME'];
    }
    return $_SERVER['HTTP_HOST'];
  }
  elseif (!empty($_SERVER['SERVER_NAME'])) {
    return $_SERVER['SERVER_NAME'];
  }
  return '';
}