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
|
/*
* "$Id: http.c 7721 2008-07-11 22:48:49Z mike $"
*
* HTTP routines for the Common UNIX Printing System (CUPS).
*
* Copyright 2007-2008 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* This file contains Kerberos support code, copyright 2006 by
* Jelmer Vernooij.
*
* These coded instructions, statements, and computer programs are the
* property of Apple Inc. and are protected by Federal copyright
* law. Distribution and use rights are outlined in the file "LICENSE.txt"
* which should have been included with this file. If this file is
* file is missing or damaged, see the license at "http://www.cups.org/".
*
* This file is subject to the Apple OS-Developed Software exception.
*
* Contents:
*
* _httpBIOMethods() - Get the OpenSSL BIO methods for HTTP connections.
* httpBlocking() - Set blocking/non-blocking behavior on a connection.
* httpCheck() - Check to see if there is a pending response from
* the server.
* httpClearCookie() - Clear the cookie value(s).
* httpClearFields() - Clear HTTP request fields.
* httpClose() - Close an HTTP connection...
* httpConnect() - Connect to a HTTP server.
* httpConnectEncrypt() - Connect to a HTTP server using encryption.
* httpDelete() - Send a DELETE request to the server.
* httpEncryption() - Set the required encryption on the link.
* httpError() - Get the last error on a connection.
* httpFlush() - Flush data from a HTTP connection.
* httpFlushWrite() - Flush data in write buffer.
* httpGet() - Send a GET request to the server.
* httpGetAuthString() - Get the current authorization string.
* httpGetBlocking() - Get the blocking/non-block state of a connection.
* httpGetCookie() - Get any cookie data from the response.
* httpGetFd() - Get the file descriptor associated with a
* connection.
* httpGetField() - Get a field value from a request/response.
* httpGetLength() - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
* httpGetLength2() - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
* httpGetStatus() - Get the status of the last HTTP request.
* httpGetSubField() - Get a sub-field value.
* httpGets() - Get a line of text from a HTTP connection.
* httpHead() - Send a HEAD request to the server.
* httpInitialize() - Initialize the HTTP interface library and set the
* default HTTP proxy (if any).
* httpOptions() - Send an OPTIONS request to the server.
* httpPost() - Send a POST request to the server.
* httpPrintf() - Print a formatted string to a HTTP connection.
* httpPut() - Send a PUT request to the server.
* httpRead() - Read data from a HTTP connection.
* httpRead2() - Read data from a HTTP connection.
* _httpReadCDSA() - Read function for the CDSA library.
* _httpReadGNUTLS() - Read function for the GNU TLS library.
* httpReconnect() - Reconnect to a HTTP server...
* httpSetAuthString() - Set the current authorization string.
* httpSetCookie() - Set the cookie value(s)...
* httpSetExpect() - Set the Expect: header in a request.
* httpSetField() - Set the value of an HTTP header.
* httpSetLength() - Set the content-length and transfer-encoding.
* httpTrace() - Send an TRACE request to the server.
* httpUpdate() - Update the current HTTP state for incoming data.
* httpWait() - Wait for data available on a connection.
* httpWrite() - Write data to a HTTP connection.
* httpWrite2() - Write data to a HTTP connection.
* _httpWriteCDSA() - Write function for the CDSA library.
* _httpWriteGNUTLS() - Write function for the GNU TLS library.
* http_bio_ctrl() - Control the HTTP connection.
* http_bio_free() - Free OpenSSL data.
* http_bio_new() - Initialize an OpenSSL BIO structure.
* http_bio_puts() - Send a string for OpenSSL.
* http_bio_read() - Read data for OpenSSL.
* http_bio_write() - Write data for OpenSSL.
* http_field() - Return the field index for a field name.
* http_read_ssl() - Read from a SSL/TLS connection.
* http_send() - Send a request with all fields and the trailing
* blank line.
* http_setup_ssl() - Set up SSL/TLS on a connection.
* http_shutdown_ssl() - Shut down SSL/TLS on a connection.
* http_upgrade() - Force upgrade to TLS encryption.
* http_wait() - Wait for data available on a connection.
* http_write() - Write data to a connection.
* http_write_ssl() - Write to a SSL/TLS connection.
*/
/*
* Include necessary headers...
*/
#include "http-private.h"
#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#ifndef WIN32
# include <signal.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif /* !WIN32 */
#ifdef HAVE_POLL
# include <sys/poll.h>
#endif /* HAVE_POLL */
/*
* Some operating systems have done away with the Fxxxx constants for
* the fcntl() call; this works around that "feature"...
*/
#ifndef FNONBLK
# define FNONBLK O_NONBLOCK
#endif /* !FNONBLK */
/*
* Local functions...
*/
static http_field_t http_field(const char *name);
static int http_send(http_t *http, http_state_t request,
const char *uri);
static int http_wait(http_t *http, int msec, int usessl);
static int http_write(http_t *http, const char *buffer,
int length);
static int http_write_chunk(http_t *http, const char *buffer,
int length);
#ifdef HAVE_SSL
static int http_read_ssl(http_t *http, char *buf, int len);
static int http_setup_ssl(http_t *http);
static void http_shutdown_ssl(http_t *http);
static int http_upgrade(http_t *http);
static int http_write_ssl(http_t *http, const char *buf, int len);
#endif /* HAVE_SSL */
/*
* Local globals...
*/
static const char * const http_fields[] =
{
"Accept-Language",
"Accept-Ranges",
"Authorization",
"Connection",
"Content-Encoding",
"Content-Language",
"Content-Length",
"Content-Location",
"Content-MD5",
"Content-Range",
"Content-Type",
"Content-Version",
"Date",
"Host",
"If-Modified-Since",
"If-Unmodified-since",
"Keep-Alive",
"Last-Modified",
"Link",
"Location",
"Range",
"Referer",
"Retry-After",
"Transfer-Encoding",
"Upgrade",
"User-Agent",
"WWW-Authenticate"
};
#if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
/*
* BIO methods for OpenSSL...
*/
static int http_bio_write(BIO *h, const char *buf, int num);
static int http_bio_read(BIO *h, char *buf, int size);
static int http_bio_puts(BIO *h, const char *str);
static long http_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int http_bio_new(BIO *h);
static int http_bio_free(BIO *data);
static BIO_METHOD http_bio_methods =
{
BIO_TYPE_SOCKET,
"http",
http_bio_write,
http_bio_read,
http_bio_puts,
NULL, /* http_bio_gets, */
http_bio_ctrl,
http_bio_new,
http_bio_free,
NULL,
};
/*
* '_httpBIOMethods()' - Get the OpenSSL BIO methods for HTTP connections.
*/
BIO_METHOD * /* O - BIO methods for OpenSSL */
_httpBIOMethods(void)
{
return (&http_bio_methods);
}
#endif /* HAVE_SSL && HAVE_LIBSSL */
/*
* 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
*/
void
httpBlocking(http_t *http, /* I - HTTP connection */
int b) /* I - 1 = blocking, 0 = non-blocking */
{
if (http)
http->blocking = b;
}
/*
* 'httpCheck()' - Check to see if there is a pending response from the server.
*/
int /* O - 0 = no data, 1 = data available */
httpCheck(http_t *http) /* I - HTTP connection */
{
return (httpWait(http, 0));
}
/*
* 'httpClearCookie()' - Clear the cookie value(s).
*
* @since CUPS 1.1.19@
*/
void
httpClearCookie(http_t *http) /* I - HTTP connection */
{
if (!http)
return;
if (http->cookie)
{
free(http->cookie);
http->cookie = NULL;
}
}
/*
* 'httpClearFields()' - Clear HTTP request fields.
*/
void
httpClearFields(http_t *http) /* I - HTTP connection */
{
if (http)
{
memset(http->fields, 0, sizeof(http->fields));
if (http->hostname[0] == '/')
httpSetField(http, HTTP_FIELD_HOST, "localhost");
else
httpSetField(http, HTTP_FIELD_HOST, http->hostname);
if (http->field_authorization)
{
free(http->field_authorization);
http->field_authorization = NULL;
}
http->expect = (http_status_t)0;
}
}
/*
* 'httpClose()' - Close an HTTP connection...
*/
void
httpClose(http_t *http) /* I - HTTP connection */
{
#ifdef HAVE_GSSAPI
OM_uint32 minor_status; /* Minor status code */
#endif /* HAVE_GSSAPI */
DEBUG_printf(("httpClose(http=%p)\n", http));
if (!http)
return;
httpAddrFreeList(http->addrlist);
if (http->cookie)
free(http->cookie);
#ifdef HAVE_SSL
if (http->tls)
http_shutdown_ssl(http);
#endif /* HAVE_SSL */
#ifdef WIN32
closesocket(http->fd);
#else
close(http->fd);
#endif /* WIN32 */
#ifdef HAVE_GSSAPI
if (http->gssctx != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER);
if (http->gssname != GSS_C_NO_NAME)
gss_release_name(&minor_status, &http->gssname);
#endif /* HAVE_GSSAPI */
#ifdef HAVE_AUTHORIZATION_H
if (http->auth_ref)
AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
#endif /* HAVE_AUTHORIZATION_H */
httpClearFields(http);
if (http->authstring && http->authstring != http->_authstring)
free(http->authstring);
free(http);
}
/*
* 'httpConnect()' - Connect to a HTTP server.
*/
http_t * /* O - New HTTP connection */
httpConnect(const char *host, /* I - Host to connect to */
int port) /* I - Port number */
{
http_encryption_t encryption; /* Type of encryption to use */
/*
* Set the default encryption status...
*/
if (port == 443)
encryption = HTTP_ENCRYPT_ALWAYS;
else
encryption = HTTP_ENCRYPT_IF_REQUESTED;
return (httpConnectEncrypt(host, port, encryption));
}
/*
* 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
*/
http_t * /* O - New HTTP connection */
httpConnectEncrypt(
const char *host, /* I - Host to connect to */
int port, /* I - Port number */
http_encryption_t encryption) /* I - Type of encryption to use */
{
http_t *http; /* New HTTP connection */
http_addrlist_t *addrlist; /* Host address data */
char service[255]; /* Service name */
DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)\n",
host ? host : "(null)", port, encryption));
if (!host)
return (NULL);
httpInitialize();
/*
* Lookup the host...
*/
sprintf(service, "%d", port);
if ((addrlist = httpAddrGetList(host, AF_UNSPEC, service)) == NULL)
return (NULL);
/*
* Allocate memory for the structure...
*/
if ((http = calloc(sizeof(http_t), 1)) == NULL)
{
httpAddrFreeList(addrlist);
return (NULL);
}
http->version = HTTP_1_1;
http->blocking = 1;
http->activity = time(NULL);
http->fd = -1;
#ifdef HAVE_GSSAPI
http->gssctx = GSS_C_NO_CONTEXT;
http->gssname = GSS_C_NO_NAME;
#endif /* HAVE_GSSAPI */
/*
* Set the encryption status...
*/
if (port == 443) /* Always use encryption for https */
http->encryption = HTTP_ENCRYPT_ALWAYS;
else
http->encryption = encryption;
/*
* Loop through the addresses we have until one of them connects...
*/
strlcpy(http->hostname, host, sizeof(http->hostname));
/*
* Connect to the remote system...
*/
http->addrlist = addrlist;
if (!httpReconnect(http))
return (http);
/*
* Could not connect to any known address - bail out!
*/
httpAddrFreeList(addrlist);
free(http);
return (NULL);
}
/*
* 'httpDelete()' - Send a DELETE request to the server.
*/
int /* O - Status of call (0 = success) */
httpDelete(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI to delete */
{
return (http_send(http, HTTP_DELETE, uri));
}
/*
* 'httpEncryption()' - Set the required encryption on the link.
*/
int /* O - -1 on error, 0 on success */
httpEncryption(http_t *http, /* I - HTTP connection */
http_encryption_t e) /* I - New encryption preference */
{
DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http, e));
#ifdef HAVE_SSL
if (!http)
return (0);
http->encryption = e;
if ((http->encryption == HTTP_ENCRYPT_ALWAYS && !http->tls) ||
(http->encryption == HTTP_ENCRYPT_NEVER && http->tls))
return (httpReconnect(http));
else if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls)
return (http_upgrade(http));
else
return (0);
#else
if (e == HTTP_ENCRYPT_ALWAYS || e == HTTP_ENCRYPT_REQUIRED)
return (-1);
else
return (0);
#endif /* HAVE_SSL */
}
/*
* 'httpError()' - Get the last error on a connection.
*/
int /* O - Error code (errno) value */
httpError(http_t *http) /* I - HTTP connection */
{
if (http)
return (http->error);
else
return (EINVAL);
}
/*
* 'httpFlush()' - Flush data from a HTTP connection.
*/
void
httpFlush(http_t *http) /* I - HTTP connection */
{
char buffer[8192]; /* Junk buffer */
int blocking; /* To block or not to block */
DEBUG_printf(("httpFlush(http=%p), state=%d\n", http, http->state));
/*
* Temporarily set non-blocking mode so we don't get stuck in httpRead()...
*/
blocking = http->blocking;
http->blocking = 0;
/*
* Read any data we can...
*/
while (httpRead2(http, buffer, sizeof(buffer)) > 0);
/*
* Restore blocking and reset the connection if we didn't get all of
* the remaining data...
*/
http->blocking = blocking;
if (http->state != HTTP_WAITING && http->fd >= 0)
{
/*
* Didn't get the data back, so close the current connection.
*/
http->state = HTTP_WAITING;
#ifdef HAVE_SSL
if (http->tls)
http_shutdown_ssl(http);
#endif /* HAVE_SSL */
#ifdef WIN32
closesocket(http->fd);
#else
close(http->fd);
#endif /* WIN32 */
http->fd = -1;
}
}
/*
* 'httpFlushWrite()' - Flush data in write buffer.
*
* @since CUPS 1.2@
*/
int /* O - Bytes written or -1 on error */
httpFlushWrite(http_t *http) /* I - HTTP connection */
{
int bytes; /* Bytes written */
DEBUG_printf(("httpFlushWrite(http=%p)\n", http));
if (!http || !http->wused)
return (0);
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
bytes = http_write_chunk(http, http->wbuffer, http->wused);
else
bytes = http_write(http, http->wbuffer, http->wused);
http->wused = 0;
return (bytes);
}
/*
* 'httpGet()' - Send a GET request to the server.
*/
int /* O - Status of call (0 = success) */
httpGet(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI to get */
{
return (http_send(http, HTTP_GET, uri));
}
/*
* 'httpGetAuthString()' - Get the current authorization string.
*
* The authorization string is set by cupsDoAuthentication() and
* httpSetAuthString(). Use httpGetAuthString() to retrieve the
* string to use with httpSetField() for the HTTP_FIELD_AUTHORIZATION
* value.
*
* @since CUPS 1.3@
*/
char * /* O - Authorization string */
httpGetAuthString(http_t *http) /* I - HTTP connection */
{
if (http)
return (http->authstring);
else
return (NULL);
}
/*
* 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
*
* @since CUPS 1.2@
*/
int /* O - 1 if blocking, 0 if non-blocking */
httpGetBlocking(http_t *http) /* I - HTTP connection */
{
return (http ? http->blocking : 0);
}
/*
* 'httpGetCookie()' - Get any cookie data from the response.
*
* @since CUPS 1.1.19@
*/
const char * /* O - Cookie data or NULL */
httpGetCookie(http_t *http) /* I - HTTP connecion */
{
return (http ? http->cookie : NULL);
}
/*
* 'httpGetFd()' - Get the file descriptor associated with a connection.
*
* @since CUPS 1.2@
*/
int /* O - File descriptor or -1 if none */
httpGetFd(http_t *http) /* I - HTTP connection */
{
return (http ? http->fd : -1);
}
/*
* 'httpGetField()' - Get a field value from a request/response.
*/
const char * /* O - Field value */
httpGetField(http_t *http, /* I - HTTP connection */
http_field_t field) /* I - Field to get */
{
if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
return (NULL);
else if (field == HTTP_FIELD_AUTHORIZATION &&
http->field_authorization)
{
/*
* Special case for WWW-Authenticate: as its contents can be
* longer than HTTP_MAX_VALUE...
*/
return (http->field_authorization);
}
else
return (http->fields[field]);
}
/*
* 'httpGetLength()' - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
*
* This function is deprecated and will not return lengths larger than
* 2^31 - 1; use httpGetLength2() instead.
*
* @deprecated@
*/
int /* O - Content length */
httpGetLength(http_t *http) /* I - HTTP connection */
{
/*
* Get the read content length and return the 32-bit value.
*/
if (http)
{
httpGetLength2(http);
return (http->_data_remaining);
}
else
return (-1);
}
/*
* 'httpGetLength2()' - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
*
* This function returns the complete content length, even for
* content larger than 2^31 - 1.
*
* @since CUPS 1.2@
*/
off_t /* O - Content length */
httpGetLength2(http_t *http) /* I - HTTP connection */
{
DEBUG_printf(("httpGetLength2(http=%p), state=%d\n", http, http->state));
if (!http)
return (-1);
if (!strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"))
{
DEBUG_puts("httpGetLength2: chunked request!");
http->data_encoding = HTTP_ENCODE_CHUNKED;
http->data_remaining = 0;
}
else
{
http->data_encoding = HTTP_ENCODE_LENGTH;
/*
* The following is a hack for HTTP servers that don't send a
* content-length or transfer-encoding field...
*
* If there is no content-length then the connection must close
* after the transfer is complete...
*/
if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
{
/*
* Default content length is 0 for errors and 2^31-1 for other
* successful requests...
*/
if (http->status >= HTTP_MULTIPLE_CHOICES)
http->data_remaining = 0;
else
http->data_remaining = 2147483647;
}
else
http->data_remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH],
NULL, 10);
DEBUG_printf(("httpGetLength2: content_length=" CUPS_LLFMT "\n",
CUPS_LLCAST http->data_remaining));
}
if (http->data_remaining <= INT_MAX)
http->_data_remaining = (int)http->data_remaining;
else
http->_data_remaining = INT_MAX;
return (http->data_remaining);
}
/*
* 'httpGetStatus()' - Get the status of the last HTTP request.
*
* @since CUPS 1.2@
*/
http_status_t /* O - HTTP status */
httpGetStatus(http_t *http) /* I - HTTP connection */
{
return (http ? http->status : HTTP_ERROR);
}
/*
* 'httpGetSubField()' - Get a sub-field value.
*
* @deprecated@
*/
char * /* O - Value or NULL */
httpGetSubField(http_t *http, /* I - HTTP connection */
http_field_t field, /* I - Field index */
const char *name, /* I - Name of sub-field */
char *value) /* O - Value string */
{
return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE));
}
/*
* 'httpGetSubField2()' - Get a sub-field value.
*
* @since CUPS 1.2@
*/
char * /* O - Value or NULL */
httpGetSubField2(http_t *http, /* I - HTTP connection */
http_field_t field, /* I - Field index */
const char *name, /* I - Name of sub-field */
char *value, /* O - Value string */
int valuelen) /* I - Size of value buffer */
{
const char *fptr; /* Pointer into field */
char temp[HTTP_MAX_VALUE], /* Temporary buffer for name */
*ptr, /* Pointer into string buffer */
*end; /* End of value buffer */
DEBUG_printf(("httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)\n",
http, field, name, value, valuelen));
if (!http || !name || !value || valuelen < 2 ||
field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
return (NULL);
end = value + valuelen - 1;
for (fptr = http->fields[field]; *fptr;)
{
/*
* Skip leading whitespace...
*/
while (isspace(*fptr & 255))
fptr ++;
if (*fptr == ',')
{
fptr ++;
continue;
}
/*
* Get the sub-field name...
*/
for (ptr = temp;
*fptr && *fptr != '=' && !isspace(*fptr & 255) &&
ptr < (temp + sizeof(temp) - 1);
*ptr++ = *fptr++);
*ptr = '\0';
DEBUG_printf(("httpGetSubField: name=\"%s\"\n", temp));
/*
* Skip trailing chars up to the '='...
*/
while (isspace(*fptr & 255))
fptr ++;
if (!*fptr)
break;
if (*fptr != '=')
continue;
/*
* Skip = and leading whitespace...
*/
fptr ++;
while (isspace(*fptr & 255))
fptr ++;
if (*fptr == '\"')
{
/*
* Read quoted string...
*/
for (ptr = value, fptr ++;
*fptr && *fptr != '\"' && ptr < end;
*ptr++ = *fptr++);
*ptr = '\0';
while (*fptr && *fptr != '\"')
fptr ++;
if (*fptr)
fptr ++;
}
else
{
/*
* Read unquoted string...
*/
for (ptr = value;
*fptr && !isspace(*fptr & 255) && *fptr != ',' && ptr < end;
*ptr++ = *fptr++);
*ptr = '\0';
while (*fptr && !isspace(*fptr & 255) && *fptr != ',')
fptr ++;
}
DEBUG_printf(("httpGetSubField: value=\"%s\"\n", value));
/*
* See if this is the one...
*/
if (!strcmp(name, temp))
return (value);
}
value[0] = '\0';
return (NULL);
}
/*
* 'httpGets()' - Get a line of text from a HTTP connection.
*/
char * /* O - Line or NULL */
httpGets(char *line, /* I - Line to read into */
int length, /* I - Max length of buffer */
http_t *http) /* I - HTTP connection */
{
char *lineptr, /* Pointer into line */
*lineend, /* End of line */
*bufptr, /* Pointer into input buffer */
*bufend; /* Pointer to end of buffer */
int bytes, /* Number of bytes read */
eol; /* End-of-line? */
DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line, length, http));
if (http == NULL || line == NULL)
return (NULL);
/*
* Read a line from the buffer...
*/
lineptr = line;
lineend = line + length - 1;
eol = 0;
while (lineptr < lineend)
{
/*
* Pre-load the buffer as needed...
*/
#ifdef WIN32
WSASetLastError(0);
#else
errno = 0;
#endif /* WIN32 */
while (http->used == 0)
{
/*
* No newline; see if there is more data to be read...
*/
if (!http->blocking && !http_wait(http, 10000, 1))
{
DEBUG_puts("httpGets: Timed out!");
#ifdef WIN32
http->error = WSAETIMEDOUT;
#else
http->error = ETIMEDOUT;
#endif /* WIN32 */
return (NULL);
}
#ifdef HAVE_SSL
if (http->tls)
bytes = http_read_ssl(http, http->buffer + http->used,
HTTP_MAX_BUFFER - http->used);
else
#endif /* HAVE_SSL */
bytes = recv(http->fd, http->buffer + http->used,
HTTP_MAX_BUFFER - http->used, 0);
DEBUG_printf(("httpGets: read %d bytes...\n", bytes));
if (bytes < 0)
{
/*
* Nope, can't get a line this time...
*/
#ifdef WIN32
if (WSAGetLastError() != http->error)
{
http->error = WSAGetLastError();
continue;
}
DEBUG_printf(("httpGets: recv() error %d!\n", WSAGetLastError()));
#else
DEBUG_printf(("httpGets: recv() error %d!\n", errno));
if (errno == EINTR)
continue;
else if (errno != http->error)
{
http->error = errno;
continue;
}
#endif /* WIN32 */
return (NULL);
}
else if (bytes == 0)
{
http->error = EPIPE;
return (NULL);
}
/*
* Yup, update the amount used...
*/
http->used += bytes;
}
/*
* Now copy as much of the current line as possible...
*/
for (bufptr = http->buffer, bufend = http->buffer + http->used;
lineptr < lineend && bufptr < bufend;)
{
if (*bufptr == 0x0a)
{
eol = 1;
bufptr ++;
break;
}
else if (*bufptr == 0x0d)
bufptr ++;
else
*lineptr++ = *bufptr++;
}
http->used -= (int)(bufptr - http->buffer);
if (http->used > 0)
memmove(http->buffer, bufptr, http->used);
if (eol)
{
/*
* End of line...
*/
http->activity = time(NULL);
*lineptr = '\0';
DEBUG_printf(("httpGets: Returning \"%s\"\n", line));
return (line);
}
}
DEBUG_puts("httpGets: No new line available!");
return (NULL);
}
/*
* 'httpHead()' - Send a HEAD request to the server.
*/
int /* O - Status of call (0 = success) */
httpHead(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI for head */
{
return (http_send(http, HTTP_HEAD, uri));
}
/*
* 'httpInitialize()' - Initialize the HTTP interface library and set the
* default HTTP proxy (if any).
*/
void
httpInitialize(void)
{
#ifdef HAVE_LIBSSL
# ifndef WIN32
struct timeval curtime; /* Current time in microseconds */
# endif /* !WIN32 */
int i; /* Looping var */
unsigned char data[1024]; /* Seed data */
#endif /* HAVE_LIBSSL */
#ifdef WIN32
WSADATA winsockdata; /* WinSock data */
static int initialized = 0; /* Has WinSock been initialized? */
if (!initialized)
WSAStartup(MAKEWORD(1,1), &winsockdata);
#elif !defined(SO_NOSIGPIPE)
/*
* Ignore SIGPIPE signals...
*/
# ifdef HAVE_SIGSET
sigset(SIGPIPE, SIG_IGN);
# elif defined(HAVE_SIGACTION)
struct sigaction action; /* POSIX sigaction data */
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &action, NULL);
# else
signal(SIGPIPE, SIG_IGN);
# endif /* !SO_NOSIGPIPE */
#endif /* WIN32 */
#ifdef HAVE_GNUTLS
gnutls_global_init();
#endif /* HAVE_GNUTLS */
#ifdef HAVE_LIBSSL
SSL_load_error_strings();
SSL_library_init();
/*
* Using the current time is a dubious random seed, but on some systems
* it is the best we can do (on others, this seed isn't even used...)
*/
#ifdef WIN32
#else
gettimeofday(&curtime, NULL);
srand(curtime.tv_sec + curtime.tv_usec);
#endif /* WIN32 */
for (i = 0; i < sizeof(data); i ++)
data[i] = rand(); /* Yes, this is a poor source of random data... */
RAND_seed(&data, sizeof(data));
#endif /* HAVE_LIBSSL */
}
/*
* 'httpOptions()' - Send an OPTIONS request to the server.
*/
int /* O - Status of call (0 = success) */
httpOptions(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI for options */
{
return (http_send(http, HTTP_OPTIONS, uri));
}
/*
* 'httpPost()' - Send a POST request to the server.
*/
int /* O - Status of call (0 = success) */
httpPost(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI for post */
{
return (http_send(http, HTTP_POST, uri));
}
/*
* 'httpPrintf()' - Print a formatted string to a HTTP connection.
*
* @private@
*/
int /* O - Number of bytes written */
httpPrintf(http_t *http, /* I - HTTP connection */
const char *format, /* I - printf-style format string */
...) /* I - Additional args as needed */
{
int bytes; /* Number of bytes to write */
char buf[16384]; /* Buffer for formatted string */
va_list ap; /* Variable argument pointer */
DEBUG_printf(("httpPrintf(http=%p, format=\"%s\", ...)\n", http, format));
va_start(ap, format);
bytes = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
DEBUG_printf(("httpPrintf: %s", buf));
if (http->data_encoding == HTTP_ENCODE_FIELDS)
return (httpWrite2(http, buf, bytes));
else
{
if (http->wused)
{
DEBUG_puts(" flushing existing data...");
if (httpFlushWrite(http) < 0)
return (-1);
}
return (http_write(http, buf, bytes));
}
}
/*
* 'httpPut()' - Send a PUT request to the server.
*/
int /* O - Status of call (0 = success) */
httpPut(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI to put */
{
return (http_send(http, HTTP_PUT, uri));
}
/*
* 'httpRead()' - Read data from a HTTP connection.
*
* This function is deprecated. Use the httpRead2() function which can
* read more than 2GB of data.
*
* @deprecated@
*/
int /* O - Number of bytes read */
httpRead(http_t *http, /* I - HTTP connection */
char *buffer, /* I - Buffer for data */
int length) /* I - Maximum number of bytes */
{
return ((int)httpRead2(http, buffer, length));
}
/*
* 'httpRead2()' - Read data from a HTTP connection.
*
* @since CUPS 1.2@
*/
ssize_t /* O - Number of bytes read */
httpRead2(http_t *http, /* I - HTTP connection */
char *buffer, /* I - Buffer for data */
size_t length) /* I - Maximum number of bytes */
{
ssize_t bytes; /* Bytes read */
char len[32]; /* Length string */
DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
http, buffer, length));
if (http == NULL || buffer == NULL)
return (-1);
http->activity = time(NULL);
if (length <= 0)
return (0);
if (http->data_encoding == HTTP_ENCODE_CHUNKED &&
http->data_remaining <= 0)
{
DEBUG_puts("httpRead2: Getting chunk length...");
if (httpGets(len, sizeof(len), http) == NULL)
{
DEBUG_puts("httpRead2: Could not get length!");
return (0);
}
http->data_remaining = strtoll(len, NULL, 16);
if (http->data_remaining < 0)
{
DEBUG_puts("httpRead2: Negative chunk length!");
return (0);
}
}
DEBUG_printf(("httpRead2: data_remaining=" CUPS_LLFMT "\n",
CUPS_LLCAST http->data_remaining));
if (http->data_remaining <= 0)
{
/*
* A zero-length chunk ends a transfer; unless we are reading POST
* data, go idle...
*/
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
httpGets(len, sizeof(len), http);
if (http->state == HTTP_POST_RECV)
http->state ++;
else
http->state = HTTP_WAITING;
/*
* Prevent future reads for this request...
*/
http->data_encoding = HTTP_ENCODE_LENGTH;
return (0);
}
else if (length > (size_t)http->data_remaining)
length = (size_t)http->data_remaining;
if (http->used == 0 && length <= 256)
{
/*
* Buffer small reads for better performance...
*/
if (!http->blocking && !httpWait(http, 10000))
return (0);
if (http->data_remaining > sizeof(http->buffer))
bytes = sizeof(http->buffer);
else
bytes = http->data_remaining;
#ifdef HAVE_SSL
if (http->tls)
bytes = http_read_ssl(http, http->buffer, bytes);
else
#endif /* HAVE_SSL */
{
DEBUG_printf(("httpRead2: reading %d bytes from socket into buffer...\n",
bytes));
bytes = recv(http->fd, http->buffer, bytes, 0);
DEBUG_printf(("httpRead2: read %d bytes from socket into buffer...\n",
bytes));
}
if (bytes > 0)
http->used = bytes;
else if (bytes < 0)
{
#ifdef WIN32
http->error = WSAGetLastError();
return (-1);
#else
if (errno != EINTR)
{
http->error = errno;
return (-1);
}
#endif /* WIN32 */
}
else
{
http->error = EPIPE;
return (0);
}
}
if (http->used > 0)
{
if (length > (size_t)http->used)
length = (size_t)http->used;
bytes = (ssize_t)length;
DEBUG_printf(("httpRead2: grabbing %d bytes from input buffer...\n", bytes));
memcpy(buffer, http->buffer, length);
http->used -= (int)length;
if (http->used > 0)
memmove(http->buffer, http->buffer + length, http->used);
}
#ifdef HAVE_SSL
else if (http->tls)
{
if (!http->blocking && !httpWait(http, 10000))
return (0);
bytes = (ssize_t)http_read_ssl(http, buffer, (int)length);
}
#endif /* HAVE_SSL */
else
{
if (!http->blocking && !httpWait(http, 10000))
return (0);
DEBUG_printf(("httpRead2: reading %d bytes from socket...\n", length));
#ifdef WIN32
bytes = (ssize_t)recv(http->fd, buffer, (int)length, 0);
#else
while ((bytes = recv(http->fd, buffer, length, 0)) < 0)
if (errno != EINTR)
break;
#endif /* WIN32 */
DEBUG_printf(("httpRead2: read %d bytes from socket...\n", bytes));
}
if (bytes > 0)
{
http->data_remaining -= bytes;
if (http->data_remaining <= INT_MAX)
http->_data_remaining = (int)http->data_remaining;
else
http->_data_remaining = INT_MAX;
}
else if (bytes < 0)
{
#ifdef WIN32
http->error = WSAGetLastError();
#else
if (errno == EINTR)
bytes = 0;
else
http->error = errno;
#endif /* WIN32 */
}
else
{
http->error = EPIPE;
return (0);
}
if (http->data_remaining == 0)
{
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
httpGets(len, sizeof(len), http);
if (http->data_encoding != HTTP_ENCODE_CHUNKED)
{
if (http->state == HTTP_POST_RECV)
http->state ++;
else
http->state = HTTP_WAITING;
}
}
#ifdef DEBUG
{
int i, j, ch;
printf("httpRead2: Read %d bytes:\n", bytes);
for (i = 0; i < bytes; i += 16)
{
printf(" ");
for (j = 0; j < 16 && (i + j) < bytes; j ++)
printf(" %02X", buffer[i + j] & 255);
while (j < 16)
{
printf(" ");
j ++;
}
printf(" ");
for (j = 0; j < 16 && (i + j) < bytes; j ++)
{
ch = buffer[i + j] & 255;
if (ch < ' ' || ch >= 127)
ch = '.';
putchar(ch);
}
putchar('\n');
}
}
#endif /* DEBUG */
return (bytes);
}
#if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
/*
* '_httpReadCDSA()' - Read function for the CDSA library.
*/
OSStatus /* O - -1 on error, 0 on success */
_httpReadCDSA(
SSLConnectionRef connection, /* I - SSL/TLS connection */
void *data, /* I - Data buffer */
size_t *dataLength) /* IO - Number of bytes */
{
OSStatus result; /* Return value */
ssize_t bytes; /* Number of bytes read */
http_t *http; /* HTTP connection */
http = (http_t *)connection;
if (!http->blocking)
{
/*
* Make sure we have data before we read...
*/
if (!http_wait(http, 10000, 0))
{
http->error = ETIMEDOUT;
return (-1);
}
}
do
{
bytes = recv(http->fd, data, *dataLength, 0);
}
while (bytes == -1 && errno == EINTR);
if (bytes == *dataLength)
{
result = 0;
}
else if (bytes > 0)
{
*dataLength = bytes;
result = errSSLWouldBlock;
}
else
{
*dataLength = 0;
if (bytes == 0)
result = errSSLClosedGraceful;
else if (errno == EAGAIN)
result = errSSLWouldBlock;
else
result = errSSLClosedAbort;
}
return (result);
}
#endif /* HAVE_SSL && HAVE_CDSASSL */
#if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
/*
* '_httpReadGNUTLS()' - Read function for the GNU TLS library.
*/
ssize_t /* O - Number of bytes read or -1 on error */
_httpReadGNUTLS(
gnutls_transport_ptr ptr, /* I - HTTP connection */
void *data, /* I - Buffer */
size_t length) /* I - Number of bytes to read */
{
http_t *http; /* HTTP connection */
http = (http_t *)ptr;
if (!http->blocking)
{
/*
* Make sure we have data before we read...
*/
if (!http_wait(http, 10000, 0))
{
http->error = ETIMEDOUT;
return (-1);
}
}
return (recv(http->fd, data, length, 0));
}
#endif /* HAVE_SSL && HAVE_GNUTLS */
/*
* 'httpReconnect()' - Reconnect to a HTTP server.
*/
int /* O - 0 on success, non-zero on failure */
httpReconnect(http_t *http) /* I - HTTP connection */
{
http_addrlist_t *addr; /* Connected address */
DEBUG_printf(("httpReconnect(http=%p)\n", http));
if (!http)
return (-1);
#ifdef HAVE_SSL
if (http->tls)
http_shutdown_ssl(http);
#endif /* HAVE_SSL */
/*
* Close any previously open socket...
*/
if (http->fd >= 0)
{
#ifdef WIN32
closesocket(http->fd);
#else
close(http->fd);
#endif /* WIN32 */
http->fd = -1;
}
/*
* Connect to the server...
*/
if ((addr = httpAddrConnect(http->addrlist, &(http->fd))) == NULL)
{
/*
* Unable to connect...
*/
#ifdef WIN32
http->error = WSAGetLastError();
#else
http->error = errno;
#endif /* WIN32 */
http->status = HTTP_ERROR;
return (-1);
}
http->hostaddr = &(addr->addr);
http->error = 0;
http->status = HTTP_CONTINUE;
#ifdef HAVE_SSL
if (http->encryption == HTTP_ENCRYPT_ALWAYS)
{
/*
* Always do encryption via SSL.
*/
if (http_setup_ssl(http) != 0)
{
# ifdef WIN32
closesocket(http->fd);
# else
close(http->fd);
# endif /* WIN32 */
return (-1);
}
}
else if (http->encryption == HTTP_ENCRYPT_REQUIRED)
return (http_upgrade(http));
#endif /* HAVE_SSL */
return (0);
}
/*
* 'httpSetAuthString()' - Set the current authorization string.
*
* This function just stores a copy of the current authorization string in
* the HTTP connection object. You must still call httpSetField() to set
* HTTP_FIELD_AUTHORIZATION prior to issuing a HTTP request using httpGet(),
* httpHead(), httpOptions(), httpPost, or httpPut().
*
* @since CUPS 1.3@
*/
void
httpSetAuthString(http_t *http, /* I - HTTP connection */
const char *scheme, /* I - Auth scheme (NULL to clear it) */
const char *data) /* I - Auth data (NULL for none) */
{
/*
* Range check input...
*/
if (!http)
return;
if (http->authstring && http->authstring != http->_authstring)
free(http->authstring);
http->authstring = http->_authstring;
if (scheme)
{
/*
* Set the current authorization string...
*/
int len = (int)strlen(scheme) + (data ? (int)strlen(data) + 1 : 0) + 1;
char *temp;
if (len > (int)sizeof(http->_authstring))
{
if ((temp = malloc(len)) == NULL)
len = sizeof(http->_authstring);
else
http->authstring = temp;
}
if (data)
snprintf(http->authstring, len, "%s %s", scheme, data);
else
strlcpy(http->authstring, scheme, len);
}
else
{
/*
* Clear the current authorization string...
*/
http->_authstring[0] = '\0';
}
}
/*
* 'httpSetCookie()' - Set the cookie value(s)...
*
* @since CUPS 1.1.19@
*/
void
httpSetCookie(http_t *http, /* I - Connection */
const char *cookie) /* I - Cookie string */
{
if (!http)
return;
if (http->cookie)
free(http->cookie);
if (cookie)
http->cookie = strdup(cookie);
else
http->cookie = NULL;
}
/*
* 'httpSetExpect()' - Set the Expect: header in a request.
*
* Currently only HTTP_CONTINUE is supported for the "expect" argument.
*
* @since CUPS 1.2@
*/
void
httpSetExpect(http_t *http, /* I - HTTP connection */
http_status_t expect) /* I - HTTP status to expect (HTTP_CONTINUE) */
{
if (http)
http->expect = expect;
}
/*
* 'httpSetField()' - Set the value of an HTTP header.
*/
void
httpSetField(http_t *http, /* I - HTTP connection */
http_field_t field, /* I - Field index */
const char *value) /* I - Value */
{
if (http == NULL ||
field < HTTP_FIELD_ACCEPT_LANGUAGE ||
field > HTTP_FIELD_WWW_AUTHENTICATE ||
value == NULL)
return;
strlcpy(http->fields[field], value, HTTP_MAX_VALUE);
/*
* Special case for Authorization: as its contents can be
* longer than HTTP_MAX_VALUE
*/
if (field == HTTP_FIELD_AUTHORIZATION)
{
if (http->field_authorization)
free(http->field_authorization);
http->field_authorization = strdup(value);
}
}
/*
* 'httpSetLength()' - Set the content-length and content-encoding.
*
* @since CUPS 1.2@
*/
void
httpSetLength(http_t *http, /* I - HTTP connection */
size_t length) /* I - Length (0 for chunked) */
{
if (!http)
return;
if (!length)
{
strcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked");
http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0';
}
else
{
http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0';
snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE,
CUPS_LLFMT, CUPS_LLCAST length);
}
}
/*
* 'httpTrace()' - Send an TRACE request to the server.
*/
int /* O - Status of call (0 = success) */
httpTrace(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI for trace */
{
return (http_send(http, HTTP_TRACE, uri));
}
/*
* 'httpUpdate()' - Update the current HTTP state for incoming data.
*/
http_status_t /* O - HTTP status */
httpUpdate(http_t *http) /* I - HTTP connection */
{
char line[32768], /* Line from connection... */
*value; /* Pointer to value on line */
http_field_t field; /* Field index */
int major, minor, /* HTTP version numbers */
status; /* Request status */
DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http, http->state));
/*
* Flush pending data, if any...
*/
if (http->wused)
{
DEBUG_puts(" flushing buffer...");
if (httpFlushWrite(http) < 0)
return (HTTP_ERROR);
}
/*
* If we haven't issued any commands, then there is nothing to "update"...
*/
if (http->state == HTTP_WAITING)
return (HTTP_CONTINUE);
/*
* Grab all of the lines we can from the connection...
*/
while (httpGets(line, sizeof(line), http) != NULL)
{
DEBUG_printf(("httpUpdate: Got \"%s\"\n", line));
if (line[0] == '\0')
{
/*
* Blank line means the start of the data section (if any). Return
* the result code, too...
*
* If we get status 100 (HTTP_CONTINUE), then we *don't* change states.
* Instead, we just return HTTP_CONTINUE to the caller and keep on
* tryin'...
*/
if (http->status == HTTP_CONTINUE)
return (http->status);
if (http->status < HTTP_BAD_REQUEST)
http->digest_tries = 0;
#ifdef HAVE_SSL
if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls)
{
if (http_setup_ssl(http) != 0)
{
# ifdef WIN32
closesocket(http->fd);
# else
close(http->fd);
# endif /* WIN32 */
return (HTTP_ERROR);
}
return (HTTP_CONTINUE);
}
#endif /* HAVE_SSL */
httpGetLength2(http);
switch (http->state)
{
case HTTP_GET :
case HTTP_POST :
case HTTP_POST_RECV :
case HTTP_PUT :
http->state ++;
case HTTP_POST_SEND :
case HTTP_HEAD :
break;
default :
http->state = HTTP_WAITING;
break;
}
return (http->status);
}
else if (strncmp(line, "HTTP/", 5) == 0)
{
/*
* Got the beginning of a response...
*/
if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &status) != 3)
return (HTTP_ERROR);
http->version = (http_version_t)(major * 100 + minor);
http->status = (http_status_t)status;
}
else if ((value = strchr(line, ':')) != NULL)
{
/*
* Got a value...
*/
*value++ = '\0';
while (isspace(*value & 255))
value ++;
/*
* Be tolerants of servers that send unknown attribute fields...
*/
if (!strcasecmp(line, "expect"))
{
/*
* "Expect: 100-continue" or similar...
*/
http->expect = (http_status_t)atoi(value);
}
else if (!strcasecmp(line, "cookie"))
{
/*
* "Cookie: name=value[; name=value ...]" - replaces previous cookies...
*/
httpSetCookie(http, value);
}
else if ((field = http_field(line)) == HTTP_FIELD_UNKNOWN)
{
DEBUG_printf(("httpUpdate: unknown field %s seen!\n", line));
continue;
}
else
httpSetField(http, field, value);
}
else
{
http->status = HTTP_ERROR;
return (HTTP_ERROR);
}
}
/*
* See if there was an error...
*/
if (http->error == EPIPE && http->status > HTTP_CONTINUE)
return (http->status);
if (http->error)
{
DEBUG_printf(("httpUpdate: socket error %d - %s\n", http->error,
strerror(http->error)));
http->status = HTTP_ERROR;
return (HTTP_ERROR);
}
/*
* If we haven't already returned, then there is nothing new...
*/
return (HTTP_CONTINUE);
}
/*
* 'httpWait()' - Wait for data available on a connection.
*
* @since CUPS 1.1.19@
*/
int /* O - 1 if data is available, 0 otherwise */
httpWait(http_t *http, /* I - HTTP connection */
int msec) /* I - Milliseconds to wait */
{
/*
* First see if there is data in the buffer...
*/
if (http == NULL)
return (0);
if (http->used)
return (1);
/*
* Flush pending data, if any...
*/
if (http->wused)
{
if (httpFlushWrite(http) < 0)
return (0);
}
/*
* If not, check the SSL/TLS buffers and do a select() on the connection...
*/
return (http_wait(http, msec, 1));
}
/*
* 'httpWrite()' - Write data to a HTTP connection.
*
* This function is deprecated. Use the httpWrite2() function which can
* write more than 2GB of data.
*
* @deprecated@
*/
int /* O - Number of bytes written */
httpWrite(http_t *http, /* I - HTTP connection */
const char *buffer, /* I - Buffer for data */
int length) /* I - Number of bytes to write */
{
return ((int)httpWrite2(http, buffer, length));
}
/*
* 'httpWrite2()' - Write data to a HTTP connection.
*
* @since CUPS 1.2@
*/
ssize_t /* O - Number of bytes written */
httpWrite2(http_t *http, /* I - HTTP connection */
const char *buffer, /* I - Buffer for data */
size_t length) /* I - Number of bytes to write */
{
ssize_t bytes; /* Bytes written */
DEBUG_printf(("httpWrite(http=%p, buffer=%p, length=%d)\n", http,
buffer, length));
/*
* Range check input...
*/
if (http == NULL || buffer == NULL)
return (-1);
/*
* Mark activity on the connection...
*/
http->activity = time(NULL);
/*
* Buffer small writes for better performance...
*/
if (length > 0)
{
if (http->wused && (length + http->wused) > sizeof(http->wbuffer))
{
DEBUG_printf((" flushing buffer (wused=%d, length=%d)\n",
http->wused, length));
httpFlushWrite(http);
}
if ((length + http->wused) <= sizeof(http->wbuffer))
{
/*
* Write to buffer...
*/
DEBUG_printf((" copying %d bytes to wbuffer...\n", length));
memcpy(http->wbuffer + http->wused, buffer, length);
http->wused += (int)length;
bytes = (ssize_t)length;
}
else
{
/*
* Otherwise write the data directly...
*/
DEBUG_printf((" writing %d bytes to socket...\n", length));
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
bytes = (ssize_t)http_write_chunk(http, buffer, (int)length);
else
bytes = (ssize_t)http_write(http, buffer, (int)length);
DEBUG_printf((" wrote %d bytes...\n", bytes));
}
if (http->data_encoding == HTTP_ENCODE_LENGTH)
http->data_remaining -= bytes;
}
else
bytes = 0;
/*
* Handle end-of-request processing...
*/
if ((http->data_encoding == HTTP_ENCODE_CHUNKED && length == 0) ||
(http->data_encoding == HTTP_ENCODE_LENGTH && http->data_remaining == 0))
{
/*
* Finished with the transfer; unless we are sending POST or PUT
* data, go idle...
*/
DEBUG_puts("httpWrite: changing states...");
if (http->wused)
httpFlushWrite(http);
if (http->data_encoding == HTTP_ENCODE_CHUNKED)
{
/*
* Send a 0-length chunk at the end of the request...
*/
http_write(http, "0\r\n\r\n", 5);
/*
* Reset the data state...
*/
http->data_encoding = HTTP_ENCODE_LENGTH;
http->data_remaining = 0;
}
if (http->state == HTTP_POST_RECV)
http->state ++;
else if (http->state == HTTP_PUT_RECV)
http->state = HTTP_STATUS;
else
http->state = HTTP_WAITING;
}
return (bytes);
}
#if defined(HAVE_SSL) && defined(HAVE_CDSASSL)
/*
* '_httpWriteCDSA()' - Write function for the CDSA library.
*/
OSStatus /* O - -1 on error, 0 on success */
_httpWriteCDSA(
SSLConnectionRef connection, /* I - SSL/TLS connection */
const void *data, /* I - Data buffer */
size_t *dataLength) /* IO - Number of bytes */
{
OSStatus result; /* Return value */
ssize_t bytes; /* Number of bytes read */
http_t *http; /* HTTP connection */
http = (http_t *)connection;
do
{
bytes = write(http->fd, data, *dataLength);
}
while (bytes == -1 && errno == EINTR);
if (bytes == *dataLength)
{
result = 0;
}
else if (bytes >= 0)
{
*dataLength = bytes;
result = errSSLWouldBlock;
}
else
{
*dataLength = 0;
if (errno == EAGAIN)
result = errSSLWouldBlock;
else
result = errSSLClosedAbort;
}
return (result);
}
#endif /* HAVE_SSL && HAVE_CDSASSL */
#if defined(HAVE_SSL) && defined(HAVE_GNUTLS)
/*
* '_httpWriteGNUTLS()' - Write function for the GNU TLS library.
*/
ssize_t /* O - Number of bytes written or -1 on error */
_httpWriteGNUTLS(
gnutls_transport_ptr ptr, /* I - HTTP connection */
const void *data, /* I - Data buffer */
size_t length) /* I - Number of bytes to write */
{
return (send(((http_t *)ptr)->fd, data, length, 0));
}
#endif /* HAVE_SSL && HAVE_GNUTLS */
#if defined(HAVE_SSL) && defined(HAVE_LIBSSL)
/*
* 'http_bio_ctrl()' - Control the HTTP connection.
*/
static long /* O - Result/data */
http_bio_ctrl(BIO *h, /* I - BIO data */
int cmd, /* I - Control command */
long arg1, /* I - First argument */
void *arg2) /* I - Second argument */
{
switch (cmd)
{
default :
return (0);
case BIO_CTRL_RESET :
h->ptr = NULL;
return (0);
case BIO_C_SET_FILE_PTR :
h->ptr = arg2;
h->init = 1;
return (1);
case BIO_C_GET_FILE_PTR :
if (arg2)
{
*((void **)arg2) = h->ptr;
return (1);
}
else
return (0);
case BIO_CTRL_DUP :
case BIO_CTRL_FLUSH :
return (1);
}
}
/*
* 'http_bio_free()' - Free OpenSSL data.
*/
static int /* O - 1 on success, 0 on failure */
http_bio_free(BIO *h) /* I - BIO data */
{
if (!h)
return (0);
if (h->shutdown)
{
h->init = 0;
h->flags = 0;
}
return (1);
}
/*
* 'http_bio_new()' - Initialize an OpenSSL BIO structure.
*/
static int /* O - 1 on success, 0 on failure */
http_bio_new(BIO *h) /* I - BIO data */
{
if (!h)
return (0);
h->init = 0;
h->num = 0;
h->ptr = NULL;
h->flags = 0;
return (1);
}
/*
* 'http_bio_puts()' - Send a string for OpenSSL.
*/
static int /* O - Bytes written */
http_bio_puts(BIO *h, /* I - BIO data */
const char *str) /* I - String to write */
{
#ifdef WIN32
return (send(((http_t *)h->ptr)->fd, str, (int)strlen(str), 0));
#else
return (send(((http_t *)h->ptr)->fd, str, strlen(str), 0));
#endif /* WIN32 */
}
/*
* 'http_bio_read()' - Read data for OpenSSL.
*/
static int /* O - Bytes read */
http_bio_read(BIO *h, /* I - BIO data */
char *buf, /* I - Buffer */
int size) /* I - Number of bytes to read */
{
http_t *http; /* HTTP connection */
http = (http_t *)h->ptr;
if (!http->blocking)
{
/*
* Make sure we have data before we read...
*/
if (!http_wait(http, 10000, 0))
{
#ifdef WIN32
http->error = WSAETIMEDOUT;
#else
http->error = ETIMEDOUT;
#endif /* WIN32 */
return (-1);
}
}
return (recv(http->fd, buf, size, 0));
}
/*
* 'http_bio_write()' - Write data for OpenSSL.
*/
static int /* O - Bytes written */
http_bio_write(BIO *h, /* I - BIO data */
const char *buf, /* I - Buffer to write */
int num) /* I - Number of bytes to write */
{
return (send(((http_t *)h->ptr)->fd, buf, num, 0));
}
#endif /* HAVE_SSL && HAVE_LIBSSL */
/*
* 'http_field()' - Return the field index for a field name.
*/
static http_field_t /* O - Field index */
http_field(const char *name) /* I - String name */
{
int i; /* Looping var */
for (i = 0; i < HTTP_FIELD_MAX; i ++)
if (strcasecmp(name, http_fields[i]) == 0)
return ((http_field_t)i);
return (HTTP_FIELD_UNKNOWN);
}
#ifdef HAVE_SSL
/*
* 'http_read_ssl()' - Read from a SSL/TLS connection.
*/
static int /* O - Bytes read */
http_read_ssl(http_t *http, /* I - HTTP connection */
char *buf, /* I - Buffer to store data */
int len) /* I - Length of buffer */
{
# if defined(HAVE_LIBSSL)
return (SSL_read((SSL *)(http->tls), buf, len));
# elif defined(HAVE_GNUTLS)
return (gnutls_record_recv(((http_tls_t *)(http->tls))->session, buf, len));
# elif defined(HAVE_CDSASSL)
int result; /* Return value */
OSStatus error; /* Error info */
size_t processed; /* Number of bytes processed */
error = SSLRead(((http_tls_t *)http->tls)->session, buf, len, &processed);
switch (error)
{
case 0 :
result = (int)processed;
break;
case errSSLClosedGraceful :
result = 0;
break;
case errSSLWouldBlock :
if (processed)
result = (int)processed;
else
{
result = -1;
errno = EINTR;
}
break;
default :
errno = EPIPE;
result = -1;
break;
}
return (result);
# endif /* HAVE_LIBSSL */
}
#endif /* HAVE_SSL */
/*
* 'http_send()' - Send a request with all fields and the trailing blank line.
*/
static int /* O - 0 on success, non-zero on error */
http_send(http_t *http, /* I - HTTP connection */
http_state_t request, /* I - Request code */
const char *uri) /* I - URI */
{
int i; /* Looping var */
char buf[1024]; /* Encoded URI buffer */
static const char * const codes[] =
{ /* Request code strings */
NULL,
"OPTIONS",
"GET",
NULL,
"HEAD",
"POST",
NULL,
NULL,
"PUT",
NULL,
"DELETE",
"TRACE",
"CLOSE"
};
DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
http, codes[request], uri));
if (http == NULL || uri == NULL)
return (-1);
/*
* Set the User-Agent field if it isn't already...
*/
if (!http->fields[HTTP_FIELD_USER_AGENT][0])
httpSetField(http, HTTP_FIELD_USER_AGENT, CUPS_MINIMAL);
/*
* Encode the URI as needed...
*/
_httpEncodeURI(buf, uri, sizeof(buf));
/*
* See if we had an error the last time around; if so, reconnect...
*/
if (http->status == HTTP_ERROR || http->status >= HTTP_BAD_REQUEST)
if (httpReconnect(http))
return (-1);
/*
* Flush any written data that is pending...
*/
if (http->wused)
httpFlushWrite(http);
/*
* Send the request header...
*/
http->state = request;
http->data_encoding = HTTP_ENCODE_FIELDS;
if (request == HTTP_POST || request == HTTP_PUT)
http->state ++;
http->status = HTTP_CONTINUE;
#ifdef HAVE_SSL
if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls)
{
httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0,SSL/2.0,SSL/3.0");
}
#endif /* HAVE_SSL */
if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1)
{
http->status = HTTP_ERROR;
return (-1);
}
for (i = 0; i < HTTP_FIELD_MAX; i ++)
if (http->fields[i][0] != '\0')
{
DEBUG_printf(("%s: %s\n", http_fields[i], httpGetField(http, i)));
if (httpPrintf(http, "%s: %s\r\n", http_fields[i],
httpGetField(http, i)) < 1)
{
http->status = HTTP_ERROR;
return (-1);
}
}
if (http->cookie)
if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1)
{
http->status = HTTP_ERROR;
return (-1);
}
if (http->expect == HTTP_CONTINUE &&
(http->state == HTTP_POST_RECV || http->state == HTTP_PUT_RECV))
if (httpPrintf(http, "Expect: 100-continue\r\n") < 1)
{
http->status = HTTP_ERROR;
return (-1);
}
if (httpPrintf(http, "\r\n") < 1)
{
http->status = HTTP_ERROR;
return (-1);
}
httpFlushWrite(http);
httpGetLength2(http);
httpClearFields(http);
/*
* The Kerberos and AuthRef authentication strings can only be used once...
*/
if (http->field_authorization && http->authstring &&
(!strncmp(http->authstring, "Negotiate", 9) ||
!strncmp(http->authstring, "AuthRef", 7)))
{
http->_authstring[0] = '\0';
if (http->authstring != http->_authstring)
free(http->authstring);
http->authstring = http->_authstring;
}
return (0);
}
#ifdef HAVE_SSL
/*
* 'http_setup_ssl()' - Set up SSL/TLS support on a connection.
*/
static int /* O - Status of connection */
http_setup_ssl(http_t *http) /* I - HTTP connection */
{
# ifdef HAVE_LIBSSL
SSL_CTX *context; /* Context for encryption */
SSL *conn; /* Connection for encryption */
BIO *bio; /* BIO data */
# elif defined(HAVE_GNUTLS)
http_tls_t *conn; /* TLS session object */
gnutls_certificate_client_credentials *credentials;
/* TLS credentials */
# elif defined(HAVE_CDSASSL)
OSStatus error; /* Error code */
http_tls_t *conn; /* CDSA connection information */
# endif /* HAVE_LIBSSL */
DEBUG_printf(("http_setup_ssl(http=%p)\n", http));
# ifdef HAVE_LIBSSL
context = SSL_CTX_new(SSLv23_client_method());
SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); /* Only use SSLv3 or TLS */
bio = BIO_new(_httpBIOMethods());
BIO_ctrl(bio, BIO_C_SET_FILE_PTR, 0, (char *)http);
conn = SSL_new(context);
SSL_set_bio(conn, bio, bio);
if (SSL_connect(conn) != 1)
{
# ifdef DEBUG
unsigned long error; /* Error code */
while ((error = ERR_get_error()) != 0)
printf("http_setup_ssl: %s\n", ERR_error_string(error, NULL));
# endif /* DEBUG */
SSL_CTX_free(context);
SSL_free(conn);
# ifdef WIN32
http->error = WSAGetLastError();
# else
http->error = errno;
# endif /* WIN32 */
http->status = HTTP_ERROR;
return (HTTP_ERROR);
}
# elif defined(HAVE_GNUTLS)
if ((conn = (http_tls_t *)malloc(sizeof(http_tls_t))) == NULL)
{
http->error = errno;
http->status = HTTP_ERROR;
return (-1);
}
credentials = (gnutls_certificate_client_credentials *)
malloc(sizeof(gnutls_certificate_client_credentials));
if (credentials == NULL)
{
free(conn);
http->error = errno;
http->status = HTTP_ERROR;
return (-1);
}
gnutls_certificate_allocate_credentials(credentials);
gnutls_init(&(conn->session), GNUTLS_CLIENT);
gnutls_set_default_priority(conn->session);
gnutls_credentials_set(conn->session, GNUTLS_CRD_CERTIFICATE, *credentials);
gnutls_transport_set_ptr(conn->session, (gnutls_transport_ptr)http);
gnutls_transport_set_pull_function(conn->session, _httpReadGNUTLS);
gnutls_transport_set_push_function(conn->session, _httpWriteGNUTLS);
if ((gnutls_handshake(conn->session)) != GNUTLS_E_SUCCESS)
{
http->error = errno;
http->status = HTTP_ERROR;
return (-1);
}
conn->credentials = credentials;
# elif defined(HAVE_CDSASSL)
conn = (http_tls_t *)calloc(1, sizeof(http_tls_t));
if (conn == NULL)
return (-1);
if ((error = SSLNewContext(false, &conn->session)))
{
http->error = error;
http->status = HTTP_ERROR;
free(conn);
return (-1);
}
/*
* Use a union to resolve warnings about int/pointer size mismatches...
*/
error = SSLSetConnection(conn->session, http);
if (!error)
error = SSLSetIOFuncs(conn->session, _httpReadCDSA, _httpWriteCDSA);
if (!error)
error = SSLSetAllowsExpiredCerts(conn->session, true);
if (!error)
error = SSLSetAllowsAnyRoot(conn->session, true);
if (!error)
error = SSLSetProtocolVersionEnabled(conn->session, kSSLProtocol2, false);
if (!error)
{
while ((error = SSLHandshake(conn->session)) == errSSLWouldBlock)
usleep(1000);
}
if (error)
{
http->error = error;
http->status = HTTP_ERROR;
SSLDisposeContext(conn->session);
free(conn);
return (-1);
}
# endif /* HAVE_CDSASSL */
http->tls = conn;
return (0);
}
#endif /* HAVE_SSL */
#ifdef HAVE_SSL
/*
* 'http_shutdown_ssl()' - Shut down SSL/TLS on a connection.
*/
static void
http_shutdown_ssl(http_t *http) /* I - HTTP connection */
{
# ifdef HAVE_LIBSSL
SSL_CTX *context; /* Context for encryption */
SSL *conn; /* Connection for encryption */
conn = (SSL *)(http->tls);
context = SSL_get_SSL_CTX(conn);
SSL_shutdown(conn);
SSL_CTX_free(context);
SSL_free(conn);
# elif defined(HAVE_GNUTLS)
http_tls_t *conn; /* Encryption session */
gnutls_certificate_client_credentials *credentials;
/* TLS credentials */
conn = (http_tls_t *)(http->tls);
credentials = (gnutls_certificate_client_credentials *)(conn->credentials);
gnutls_bye(conn->session, GNUTLS_SHUT_RDWR);
gnutls_deinit(conn->session);
gnutls_certificate_free_credentials(*credentials);
free(credentials);
free(conn);
# elif defined(HAVE_CDSASSL)
http_tls_t *conn; /* CDSA connection information */
conn = (http_tls_t *)(http->tls);
while (SSLClose(conn->session) == errSSLWouldBlock)
usleep(1000);
SSLDisposeContext(conn->session);
if (conn->certsArray)
CFRelease(conn->certsArray);
free(conn);
# endif /* HAVE_LIBSSL */
http->tls = NULL;
}
#endif /* HAVE_SSL */
#ifdef HAVE_SSL
/*
* 'http_upgrade()' - Force upgrade to TLS encryption.
*/
static int /* O - Status of connection */
http_upgrade(http_t *http) /* I - HTTP connection */
{
int ret; /* Return value */
http_t myhttp; /* Local copy of HTTP data */
DEBUG_printf(("http_upgrade(%p)\n", http));
/*
* Copy the HTTP data to a local variable so we can do the OPTIONS
* request without interfering with the existing request data...
*/
memcpy(&myhttp, http, sizeof(myhttp));
/*
* Send an OPTIONS request to the server, requiring SSL or TLS
* encryption on the link...
*/
http->field_authorization = NULL; /* Don't free the auth string */
httpClearFields(http);
httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade");
httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0, SSL/2.0, SSL/3.0");
if ((ret = httpOptions(http, "*")) == 0)
{
/*
* Wait for the secure connection...
*/
while (httpUpdate(http) == HTTP_CONTINUE);
}
httpFlush(http);
/*
* Restore the HTTP request data...
*/
memcpy(http->fields, myhttp.fields, sizeof(http->fields));
http->data_encoding = myhttp.data_encoding;
http->data_remaining = myhttp.data_remaining;
http->_data_remaining = myhttp._data_remaining;
http->expect = myhttp.expect;
http->field_authorization = myhttp.field_authorization;
http->digest_tries = myhttp.digest_tries;
/*
* See if we actually went secure...
*/
if (!http->tls)
{
/*
* Server does not support HTTP upgrade...
*/
DEBUG_puts("Server does not support HTTP upgrade!");
# ifdef WIN32
closesocket(http->fd);
# else
close(http->fd);
# endif
http->fd = -1;
return (-1);
}
else
return (ret);
}
#endif /* HAVE_SSL */
/*
* 'http_wait()' - Wait for data available on a connection.
*/
static int /* O - 1 if data is available, 0 otherwise */
http_wait(http_t *http, /* I - HTTP connection */
int msec, /* I - Milliseconds to wait */
int usessl) /* I - Use SSL context? */
{
#ifdef HAVE_POLL
struct pollfd pfd; /* Polled file descriptor */
#else
fd_set input_set; /* select() input set */
struct timeval timeout; /* Timeout */
#endif /* HAVE_POLL */
int nfds; /* Result from select()/poll() */
DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http, msec));
if (http->fd < 0)
return (0);
/*
* Check the SSL/TLS buffers for data first...
*/
#ifdef HAVE_SSL
if (http->tls && usessl)
{
# ifdef HAVE_LIBSSL
if (SSL_pending((SSL *)(http->tls)))
return (1);
# elif defined(HAVE_GNUTLS)
if (gnutls_record_check_pending(((http_tls_t *)(http->tls))->session))
return (1);
# elif defined(HAVE_CDSASSL)
size_t bytes; /* Bytes that are available */
if (!SSLGetBufferedReadSize(((http_tls_t *)http->tls)->session, &bytes) && bytes > 0)
return (1);
# endif /* HAVE_LIBSSL */
}
#endif /* HAVE_SSL */
/*
* Then try doing a select() or poll() to poll the socket...
*/
#ifdef HAVE_POLL
pfd.fd = http->fd;
pfd.events = POLLIN;
while ((nfds = poll(&pfd, 1, msec)) < 0 && errno == EINTR);
#else
do
{
FD_ZERO(&input_set);
FD_SET(http->fd, &input_set);
DEBUG_printf(("http_wait: msec=%d, http->fd=%d\n", msec, http->fd));
if (msec >= 0)
{
timeout.tv_sec = msec / 1000;
timeout.tv_usec = (msec % 1000) * 1000;
nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout);
}
else
nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL);
DEBUG_printf(("http_wait: select() returned %d...\n", nfds));
}
# ifdef WIN32
while (nfds < 0 && WSAGetLastError() == WSAEINTR);
# else
while (nfds < 0 && errno == EINTR);
# endif /* WIN32 */
#endif /* HAVE_POLL */
DEBUG_printf(("http_wait: returning with nfds=%d...\n", nfds));
return (nfds > 0);
}
/*
* 'http_write()' - Write a buffer to a HTTP connection.
*/
static int /* O - Number of bytes written */
http_write(http_t *http, /* I - HTTP connection */
const char *buffer, /* I - Buffer for data */
int length) /* I - Number of bytes to write */
{
int tbytes, /* Total bytes sent */
bytes; /* Bytes sent */
tbytes = 0;
while (length > 0)
{
#ifdef HAVE_SSL
if (http->tls)
bytes = http_write_ssl(http, buffer, length);
else
#endif /* HAVE_SSL */
bytes = send(http->fd, buffer, length, 0);
if (bytes < 0)
{
#ifdef WIN32
if (WSAGetLastError() != http->error)
{
http->error = WSAGetLastError();
continue;
}
#else
if (errno == EINTR)
continue;
else if (errno != http->error && errno != ECONNRESET)
{
http->error = errno;
continue;
}
#endif /* WIN32 */
DEBUG_puts("http_write: error writing data...\n");
return (-1);
}
buffer += bytes;
tbytes += bytes;
length -= bytes;
}
#ifdef DEBUG
{
int i, j, ch;
printf("http_write: wrote %d bytes: \n", tbytes);
for (i = 0, buffer -= tbytes; i < tbytes; i += 16)
{
printf(" ");
for (j = 0; j < 16 && (i + j) < tbytes; j ++)
printf(" %02X", buffer[i + j] & 255);
while (j < 16)
{
printf(" ");
j ++;
}
printf(" ");
for (j = 0; j < 16 && (i + j) < tbytes; j ++)
{
ch = buffer[i + j] & 255;
if (ch < ' ' || ch == 127)
ch = '.';
putchar(ch);
}
putchar('\n');
}
}
#endif /* DEBUG */
return (tbytes);
}
/*
* 'http_write_chunk()' - Write a chunked buffer.
*/
static int /* O - Number bytes written */
http_write_chunk(http_t *http, /* I - HTTP connection */
const char *buffer, /* I - Buffer to write */
int length) /* I - Length of buffer */
{
char header[255]; /* Chunk header */
int bytes; /* Bytes written */
DEBUG_printf(("http_write_chunk(http=%p, buffer=%p, length=%d)\n",
http, buffer, length));
/*
* Write the chunk header, data, and trailer.
*/
sprintf(header, "%x\r\n", length);
if (http_write(http, header, (int)strlen(header)) < 0)
{
DEBUG_puts(" http_write of length failed!");
return (-1);
}
if ((bytes = http_write(http, buffer, length)) < 0)
{
DEBUG_puts(" http_write of buffer failed!");
return (-1);
}
if (http_write(http, "\r\n", 2) < 0)
{
DEBUG_puts(" http_write of CR LF failed!");
return (-1);
}
return (bytes);
}
#ifdef HAVE_SSL
/*
* 'http_write_ssl()' - Write to a SSL/TLS connection.
*/
static int /* O - Bytes written */
http_write_ssl(http_t *http, /* I - HTTP connection */
const char *buf, /* I - Buffer holding data */
int len) /* I - Length of buffer */
{
# if defined(HAVE_LIBSSL)
return (SSL_write((SSL *)(http->tls), buf, len));
# elif defined(HAVE_GNUTLS)
return (gnutls_record_send(((http_tls_t *)(http->tls))->session, buf, len));
# elif defined(HAVE_CDSASSL)
int result; /* Return value */
OSStatus error; /* Error info */
size_t processed; /* Number of bytes processed */
error = SSLWrite(((http_tls_t *)http->tls)->session, buf, len, &processed);
switch (error)
{
case 0 :
result = (int)processed;
break;
case errSSLClosedGraceful :
result = 0;
break;
case errSSLWouldBlock :
if (processed)
result = (int)processed;
else
{
result = -1;
errno = EINTR;
}
break;
default :
errno = EPIPE;
result = -1;
break;
}
return (result);
# endif /* HAVE_LIBSSL */
}
#endif /* HAVE_SSL */
/*
* End of "$Id: http.c 7721 2008-07-11 22:48:49Z mike $".
*/
|