package gapi-ocaml
A simple OCaml client for Google Services
Install
Dune Dependency
Authors
Maintainers
Sources
v0.4.6.tar.gz
sha256=b84b680528a5e050014103a8e7a60a5d43efd5fefc3f838310bd46769775ab48
md5=8ee26acf1f6c6f5e24c7b57fa070a0a2
doc/src/gapi-ocaml.netstring-local/neturl.ml.html
Source file neturl.ml
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
(* $Id$ * ---------------------------------------------------------------------- * *) open Printf exception Malformed_URL type url_syntax_option = Url_part_not_recognized | Url_part_allowed | Url_part_required type url_syntax = { url_enable_scheme : url_syntax_option; url_enable_user : url_syntax_option; url_enable_user_param : url_syntax_option; url_enable_password : url_syntax_option; url_enable_host : url_syntax_option; url_enable_port : url_syntax_option; url_enable_path : url_syntax_option; url_enable_param : url_syntax_option; url_enable_query : url_syntax_option; url_enable_fragment : url_syntax_option; url_enable_other : url_syntax_option; url_accepts_8bits : bool; url_is_valid : url -> bool; url_enable_relative : bool; } and url = { url_syntax : url_syntax; mutable url_validity : bool; url_scheme : string option; url_user : string option; url_user_param : string list; url_password : string option; url_host : string option; url_port : int option; url_path : string list; url_param : string list; url_query : string option; url_fragment : string option; url_other : string option; } ;; type char_category = Accepted | Rejected | Separator let scan_url_part s k_from k_to cats accept_8bits = (* Scans the longest word of accepted characters from position 'k_from' * in 's' until at most position 'k_to'. The character following the * word (if any) must be a separator character. * On success, the function returns the position of the last character * of the word + 1. * If there is any rejected character before the separator or the end * of the string (i.e. position 'k_to') is reached, the exception * Malformed_URL is raised. * Furthermore, if the character '%' is accepted it is checked whether * two hexadecimal digits follow (which must be accepted, too). If this * is not true, the exception Malformed_URL is raised, too. * 'cats': contains for every character code (0 to 255) the category * of the character. *) let check_hex c = if cats.( Char.code c ) <> Accepted then raise Malformed_URL; match c with ('0'..'9'|'A'..'F'|'a'..'f') -> () | _ -> raise Malformed_URL in let rec scan k = if k >= k_to then k else begin let c = s.[k] in let cat = cats.(Char.code c) in match cat with Accepted -> if c = '%' then begin if k+2 >= k_to then raise Malformed_URL; let c1 = s.[k+1] in let c2 = s.[k+2] in check_hex c1; check_hex c2; scan (k+3) end else scan (k+1) | Separator -> k | Rejected -> if accept_8bits && c >= '\128' then scan (k+1) else raise Malformed_URL end in assert (Array.length cats = 256); assert (k_from >= 0); assert (k_from <= k_to); assert (k_to <= String.length s); scan k_from ;; (* Create a categorization: *) let lalpha = [ 'a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'; 'k'; 'l'; 'm'; 'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't'; 'u'; 'v'; 'w'; 'x'; 'y'; 'z' ] let ualpha = [ 'A'; 'B'; 'C'; 'D'; 'E'; 'F'; 'G'; 'H'; 'I'; 'J'; 'K'; 'L'; 'M'; 'N'; 'O'; 'P'; 'Q'; 'R'; 'S'; 'T'; 'U'; 'V'; 'W'; 'X'; 'Y'; 'Z' ] let digit = [ '0'; '1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9' ] let hex_digit = [ 'a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'A'; 'B'; 'C'; 'D'; 'E'; 'F'; ] @ digit let safe = [ '$'; '-'; '_'; '.'; '+' ] let extra = [ '!'; '*'; '\''; '('; ')'; ',' ] let make_cats accepted separators = (* create a categorization: * - All characters listed in 'separators' are separators. * - All characters listed in 'accepted' and which do not occur in * 'separators' are accepted characters. * - All other characters are rejected. *) let cats = Array.make 256 Rejected in List.iter (fun c -> cats.(Char.code c) <- Accepted ) accepted; List.iter (fun c -> cats.(Char.code c) <- Separator ) separators; cats ;; let scheme_cats = make_cats (lalpha @ ualpha @ ['+'; '-'; '.']) [':'] ;; (* scheme_cats: character categorization to _extract_ the URL scheme *) let login_cats = make_cats (lalpha @ ualpha @ digit @ safe @ extra @ [';'; '&'; '='; '%']) [':'; '@'; '/'; '#'; '?' ] ;; (* login_cats: character categorization to _extract_ user name, password, * host name, and port. * * Note: user_params are extracted in a second step. * * Note: '?' is now a separator, as described in RFC 2396. *) let host_cats = make_cats (lalpha @ ualpha @ digit @ ['.'; '-']) [] ;; (* host_cats: character categorization to _check_ whether the host name * is formed only by legal characters. * Especially '%' is not allowed here! * IPv6 addresses are checked separately. *) let ipv6_cats = make_cats (hex_digit @ [ ':' ]) [ ] let ipv6_sep_cats = make_cats (hex_digit @ [ ':' ]) [ ']' ] let port_cats = make_cats digit [] ;; (* port_cats: character categorization to _check_ whether the port number * is formed only by legal characters. * Especially '%' is not allowed here! *) let path_cats separators = make_cats (lalpha @ ualpha @ digit @ safe @ extra @ ['?'; ':'; '@'; '&'; '='; ';'; '%'; '/'; '~']) separators ;; let separators_from_syntax syn = let include_if syn_option clist = if syn_option <> Url_part_not_recognized then clist else [] in (include_if syn.url_enable_param [';']) @ (include_if syn.url_enable_query ['?']) @ (include_if syn.url_enable_fragment ['#']) ;; let path_cats_from_syntax syn extraseps = let separators = separators_from_syntax syn in path_cats (separators @ extraseps) ;; (* path_cats_from_syntax: * Computes a character categorization to extract the path from an URL. * This depends on the syntax because the list of possible separators * contains the characters that may begin the next URL clause. * * Notes: * - The '#' is rejected unless fragments are enabled. * - The '~' is accepted although this violates RFC 1738 (but it is ok * according to RFC 2396) *) let other_cats_from_syntax syn = let include_if syn_option clist = if syn_option <> Url_part_not_recognized then clist else [] in let separators = (include_if syn.url_enable_param [';']) @ (include_if syn.url_enable_query ['?']) @ (include_if syn.url_enable_fragment ['#']) in make_cats (lalpha @ ualpha @ digit @ safe @ extra @ (separators @ ['?'; ':'; '@'; '&'; '='; ';'; '%'; '/'])) [] ;; (* other_cats: character categorization to extract or check the * "other" part of the URL. *) let extract_url_scheme s = let l = String.length s in let k = scan_url_part s 0 l scheme_cats false in (* or raise Malformed_URL *) if k = l then raise Malformed_URL; assert (s.[k] = ':'); STRING_LOWERCASE (String.sub s 0 k) ;; let ( => ) a b = not a || b;; (* implication *) let ( <=> ) (a:bool) b = ( a = b );; (* equivalence *) let url_syntax_is_valid syn = let recognized x = x <> Url_part_not_recognized in let _not_recognized x = x = Url_part_not_recognized in (recognized syn.url_enable_password => recognized syn.url_enable_user) && (recognized syn.url_enable_user_param => recognized syn.url_enable_user) && (recognized syn.url_enable_port => recognized syn.url_enable_host) && (recognized syn.url_enable_user => recognized syn.url_enable_host) && not ( (recognized syn.url_enable_user || recognized syn.url_enable_password || recognized syn.url_enable_host || recognized syn.url_enable_port || recognized syn.url_enable_path) && (recognized syn.url_enable_other)) ;; let partial_url_syntax syn = let weaken = function Url_part_not_recognized -> Url_part_not_recognized | Url_part_allowed -> Url_part_allowed | Url_part_required -> Url_part_allowed in if not syn.url_enable_relative then failwith "Neturl.partial_url_syntax: This syntax does not support relative URLs"; { url_enable_scheme = weaken syn.url_enable_scheme; url_enable_user = weaken syn.url_enable_user; url_enable_user_param= weaken syn.url_enable_user_param; url_enable_password = weaken syn.url_enable_password; url_enable_host = weaken syn.url_enable_host; url_enable_port = weaken syn.url_enable_port; url_enable_path = weaken syn.url_enable_path; url_enable_param = weaken syn.url_enable_param; url_enable_query = weaken syn.url_enable_query; url_enable_fragment = weaken syn.url_enable_fragment; url_enable_other = weaken syn.url_enable_other; url_accepts_8bits = syn.url_accepts_8bits; url_is_valid = syn.url_is_valid; url_enable_relative = true; } ;; let file_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_allowed; url_enable_port = Url_part_not_recognized; url_enable_path = Url_part_required; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let ftp_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_allowed; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_allowed; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_allowed; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let http_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_allowed; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_allowed; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let pop_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_allowed; url_enable_user_param= Url_part_allowed; url_enable_password = Url_part_allowed; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_not_recognized; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = false; } ;; let imap_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_allowed; url_enable_user_param= Url_part_allowed; url_enable_password = Url_part_allowed; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let mailto_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_not_recognized; url_enable_port = Url_part_not_recognized; url_enable_path = Url_part_not_recognized; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_required; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = false; } ;; let news_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_not_recognized; url_enable_port = Url_part_not_recognized; url_enable_path = Url_part_not_recognized; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_required; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = false; } ;; let cid_url_syntax = news_url_syntax;; let data_url_syntax = news_url_syntax;; let nntp_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_required; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun url -> List.length url.url_path = 3 ); url_enable_relative = true; } ;; let ipp_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_required; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let null_url_syntax = { url_enable_scheme = Url_part_not_recognized; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_not_recognized; url_enable_port = Url_part_not_recognized; url_enable_path = Url_part_not_recognized; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = false; } ;; let ip_url_syntax = { url_enable_scheme = Url_part_allowed; url_enable_user = Url_part_allowed; url_enable_user_param= Url_part_not_recognized; (* so user parameters are parsed as part * of the user string! (Most generic.) *) url_enable_password = Url_part_allowed; url_enable_host = Url_part_allowed; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_allowed; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_allowed; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let ldap_url_syntax = { url_enable_scheme = Url_part_required; url_enable_user = Url_part_not_recognized; url_enable_user_param= Url_part_not_recognized; url_enable_password = Url_part_not_recognized; url_enable_host = Url_part_allowed; url_enable_port = Url_part_allowed; url_enable_path = Url_part_allowed; url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_allowed; url_enable_fragment = Url_part_not_recognized; url_enable_other = Url_part_not_recognized; url_accepts_8bits = false; url_is_valid = (fun _ -> true); url_enable_relative = true; } ;; let common_url_syntax = let h = Hashtbl.create 10 in Hashtbl.add h "file" file_url_syntax; Hashtbl.add h "ftp" ftp_url_syntax; Hashtbl.add h "http" http_url_syntax; Hashtbl.add h "https" http_url_syntax; Hashtbl.add h "pop" pop_url_syntax; Hashtbl.add h "pops" pop_url_syntax; Hashtbl.add h "imap" imap_url_syntax; Hashtbl.add h "imaps" imap_url_syntax; Hashtbl.add h "mailto" mailto_url_syntax; Hashtbl.add h "news" news_url_syntax; Hashtbl.add h "nntp" nntp_url_syntax; Hashtbl.add h "nntps" nntp_url_syntax; Hashtbl.add h "cid" cid_url_syntax; Hashtbl.add h "mid" cid_url_syntax; Hashtbl.add h "data" data_url_syntax; Hashtbl.add h "ipp" ipp_url_syntax; Hashtbl.add h "ipps" ipp_url_syntax; Hashtbl.add h "ldap" ldap_url_syntax; Hashtbl.add h "ldaps" ldap_url_syntax; h ;; let url_conforms_to_syntax url = let recognized x = x <> Url_part_not_recognized in let required x = x = Url_part_required in let present x = x <> None in let syn = url.url_syntax in (present url.url_scheme => recognized syn.url_enable_scheme) && (present url.url_user => recognized syn.url_enable_user) && ((url.url_user_param <> []) => recognized syn.url_enable_user_param) && (present url.url_password => recognized syn.url_enable_password) && (present url.url_host => recognized syn.url_enable_host) && (present url.url_port => recognized syn.url_enable_port) && ((url.url_path <> []) => recognized syn.url_enable_path) && ((url.url_param <> []) => recognized syn.url_enable_param) && (present url.url_query => recognized syn.url_enable_query) && (present url.url_fragment => recognized syn.url_enable_fragment) && (present url.url_other => recognized syn.url_enable_other) && (required syn.url_enable_scheme => present url.url_scheme) && (required syn.url_enable_user => present url.url_user) && (required syn.url_enable_user_param => (url.url_user_param <> [])) && (required syn.url_enable_password => present url.url_password) && (required syn.url_enable_host => present url.url_host) && (required syn.url_enable_port => present url.url_port) && (required syn.url_enable_path => (url.url_path <> [])) && (required syn.url_enable_param => (url.url_param <> [])) && (required syn.url_enable_query => present url.url_query) && (required syn.url_enable_fragment => present url.url_fragment) && (required syn.url_enable_other => present url.url_other) && (url.url_validity || syn.url_is_valid url) ;; let url_syntax_of_url url = url.url_syntax ;; let host_of_addr ip = match Netsys.domain_of_inet_addr ip with | Unix.PF_INET -> Unix.string_of_inet_addr ip | Unix.PF_INET6 -> "[" ^ Unix.string_of_inet_addr ip ^ "]" | _ -> assert false let modify_url ?syntax ?(encoded = false) ?scheme ?user ?user_param ?password ?host ?addr ?port ?socksymbol ?path ?param ?query ?fragment ?other url = let enc ?(plus=false) x = if encoded then x else match x with None -> None | Some x' -> Some (Netencoding.Url.encode ~plus x') in let enc_list ?(plus=false) l = if encoded then l else List.map (Netencoding.Url.encode ~plus) l in let new_syntax = match syntax with None -> url.url_syntax | Some syn -> syn in let check_string s_opt cats = match s_opt with None -> () | Some s -> let l = String.length s in let k = scan_url_part s 0 l cats new_syntax.url_accepts_8bits in (* or raise Malformed_URL *) if k <> l then raise Malformed_URL in let check_string_list p cats sep = List.iter (fun p_component -> let l = String.length p_component in let k = scan_url_part p_component 0 l cats new_syntax.url_accepts_8bits in (* or raise Malformed_URL *) if k <> l then raise Malformed_URL; if String.contains p_component sep then raise Malformed_URL; ) p in let check_host s = let l = String.length s in if String.length s >= 2 && s.[0] = '[' then ( if s.[l-1] <> ']' then raise Malformed_URL; let ipv6 = String.sub s 1 (l-2) in check_string (Some ipv6) ipv6_cats ) else check_string (Some s) host_cats in (* Create the modified record: *) let url_host_0 = match addr with | Some a -> Some(host_of_addr a) | None -> ( match host with | Some h -> Some h | None -> url.url_host ) in let url_port_0 = match port with | Some p -> Some p | None -> url.url_port in let (url_host, url_port) = match (socksymbol : Netsockaddr.socksymbol option) with | Some(`Inet(ip,p)) -> (Some(host_of_addr ip), Some p) | Some(`Inet_byname(h,p)) -> (Some h, Some p) | Some _ -> failwith "Neturl: Unacceptable socksymbol" | None -> (url_host_0, url_port_0) in let url' = { url_syntax = new_syntax; url_validity = false; url_scheme = if scheme = None then url.url_scheme else scheme; url_user = if user = None then url.url_user else enc user; url_user_param = ( match user_param with None -> url.url_user_param | Some p -> enc_list p); url_password = if password = None then url.url_password else enc password; url_host = url_host; url_port = url_port; url_path = (match path with None -> url.url_path | Some p -> enc_list p); url_param = (match param with None -> url.url_param | Some p -> enc_list p); url_query = if query = None then url.url_query else enc ~plus:true query; url_fragment = if fragment = None then url.url_fragment else enc fragment; url_other = if other = None then url.url_other else enc other; } in (* Check whether the URL conforms to the syntax: *) if not (url_conforms_to_syntax url') then raise Malformed_URL; if url'.url_password <> None && url'.url_user = None then raise Malformed_URL; if url'.url_user_param <> [] && url'.url_user = None then raise Malformed_URL; if url'.url_user <> None && url'.url_host = None then raise Malformed_URL; if url'.url_port <> None && url'.url_host = None then raise Malformed_URL; (* Check every part: *) check_string url'.url_scheme scheme_cats; check_string url'.url_user login_cats; check_string_list url'.url_user_param login_cats ';'; check_string url'.url_password login_cats; ( match url'.url_host with | None -> () | Some s -> check_host s ); (match url'.url_port with None -> () | Some p -> if p < 0 || p > 65535 then raise Malformed_URL ); let path_cats = path_cats_from_syntax new_syntax [] in let other_cats = other_cats_from_syntax new_syntax in let query_cats = let syn = { new_syntax with url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized } in path_cats_from_syntax syn [] in check_string url'.url_query query_cats; check_string url'.url_fragment path_cats; check_string url'.url_other other_cats; (* Check the lists: *) check_string_list url'.url_param path_cats ';'; check_string_list url'.url_path path_cats '/'; (* Further path checks: *) begin match url'.url_path with [] -> (* The path is empty: There must not be 'param' *) if url'.url_host <> None then begin if url'.url_param <> [] then raise Malformed_URL; (* if url'.url_query <> None then raise Malformed_URL; * Allowed since RFC 2396 *) end | ["";""] -> (* This is illegal. *) raise Malformed_URL; | "" :: p' -> (* The path is absolute: ensure there is no double slash with host *) ( match url'.url_path with | "" :: "" :: _ -> (* Double slash at beginning of path: Only allowed we have * a host name! *) if host = None then raise Malformed_URL | _ -> () ) | _ -> (* The path is relative: there must not be a host *) if url'.url_host <> None then raise Malformed_URL; end; (* Cache that the URL is valid: *) url'.url_validity <- true; url' ;; let null_url = { url_syntax = null_url_syntax; url_validity = true; url_scheme = None; url_user = None; url_user_param = []; url_password = None; url_host = None; url_port = None; url_path = []; url_param = []; url_query = None; url_fragment = None; url_other = None; } ;; let make_url ?(encoded = false) ?scheme ?user ?user_param ?password ?host ?addr ?port ?socksymbol ?path ?param ?query ?fragment ?other syntax = if not (url_syntax_is_valid syntax) then invalid_arg "Neturl.make_url"; modify_url ~encoded ~syntax ?scheme ?user ?user_param ?password ?host ?addr ?port ?socksymbol ?path ?param ?query ?fragment ?other null_url ;; let remove_from_url ?(scheme = false) ?(user = false) ?(user_param = false) ?(password = false) ?(host = false) ?(port = false) ?(path = false) ?(param = false) ?(query = false) ?(fragment = false) ?(other = false) url = make_url ~encoded: true ?scheme: (if scheme then None else url.url_scheme) ?user: (if user then None else url.url_user) ?user_param:(if user_param then None else Some url.url_user_param) ?password: (if password then None else url.url_password) ?host: (if host then None else url.url_host) ?port: (if port then None else url.url_port) ?path: (if path then None else Some url.url_path) ?param: (if param then None else Some url.url_param) ?query: (if query then None else url.url_query) ?fragment: (if fragment then None else url.url_fragment) ?other: (if other then None else url.url_other) url.url_syntax ;; let default_url ?(encoded = false) ?scheme ?user ?(user_param = []) ?password ?host ?port ?(path = []) ?(param = []) ?query ?fragment ?other url = let encode = Netencoding.Url.encode ~plus:true in let enc x = if encoded then x else match x with None -> None | Some x' -> Some (encode x') in let enc_list l = if encoded then l else List.map encode l in let pass_if_missing current arg = match current with None -> arg | _ -> current in make_url ~encoded: true ?scheme: (pass_if_missing url.url_scheme scheme) ?user: (pass_if_missing url.url_user (enc user)) ~user_param:(if url.url_user_param = [] then enc_list user_param else url.url_user_param) ?password: (pass_if_missing url.url_password (enc password)) ?host: (pass_if_missing url.url_host host) ?port: (pass_if_missing url.url_port port) ~path: (if url.url_path = [] then enc_list path else url.url_path) ~param: (if url.url_param = [] then enc_list param else url.url_param) ?query: (pass_if_missing url.url_query (enc query)) ?fragment: (pass_if_missing url.url_fragment (enc fragment)) ?other: (pass_if_missing url.url_other (enc other)) url.url_syntax ;; let undefault_url ?scheme ?user ?user_param ?password ?host ?port ?path ?param ?query ?fragment ?other url = let remove_if_matching current arg = match current with None -> None | Some x -> (match arg with None -> current | Some x' -> if x=x' then None else current) in let remove_list_if_matching current arg = match arg with None -> current | Some x -> if x = current then [] else current in make_url ~encoded: true ?scheme: (remove_if_matching url.url_scheme scheme) ?user: (remove_if_matching url.url_user user) ~user_param:(remove_list_if_matching url.url_user_param user_param) ?password: (remove_if_matching url.url_password password) ?host: (remove_if_matching url.url_host host) ?port: (remove_if_matching url.url_port port) ~path: (remove_list_if_matching url.url_path path) ~param: (remove_list_if_matching url.url_param param) ?query: (remove_if_matching url.url_query query) ?fragment: (remove_if_matching url.url_fragment fragment) ?other: (remove_if_matching url.url_other other) url.url_syntax ;; let url_provides ?(scheme = false) ?(user = false) ?(user_param = false) ?(password = false) ?(host = false) ?(port = false) ?(path = false) ?(param = false) ?(query = false) ?(fragment = false) ?(other = false) url = (scheme => (url.url_scheme <> None)) && (user => (url.url_user <> None)) && (user_param => (url.url_param <> [])) && (password => (url.url_password <> None)) && (host => (url.url_host <> None)) && (port => (url.url_port <> None)) && (path => (url.url_path <> [])) && (param => (url.url_param <> [])) && (query => (url.url_query <> None)) && (fragment => (url.url_fragment <> None)) && (other => (url.url_other <> None)) ;; let return_if value = match value with None -> raise Not_found | Some x -> x ;; let decode_if ?(plus=false) want_encoded value = let value' = return_if value in if want_encoded then value' else Netencoding.Url.decode ~plus value' ;; let decode_path_if ?(plus=false) want_encoded value = if want_encoded then value else List.map (Netencoding.Url.decode ~plus) value ;; let url_scheme url = return_if url.url_scheme;; let url_user ?(encoded=false) url = decode_if encoded url.url_user;; let url_user_param ?(encoded=false) url = decode_path_if encoded url.url_user_param;; let url_password ?(encoded=false) url = decode_if encoded url.url_password;; let url_host url = return_if url.url_host;; let url_port url = return_if url.url_port;; let url_path ?(encoded=false) url = decode_path_if encoded url.url_path;; let url_param ?(encoded=false) url = decode_path_if encoded url.url_param;; let url_query ?(encoded=false) url = decode_if ~plus:true encoded url.url_query;; let url_fragment ?(encoded=false) url = decode_if encoded url.url_fragment;; let url_other ?(encoded=false) url = decode_if encoded url.url_other;; let url_addr url = match url.url_host with | None -> raise Not_found | Some h -> let l = String.length h in if l >= 2 && h.[0] = '[' && h.[l-1] = ']' then let a = String.sub h 1 (l-2) in try Unix.inet_addr_of_string a with _ -> raise Not_found else ( try Unix.inet_addr_of_string h with _ -> raise Not_found ) let url_socksymbol url dp = let p = match url.url_port with | None -> dp | Some p -> p in try let a = url_addr url in `Inet(a, p) with | Not_found -> (match url.url_host with | None -> raise Not_found | Some h -> `Inet_byname(h, p) ) let string_of_url url = if not (url.url_validity) then failwith "Neturl.string_of_url: URL not flagged as valid"; (match url.url_scheme with None -> "" | Some s -> s ^ ":") ^ (match url.url_host with None -> "" | Some host -> "//" ^ (match url.url_user with None -> "" | Some user -> user ^ (String.concat "" (List.map (fun p -> ";" ^ p) url.url_user_param)) ^ (match url.url_password with None -> "" | Some password -> ":" ^ password ) ^ "@") ^ host ^ (match url.url_port with None -> "" | Some port -> ":" ^ string_of_int port)) ^ (match url.url_path with | [""] -> "/" | x :: p when url.url_scheme = None && url.url_host = None && String.contains x ':' -> (* Really a special case: The colon contained in 'x' may cause * that a prefix of 'x' is interpreted as URL scheme. In this * case, "./" is prepended (as recommended in RFC 1808, 5.3). *) "./" | _ -> "" ) ^ String.concat "/" url.url_path ^ (match url.url_other with None -> "" | Some other -> other) ^ String.concat "" (List.map (fun s -> ";" ^ s) url.url_param) ^ (match url.url_query with None -> "" | Some query -> "?" ^ query) ^ (match url.url_fragment with None -> "" | Some fragment -> "#" ^ fragment) ;; let semi_re = Netstring_str.regexp ";";; let url_of_string url_syntax s = let l = String.length s in let recognized x = x <> Url_part_not_recognized in let rec collect_words ?(ipv6=false) terminators eof_char cats k = (* Collect words as recognized by 'cats', starting at position 'k' in * 's'. Collection stops if one the characters listed in 'terminators' * is found. If the end of the string is reached, it is treated as * 'eof_char'. * * if ipv6: words "[ipv6addr]" are also recognized. *) let word, sep, k_end = if ipv6 && k < l && s.[k] = '[' then ( let k' = scan_url_part s (k+1) l ipv6_sep_cats false in if k' >= l then raise Malformed_URL; if s.[k'] <> ']' then raise Malformed_URL; let word, sep = String.sub s k (k'+1-k), (if k'+1<l then s.[k'+1] else eof_char) in if sep <> eof_char then ( if cats.(Char.code sep) <> Separator then raise Malformed_URL ); (word, sep, k'+1) ) else ( let k' = scan_url_part s k l cats url_syntax.url_accepts_8bits in (* or raise Malformed_URL *) let word, sep = String.sub s k (k'-k), (if k'<l then s.[k'] else eof_char) in (word, sep, k') ) in if List.mem sep terminators then [word, sep], k_end else let word_sep_list', k_end' = collect_words ~ipv6 terminators eof_char cats (k_end+1) in ((word, sep) :: word_sep_list'), k_end' in (* Try to extract the scheme name: *) let scheme, k1 = if recognized url_syntax.url_enable_scheme then try let k = scan_url_part s 0 l scheme_cats false in (* or raise Malformed_URL *) if k = l then raise Malformed_URL; assert (s.[k] = ':'); Some (String.sub s 0 k), (k+1) with Malformed_URL -> None, 0 else None, 0 in (* If there is a "//", a host will follow: *) let host, port, userinfo, password, k2 = if recognized url_syntax.url_enable_host && k1 + 2 <= l && s.[k1]='/' && s.[k1+1]='/' then begin let word_sep_list, k' = collect_words ~ipv6:true [ '/'; '?'; '#' ] '/' login_cats (k1+2) in (* or raise Malformed_URL *) let int x = try int_of_string x with _ -> raise Malformed_URL in match word_sep_list with [ host, ('/'|'?'|'#') ] -> Some host, None, None, None, k' | [ host, ':'; port, ('/'|'?'|'#') ] -> Some host, Some (int port), None, None, k' | [ user, '@'; host, ('/'|'?'|'#') ] -> Some host, None, Some user, None, k' | [ user, '@'; host, ':'; port, ('/'|'?'|'#') ] -> Some host, Some (int port), Some user, None, k' | [ user, ':'; password, '@'; host, ('/'|'?'|'#') ] -> Some host, None, Some user, Some password, k' | [ user, ':'; password, '@'; host, ':'; port, ('/'|'?'|'#') ] -> Some host, Some (int port), Some user, Some password, k' | _ -> raise Malformed_URL end else None, None, None, None, k1 in (* Separate user from user_param: *) let user, user_param = match userinfo with None -> (None, []) | Some u -> if recognized url_syntax.url_enable_user_param then ( let l = Netstring_str.split_delim semi_re u in match l with [] -> (Some "", []) | user :: user_param -> (Some user, user_param) ) else (Some u, []) in let path, k3 = if recognized url_syntax.url_enable_path && k2 < l (* && s.[k2]='/' *) then begin let cats = path_cats_from_syntax url_syntax [ '/' ] in let seps = separators_from_syntax url_syntax in (* Note: '>' is not allowed within URLs; because of this we can use * it as end-of-string character. *) let word_sep_list, k' = collect_words ('>'::seps) '>' cats k2 in (* or raise Malformed_URL *) match word_sep_list with [ "", '/'; "", _ ] -> [ "" ], k' | [ "", _ ] -> [], k' | _ -> List.map fst word_sep_list, k' end else begin (* If there is a single '/': skip it *) if not (recognized url_syntax.url_enable_other) && k2 < l && s.[k2]='/' then [], (k2+1) else [], k2 end in let other, k4 = if recognized url_syntax.url_enable_other && k3 < l then begin let cats = other_cats_from_syntax url_syntax in (* Note: '>' is not allowed within URLs; because of this we can use * it as end-of-string character. *) let word_sep_list, k' = collect_words ['>';'#'] '>' cats k3 in (* or raise Malformed_URL *) match word_sep_list with [ other, _ ] -> Some other, k' | _ -> assert false end else None, k3 in let param, k5 = if recognized url_syntax.url_enable_param && k4 < l && s.[k4]=';' then begin let cats = path_cats_from_syntax url_syntax [] in let seps = separators_from_syntax url_syntax in let seps' = List.filter (fun c -> c <> ';') seps in (* Note: '>' is not allowed within URLs; because of this we can use * it as end-of-string character. *) let word_sep_list, k' = collect_words ('>'::seps') '>' cats (k4+1) in (* or raise Malformed_URL *) List.map fst word_sep_list, k' end else [], k4 in let query, k6 = if recognized url_syntax.url_enable_query && k5 < l && s.[k5]='?' then begin let syn = { url_syntax with url_enable_param = Url_part_not_recognized; url_enable_query = Url_part_not_recognized } in let cats = path_cats_from_syntax syn [] in let seps = separators_from_syntax syn in (* Note: '>' is not allowed within URLs; because of this we can use * it as end-of-string character. *) let word_sep_list, k' = collect_words ('>'::seps) '>' cats (k5+1) in (* or raise Malformed_URL *) match word_sep_list with [ query, _ ] -> Some query, k' | _ -> assert false end else None, k5 in let fragment, k7 = if recognized url_syntax.url_enable_fragment && k6 < l && s.[k6]='#' then begin let cats = path_cats_from_syntax url_syntax [] in let seps = separators_from_syntax url_syntax in (* Note: '>' is not allowed within URLs; because of this we can use * it as end-of-string character. *) let word_sep_list, k' = collect_words ('>'::seps) '>' cats (k6+1) in (* or raise Malformed_URL *) match word_sep_list with [ fragment, _ ] -> Some fragment, k' | _ -> assert false end else None, k6 in if k7 <> l then raise Malformed_URL; make_url ~encoded:true ?scheme ?user ~user_param ?password ?host ?port ~path ~param ?query ?fragment ?other url_syntax ;; let problem_re = Netstring_str.regexp "[ <>\"{}|\\^`]" let problem_hash_re = Netstring_str.regexp "[ <>\"{}|\\^`#]" let fixup_url_string ?(escape_hash=false) = Netstring_str.global_substitute (if escape_hash then problem_hash_re else problem_re) (fun m s -> sprintf "%%%02x" (Char.code s.[Netstring_str.match_beginning m])) ;; let parse_url ?(schemes = common_url_syntax) ?base_syntax ?(accept_8bits = false) ?(enable_fragment = false) s = let scheme = try Some (extract_url_scheme s) with Malformed_URL -> None in let syntax = match scheme with None -> ( match base_syntax with None -> raise Malformed_URL | Some syn -> partial_url_syntax syn ) | Some sch -> try Hashtbl.find schemes sch with Not_found -> raise Malformed_URL in let syntax' = if accept_8bits then { syntax with url_accepts_8bits = true } else syntax in let syntax'' = if enable_fragment && syntax.url_enable_fragment = Url_part_not_recognized then { syntax' with url_enable_fragment = Url_part_allowed } else syntax' in url_of_string syntax'' s ;; let split_path s = let l = String.length s in let rec collect_words k = let k' = try String.index_from s k '/' with Not_found -> l in let word = String.sub s k (k'-k) in if k' >= l then [word] else word :: collect_words (k'+1) in match collect_words 0 with [ "" ] -> [] | [ "";"" ] -> [ "" ] | other -> other ;; let join_path l = match l with [ "" ] -> "/" | _ -> String.concat "/" l;; let norm_path l = let rec remove_slash_slash l first = match l with | [ "" ] -> [ "" ] | [ ""; "" ] when first -> [ "" ] | "" :: l' when not first -> remove_slash_slash l' false | x :: l' -> x :: remove_slash_slash l' false | [] -> [] in let rec remove_dot l first = match l with | ([ "." ] | ["."; ""]) -> if first then [] else [ "" ] | "." :: x :: l' -> remove_dot (x :: l') false | x :: l' -> x :: remove_dot l' false | [] -> [] in let rec remove_dot_dot_once l first = match l with x :: ".." :: [] when x <> "" && x <> ".." && not first -> [ "" ] | x :: ".." :: l' when x <> "" && x <> ".." -> l' | x :: l' -> x :: remove_dot_dot_once l' false | [] -> raise Not_found in let rec remove_dot_dot l = try let l' = remove_dot_dot_once l true in remove_dot_dot l' with Not_found -> l in let l' = remove_dot_dot (remove_dot (remove_slash_slash l true) true) in match l' with [".."] -> [".."; ""] | ["";""] -> [ "" ] | _ -> l' ;; let apply_relative_url baseurl relurl = if not (baseurl.url_validity) || not (relurl.url_validity) then failwith "Neturl.apply_relative_url: URL not flagged as valid"; if relurl.url_scheme <> None then modify_url ~syntax:baseurl.url_syntax (* inherit syntax *) relurl else if relurl.url_host <> None then modify_url ~syntax:baseurl.url_syntax (* inherit syntax and scheme *) ?scheme:baseurl.url_scheme relurl else match relurl.url_path with "" :: other -> (* An absolute path *) modify_url ~syntax:baseurl.url_syntax (* inherit syntax, scheme, and *) ~encoded:true ?scheme:baseurl.url_scheme (* login info *) ?host:baseurl.url_host ?port:baseurl.url_port ?user:baseurl.url_user ~user_param:baseurl.url_user_param ?password:baseurl.url_password relurl | [] -> (* Empty: Inherit also path, params, query, and fragment *) let new_params, new_query, new_fragment = match relurl.url_param, relurl.url_query, relurl.url_fragment with [], None, None -> (* Inherit all three *) baseurl.url_param, baseurl.url_query, baseurl.url_fragment | [], None, f -> (* Inherit params and query *) baseurl.url_param, baseurl.url_query, f | [], q, f -> (* Inherit params *) baseurl.url_param, q, f | p, q, f -> (* Inherit none of them *) p, q, f in modify_url ~syntax:baseurl.url_syntax ~encoded:true ?scheme:baseurl.url_scheme ?host:baseurl.url_host ?port:baseurl.url_port ?user:baseurl.url_user ~user_param:baseurl.url_user_param ?password:baseurl.url_password ~path:baseurl.url_path ~param:new_params ?query:new_query ?fragment:new_fragment relurl | relpath -> (* A relative path *) let rec change_path basepath = match basepath with | [] -> if baseurl.url_host = None then relpath else "" :: relpath | [ "" ] -> "" :: relpath | [ x ] -> relpath | x :: basepath' -> x :: change_path basepath' in let new_path = norm_path (change_path baseurl.url_path) in modify_url ~syntax:baseurl.url_syntax (* inherit syntax, scheme, and *) ~encoded:true ?scheme:baseurl.url_scheme (* login info *) ?host:baseurl.url_host ?port:baseurl.url_port ?user:baseurl.url_user ~user_param:baseurl.url_user_param ?password:baseurl.url_password ~path:new_path (* and change path *) relurl ;; let ensure_absolute_url ?base u = if u.url_scheme = None then ( match base with None -> raise Malformed_URL | Some b -> apply_relative_url b u ) else u ;; let print_url url = Format.print_string ("<URL:" ^ string_of_url url ^ ">") ;; let backslash_re = Netstring_str.regexp "\\\\";; let drive_letter_re = Netstring_str.regexp "^\\([A-Za-z]\\):/";; let drive_letter_re' = Netstring_str.regexp "^/\\([A-Za-z]\\):/";; let unc_path_re = Netstring_str.regexp "^//\\([^/]+\\)\\(/|$\\)";; let os_type = Sys.os_type;; let classify_path p = match os_type with "Unix" -> if p <> "" && p.[0] = '/' then `Absolute_local p else `Relative p | "Win32" -> let p' = Netstring_str.global_replace backslash_re "/" p in ( match Netstring_str.string_match drive_letter_re p' 0 with Some m -> `Absolute_local ("/" ^ p') | None -> ( match Netstring_str.string_match unc_path_re p' 0 with Some m -> let host = Netstring_str.matched_group m 1 p' in let host_e = Netstring_str.group_end m 1 in let path = String.sub p' host_e (String.length p' - host_e) in let path = if path = "" then "/" else path in `Absolute_remote(host,path) | None -> if p' <> "" && p'.[0] = '/' then `Relative_drive p' else `Relative p' ) ) | "Cygwin" -> let p' = Netstring_str.global_replace backslash_re "/" p in ( match Netstring_str.string_match drive_letter_re p' 0 with Some m -> let letter = Netstring_str.matched_group m 1 p' in let rest = String.sub p' 2 (String.length p' - 2) in `Absolute_local("/cygdrive/" ^ letter ^ rest) | None -> ( match Netstring_str.string_match unc_path_re p' 0 with Some m -> let host = Netstring_str.matched_group m 1 p' in let host_e = Netstring_str.group_end m 1 in let path = String.sub p' host_e (String.length p' - host_e) in let path = if path = "" then "/" else path in `Absolute_remote(host,path) | None -> if p' <> "" && p'.[0] = '/' then `Absolute_local p' else `Relative p' ) ) | _ -> assert false ;; let file_url_of_local_path ?(getcwd = Sys.getcwd) p = (* Classify p, and make it absolute: *) let p_class = classify_path p in let p_abs_class = match p_class with `Relative r -> ( match classify_path (getcwd()) with `Absolute_local l -> if l = "/" then `Absolute_local("/" ^ r) else `Absolute_local(l ^ "/" ^ r) | `Absolute_remote(h,l) -> if l = "/" then `Absolute_remote(h,"/" ^ r) else `Absolute_remote(h,l ^ "/" ^ r) | _ -> failwith "Neturl.file_url_of_local_path: cwd is not absolute" ) | `Relative_drive r -> ( match classify_path (getcwd()) with `Absolute_local l -> ( match Netstring_str.string_match drive_letter_re' l 0 with Some m -> let letter = Netstring_str.matched_group m 1 l in `Absolute_local("/" ^ letter ^ ":" ^ r) | None -> assert false ) | `Absolute_remote(h,l) -> `Absolute_remote(h,r) | _ -> failwith "Neturl.file_url_of_local_path: cwd is not absolute" ) | other -> other in (* Generate the URL: *) let syntax = { file_url_syntax with url_accepts_8bits = true } in match p_abs_class with `Absolute_local l -> let path = split_path l in make_url ~scheme:"file" ~host:"localhost" ~path syntax | `Absolute_remote(host,l) -> let path = split_path l in make_url ~scheme:"file" ~host ~path syntax | _ -> assert false ;; let drive_letter_comp_re = Netstring_str.regexp "^\\([A-Za-z]\\)\\(:|\\|\\)";; let local_path_of_file_url u = let local_path p = if p = [] || List.hd p <> "" then failwith "Neturl.local_path_of_file_url: URL is not absolute"; match os_type with "Unix" -> join_path p | "Win32" -> (* There must be a drive letter: *) ( match p with ("" :: drive :: rest) -> (match Netstring_str.string_match drive_letter_comp_re drive 0 with Some m -> let letter = Netstring_str.matched_group m 1 drive in let rest = if rest = [] then [""] else rest in join_path((letter ^ ":") :: rest) | None -> failwith "Neturl.local_path_of_file_url: URL is not absolute"; ) | _ -> failwith "Neturl.local_path_of_file_url: URL is not absolute"; ) | "Cygwin" -> (* Recognize drive letters: *) ( match p with ("" :: drive :: rest) -> (match Netstring_str.string_match drive_letter_comp_re drive 0 with Some m -> let letter = Netstring_str.matched_group m 1 drive in join_path("" :: "cygdrive" :: letter :: rest) | None -> join_path p ) | _ -> join_path p ) | _ -> assert false in let remote_path host p = if p = [] || List.hd p <> "" then failwith "Neturl.local_path_of_file_url: URL is not absolute"; match os_type with "Unix" -> failwith "Neturl.local_path_of_file_url: Cannot process non-local file URLs" | "Win32" | "Cygwin" -> join_path( "" :: "" :: host :: List.tl p) | _ -> assert false in let opt f = try Some(f u) with Not_found -> None in match (opt url_scheme), (opt url_host), (url_path u) with (Some "file", (Some("localhost"|"")|None), []) -> local_path [""] | (Some "file", (Some("localhost"|"")|None), p) -> local_path p | (Some "file", Some host, []) -> remote_path host [""] | (Some "file", Some host, p) -> remote_path host p | (Some _, _, _) -> failwith "Neturl.local_path_of_file_url: Unexpected scheme" | (None, _, _) -> failwith "Neturl.local_path_of_file_url: Missing scheme (relative URL?)" ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>