fan-1.0 /src/sys/dotnet/fan/sys/Uri.cs

Language C# Lines 1503
MD5 Hash 497758ab37b9372d490437b349b6eb34 Estimated Cost $31,659 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
   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
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   19 Oct 06  Andy Frank  Creation
//   21 Aug 07  Brian        Rewrite with our own parser/encoder
//

using System;
using System.Collections;
using System.Text;
using Fanx.Util;
using Fanx.Serial;

namespace Fan.Sys
{
  /// <summary>
  /// Uri is used to immutably represent a Universal Resource Identifier.
  /// </summary>
  public sealed class Uri : FanObj, Literal
  {

  //////////////////////////////////////////////////////////////////////////
  // Construction
  //////////////////////////////////////////////////////////////////////////

    public static Uri fromStr(string s) { return fromStr(s, true); }
    public static Uri fromStr(string s, bool check)
    {
      try
      {
        return new Uri(new Decoder(s, false).decode());
      }
      catch (ParseErr.Val e)
      {
        if (!check) return null;
        throw ParseErr.make("Uri",  s, e.m_err.msg()).val;
      }
      catch (Exception)
      {
        if (!check) return null;
        throw ParseErr.make("Uri",  s).val;
      }
    }

    public static Uri decode(string s) { return decode(s, true); }
    public static Uri decode(string s, bool check)
    {
      try
      {
        return new Uri(new Decoder(s, true).decode());
      }
      catch (ParseErr.Val e)
      {
        if (!check) return null;
        throw ParseErr.make("Uri",  s, e.m_err.msg()).val;
      }
      catch (Exception)
      {
        if (!check) return null;
        throw ParseErr.make("Uri",  s).val;
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Utils
  //////////////////////////////////////////////////////////////////////////

    public static Map decodeQuery(string s)
    {
      try
      {
        return new Decoder(s, true).decodeQuery();
      }
      catch (ArgErr.Val e)
      {
        throw ArgErr.make("Invalid Uri query: `" + s + "`: " + e.m_err.msg()).val;
      }
      catch (Exception)
      {
        throw ArgErr.make("Invalid Uri query: `" + s + "`").val;
      }
    }

    public static string encodeQuery(Map map)
    {
      StringBuilder buf = new StringBuilder(256);

      IEnumerator en = map.keysEnumerator();
      while (en.MoveNext())
      {
        string key = (string)en.Current;
        string val = (string)map.get(key);
        if (buf.Length > 0) buf.Append('&');
        encodeQueryStr(buf, key);
        if (val != null)
        {
          buf.Append('=');
          encodeQueryStr(buf, val);
        }
      }
      return buf.ToString();
    }

    static void encodeQueryStr(StringBuilder buf, string str)
    {
      for (int i=0; i<str.Length; ++i)
      {
        int c = str[i];
        if (c < 128 && (charMap[c] & QUERY) != 0 && (delimEscMap[c] & QUERY) == 0)
          buf.Append((char)c);
        else if (c == ' ')
          buf.Append('+');
        else
          percentEncodeChar(buf, c);
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Java Constructors
  //////////////////////////////////////////////////////////////////////////

    private Uri(Sections x)
    {
      m_scheme   = x.scheme;
      m_userInfo = x.userInfo;
      m_host     = x.host;
      m_port     = x.port;
      m_pathStr  = x.pathStr;
      m_path     = x.path.ro();
      m_queryStr = x.queryStr;
      m_query    = x.query.ro();
      m_frag     = x.frag;
      m_str      = x.str != null ? x.str : new Encoder(this, false).encode();
    }

  //////////////////////////////////////////////////////////////////////////
  // Sections
  //////////////////////////////////////////////////////////////////////////

    class Sections
    {
      internal void setAuth(Uri x)  { userInfo = x.m_userInfo; host = x.m_host; port = x.m_port; }
      internal void setPath(Uri x)  { pathStr = x.m_pathStr; path = x.m_path; }
      internal void setQuery(Uri x) { queryStr = x.m_queryStr; query = x.m_query; }
      internal void setFrag(Uri x)  { frag = x.m_frag; }

      internal void normalize()
      {
        normalizeSchemes();
        normalizePath();
        normalizeQuery();
      }

      private void normalizeSchemes()
      {
        if (scheme == null) return;
        if (scheme == "http")  { normalizeScheme(80);  return; }
        if (scheme == "https") { normalizeScheme(443); return; }
        if (scheme == "ftp")   { normalizeScheme(21);  return; }
      }

      private void normalizeScheme(int p)
      {
        // port 80 -> null
        if (port != null && port.longValue() == p) port = null;

        // if path is "" -> "/"
        if (pathStr == null || pathStr.Length == 0)
        {
          pathStr = FanStr.m_ascii['/'];
          if (path == null) path = emptyPath();
        }
      }

      private void normalizePath()
      {
        if (path == null) return;

        bool isAbs = pathStr.StartsWith("/");
        bool isDir = pathStr.EndsWith("/");
        bool dotLast = false;
        bool modified = false;
        for (int i=0; i<path.sz(); ++i)
        {
          string seg = (string)path.get(i);
          if (seg == "." && (path.sz() > 1 || host != null))
          {
            path.removeAt(i);
            modified = true;
            dotLast = true;
            i -= 1;
          }
          else if (seg == ".." && i > 0 && path.get(i-1).ToString() != "..")
          {
            path.removeAt(i);
            path.removeAt(i-1);
            modified = true;
            i -= 2;
            dotLast = true;
          }
          else
          {
            dotLast = false;
          }
        }

        if (modified)
        {
          if (dotLast) isDir = true;
          if (path.sz() == 0 || path.last().ToString() == "..") isDir = false;
          pathStr = toPathStr(isAbs, path, isDir);
        }
      }

      private void normalizeQuery()
      {
        if (query == null)
          query = emptyQuery();
      }

      internal string scheme;
      internal string host;
      internal string userInfo;
      internal Long port;
      internal string pathStr;
      internal List path;
      internal string queryStr;
      internal Map query;
      internal string frag;
      internal string str;
    }

  //////////////////////////////////////////////////////////////////////////
  // Decoder
  //////////////////////////////////////////////////////////////////////////

    class Decoder : Sections
    {
      internal Decoder(string str, bool decoding)
      {
        this.str = str;
        this.decoding = decoding;
      }

      internal Decoder decode()
      {
        string str = this.str;
        int len = str.Length;
        int pos = 0;

        // ==== scheme ====

        // scan the string from the beginning looking for either a
        // colon or any character which doesn't fit a valid scheme
        bool hasUpper = false;
        for (int i=0; i<len; ++i)
        {
          int c = str[i];
          if (isScheme(c)) { hasUpper |= isUpper(c); continue; }
          if (c != ':') break;

          // at this point we have a scheme; if we detected
          // any upper case characters normalize to lowercase
          pos = i + 1;
          string scheme = str.Substring(0, i);
          if (hasUpper) scheme = FanStr.lower(scheme);
          this.scheme = scheme;
          break;
        }

        // ==== authority ====

        // authority must start with //
        if (pos+1 < len && str[pos] == '/' && str[pos+1] == '/')
        {
          // find end of authority which is /, ?, #, or end of string;
          // while we're scanning look for @ and last colon which isn't
          // inside an [] IPv6 literal
          int authStart = pos+2, authEnd = len, at = -1, colon = -1;
          for (int i=authStart; i<len; ++i)
          {
            int c = str[i];
            if (c == '/' || c == '?' || c == '#') { authEnd = i; break; }
            else if (c == '@' && at < 0) { at = i; colon = -1; }
            else if (c == ':') colon = i;
            else if (c == ']') colon = -1;
          }

          // start with assumption that there is no userinfo or port
          int hostStart = authStart, hostEnd = authEnd;

          // if we found an @ symbol, parse out userinfo
          if (at > 0)
          {
            this.userInfo = substr(authStart, at, USER);
            hostStart = at+1;
          }

          // if we found an colon, parse out port
          if (colon > 0)
          {
            this.port = Long.valueOf(Convert.ToInt64(str.Substring(colon+1, authEnd-colon-1)));
            hostEnd = colon;
          }

          // host is everything left in the authority
          this.host = substr(hostStart, hostEnd, HOST);
          pos = authEnd;
        }

        // ==== path ====

        // scan the string looking '?' or '#' which ends the path
        // section; while we're scanning count the number of slashes
        int pathStart = pos, pathEnd = len, numSegs = 1, prev = 0;
        for (int i=pathStart; i<len; ++i)
        {
          int c = str[i];
          if (prev != '\\')
          {
            if (c == '?' || c == '#') { pathEnd = i; break; }
            if (i != pathStart && c == '/') ++numSegs;
            prev = c;
          }
          else
          {
            prev = (c != '\\') ? c : 0;
          }
        }

        // we now have the complete path section
        this.pathStr = substr(pathStart, pathEnd, PATH);
        this.path = pathSegments(pathStr, numSegs);
        pos = pathEnd;

        // ==== query ====

        if (pos < len && str[pos] == '?')
        {
          // look for end of query which is # or end of string
          int queryStart = pos+1, queryEnd = len;
          prev = 0;
          for (int i=queryStart; i<len; ++i)
          {
            int c = str[i];
            if (prev != '\\')
            {
              if (c == '#') { queryEnd = i; break; }
              prev = c;
            }
            else
            {
              prev = (c != '\\') ? c : 0;
            }
          }

          // we now have the complete query section
          this.queryStr = substr(queryStart, queryEnd, QUERY);
          this.query = parseQuery(queryStr);
          pos = queryEnd;
        }

        // ==== frag ====

        if (pos < len  && str[pos] == '#')
        {
          this.frag = substr(pos+1, len, FRAG);
        }

        // === normalize ===
        normalize();
        return this;
      }

      private List pathSegments(string pathStr, int numSegs)
      {
        // if pathStr is "/" then path si the empty list
        int len = pathStr.Length;
        if (len == 0 || (len == 1 && pathStr[0] == '/'))
          return emptyPath();

        // check for trailing slash
        if (len > 1 && pathStr[len-1] == '/')
        {
          numSegs--;
          len--;
        }

        // parse the segments
        string[] path = new string[numSegs];
        int n = 0;
        int segStart = 0, prev = 0;
        for (int i=0; i<pathStr.Length; ++i)
        {
          int c = pathStr[i];
          if (prev != '\\')
          {
            if (c == '/')
            {
              if (i > 0) path[n++] = pathStr.Substring(segStart, i-segStart);
              segStart = i+1;
            }
            prev = c;
          }
          else
          {
            prev = (c != '\\') ? c : 0;
          }
        }
        if (segStart < len)
          path[n++] = pathStr.Substring(segStart, pathStr.Length-segStart);

        return new List(Sys.StrType, path);
      }

      internal Map decodeQuery()
      {
        return parseQuery(substring(0, str.Length, QUERY));
      }

      private Map parseQuery(string q)
      {
        if (q == null) return null;
        Map map = new Map(Sys.StrType, Sys.StrType);

        try
        {
          int start = 0, eq = 0, len = q.Length, prev = 0;
          bool escaped = false;
          for (int i=0; i<len; ++i)
          {
            int ch = q[i];
            if (prev != '\\')
            {
              if (ch == '=') eq = i;
              if (ch != '&' && ch != ';') { prev = ch; continue; }
            }
            else
            {
              escaped = true;
              prev = (ch != '\\') ? ch : 0;
              continue;
            }

            if (start < i)
            {
              addQueryParam(map, q, start, eq, i, escaped);
              escaped = false;
            }

            start = eq = i+1;
          }

          if (start < len)
            addQueryParam(map, q, start, eq, len, escaped);
        }
        catch (Exception e)
        {
          // don't let internal error bring down whole uri
          Err.dumpStack(e);
        }

        return map;
      }

      private void addQueryParam(Map map, string q, int start, int eq, int end, bool escaped)
      {
        string key, val;
        if (start == eq && q[start] != '=')
        {
          key = toQueryStr(q, start, end, escaped);
          val = "true";
        }
        else
        {
          key = toQueryStr(q, start, eq, escaped);
          val = toQueryStr(q, eq+1, end, escaped);
        }

        string dup = (string)map.get(key);
        if (dup != null) val = dup + "," + val;
        map.set(key, val);
      }

      private string toQueryStr(string q, int start, int end, bool escaped)
      {
        if (!escaped) return q.Substring(start, end-start);
        StringBuilder s = new StringBuilder(end-start);
        int prev = 0;
        for (int i=start; i<end; ++i)
        {
          int c = q[i];
          if (c != '\\')
          {
            s.Append((char)c);
            prev = c;
          }
          else
          {
            if (prev == '\\') { s.Append((char)c); prev = 0; }
            else prev = c;
          }
        }
        return s.ToString();
      }

      private string substr(int start, int end, int section)
      {
        return substring(start, end, section);
      }

      private string substring(int start, int end, int section)
      {
        if (!decoding) return str.Substring(start, end-start);

        StringBuilder buf = new StringBuilder(end-start);
        dpos = start;
        while (dpos < end)
        {
           int ch = nextChar(section);
           if (nextCharWasEscaped && ch < delimEscMap.Length && (delimEscMap[ch] & section) != 0)
             buf.Append('\\');
           buf.Append((char)ch);
        }
        return buf.ToString();
      }

      private int nextChar(int section)
      {
        int c = nextOctet(section);
        if (c < 0) return -1;
        int c2, c3;
        switch (c >> 4)
        {
          case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
            /* 0xxxxxxx*/
            return c;
          case 12: case 13:
            /* 110x xxxx   10xx xxxx*/
            c2 = nextOctet(section);
            if ((c2 & 0xC0) != 0x80)
              throw err("Invalid UTF-8 encoding");
            return ((c & 0x1F) << 6) | (c2 & 0x3F);
          case 14:
            /* 1110 xxxx  10xx xxxx  10xx xxxx */
            c2 = nextOctet(section);
            c3 = nextOctet(section);
            if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
              throw err("Invalid UTF-8 encoding");
            return (((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
          default:
            throw err("Invalid UTF-8 encoding");
        }
      }

      private int nextOctet(int section)
      {
        int c = str[dpos++];

        // if percent encoded applied to all sections except
        // scheme which should never never use this method
        if (c == '%')
        {
          nextCharWasEscaped = true;
          return (hexNibble(str[dpos++]) << 4) | hexNibble(str[dpos++]);
        }
        else
        {
          nextCharWasEscaped = false;
        }

        // + maps to space only in query
        if (c == '+' && section == QUERY)
          return ' ';

        // verify character ok
        if (c >= charMap.Length || (charMap[c] & section) == 0)
          throw err("Invalid char in " + toSection(section) + " at index " + (dpos-1));

        // return character as is
        return c;
      }

      new string str;
      bool decoding;
      int dpos;
      bool nextCharWasEscaped;
   }

  //////////////////////////////////////////////////////////////////////////
  // Encoder
  //////////////////////////////////////////////////////////////////////////

    class Encoder
    {
      internal Encoder(Uri uri, bool encoding)
      {
        this.uri = uri;
        this.encoding = encoding;
        this.buf = new StringBuilder();
      }

      internal string encode()
      {
        // scheme
        if (uri.m_scheme != null) buf.Append(uri.m_scheme).Append(':');

        // authority
        if (uri.m_userInfo != null || uri.m_host != null || uri.m_port != null)
        {
          buf.Append('/').Append('/');
          if (uri.m_userInfo != null) encode(uri.m_userInfo, USER).Append('@');
          if (uri.m_host != null) encode(uri.m_host, HOST);
          if (uri.m_port != null) buf.Append(':').Append(uri.m_port);
        }

        // path
        if (uri.m_pathStr != null)
          encode(uri.m_pathStr, PATH);

        // query
        if (uri.m_queryStr != null)
          { buf.Append('?'); encode(uri.m_queryStr, QUERY); }

        // frag
        if (uri.m_frag != null)
          { buf.Append('#'); encode(uri.m_frag, FRAG); }

        return buf.ToString();
      }

      internal StringBuilder encode(string s, int section)
      {
        if (!encoding) return buf.Append(s);

        int len = s.Length;
        int c = 0, prev;
        for (int i=0; i<len; ++i)
        {
          prev = c;
          c = s[i];

          // unreserved character
          if (c < 128 && (charMap[c] & section) != 0 && prev != '\\')
          {
            buf.Append((char)c);
            continue;
          }

          // the backslash esc itself doesn't get encoded
          if (c == '\\' && prev != '\\') continue;

          // we have a reserved, escaped, or non-ASCII

          // encode
          if (c == ' ' && section == QUERY)
            buf.Append('+');
          else
            percentEncodeChar(buf, c);

          // if we just encoded backslash, then it
          // doesn't escape the next char
          if (c == '\\') c = 0;
        }
        return buf;
      }

      Uri uri;
      bool encoding;
      StringBuilder buf;
    }

    static void percentEncodeChar(StringBuilder buf, int c)
    {
      if (c <= 0x007F)
      {
        percentEncodeByte(buf, c);
      }
      else if (c > 0x07FF)
      {
        percentEncodeByte(buf, 0xE0 | ((c >> 12) & 0x0F));
        percentEncodeByte(buf, 0x80 | ((c >>  6) & 0x3F));
        percentEncodeByte(buf, 0x80 | ((c >>  0) & 0x3F));
      }
      else
      {
        percentEncodeByte(buf, 0xC0 | ((c >>  6) & 0x1F));
        percentEncodeByte(buf, 0x80 | ((c >>  0) & 0x3F));
      }
    }

    static void percentEncodeByte(StringBuilder buf, int c)
    {
      buf.Append('%');
      int hi = (c >> 4) & 0xf;
      int lo = c & 0xf;
      buf.Append((char)(hi < 10 ? '0'+hi : 'A'+(hi-10)));
      buf.Append((char)(lo < 10 ? '0'+lo : 'A'+(lo-10)));
    }

  //////////////////////////////////////////////////////////////////////////
  // Identity
  //////////////////////////////////////////////////////////////////////////

    public override bool Equals(object obj)
    {
      if (obj is Uri)
      {
        return m_str == ((Uri)obj).m_str;
      }
      return false;
    }

    public override int GetHashCode()
    {
      return m_str.GetHashCode();
    }

    public override long hash()
    {
      return FanStr.hash(m_str);
    }

    public override string toStr()
    {
      return m_str;
    }

    public string toLocale()
    {
      return m_str;
    }

    public void encode(ObjEncoder @out)
    {
      @out.wStrLiteral(m_str, '`');
    }

    public override Type @typeof()
    {
      return Sys.UriType;
    }

    public string encode()
    {
      string x = m_encoded;
      if (x != null) return x;
      return m_encoded = new Encoder(this, true).encode();
    }

  //////////////////////////////////////////////////////////////////////////
  // Components
  //////////////////////////////////////////////////////////////////////////

    public bool isAbs()
    {
      return m_scheme != null;
    }

    public bool isRel()
    {
      return m_scheme == null;
    }

    public bool isDir()
    {
      if (m_pathStr != null)
      {
        string p = m_pathStr;
        int len = p.Length;
        if (len > 0 && p[len-1] == '/')
          return true;
      }
      return false;
    }

    public string scheme()
    {
      return m_scheme;
    }

    public string auth()
    {
      if (m_host == null) return null;
      if (m_port == null)
      {
        if (m_userInfo == null) return m_host;
        else return m_userInfo + '@' + m_host;
      }
      else
      {
        if (m_userInfo == null) return m_host + ':' + m_port;
        else return m_userInfo + '@' + m_host + ':' + m_port;
      }
    }

    public string host()
    {
      return m_host;
    }

    public string userInfo()
    {
      return m_userInfo;
    }

    public Long port()
    {
      return m_port;
    }

    public string path(int depth) { return ((string)m_path.get(depth)); }
    public List path()
    {
      return m_path;
    }

    public string pathStr()
    {
      return m_pathStr;
    }

    public bool isPathAbs()
    {
      if (m_pathStr == null || m_pathStr.Length == 0)
        return false;
      else
        return m_pathStr[0] == '/';
    }

    public bool isPathOnly()
    {
      return m_scheme == null && m_host == null && m_port == null &&
        m_userInfo == null && m_queryStr == null && m_frag == null;
    }

    public string name()
    {
      if (m_path.sz() == 0) return string.Empty;
      return (string)m_path.last();
    }

    public string basename()
    {
      string n = this.name();
      int dot = n.LastIndexOf('.');
      if (dot < 2)
      {
        if (dot < 0) return n;
        if (n == ".") return n;
        if (n == "..") return n;
      }
      return n.Substring(0, dot);
    }

    public string ext()
    {
      string n = this.name();
      int dot = n.LastIndexOf('.');
      if (dot < 2)
      {
        if (dot < 0) return null;
        if (n == ".") return null;
        if (n == "..") return null;
      }
      return n.Substring(dot+1);
    }

    public MimeType mimeType()
    {
      if (isDir()) return MimeType.m_dir;
      return MimeType.forExt(ext());
    }

    public Map query()
    {
      return m_query;
    }

    public string queryStr()
    {
      return m_queryStr;
    }

    public string frag()
    {
      return m_frag;
    }

  //////////////////////////////////////////////////////////////////////////
  // Utils
  //////////////////////////////////////////////////////////////////////////

    public Uri parent()
    {
      // if no path bail
      if (m_path.sz() == 0) return null;

      // if just a simple filename, then no parent
      string p = m_pathStr;
      if (m_path.sz() == 1 && !isPathAbs() && !isDir()) return null;

      // use slice
      return getRange(parentRange);
    }

    public Uri pathOnly()
    {
      if (m_pathStr == null)
        throw Err.make("Uri has no path: " + this).val;

      if (m_scheme == null && m_userInfo == null && m_host == null &&
          m_port == null && m_queryStr == null && m_frag == null)
        return this;

      Sections t = new Sections();
      t.path     = this.m_path;
      t.pathStr  = this.m_pathStr;
      t.query    = emptyQuery();
      t.str      = this.m_pathStr;
      return new Uri(t);
    }

    public Uri getRange(Range range) { return slice(range, false); }

    public Uri getRangeToPathAbs(Range range) { return slice(range, true); }

    private Uri slice(Range range, bool forcePathAbs)
    {
      if (m_pathStr == null)
        throw Err.make("Uri has no path: " + this).val;

      int size = m_path.sz();
      int s = range.start(size);
      int e = range.end(size);
      int n = e - s + 1;
      if (n < 0) throw IndexErr.make(range).val;

      bool head = (s == 0);
      bool tail = (e == size-1);
      if (head && tail && (!forcePathAbs || isPathAbs())) return this;

      Sections t = new Sections();
      t.path = m_path.getRange(range);

      StringBuilder sb = new StringBuilder(m_pathStr.Length);
      if ((head && isPathAbs()) || forcePathAbs) sb.Append('/');
      for (int i=0; i<t.path.sz(); ++i)
      {
        if (i > 0) sb.Append('/');
        sb.Append(t.path.get(i));
      }
      if (t.path.sz() > 0 && (!tail || isDir())) sb.Append('/');
      t.pathStr = sb.ToString();

      if (head)
      {
        t.scheme   = m_scheme;
        t.userInfo = m_userInfo;
        t.host     = m_host;
        t.port     = m_port;
      }

      if (tail)
      {
        t.queryStr = m_queryStr;
        t.query    = m_query;
        t.frag     = m_frag;
      }
      else
      {
        t.query    = emptyQuery();
      }

      if (!head && !tail)
      {
        t.str = t.pathStr;
      }

      return new Uri(t);
    }

  //////////////////////////////////////////////////////////////////////////
  // Relativize
  //////////////////////////////////////////////////////////////////////////

    public Uri relTo(Uri baseUri)
    {
      if (!OpUtil.compareEQ(this.m_scheme,   baseUri.m_scheme) ||
          !OpUtil.compareEQ(this.m_userInfo, baseUri.m_userInfo) ||
          !OpUtil.compareEQ(this.m_host,     baseUri.m_host) ||
          !OpUtil.compareEQ(this.m_port,     baseUri.m_port))
        return this;

      // at this point we know we have the same scheme and auth, and
      // we're going to create a new URI which is a subset of this one
      Sections t = new Sections();
      t.query    = this.m_query;
      t.queryStr = this.m_queryStr;
      t.frag     = this.m_frag;

      // find divergence
      int d=0;
      int len = Math.Min(this.m_path.sz(), baseUri.m_path.sz());
      for (; d<len; ++d)
        if (!this.m_path.get(d).Equals(baseUri.m_path.get(d)))
          break;

      // if diverenge is at root, then no commonality
      if (d == 0)
      {
        // `/a/b/c`.relTo(`/`) should be `a/b/c`
        if (baseUri.m_path.isEmpty() && this.m_pathStr.StartsWith("/"))
        {
          t.path = this.m_path;
          t.pathStr = this.m_pathStr.Substring(1);
        }
        else
        {
          t.path = this.m_path;
          t.pathStr = this.m_pathStr;
        }
      }

      // if paths are exactly the same
      else if (d == this.m_path.sz() && d == baseUri.m_path.sz())
      {
        t.path = emptyPath();
        t.pathStr = string.Empty;
      }

      // create sub-path at divergence point
      else
      {
        // slice my path
        t.path = this.m_path.getRange(Range.makeInclusive(d, -1));

        // insert .. backup if needed
        int backup = baseUri.m_path.sz() - d;
        if (!baseUri.isDir()) backup--;
        while (backup-- > 0) t.path.insert(0, dotDot);

        // format the new path string
        t.pathStr = toPathStr(false, t.path, this.isDir());
      }

      return new Uri(t);
    }

    public Uri relToAuth()
    {
      if (m_scheme == null && m_userInfo == null &&
          m_host == null && m_port == null)
        return this;

      Sections t = new Sections();
      t.path     = this.m_path;
      t.pathStr  = this.m_pathStr;
      t.query    = this.m_query;
      t.queryStr = this.m_queryStr;
      t.frag     = this.m_frag;
      return new Uri(t);
    }

  //////////////////////////////////////////////////////////////////////////
  // Plus
  //////////////////////////////////////////////////////////////////////////

    public Uri plus(Uri r)
    {
      // if r is more or equal as absolute as base, return r
      if (r.m_scheme != null) return r;
      if (r.m_host != null && this.m_scheme == null) return r;
      if (r.isPathAbs() && this.m_host == null) return r;

      // this algorthm is lifted straight from
      // RFC 3986 (5.2.2) Transform References;
      Uri baseUri = this;
      Sections t = new Sections();
      if (r.m_host != null)
      {
        t.setAuth(r);
        t.setPath(r);
        t.setQuery(r);
      }
      else
      {
        if (r.m_pathStr == null || r.m_pathStr == "")
        {
          t.setPath(baseUri);
          if (r.m_queryStr != null)
            t.setQuery(r);
          else
            t.setQuery(baseUri);
        }
        else
        {
          if (r.m_pathStr.StartsWith("/"))
            t.setPath(r);
          else
            merge(t, baseUri, r);
          t.setQuery(r);
        }
        t.setAuth(baseUri);
      }
      t.scheme = baseUri.m_scheme;
      t.frag   = r.m_frag;
      t.normalize();
      return new Uri(t);
    }

    static void merge(Sections t, Uri baseUri, Uri r)
    {
      bool baseIsAbs = baseUri.isPathAbs();
      bool baseIsDir = baseUri.isDir();
      bool rIsDir    = r.isDir();
      List rPath     = r.m_path;
      bool dotLast   = false;

      // compute the target path taking into account whether
      // the base is a dir and any dot segments in relative ref
      List tPath;
      if (baseUri.m_path.sz() == 0)
      {
        tPath = r.m_path;
      }
      else
      {
        tPath = baseUri.m_path.rw();
        if (!baseIsDir) tPath.pop();
        for (int i=0; i<rPath.sz(); ++i)
        {
          string rSeg = (string)rPath.get(i);
          if (rSeg == ".") { dotLast = true; continue; }
          if (rSeg == "..")
          {
            if (!tPath.isEmpty()) { tPath.pop(); dotLast = true; continue; }
            if (baseIsAbs) continue;
          }
          tPath.add(rSeg); dotLast = false;
        }
        //tPath = tPath;
      }

      t.path = tPath;
      t.pathStr = toPathStr(baseIsAbs, tPath, rIsDir || dotLast);
    }

    static string toPathStr(bool isAbs, List path, bool isDir)
    {
      StringBuilder buf = new StringBuilder();
      if (isAbs) buf.Append('/');
      for (int i=0; i<path.sz(); ++i)
      {
        if (i > 0) buf.Append('/');
        buf.Append(path.get(i));
      }
      if (isDir && !(buf.Length > 0 && buf[buf.Length-1] == '/'))
        buf.Append('/');
      return buf.ToString();
    }

    public Uri plusName(string name) { return plusName(name, false); }
    public Uri plusName(string name, bool asDir)
    {
      int size        = m_path.sz();
      bool isDir      = this.isDir() || m_path.isEmpty();
      int newSize     = isDir ? size + 1 : size;
      string[] temp   = (string[])m_path.toArray(new string[newSize]);
      temp[newSize-1] = name;

      Sections t = new Sections();
      t.scheme   = this.m_scheme;
      t.userInfo = this.m_userInfo;
      t.host     = this.m_host;
      t.port     = this.m_port;
      t.query    = emptyQuery();
      t.queryStr = null;
      t.frag     = null;
      t.path     = new List(Sys.StrType, temp);
      t.pathStr  = toPathStr(isAbs() || isPathAbs(), t.path, asDir);
      return new Uri(t);
    }

    public Uri plusSlash()
    {
      if (isDir()) return this;
      Sections t = new Sections();
      t.scheme   = this.m_scheme;
      t.userInfo = this.m_userInfo;
      t.host     = this.m_host;
      t.port     = this.m_port;
      t.query    = this.m_query;
      t.queryStr = this.m_queryStr;
      t.frag     = this.m_frag;
      t.path     = this.m_path;
      t.pathStr  = this.m_pathStr + "/";
      return new Uri(t);
    }

    public Uri plusQuery(Map q)
    {
      if (q == null || q.isEmpty()) return this;

      Map merge = m_query.dup().setAll(q);

      StringBuilder s = new StringBuilder(256);
      IDictionaryEnumerator en = merge.pairsIterator();
      while (en.MoveNext())
      {
        if (s.Length > 0) s.Append('&');
        string key = (string)en.Key;
        string val = (string)en.Value;
        appendQueryStr(s, key);
        s.Append('=');
        appendQueryStr(s, val);
      }

      Sections t = new Sections();
      t.scheme   = m_scheme;
      t.userInfo = m_userInfo;
      t.host     = m_host;
      t.port     = m_port;
      t.frag     = m_frag;
      t.pathStr  = m_pathStr;
      t.path     = m_path;
      t.query    = merge.ro();
      t.queryStr = s.ToString();
      return new Uri(t);
    }

    static void appendQueryStr(StringBuilder buf, string str)
    {
      for (int i=0; i<str.Length; ++i)
      {
        int c = str[i];
        if (c < delimEscMap.Length && (delimEscMap[c] & QUERY) != 0)
          buf.Append('\\');
        buf.Append((char)c);
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Resolution
  //////////////////////////////////////////////////////////////////////////

    public File toFile()
    {
      return File.make(this);
    }

    public object get() { return get(null, true); }
    public object get(object @base) { return get(@base, true); }
    public object get(object @base, bool check)
    {
      // if we have a relative uri, we need to resolve against
      // the base object's uri
      Uri uri = this;
      if (m_scheme == null)
      {
        if (@base == null) throw UnresolvedErr.make("Relative uri with no base: " + this).val;
        Uri baseUri = null;
        try
        {
          baseUri = (Uri)trap(@base, "uri", null);
          if (baseUri == null)
            throw UnresolvedErr.make("Base object's uri is null: " + this).val;
        }
        catch (System.Exception e)
        {
          throw UnresolvedErr.make("Cannot access base '" + FanObj.@typeof(@base) + ".uri' to normalize: " + this, e).val;
        }
        if (baseUri.m_scheme == null)
          throw UnresolvedErr.make("Base object's uri is not absolute: " + baseUri).val;
        uri = baseUri.plus(this);
      }

      // resolve scheme handler
      UriScheme scheme = UriScheme.find(uri.m_scheme);

      // route to scheme
      try
      {
        return scheme.get(uri, @base);
      }
      catch (UnresolvedErr.Val e)
      {
        if (check) throw e;
        return null;
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Conversion
  //////////////////////////////////////////////////////////////////////////

    public string toCode()
    {
      StringBuilder s = new StringBuilder(m_str.Length+4);
      s.Append('`');

      int len = m_str.Length;
      for (int i=0; i<len; ++i)
      {
        int c = m_str[i];
        switch (c)
        {
          case '\n': s.Append('\\').Append('n'); break;
          case '\r': s.Append('\\').Append('r'); break;
          case '\f': s.Append('\\').Append('f'); break;
          case '\t': s.Append('\\').Append('t'); break;
          case '`':  s.Append('\\').Append('`'); break;
          case '$':  s.Append('\\').Append('$'); break;
          default:   s.Append((char)c); break;
        }
      }

      // closing quote
      return s.Append('`').ToString();
    }

  //////////////////////////////////////////////////////////////////////////
  // Utils
  //////////////////////////////////////////////////////////////////////////

    public static bool isName(string name)
    {
      int len = name.Length;

      // must be at least one character long
      if (len == 0) return false;

      // check for "." and ".."
      if (name[0] == '.' && len <= 2)
      {
        if (len == 1) return false;
        if (name[1] == '.') return false;
      }

      // check that each char is unreserved
      for (int i=0; i<len; ++i)
      {
        int c = name[i];
        if (c < 128 && nameMap[c]) continue;
        return false;
      }

      return true;
    }

    public static void checkName(string name)
    {
      if (!isName(name))
        throw NameErr.make(name).val;
    }

    static bool isUpper(int c)
    {
      return 'A' <= c && c <= 'Z';
    }

    static int hexNibble(int ch)
    {
      if ((charMap[ch] & HEX) == 0) throw err("Invalid percent encoded hex: '" + (char)ch);
      if (ch <= '9') return ch - '0';
      if (ch <= 'Z') return (ch - 'A') + 10;
      return (ch - 'a') + 10;
    }

    static Exception err(string msg)
    {
      return ParseErr.make(msg).val;
    }

  //////////////////////////////////////////////////////////////////////////
  // Character Map
  //////////////////////////////////////////////////////////////////////////

    static string toSection(int section)
    {
      switch (section)
      {
        case SCHEME: return "scheme";
        case USER:   return "userInfo";
        case HOST:   return "host";
        case PATH:   return "path";
        case QUERY:  return "query";
        case FRAG:   return "frag";
        default:     return "uri";
      }
    }

    static bool isScheme(int c) { return c < 128 ? (charMap[c] & SCHEME) != 0 : false; }

    static readonly byte[] charMap = new byte[128];
    static readonly bool[] nameMap = new bool[128];
    static readonly byte[] delimEscMap = new byte[128];
    const byte SCHEME     = 0x01;
    const byte USER       = 0x02;
    const byte HOST       = 0x04;
    const byte PATH       = 0x08;
    const byte QUERY      = 0x10;
    const byte FRAG       = 0x20;
    const byte DIGIT      = 0x40;
    const byte HEX        = 0x80;
    static Uri()
    {
      // alpha/digits characters
      byte unreserved = SCHEME | USER | HOST | PATH | QUERY | FRAG;
      for (int i='a'; i<='z'; ++i) { charMap[i] = unreserved; nameMap[i] = true; }
      for (int i='A'; i<='Z'; ++i) { charMap[i] = unreserved; nameMap[i] = true; }
      for (int i='0'; i<='9'; ++i) { charMap[i] = unreserved; nameMap[i] = true; }

      // unreserved symbols
      charMap['-'] = unreserved; nameMap['-'] = true;
      charMap['.'] = unreserved; nameMap['.'] = true;
      charMap['_'] = unreserved; nameMap['_'] = true;
      charMap['~'] = unreserved; nameMap['~'] = true;

      // hex
      for (int i='0'; i<='9'; ++i) charMap[i] |= HEX | DIGIT;
      for (int i='a'; i<='f'; ++i) charMap[i] |= HEX;
      for (int i='A'; i<='F'; ++i) charMap[i] |= HEX;

      // sub-delimiter symbols
      charMap['!']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['$']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['&']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['\''] = USER | HOST | PATH | QUERY | FRAG;
      charMap['(']  = USER | HOST | PATH | QUERY | FRAG;
      charMap[')']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['*']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['+']  = SCHEME | USER | HOST | PATH | FRAG;
      charMap[',']  = USER | HOST | PATH | QUERY | FRAG;
      charMap[';']  = USER | HOST | PATH | QUERY | FRAG;
      charMap['=']  = USER | HOST | PATH | QUERY | FRAG;

      // gen-delimiter symbols
      charMap[':'] = PATH | USER | QUERY | FRAG;
      charMap['/'] = PATH | QUERY | FRAG;
      charMap['?'] = QUERY | FRAG;
      charMap['#'] = 0;
      charMap['['] = 0;
      charMap[']'] = 0;
      charMap['@'] = PATH | QUERY | FRAG;

      // delimiter escape map - which characters need to
      // be backslashed escaped in each section
      delimEscMap[':']  = PATH;
      delimEscMap['/']  = PATH;
      delimEscMap['?']  = PATH;
      delimEscMap['#']  = PATH | QUERY;
      delimEscMap['&']  = QUERY;
      delimEscMap[';']  = QUERY;
      delimEscMap['=']  = QUERY;
      delimEscMap['\\'] = SCHEME | USER | HOST | PATH | QUERY | FRAG;
    }

  //////////////////////////////////////////////////////////////////////////
  // Empty Path/Query
  //////////////////////////////////////////////////////////////////////////

    static List emptyPath()
    {
      List p = m_emptyPath;
      if (p == null) p = m_emptyPath = (List)new List(Sys.StrType).toImmutable();
      return p;
    }
    static List m_emptyPath;

    static Map emptyQuery()
    {
      Map q = m_emptyQuery;
      if (q == null) q = m_emptyQuery = (Map)new Map(Sys.StrType, Sys.StrType).toImmutable();
      return q;
    }
    static Map m_emptyQuery;

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    static readonly Range parentRange = Range.make(0, -2, false);
    static readonly string dotDot = "..";

    public static readonly Uri m_defVal = fromStr("");

    internal readonly string m_str;
    internal readonly string m_scheme;
    internal readonly string m_userInfo;
    internal readonly string m_host;
    internal readonly Long m_port;
    internal readonly List m_path;
    internal readonly string m_pathStr;
    internal readonly Map m_query;
    internal readonly string m_queryStr;
    internal readonly string m_frag;
    internal string m_encoded;

  }
}
Back to Top