vibe.stream.wrapper 23/114(20%) 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
2825
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
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
9025
910
9225
9325
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
11626
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
30514
3060
30714
3080
3090
3100
3110
31214
3130
3140
3150
3160
31714
31814
31914
3200
3210
3220
3230
3240
3250
3260
3270
3280
32913
33013
3310
3320
3330
3340
3350
336229
3370
3380
3390
3400
3410
342445
343216
344216
345216
346216
347216
3480
3490
3500
35113
352229
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
36814
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
/** Stream proxy and wrapper facilities. Copyright: © 2013-2016 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.stream.wrapper; public import vibe.core.stream; import std.algorithm : min; import std.exception; import core.time; import vibe.internal.interfaceproxy; import vibe.internal.freelistref : FreeListRef; ProxyStream createProxyStream(Stream)(Stream stream) if (isStream!Stream) { return new ProxyStream(interfaceProxy!(.Stream)(stream), true); } ProxyStream createProxyStream(InputStream, OutputStream)(InputStream input, OutputStream output) if (isInputStream!InputStream && isOutputStream!OutputStream) { return new ProxyStream(interfaceProxy!(.InputStream)(input), interfaceProxy!(.OutputStream)(output), true); } ConnectionProxyStream createConnectionProxyStream(Stream, ConnectionStream)(Stream stream, ConnectionStream connection_stream) if (isStream!Stream && isConnectionStream!ConnectionStream) { mixin validateStream!Stream; mixin validateConnectionStream!ConnectionStream; return new ConnectionProxyStream(interfaceProxy!(.Stream)(stream), interfaceProxy!(.ConnectionStream)(connection_stream), true); } /// private FreeListRef!ConnectionProxyStream createConnectionProxyStreamFL(Stream, ConnectionStream)(Stream stream, ConnectionStream connection_stream) if (isStream!Stream && isConnectionStream!ConnectionStream) { mixin validateStream!Stream; mixin validateConnectionStream!ConnectionStream; return FreeListRef!ConnectionProxyStream(interfaceProxy!(.Stream)(stream), interfaceProxy!(.ConnectionStream)(connection_stream), true); } ConnectionProxyStream createConnectionProxyStream(InputStream, OutputStream, ConnectionStream)(InputStream input, OutputStream output, ConnectionStream connection_stream) if (isInputStream!InputStream && isOutputStream!OutputStream && isConnectionStream!ConnectionStream) { return new ConnectionProxyStream(interfaceProxy!(.InputStream)(input), interfaceProxy!(.OutputStream)(output), interfaceProxy!(.ConnectionStream)(connection_stream), true); } /** Provides a way to access varying streams using a constant stream reference. */ class ProxyStream : Stream { @safe: private { InterfaceProxy!(.InputStream) m_input; InterfaceProxy!(.OutputStream) m_output; InterfaceProxy!(.Stream) m_underlying; } deprecated("Use createProxyStream instead.") this(Stream stream = null) { m_underlying = interfaceProxy!Stream(stream); m_input = interfaceProxy!InputStream(stream); m_output = interfaceProxy!OutputStream(stream); } deprecated("Use createProxyStream instead.") this(InputStream input, OutputStream output) { m_input = interfaceProxy!InputStream(input); m_output = interfaceProxy!OutputStream(output); } /// private this(InterfaceProxy!Stream stream, bool dummy) { m_underlying = stream; m_input = stream; m_output = stream; } /// private this(InterfaceProxy!InputStream input, InterfaceProxy!OutputStream output, bool dummy) { m_input = input; m_output = output; } /// The stream that is wrapped by this one @property inout(InterfaceProxy!Stream) underlying() inout { return m_underlying; } /// ditto @property void underlying(InterfaceProxy!Stream value) { m_underlying = value; m_input = value; m_output = value; } /// ditto static if (!is(Stream == InterfaceProxy!Stream)) @property void underlying(Stream value) { this.underlying = interfaceProxy!Stream(value); } @property bool empty() { return m_input ? m_input.empty : true; } @property ulong leastSize() { return m_input ? m_input.leastSize : 0; } @property bool dataAvailableForRead() { return m_input ? m_input.dataAvailableForRead : false; } const(ubyte)[] peek() { return m_input.peek(); } size_t read(scope ubyte[] dst, IOMode mode) { return m_input.read(dst, mode); } alias read = Stream.read; size_t write(in ubyte[] bytes, IOMode mode) { return m_output.write(bytes, mode); } alias write = Stream.write; void flush() { m_output.flush(); } void finalize() { m_output.finalize(); } } /** Special kind of proxy stream for streams nested in a ConnectionStream. This stream will forward all stream operations to the selected stream, but will forward all connection related operations to the given ConnectionStream. This allows wrapping embedded streams, such as SSL streams in a ConnectionStream. */ class ConnectionProxyStream : ConnectionStream { @safe: private { InterfaceProxy!ConnectionStream m_connection; InterfaceProxy!Stream m_underlying; InterfaceProxy!InputStream m_input; InterfaceProxy!OutputStream m_output; } deprecated("Use createConnectionProxyStream instead.") this(Stream stream, ConnectionStream connection_stream) { this(interfaceProxy!Stream(stream), interfaceProxy!ConnectionStream(connection_stream), true); } deprecated("Use createConnectionProxyStream instead.") this(InputStream input, OutputStream output, ConnectionStream connection_stream) { this(interfaceProxy!InputStream(input), interfaceProxy!OutputStream(output), interfaceProxy!ConnectionStream(connection_stream), true); } /// private this(InterfaceProxy!Stream stream, InterfaceProxy!ConnectionStream connection_stream, bool dummy) { assert(!!stream); m_underlying = stream; m_input = stream; m_output = stream; m_connection = connection_stream; } /// private this(InterfaceProxy!InputStream input, InterfaceProxy!OutputStream output, InterfaceProxy!ConnectionStream connection_stream, bool dummy) { m_input = input; m_output = output; m_connection = connection_stream; } @property bool connected() const { if (!m_connection) return true; return m_connection.connected; } void close() { if (!m_connection) return; if (m_connection.connected) finalize(); m_connection.close(); } bool waitForData(Duration timeout = 0.seconds) { if (this.dataAvailableForRead) return true; if (!m_connection) return timeout == 0.seconds ? !this.empty : false; return m_connection.waitForData(timeout); } /// The stream that is wrapped by this one @property inout(InterfaceProxy!Stream) underlying() inout { return m_underlying; } /// ditto @property void underlying(InterfaceProxy!Stream value) { m_underlying = value; m_input = value; m_output = value; } /// ditto static if (!is(Stream == InterfaceProxy!Stream)) @property void underlying(Stream value) { this.underlying = InterfaceProxy!Stream(value); } @property bool empty() { return m_input ? m_input.empty : true; } @property ulong leastSize() { return m_input ? m_input.leastSize : 0; } @property bool dataAvailableForRead() { return m_input ? m_input.dataAvailableForRead : false; } const(ubyte)[] peek() { return m_input.peek(); } size_t read(scope ubyte[] dst, IOMode mode) { return m_input.read(dst, mode); } alias read = ConnectionStream.read; size_t write(in ubyte[] bytes, IOMode mode) { return m_output.write(bytes, mode); } alias write = ConnectionStream.write; void flush() { m_output.flush(); } void finalize() { m_output.finalize(); } } /** Implements an input range interface on top of an InputStream using an internal buffer. The buffer is GC allocated and is filled chunk wise. Thus an InputStream that has been wrapped in a StreamInputRange cannot be used reliably on its own anymore. Reading occurs in a fully lazy fashion. The first call to either front, popFront or empty will potentially trigger waiting for the next chunk of data to arrive - but especially popFront will not wait if it was called after a call to front. This property allows the range to be used in request-response scenarios. */ struct StreamInputRange { @safe: private { struct Buffer { ubyte[256] data = void; size_t fill = 0; } InputStream m_stream; Buffer* m_buffer; } this (InputStream stream) { m_stream = stream; m_buffer = new Buffer; } @property bool empty() { return !m_buffer.fill && m_stream.empty; } ubyte front() { if (m_buffer.fill < 1) readChunk(); return m_buffer.data[$ - m_buffer.fill]; } void popFront() { assert(!empty); if (m_buffer.fill < 1) readChunk(); m_buffer.fill--; } private void readChunk() { auto sz = min(m_stream.leastSize, m_buffer.data.length); assert(sz > 0); m_stream.read(m_buffer.data[$-sz .. $]); m_buffer.fill = sz; } } /** Implements a buffered output range interface on top of an OutputStream. */ StreamOutputRange!OutputStream StreamOutputRange()(OutputStream stream) { return StreamOutputRange!OutputStream(stream); } /// ditto struct StreamOutputRange(OutputStream, size_t buffer_size = 256) if (isOutputStream!OutputStream) { @safe: private { OutputStream m_stream; size_t m_fill = 0; ubyte[buffer_size] m_data = void; } @disable this(this); this(OutputStream stream) { m_stream = stream; } ~this() { flush(); } void flush() { if (m_fill == 0) return; m_stream.write(m_data[0 .. m_fill]); m_fill = 0; } void drop() { m_fill = 0; } void put(ubyte bt) { m_data[m_fill++] = bt; if (m_fill >= m_data.length) flush(); } void put(const(ubyte)[] bts) { // avoid writing more chunks than necessary if (bts.length + m_fill >= m_data.length * 2) { flush(); m_stream.write(bts); return; } while (bts.length) { auto len = min(m_data.length - m_fill, bts.length); m_data[m_fill .. m_fill + len] = bts[0 .. len]; m_fill += len; bts = bts[len .. $]; if (m_fill >= m_data.length) flush(); } } void put(char elem) { put(cast(ubyte)elem); } void put(const(char)[] elems) { put(cast(const(ubyte)[])elems); } void put(dchar elem) { import std.utf; char[4] chars; auto len = encode(chars, elem); put(chars[0 .. len]); } void put(const(dchar)[] elems) { foreach( ch; elems ) put(ch); } } /// ditto auto streamOutputRange(size_t buffer_size = 256, OutputStream)(OutputStream stream) if (isOutputStream!OutputStream) { return StreamOutputRange!(OutputStream, buffer_size)(stream); } unittest { static long writeLength(ARGS...)(ARGS args) { import vibe.stream.memory; auto dst = createMemoryOutputStream; { auto rng = StreamOutputRange(dst); foreach (a; args) rng.put(a); } return dst.data.length; } assert(writeLength("hello", ' ', "world") == "hello world".length); assert(writeLength("h\u00E4llo", ' ', "world") == "h\u00E4llo world".length); assert(writeLength("hello", '\u00E4', "world") == "hello\u00E4world".length); assert(writeLength("h\u1000llo", '\u1000', "world") == "h\u1000llo\u1000world".length); auto test = "häl"; assert(test.length == 4); assert(writeLength(test[0], test[1], test[2], test[3]) == test.length); }