From a3e2b406359d4382d39d5934a49aa3020a0f7aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 17 Feb 2024 14:33:48 +0100 Subject: [PATCH 1/2] Update code base from latest vibe.d master. --- source/vibe/http/client.d | 28 +++++++++----------- source/vibe/http/common.d | 48 +++++++++++++++++++++++++++++------ source/vibe/http/log.d | 2 +- source/vibe/http/proxy.d | 4 +-- source/vibe/http/router.d | 2 +- source/vibe/http/server.d | 30 ++++++++++------------ source/vibe/http/websockets.d | 1 - 7 files changed, 69 insertions(+), 46 deletions(-) diff --git a/source/vibe/http/client.d b/source/vibe/http/client.d index 5a0062c..3e51f00 100644 --- a/source/vibe/http/client.d +++ b/source/vibe/http/client.d @@ -11,6 +11,9 @@ public import vibe.core.net; public import vibe.http.common; public import vibe.inet.url; +import vibe.container.dictionarylist; +import vibe.container.internal.utilallocator; +import vibe.container.ringbuffer : RingBuffer; import vibe.core.connectionpool; import vibe.core.core; import vibe.core.log; @@ -22,9 +25,6 @@ import vibe.stream.tls; import vibe.stream.operations; import vibe.stream.wrapper : createConnectionProxyStream; import vibe.stream.zlib; -import vibe.utils.array; -import vibe.utils.dictionarylist; -import vibe.internal.allocator; import vibe.internal.freelistref; import vibe.internal.interfaceproxy : InterfaceProxy, interfaceProxy; @@ -207,7 +207,7 @@ auto connectHTTP(string host, ushort port = 0, bool use_tls = false, const(HTTPC ret.connect(host, port, use_tls, sttngs); return ret; }); - if (s_connections.full) s_connections.popFront(); + if (s_connections.full) s_connections.removeFront(); s_connections.put(tuple(ckey, pool)); } @@ -231,7 +231,7 @@ static ~this() } private struct ConnInfo { string host; string tlsPeerName; ushort port; bool useTLS; string proxyIP; ushort proxyPort; NetworkAddress bind_addr; } -private static vibe.utils.array.FixedRingBuffer!(Tuple!(ConnInfo, ConnectionPool!HTTPClient), 16) s_connections; +private static RingBuffer!(Tuple!(ConnInfo, ConnectionPool!HTTPClient), 16) s_connections; /**************************************************************************************************/ @@ -468,11 +468,8 @@ final class HTTPClient { private void doProxyRequest(T, U)(ref T res, U requester, ref bool close_conn, ref bool has_body) @trusted { // scope new import std.conv : to; - import vibe.internal.utilallocator: RegionListAllocator; - version (VibeManualMemoryManagement) - scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance); - else - scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance); + scope request_allocator = createRequestAllocator(); + scope (exit) freeRequestAllocator(request_allocator); res.dropBody(); scope(failure) @@ -542,11 +539,8 @@ final class HTTPClient { */ void request(scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse) responder) @trusted { // scope new - import vibe.internal.utilallocator: RegionListAllocator; - version (VibeManualMemoryManagement) - scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance); - else - scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance); + scope request_allocator = createRequestAllocator(); + scope (exit) freeRequestAllocator(request_allocator); scope (failure) { m_responding = false; @@ -787,6 +781,8 @@ private auto connectTCPWithTimeout(NetworkAddress addr, NetworkAddress bind_addr Represents a HTTP client request (as sent to the server). */ final class HTTPClientRequest : HTTPRequest { + import vibe.internal.array : FixedAppender; + private { InterfaceProxy!OutputStream m_bodyWriter; FreeListRef!ChunkedOutputStream m_chunkedStream; @@ -1028,7 +1024,7 @@ final class HTTPClientResponse : HTTPResponse { m_closeConn = close_conn; } - private void initialize(bool has_body, IAllocator alloc, SysTime connected_time = Clock.currTime(UTC())) + private void initialize(Allocator)(bool has_body, Allocator alloc, SysTime connected_time = Clock.currTime(UTC())) { scope(failure) finalize(true); diff --git a/source/vibe/http/common.d b/source/vibe/http/common.d index e9d368e..3ed191f 100644 --- a/source/vibe/http/common.d +++ b/source/vibe/http/common.d @@ -9,17 +9,16 @@ module vibe.http.common; public import vibe.http.status; +import vibe.container.dictionarylist; +import vibe.container.internal.appender; +import vibe.container.internal.utilallocator; import vibe.core.log; import vibe.core.net; import vibe.inet.message; import vibe.stream.operations; import vibe.textfilter.urlencode : urlEncode, urlDecode; -import vibe.utils.array; -import vibe.utils.dictionarylist; -import vibe.internal.allocator; import vibe.internal.freelistref; import vibe.internal.interfaceproxy : InterfaceProxy, interfaceProxy; -import vibe.utils.string; import std.algorithm; import std.array; @@ -300,7 +299,7 @@ class HTTPResponse { InetHeaderMap headers; /// All cookies that shall be set on the client for this request - @property ref DictionaryList!Cookie cookies() { return m_cookies; } + @property ref DictionaryList!Cookie cookies() return scope { return m_cookies; } } scope: @@ -489,7 +488,7 @@ final class ChunkedOutputStream : OutputStream { } /// private - this(InterfaceProxy!OutputStream stream, IAllocator alloc, bool dummy) + this(Allocator)(InterfaceProxy!OutputStream stream, Allocator alloc, bool dummy) { m_out = stream; m_buffer = AllocAppender!(ubyte[])(alloc); @@ -611,13 +610,23 @@ final class ChunkedOutputStream : OutputStream { } /// Creates a new `ChunkedInputStream` instance. -ChunkedOutputStream createChunkedOutputStream(OS)(OS destination_stream, IAllocator allocator = theAllocator()) if (isOutputStream!OS) +ChunkedOutputStream createChunkedOutputStream(OS)(OS destination_stream) if (isOutputStream!OS) +{ + return createChunkedOutputStream(destination_stream, theAllocator()); +} +/// ditto +ChunkedOutputStream createChunkedOutputStream(OS, Allocator)(OS destination_stream, Allocator allocator) if (isOutputStream!OS) { return new ChunkedOutputStream(interfaceProxy!OutputStream(destination_stream), allocator, true); } /// Creates a new `ChunkedOutputStream` instance. -FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS)(OS destination_stream, IAllocator allocator = theAllocator()) if (isOutputStream!OS) +FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS)(OS destination_stream) if (isOutputStream!OS) +{ + return createChunkedOutputStreamFL(destination_stream, theAllocator()); +} +/// ditto +FreeListRef!ChunkedOutputStream createChunkedOutputStreamFL(OS, Allocator)(OS destination_stream, Allocator allocator) if (isOutputStream!OS) { return FreeListRef!ChunkedOutputStream(interfaceProxy!OutputStream(destination_stream), allocator, true); } @@ -1069,3 +1078,26 @@ unittest { *("foo" in m) = "baz"; assert(m["foo"] == "baz"); } + + +package auto createRequestAllocator() +{ + import vibe.container.internal.utilallocator: RegionListAllocator; + + static if (is(RegionListAllocator!(shared(GCAllocator), true) == struct)) { + version (VibeManualMemoryManagement) + return allocatorObject(RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance)); + else + return allocatorObject(RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance)); + } else { + version (VibeManualMemoryManagement) + return new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance); + else + return new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance); + } +} + +package void freeRequestAllocator(Allocator)(ref Allocator alloc) +{ + destroy(alloc); +} diff --git a/source/vibe/http/log.d b/source/vibe/http/log.d index 5357b2d..6e25f08 100644 --- a/source/vibe/http/log.d +++ b/source/vibe/http/log.d @@ -11,7 +11,7 @@ import vibe.core.file; import vibe.core.log; import vibe.core.sync : InterruptibleTaskMutex, performLocked; import vibe.http.server; -import vibe.utils.array : FixedAppender; +import vibe.container.internal.appender : FixedAppender; import std.array; import std.conv; diff --git a/source/vibe/http/proxy.d b/source/vibe/http/proxy.d index b4b38e6..6178711 100644 --- a/source/vibe/http/proxy.d +++ b/source/vibe/http/proxy.d @@ -128,7 +128,7 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) import std.algorithm : splitter, canFind; - import vibe.utils.string : icmp2; + import vibe.internal.string : icmp2; bool isUpgrade = pConnection && (*pConnection).splitter(',').canFind!(a => a.icmp2("upgrade")); void setupClientRequest(scope HTTPClientRequest creq) @@ -152,8 +152,6 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) void handleClientResponse(scope HTTPClientResponse cres) { - import vibe.utils.string; - // copy the response to the original requester res.statusCode = cres.statusCode; diff --git a/source/vibe/http/router.d b/source/vibe/http/router.d index c9a5f00..31b9219 100644 --- a/source/vibe/http/router.d +++ b/source/vibe/http/router.d @@ -1024,7 +1024,7 @@ private struct MatchGraphBuilder { //logInfo("Disambiguate with %s initial nodes", m_nodes.length); if (!m_nodes.length) return; - import vibe.utils.hashmap; + import vibe.container.hashmap : HashMap; HashMap!(LinkedSetHash, NodeIndex) combined_nodes; Array!bool visited; visited.length = m_nodes.length * 2; diff --git a/source/vibe/http/server.d b/source/vibe/http/server.d index 3a1482a..3416f73 100644 --- a/source/vibe/http/server.d +++ b/source/vibe/http/server.d @@ -11,6 +11,8 @@ public import vibe.core.net; public import vibe.http.common; public import vibe.http.session; +import vibe.container.internal.appender : FixedAppender; +import vibe.container.internal.utilallocator; import vibe.core.file; import vibe.core.log; import vibe.data.json; @@ -26,10 +28,8 @@ import vibe.stream.tls; import vibe.stream.wrapper : ConnectionProxyStream, createConnectionProxyStream, createConnectionProxyStreamFL; import vibe.stream.zlib; import vibe.textfilter.urlencode; -import vibe.utils.array; -import vibe.internal.allocator; import vibe.internal.freelistref; -import vibe.utils.string; +import vibe.internal.string : formatAlloc, icmp2; import core.atomic; import core.vararg; @@ -243,12 +243,8 @@ void handleHTTPConnection(TCPConnection connection, HTTPServerContext context) } () @trusted { - import vibe.internal.utilallocator: RegionListAllocator; - - version (VibeManualMemoryManagement) - scope request_allocator = new RegionListAllocator!(shared(Mallocator), false)(1024, Mallocator.instance); - else - scope request_allocator = new RegionListAllocator!(shared(GCAllocator), true)(1024, GCAllocator.instance); + scope request_allocator = createRequestAllocator(); + scope (exit) freeRequestAllocator(request_allocator); handleRequest(http_stream, connection, context, settings, keep_alive, request_allocator); } (); @@ -852,7 +848,7 @@ enum SessionOption { */ final class HTTPServerRequest : HTTPRequest { import std.variant : Variant; - import vibe.utils.dictionarylist : DictionaryList; + import vibe.container.dictionarylist : DictionaryList; private { SysTime m_timeCreated; @@ -1183,11 +1179,13 @@ final class HTTPServerRequest : HTTPRequest { Represents a HTTP response as sent from the server side. */ final class HTTPServerResponse : HTTPResponse { + alias Allocator = typeof(vibeThreadAllocator()); + private { InterfaceProxy!Stream m_conn; InterfaceProxy!ConnectionStream m_rawConnection; InterfaceProxy!OutputStream m_bodyWriter; - IAllocator m_requestAlloc; + Allocator m_requestAlloc; FreeListRef!ChunkedOutputStream m_chunkedBodyWriter; FreeListRef!CountingOutputStream m_countingWriter; FreeListRef!ZlibOutputStream m_zlibOutputStream; @@ -1201,13 +1199,13 @@ final class HTTPServerResponse : HTTPResponse { } static if (!is(Stream == InterfaceProxy!Stream)) { - this(Stream conn, ConnectionStream raw_connection, HTTPServerSettings settings, IAllocator req_alloc) + this(Stream conn, ConnectionStream raw_connection, HTTPServerSettings settings, Allocator req_alloc) @safe scope { this(InterfaceProxy!Stream(conn), InterfaceProxy!ConnectionStream(raw_connection), settings, req_alloc); } } - this(InterfaceProxy!Stream conn, InterfaceProxy!ConnectionStream raw_connection, HTTPServerSettings settings, IAllocator req_alloc) + this(InterfaceProxy!Stream conn, InterfaceProxy!ConnectionStream raw_connection, HTTPServerSettings settings, Allocator req_alloc) @safe scope { m_conn = conn; m_rawConnection = raw_connection; @@ -1762,7 +1760,7 @@ scope: logTrace("---------------------"); // write cookies - foreach (n, cookie; this.cookies.byKeyValue) { + foreach (n, cookie; () @trusted { return this.cookies.byKeyValue; } ()) { dst.put("Set-Cookie: "); cookie.writeString(() @trusted { return &dst; } (), n); dst.put("\r\n"); @@ -2105,7 +2103,7 @@ private HTTPListener listenHTTPPlain(HTTPServerSettings settings, HTTPServerRequ private alias TLSStreamType = ReturnType!(createTLSStreamFL!(InterfaceProxy!Stream)); -private bool handleRequest(InterfaceProxy!Stream http_stream, TCPConnection tcp_connection, HTTPServerContext listen_info, ref HTTPServerSettings settings, ref bool keep_alive, scope IAllocator request_allocator) +private bool handleRequest(Allocator)(InterfaceProxy!Stream http_stream, TCPConnection tcp_connection, HTTPServerContext listen_info, ref HTTPServerSettings settings, ref bool keep_alive, scope Allocator request_allocator) @safe { import std.algorithm.searching : canFind; @@ -2375,7 +2373,7 @@ private bool handleRequest(InterfaceProxy!Stream http_stream, TCPConnection tcp_ } -private void parseRequestHeader(InputStream)(HTTPServerRequest req, InputStream http_stream, IAllocator alloc, ulong max_header_size, size_t max_header_line_size) +private void parseRequestHeader(InputStream, Allocator)(HTTPServerRequest req, InputStream http_stream, Allocator alloc, ulong max_header_size, size_t max_header_line_size) if (isInputStream!InputStream) { auto stream = FreeListRef!LimitedHTTPInputStream(http_stream, max_header_size); diff --git a/source/vibe/http/websockets.d b/source/vibe/http/websockets.d index c8663eb..e9f22e3 100644 --- a/source/vibe/http/websockets.d +++ b/source/vibe/http/websockets.d @@ -37,7 +37,6 @@ import vibe.stream.operations; import vibe.http.server; import vibe.http.client; import vibe.core.connectionpool; -import vibe.utils.array; import core.time; import std.algorithm: equal, splitter; From 0df8c810a657f609902faedbf48a7c9f5e69082a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 17 Feb 2024 14:34:21 +0100 Subject: [PATCH 2/2] Remove dependencies to vibe-d package. --- dub.sdl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dub.sdl b/dub.sdl index 5606514..7d22eb0 100644 --- a/dub.sdl +++ b/dub.sdl @@ -8,9 +8,9 @@ authors "Sönke Ludwig" "Mathias 'Geod24' Lang" "Etienne Cimon" "Martin Nowak" \ "Francesco Galla" "Sebastian Wilzbach" "Mihails 'Dicebot' Strasuns" \ "over 200 contributors" -dependency "vibe-d:crypto" version="~>0.9.7" -dependency "vibe-d:inet" version="~>0.9.7" -dependency "vibe-d:tls" version="~>0.9.7" -dependency "vibe-d:textfilter" version="~>0.9.0" +dependency "vibe-inet:crypto" version="~>1.0" +dependency "vibe-inet" version="~>1.0" +dependency "vibe-stream:tls" version="~>1.0" +dependency "vibe-inet:textfilter" version="~>1.0" dependency "diet-ng" version="~>1.2" targetType "library"