Skip to content

Commit

Permalink
Merge pull request #27 from vibe-d/prepare_for_vibed_switch
Browse files Browse the repository at this point in the history
Prepare for vibe.d switch
  • Loading branch information
l-kramer authored Feb 17, 2024
2 parents e2cbe46 + 0df8c81 commit a6a55b6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 50 deletions.
8 changes: 4 additions & 4 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
28 changes: 12 additions & 16 deletions source/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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));
}

Expand All @@ -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;


/**************************************************************************************************/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
48 changes: 40 additions & 8 deletions source/vibe/http/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion source/vibe/http/log.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions source/vibe/http/proxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion source/vibe/http/router.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 14 additions & 16 deletions source/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
} ();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion source/vibe/http/websockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a6a55b6

Please sign in to comment.