-
Notifications
You must be signed in to change notification settings - Fork 1
/
uri.d
1423 lines (1199 loc) · 42.5 KB
/
uri.d
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
/**
* Provides access to a URI address components.
*
* Requires std.experimental.allocator. Should be moved into std.uri once it is merged with the std namespace.
*
* Copyright: Copyright Digital Mars 2000 - 2015.
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: $(WEB digitalmars.com, Richard Andrew Cattermole)
* Source: $(PHOBOSSRC std/experimental_uri.d)
*/
module std.experimental.uri;
import std.experimental.allocator : IAllocator, makeArray, theAllocator,
expandArray, shrinkArray, dispose, make;
import std.typecons : Nullable;
public import std.uri;
private
{
// santisization uses this
IAllocator _allocatedBy;
char[] charBuffer;
size_t[] charIndexBuffer;
char[] tCopyBuffer;
static this() {
import std.experimental.allocator : theAllocator;
_allocatedBy = theAllocator();
charBuffer = _allocatedBy.makeArray!char(1024);
charIndexBuffer = _allocatedBy.makeArray!size_t(128);
tCopyBuffer = _allocatedBy.makeArray!char(1024);
}
static ~this() {
_allocatedBy.dispose(charBuffer);
_allocatedBy.dispose(charIndexBuffer);
_allocatedBy.dispose(tCopyBuffer);
}
}
/**
* The URI address wrapper.
* Provides access to each of the components, however they are readonly.
* It can be used to create new addresses that are compliant with the specification.
*
* It will not perform encoding or decoding of any part of a URI. You must do it as necessary during usage.
*/
struct URIAddress
{
string value;
alias value this;
private
{
IAllocator alloc;
char[][] arrbuffDirectories;
bool shouldDeallocate = true;
}
/// Duplicates by copying the path string
this(this)
{
arrbuffDirectories = alloc.makeArray!(char[])(arrbuffDirectories.length);
auto t = alloc.makeArray!char(value.length);
t[0 .. $] = value[];
value = cast(string) t;
}
/// Deallocates the path string
~this() nothrow
{
try {
alloc.dispose(cast(char[]) value);
alloc.dispose(arrbuffDirectories);
} catch (Exception) {}
}
/**
* Takes a string that represents a URI path
*
* Rerpresents a URI in the form of "$(LT)protName://$(GT)[user[:pass]]@[host/ip[:port]][/]$(LT)$(LT)drive$(GT):\$(GT)][<dir/>...][file[.ext]]".
* Will automatically sanitise the input after duplicating it. If not specified file will be set as the protocol.
*
* Can be used to represent a file system entry (such as a Windows drive).
*
* Params:
* value = The path value, defaults to current working directory.
* alloc = The allocator to allocate return values as necssary.
*/
static URIAddress opCall(string value = ".", IAllocator alloc = theAllocator())
{
URIAddress self;
self.alloc = alloc;
// duplicate the value
self.value = cast(immutable) alloc.makeArray!char(value.length);
(cast(char[]) self.value[]) = cast(char[]) value[];
// sanitise the value
self.sanitise();
return self;
}
@property
{
///
IAllocator allocator()
{
return alloc;
}
/**
* Gets the connection information with protocol from the given string.
*
* May commonly be just "file://".
* Is of the format "$(LT)protName://$(GT)[user[:pass]]@[host/ip[:port]]"
*
* Returns:
* The connection string
*/
string connectionInfo()
{
import std.string : indexOf;
// $(LT)protName://$(GT)
string prot = scheme;
string value2 = value[prot.length + 3 .. $];
// [user[:pass]]@[host/ip[:port]][/][$(LT)dir/$(GT)...][file[.ext]]
if (prot == "file")
return value[0 .. 7];
ptrdiff_t i;
if ((i = value2.indexOf("/")) >= 0)
{
return value[0 .. i + prot.length + 3];
}
else
{
return value;
}
}
///
unittest
{
assert(URIAddress("").connectionInfo == "file://");
assert(URIAddress("/abc").connectionInfo == "file://");
assert(URIAddress("/abc/def").connectionInfo == "file://");
assert(URIAddress("c:\\test.txt").connectionInfo == "file://");
assert(URIAddress("smb://test").connectionInfo == "smb://test");
assert(URIAddress("smb://test/what").connectionInfo == "smb://test");
assert(URIAddress("smb://user@test").connectionInfo == "smb://user@test");
assert(URIAddress("smb://user:pass@test").connectionInfo == "smb://user:pass@test");
assert(URIAddress("smb://user:pass@test/dir1/dir2").connectionInfo == "smb://user:pass@test");
}
/**
* The protocol prefix
*
* Otherwise known as being "$(LT)protName:[//]$(GT)"
* Forwards call to uriScheme
*
* Returns:
* Returns the protocol prefix without "://" if provided
*/
string scheme()
{
return uriScheme(value);
}
/**
* The raw string representation of the path segments
*
* Returns:
* The path segments in raw string form or null
*/
string rawPathSegments()
{
return uriEntries(value);
}
/**
* The query string
*
* Will be in the format of <var>=<value>&...
* It will be missing the preceding question mark.
*
* Returns:
* The query string in raw string form or null
*/
string rawQueryString() {
return uriQuery(value);
}
/**
* The drive letter assigned as part of the directory entries
*
* Returns:
* The drive letter. Null otherwise
*/
Nullable!char driveLetter()
{
string addr = rawPathSegments;
if (addr.length > 2 && addr[1] == ':' && addr[2] == '\\')
return Nullable!char(addr[0]);
else
{
Nullable!char ret;
ret.nullify();
return ret;
}
}
///
unittest
{
assert(URIAddress("C:\\test").driveLetter == 'C');
assert(URIAddress("/etc/dir").driveLetter.isNull);
assert(URIAddress("smb://server/etc/dir").driveLetter.isNull);
assert(URIAddress("smb://server/C:\\test").driveLetter == 'C');
}
/**
* The username as part of connection string
*
* Returns:
* The username specified in the connection string, without password or null if not provided
*/
string username()
{
import std.string : indexOf;
string prefix = uriAuthentication(value);
ptrdiff_t i;
if ((i = prefix.indexOf(":")) > 0)
{
return prefix[0 .. i];
}
else if (i == 0)
{
return null;
}
else
{
return prefix;
}
}
///
unittest
{
assert(URIAddress("smb://:mypass@server").username is null);
assert(URIAddress("smb://test@server").username == "test");
assert(URIAddress("smb://test:mypass@server").username == "test");
assert(URIAddress("smb://test:@server").username == "test");
}
/**
* The password as part of the connection string.
*
* Returns:
* The password specified in the connection string or null if not provided.
*/
string password()
{
import std.string : indexOf;
string prefix = uriAuthentication(value);
ptrdiff_t i;
if ((i = prefix.indexOf(":")) >= 0)
if (prefix.length - (i + 1) != 0)
return prefix[i + 1 .. $];
return null;
}
///
unittest
{
assert(URIAddress("smb://:mypass@server").password == "mypass");
assert(URIAddress("smb://test@server").password is null);
assert(URIAddress("smb://test:mypass@server").password == "mypass");
assert(URIAddress("smb://test:@server").password is null);
}
/**
* The hostname/IP provided as part of the connection string.
*
* Returns:
* The hostname/IP specified in the connection string or null if not provided.
*/
string hostname()
{
import std.string : indexOf;
import std.conv : to;
string prefix = uriConnection(value);
ptrdiff_t i;
if ((i = prefix.indexOf(":")) > 0)
return prefix[0 .. i];
else if (i == -1)
return prefix;
else
return null;
}
///
unittest
{
assert(URIAddress("smb://server:89").hostname == "server");
assert(URIAddress("smb://server:").hostname == "server");
assert(URIAddress("smb://server").hostname == "server");
assert(URIAddress("smb://:89").hostname is null);
}
/**
* The port provided as part of the connection string.
*
* Returns:
* The port specified in the connection string or null if not provided.
*/
Nullable!ushort port()
{
import std.string : indexOf;
import std.conv : to;
string prefix = uriConnection(value);
ptrdiff_t i;
if ((i = prefix.indexOf(":")) > 0 && prefix.length - i > 1)
return Nullable!ushort(to!ushort(prefix[i + 1 .. $]));
else
{
Nullable!ushort ret;
ret.nullify();
return ret;
}
}
///
unittest
{
assert(URIAddress("smb://server:89").port == 89);
assert(URIAddress("smb://server:").port.isNull);
assert(URIAddress("smb://server").port.isNull);
}
/**
* The seperate pathSegments of the directory entries.
*
* Returns:
* An array containing each of the different entries.
* Without path seperators. Except after drive letters.
*/
immutable(string[]) pathSegments()
{
size_t numDir;
string addr = rawPathSegments;
bool haveDrive;
string driveText;
if (addr.length > 2 && addr[1] == ':' && addr[2] == '\\')
{
haveDrive = true;
driveText = addr[0 .. 3];
addr = addr[3 .. $];
numDir++;
}
if (addr.length > 0)
{
foreach (i, c; addr)
{
if (i > 0 && i < addr.length - 1 && c == '/')
{
numDir++;
}
}
if (addr[$ - 1] != '/')
numDir++;
}
if (numDir == 0)
return null;
if (arrbuffDirectories.length < numDir)
alloc.expandArray(arrbuffDirectories, numDir - arrbuffDirectories.length);
string t;
size_t numDirI;
size_t lastI;
if (addr.length > 0 && addr[0] == '/')
lastI = 1;
if (haveDrive)
{
numDirI = 1;
arrbuffDirectories[0] = cast(char[]) driveText;
}
foreach (i, c; addr)
{
if (c == '/' && i > 0)
{
arrbuffDirectories[numDirI] = cast(char[]) t;
numDirI++;
t = null;
lastI = i + 1;
}
else
{
t = addr[lastI .. i + 1];
}
}
if (t !is null)
arrbuffDirectories[numDirI] = cast(char[]) t;
return cast(immutable(string[])) arrbuffDirectories[0 .. numDir];
}
///
unittest
{
URIAddress addr = URIAddress("/dir1/file.txt");
assert(addr.pathSegments.length == 2);
assert(addr.pathSegments[0] == "dir1");
assert(addr.pathSegments[1] == "file.txt");
addr.value = "/dir1/dir2/file.txt";
addr.sanitise();
assert(addr.pathSegments.length == 3);
assert(addr.pathSegments[0] == "dir1");
assert(addr.pathSegments[1] == "dir2");
assert(addr.pathSegments[2] == "file.txt");
addr.value = "/dir1/file.txt";
addr.sanitise();
assert(addr.pathSegments.length == 2);
assert(addr.pathSegments[0] == "dir1");
assert(addr.pathSegments[1] == "file.txt");
assert(addr.arrbuffDirectories.length == 3);
addr = URIAddress("smb://server/dir1/file.txt");
immutable(string[]) pathSegments = addr.pathSegments;
assert(pathSegments.length == 2);
assert(pathSegments[0] == "dir1");
assert(pathSegments[1] == "file.txt");
}
/**
* Produces pathSegments conjoined together in a stair case format.
* So that a 3 directory structure results in 3 different values with 1 to 3 being included.
*
* Params:
* reverse = Should the result be reversed? Only effects the order in the output array, not the indevidual values.
*
* Returns:
* The stair cased version of the URI directory entries.
*/
immutable(string[]) pathSegmentsStairCase(bool reverse = false)
{
import std.string : indexOf;
size_t startI;
string addr = rawPathSegments;
string[] ret = cast(string[])pathSegments();
foreach (i, part; ret)
{
startI += addr[startI .. $].indexOf(part) + part.length;
ret[i] = addr[0 .. startI];
}
if (reverse)
{
foreach (i1; 0 .. ret.length / 2)
{
size_t i2 = ret.length - (i1 + 1);
if (i2 != i1)
{
string v1 = ret[i1];
string v2 = ret[i2];
ret[i2] = v1;
ret[i1] = v2;
}
}
}
return cast(immutable) ret;
}
///
unittest
{
URIAddress addr = URIAddress("/dir1/dir2/file.ext");
immutable(string[]) pathSegments = addr.pathSegmentsStairCase;
assert(pathSegments.length == 3);
assert(pathSegments[0] == "/dir1");
assert(pathSegments[1] == "/dir1/dir2");
assert(pathSegments[2] == "/dir1/dir2/file.ext");
addr = URIAddress("/dir1/dir2/file.ext");
immutable(string[]) pathSegments2 = addr.pathSegmentsStairCase(true);
assert(pathSegments2.length == 3);
assert(pathSegments2[0] == "/dir1/dir2/file.ext");
assert(pathSegments2[1] == "/dir1/dir2");
assert(pathSegments2[2] == "/dir1");
}
/**
* The anchor fragment of the URI
*
* Returns:
* The anchor fragment of the URI or null
*/
string anchor() {
return uriAnchor(value);
}
}
/**
* Sanitises the input so it meets a predefined formula.
*
* Modifies the given input so that all URI strings contain a protocol and are using forward slashes except after drive letters.
* Defaulting protocol is "file".
* It will allocate the final output and use buffers for manipulating where possible.
*/
void sanitise()
{
import std.uni : toUpper;
char[] temp = cast(char[]) value;
if (temp.length == 0 || (temp.length == 1 && (temp[0] == '/' || temp[0] == '\\')))
{
// no value explicit so set value
alloc.expandArray(temp, 8 - temp.length);
temp[0 .. 8] = "file:///"[];
value = cast(string) temp;
return;
}
// make sure first directory entry starts with either a drive, root(/), . if not a protocol/drive or ~
if (uriConnection(value) is null)
{
if (temp.length > 0 && temp[0] == '~')
{
if (temp.length > 1 && !(temp[1] == '/' || temp[1] == '\\'))
{
char[] temp2 = alloc.makeArray!char(temp.length + 1);
temp2[0] = temp[0];
temp2[1] = '/';
temp2[2 .. $] = temp[1 .. $];
alloc.dispose(temp);
temp = temp2;
value = cast(immutable) temp;
}
}
}
if (uriScheme(value) is null)
{
// add file://
value = cast(string) temp;
if (value.length > 0 && (value[0] == '/' && value[0] == '\\'))
{
temp = alloc.makeArray!char(temp.length + 6);
temp[7 .. $] = value[1 .. $];
}
else
{
temp = alloc.makeArray!char(temp.length + 7);
temp[7 .. $] = value[0 .. $];
}
temp[0 .. 7] = "file://"[];
alloc.dispose(cast(char[]) value);
}
else
{
// ensure forward slashes after protocol
size_t offset = uriScheme(cast(string) temp).length;
temp[offset + 1] = '/';
temp[offset + 2] = '/';
}
value = cast(string) temp;
// make directory seperator only /
bool preColon;
foreach (ref c; cast(char[]) uriEntries(value))
{
if (c == ':')
preColon = true;
else if (c == '\\' && !preColon)
c = '/';
else
preColon = false;
}
size_t i = uriScheme(value).length;
// remove last / if added
if (temp[$ - 1] == '/' && temp.length > 1 && temp.length > i + 3)
alloc.shrinkArray(temp, 1);
// work around, should not be needed
value = cast(immutable) temp;
size_t dashes;
if (i == 1 && temp.length > 4 && temp[4] == '/')
{
char[] temp2 = charBuffer[0 .. temp.length - 5];
temp2[] = temp[5 .. $];
temp[4 .. temp2.length + 4] = temp2[];
temp = temp[0 .. temp2.length + 4];
}
if (i > 0)
i += 3; // <prot>://
bool alreadyHitColon;
for (;;)
{
if (i >= temp.length)
break;
if (temp[i] == '/')
{
i++;
dashes++;
}
else if (dashes > 1)
{
char[] temp2 = charBuffer[0 .. temp.length - (dashes - 1)];
temp2[0 .. i - dashes] = temp[0 .. i - dashes];
temp2[i - dashes .. $] = temp[i - (dashes - 1) .. $];
temp[0 .. temp2.length] = temp2[];
temp = temp[0 .. temp2.length];
i -= (dashes - 1);
//i++;
dashes = 0;
}
else
{
if (temp[i] == ':' && !alreadyHitColon)
{
alreadyHitColon = true;
if (i > 0 && i + 1 < temp.length)
{
if (temp[i + 1] == '/' || temp[i + 1] == '\\')
{
temp[i + 1] = '\\';
temp[i - 1] = cast(char) toUpper(temp[i - 1]);
if (temp[0 .. i - 1] == "file:///")
{
char[] temp2 = charBuffer[0 .. temp.length - 1];
temp2[0 .. 7] = "file://";
temp2[7 .. $] = temp[i - 1 .. $];
temp[0 .. temp2.length] = temp2[];
temp = temp[0 .. temp2.length];
i--;
}
if (i + 3 < temp.length && temp[i + 2] == '/')
{
char[] temp2 = charBuffer[0 .. temp.length - 1];
temp2[0 .. i + 2] = temp[0 .. i + 2];
temp2[i + 2 .. $] = temp[i + 3 .. $];
temp[0 .. temp2.length] = temp2[];
temp = temp[0 .. temp2.length];
i--;
}
}
}
}
dashes = 0;
i++;
}
}
// work around, should not be needed
value = cast(immutable) temp;
}
///
unittest
{
assert(URIAddress("./file.ext") == "file://./file.ext");
assert(URIAddress(".file.ext") == "file://.file.ext");
assert(URIAddress("~/file.ext") == "file://~/file.ext");
assert(URIAddress("~file.ext") == "file://~/file.ext");
assert(URIAddress("/dir1//dir2/file.ext") == "file:///dir1/dir2/file.ext");
assert(URIAddress("C:\\dir1//dir2/file.ext") == "file://C:\\dir1/dir2/file.ext");
}
/**
* Expands out an address so that parent and current working directories are no longer part of it.
* Also replaces the home directory and current working directory at the start as needed.
*
* Params:
* homeDirectory = The home directory of the "running" user.
* cwd = The current working directory of the "running" application.
*
* Returns:
* The expanded form of the current address.
*/
URIAddress expand(URIAddress homeDirectory, URIAddress cwd)
{
import std.string : indexOf;
string prot = scheme;
string temp = value;
char[] retbuf;
size_t offsetDirs;
// grabs $(LT)protName://$(GT)
if (prot.length > 1)
{
ptrdiff_t offseti;
retbuf = charBuffer[0 .. prot.length + 3];
retbuf[0 .. prot.length] = prot[];
retbuf[prot.length .. $] = "://";
temp = temp[prot.length + 3 .. $];
if (prot != "file")
offseti = temp.indexOf("/");
// assuming sanitised correctly then this will /always/ exist
assert(offseti >= 0);
// grabs [user[:pass]]@[host/ip[:port]]
retbuf = charBuffer[0 .. retbuf.length + offseti];
retbuf[$ - offseti .. $] = temp[0 .. offseti];
temp = temp[offseti .. $];
offsetDirs = retbuf.length;
}
if (temp[0] == '/')
temp = temp[1 .. $];
if (temp.length > 1 && temp[1] != '/') {}
else if (temp[0] == '.' || temp[0] == '~')
{
string replaceTo;
// perform expansion
if (temp[0] == '.')
{
string protPref = cwd.scheme;
if (protPref !is null && protPref != "file")
throw new Exception("Protocol as directory cannot be used in expansion");
replaceTo = cwd.rawPathSegments;
}
else if (temp[0] == '~')
{
string protPref = homeDirectory.scheme;
if (protPref !is null && protPref != "file")
throw new Exception("Protocol as directory cannot be used in expansion");
replaceTo = homeDirectory.rawPathSegments;
}
if ((replaceTo.length > 0 && replaceTo[0] == '/')
|| (retbuf == "file://" || retbuf.length == 0))
{
retbuf = charBuffer[0 .. retbuf.length + replaceTo.length];
}
else
{
retbuf = charBuffer[0 .. retbuf.length + replaceTo.length + 1];
retbuf[$ - (replaceTo.length + 1)] = '/';
}
retbuf[$ - replaceTo.length .. $] = replaceTo[];
if (replaceTo[$-1] != '/' && replaceTo[$-1] != '\\') {
retbuf = charBuffer[0 .. retbuf.length + 1];
retbuf[$ - 1] = '/';
}
temp = temp[1 .. $];
}
bool haveDrive;
if (retbuf.length > 9 && retbuf[8] == ':' && retbuf[9] == '\\')
{
haveDrive = true;
if (temp.length > 0 && temp[0] == '/')
{
retbuf = charBuffer[0 .. retbuf.length + (temp.length - 1)];
retbuf[$ - (temp.length - 1) .. $] = temp[1 .. $];
offsetDirs += 3;
}
else
{
retbuf = charBuffer[0 .. retbuf.length + temp.length];
retbuf[$ - temp.length .. $] = temp[];
}
}
else if (temp.length > 0 && temp[0] == '/')
{
retbuf = charBuffer[0 .. retbuf.length + temp.length];
retbuf[$ - temp.length .. $] = temp[];
}
else
{
retbuf = charBuffer[0 .. retbuf.length + (temp.length + 1)];
retbuf[$ - (temp.length + 1)] = '/';
retbuf[$ - temp.length .. $] = temp[];
}
// expansion of parent and current directory is simple.
// current directory gets removed out right.
// parent directory removes it and its parent node if it should exist.
// C://dir1/dir2/../../dir3 C://dir3
URIAddress* addrt = alloc.make!URIAddress(cast(string) retbuf, alloc);
retbuf = charBuffer[0 .. addrt.value.length];
retbuf[] = addrt.value[];
size_t skipDirs;
auto pathSegments = addrt.pathSegments;
size_t[] ibuffers;
if (haveDrive && pathSegments !is null)
{
(cast() pathSegments) = pathSegments[1 .. $];
}
foreach_reverse (i, p; pathSegments)
{
if (p == ".")
continue;
else if (p == "..")
{
skipDirs++;
continue;
}
else if (skipDirs > 0)
{
skipDirs--;
continue;
}
ibuffers = charIndexBuffer[0 .. ibuffers.length + 1];
ibuffers[$ - 1] = i;
}
retbuf = retbuf[0 .. offsetDirs];
// prot://user:pass@server:port/
foreach_reverse (i; ibuffers)
{
size_t len = pathSegments[i].length;
retbuf = charBuffer[0 .. retbuf.length + len + 1];
retbuf[$ - (len + 1)] = '/';
retbuf[$ - len .. $] = pathSegments[i];
}
alloc.dispose(addrt);
try
{
return URIAddress(cast(string) retbuf, alloc);
}
catch (Error e)
{
assert(0, retbuf);
}
}
///
unittest
{
assert(URIAddress("./test/file.ext").expand(URIAddress("/"),
URIAddress("C:\\")) == "file://C:\\test/file.ext");
assert(URIAddress("./test/file.ext").expand(URIAddress("/"),
URIAddress("/etc")) == "file:///etc/test/file.ext");
assert(URIAddress("~/test/file.ext").expand(URIAddress("C:\\"),
URIAddress("/")) == "file://C:\\test/file.ext");
assert(URIAddress("~/test/file.ext").expand(URIAddress("/etc"),
URIAddress("/")) == "file:///etc/test/file.ext");
assert(URIAddress("./test/file.ext").expand(URIAddress("C:\\"),
URIAddress("/")) == "file:///test/file.ext");
assert(URIAddress("./test/file.ext").expand(URIAddress("/etc"),
URIAddress("/")) == "file:///test/file.ext");
assert(URIAddress("~/test/file.ext").expand(URIAddress("/"),
URIAddress("C:\\")) == "file:///test/file.ext");
assert(URIAddress("~/test/file.ext").expand(URIAddress("/"),
URIAddress("/etc")) == "file:///test/file.ext");
assert(URIAddress("./dir1/dir2/../../dir3").expand(URIAddress("/"),
URIAddress("C:\\")) == "file://C:\\dir3");
assert(URIAddress("./dir1/dir2/../../dir3").expand(URIAddress("C:\\"),
URIAddress("/")) == "file:///dir3");
assert(URIAddress("~/dir1/dir2/../../dir3").expand(URIAddress("C:\\"),
URIAddress("/")) == "file://C:\\dir3");
assert(URIAddress("~/dir1/dir2/../../dir3").expand(URIAddress("/"),
URIAddress("C:\\")) == "file:///dir3");
assert(URIAddress("smb://server/./test").expand(URIAddress("/"),
URIAddress("/")) == "smb://server/test");
assert(URIAddress("smb://server/test").expand(URIAddress("/"),
URIAddress("/")) == "smb://server/test");
assert(URIAddress("smb://server/~/test").expand(URIAddress("/etc"),
URIAddress("/")) == "smb://server/etc/test");
assert(URIAddress("smb://server/./test").expand(URIAddress("/"),
URIAddress("/etc")) == "smb://server/etc/test");
assert(URIAddress("smb://server/dir1/dir2/../../dir3").expand(URIAddress("/"),
URIAddress("/")) == "smb://server/dir3");
assert(URIAddress("smb://server/~").expand(URIAddress("C:\\"),
URIAddress("/")) == "smb://server/C:\\");
assert(URIAddress("smb://server/.").expand(URIAddress("/"),
URIAddress("C:\\")) == "smb://server/C:\\");
assert(URIAddress("file://~foo/bar").expand(URIAddress("file:///home/dhasenan"),
URIAddress("file:///tmp")) == "file:///~foo/bar");
assert(URIAddress("file://./mytestfile.txt").expand(URIAddress("file://E:\\cygwin/home/rikki"),
URIAddress("file://P:\\alphaPhobos")) == "file://P:\\alphaPhobos/mytestfile.txt");
}
/**
* The parent directory entry of the current URI address.
*
* Returns:
* The parent directories URI address.
*/
URIAddress parent()
{
import std.string : indexOf;
string current = rawPathSegments;
size_t start;
foreach_reverse (i, c; current)
{
if (c == '/')
{
start = i;
break;
}
}
return URIAddress(value[0 .. value.indexOf(current) + start], alloc);
}
///
unittest
{
assert(URIAddress(".").parent == "file://");
assert(URIAddress("./dir1/dir2").parent == "file://./dir1");
assert(URIAddress("~/dir1/dir2").parent == "file://~/dir1");