vibe.textfilter.urlencode 6/31(19%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
542300
55550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
71550
72550
730
74550
750
760
77600
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
/** URL-encoding implementation Copyright: © 2012-2015 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Jan Krüger, Sönke Ludwig */ module vibe.textfilter.urlencode; import vibe.utils.string; import std.algorithm; import std.array; import std.conv; import std.exception; import std.format; /** * Returns: * the URL encoded version of a given string, in a newly-allocated string. */ T[] urlEncode(T)(T[] str, const(char)[] allowed_chars = null) if (is(T[] : const(char)[])) { auto dst = StringSliceAppender!(T[])(str); filterURLEncode(dst, str, allowed_chars); return dst.data; } @safe unittest { string s = "hello-world"; assert(s.urlEncode().ptr == s.ptr); } private auto isCorrectHexNum(const(char)[] str) @safe { foreach (char c; str) { switch(c) { case '0': .. case '9': case 'A': .. case 'F': case 'a': .. case 'f': break; default: return false; } } return true; } /** Checks whether a given string has valid URL encoding. */ bool isURLEncoded(const(char)[] str, const(char)[] reserved_chars = null) @safe { for (size_t i = 0; i < str.length; i++) { switch (str[i]) { case '-': case '.': case '0': .. case '9': case 'A': .. case 'Z': case '_': case 'a': .. case 'z': case '~': break; case '%': if (i + 2 >= str.length) return false; if (!isCorrectHexNum(str[i+1 .. i+3])) return false; i += 2; break; default: if (reserved_chars.canFind(str[i])) return false; break; } } return true; } @safe unittest { assert(isURLEncoded("hello-world")); assert(isURLEncoded("he%2F%af")); assert(!isURLEncoded("hello world", " ")); assert(!isURLEncoded("he%f")); assert(!isURLEncoded("he%fx")); } /** Returns the decoded version of a given URL encoded string. */ T[] urlDecode(T)(T[] str) if (is(T[] : const(char)[])) { if (!str.anyOf("%")) return str; auto dst = StringSliceAppender!(T[])(str); filterURLDecode(dst, str); return dst.data; } /** Returns the form encoded version of a given string. Form encoding is the same as normal URL encoding, except that spaces are replaced by plus characters. Note that newlines should always be represented as \r\n sequences according to the HTTP standard. */ T[] formEncode(T)(T[] str, const(char)[] allowed_chars = null) if (is(T[] : const(char)[])) { auto dst = StringSliceAppender!(T[])(str); filterURLEncode(dst, str, allowed_chars, true); return dst.data; } /** Returns the decoded version of a form encoded string. Form encoding is the same as normal URL encoding, except that spaces are replaced by plus characters. */ T[] formDecode(T)(T[] str) if (is(T[] : const(char)[])) { if (!str.anyOf("%+")) return str; auto dst = StringSliceAppender!(T[])(str); filterURLDecode(dst, str, true); return dst.data; } /** Writes the URL encoded version of the given string to an output range. */ void filterURLEncode(R)(ref R dst, const(char)[] str, const(char)[] allowed_chars = null, bool form_encoding = false) { while (str.length > 0) { switch (str[0]) { default: if (allowed_chars.canFind(str[0])) dst.put(str[0]); else { static if (is(typeof({ R a, b; b = a; }))) formattedWrite(dst, "%%%02X", str[0]); else formattedWrite(() @trusted { return &dst; } (), "%%%02X", str[0]); } break; case ' ': if (form_encoding) { dst.put('+'); break; } goto default; case 'A': .. case 'Z': case 'a': .. case 'z': case '0': .. case '9': case '-': case '_': case '.': case '~': dst.put(str[0]); break; } str = str[1 .. $]; } } /** Writes the decoded version of the given URL encoded string to an output range. */ void filterURLDecode(R)(ref R dst, const(char)[] str, bool form_encoding = false) { while( str.length > 0 ) { switch(str[0]) { case '%': enforce(str.length >= 3, "invalid percent encoding"); auto hex = str[1..3]; auto c = cast(char)parse!int(hex, 16); enforce(hex.length == 0, "invalid percent encoding"); dst.put(c); str = str[3 .. $]; break; case '+': if (form_encoding) { dst.put(' '); str = str[1 .. $]; break; } goto default; default: dst.put(str[0]); str = str[1 .. $]; break; } } } @safe unittest { assert(urlEncode("\r\n") == "%0D%0A"); // github #65 assert(urlEncode("This-is~a_test") == "This-is~a_test"); assert(urlEncode("This is a test") == "This%20is%20a%20test"); assert(urlEncode("This{is}test") == "This%7Bis%7Dtest"); assert(formEncode("This is a test") == "This+is+a+test"); assert(formEncode("this/test", "/") == "this/test"); assert(formEncode("this/test") == "this%2Ftest"); assert(urlEncode("%") == "%25"); assert(urlEncode("!") == "%21"); assert(urlDecode("%0D%0a") == "\r\n"); assert(urlDecode("%c2%aE") == "®"); assert(urlDecode("This+is%20a+test") == "This+is a+test"); assert(formDecode("This+is%20a+test") == "This is a test"); string a = "This~is a-test!\r\nHello, Wörld.. "; string aenc = urlEncode(a); assert(aenc == "This~is%20a-test%21%0D%0AHello%2C%20W%C3%B6rld..%20"); assert(urlDecode(urlEncode(a)) == a); } private struct StringSliceAppender(S) { private { Appender!S m_appender; S m_source; size_t m_prefixLength; } this(S source) { m_source = source; if (m_source.length == 0) m_appender = appender!S(); } @disable this(this); void put(char ch) { if (m_source.length) { if (m_prefixLength < m_source.length && m_source[m_prefixLength] == ch) { m_prefixLength++; return; } m_appender = appender!S(); m_appender.put(m_source[0 .. m_prefixLength]); m_appender.put(ch); m_source = S.init; } else m_appender.put(ch); } void put(S s) { if (m_source.length) { foreach (char ch; s) put(ch); } else m_appender.put(s); } void put(dchar ch) { import std.encoding : encode; char[6] chars; auto n = encode(ch, chars[]); foreach (char c; chars[0 .. n]) put(c); } @property S data() { return m_source.length ? m_source[0 .. m_prefixLength] : m_appender.data; } } @safe unittest { string s = "foo"; auto a = StringSliceAppender!string(s); a.put("f"); assert(a.data == "f"); assert(a.data.ptr is s.ptr); a.put('o'); assert(a.data == "fo"); assert(a.data.ptr is s.ptr); a.put('o'); assert(a.data == "foo"); assert(a.data.ptr is s.ptr); a.put('ä'); assert(a.data == "fooä"); a = StringSliceAppender!string(s); a.put('f'); assert(a.data == "f"); assert(a.data.ptr is s.ptr); a.put("oobar"); assert(a.data == "foobar"); a = StringSliceAppender!string(s); a.put(cast(dchar)'f'); assert(a.data == "f"); assert(a.data.ptr is s.ptr); a.put('b'); assert(a.data == "fb"); a = StringSliceAppender!string(s); a.put('f'); assert(a.data == "f"); assert(a.data.ptr is s.ptr); a.put("b"); assert(a.data == "fb"); a = StringSliceAppender!string(s); a.put('f'); assert(a.data == "f"); assert(a.data.ptr is s.ptr); a.put("ä"); assert(a.data == "fä"); a = StringSliceAppender!string(s); a.put("bar"); assert(a.data == "bar"); a = StringSliceAppender!string(s); a.put('b'); assert(a.data == "b"); a = StringSliceAppender!string(s); a.put('ä'); assert(a.data == "ä"); a = StringSliceAppender!string(s); a.put("foo"); assert(a.data == "foo"); assert(a.data.ptr is s.ptr); a.put("bar"); assert(a.data == "foobar"); a = StringSliceAppender!string(s); a.put("foo"); assert(a.data == "foo"); assert(a.data.ptr is s.ptr); a.put('b'); assert(a.data == "foob"); }