Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Jun 7, 2024
1 parent de5d09f commit 56bc6fd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
31 changes: 11 additions & 20 deletions source/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ final class HTTPClient {
}
}

private void doProxyRequest(T, U)(ref T res, U requester, ref bool close_conn, ref bool has_body)
private void doProxyRequest(T, U)(ref T res, U requester, ref HTTPClientExchange exchange, ref bool has_body)
@trusted { // scope new
import std.conv : to;
scope request_allocator = createRequestAllocator();
Expand Down Expand Up @@ -547,18 +547,17 @@ final class HTTPClient {
disconnect();
}

bool close_conn;
HTTPClientExchange exchange;
SysTime connected_time;
bool has_body = doRequestWithRetry(requester, false, close_conn, connected_time);
bool has_body = doRequestWithRetry(requester, false, exchange, connected_time);

m_responding = true;
auto exchange = new HTTP1ClientExchange(this, close_conn);
auto res = scoped!HTTPClientResponse(exchange);
res.initialize(has_body, request_allocator, connected_time);

// proxy implementation
if (res.headers.get("Proxy-Authenticate", null) !is null) {
doProxyRequest(res, requester, close_conn, has_body);
doProxyRequest(res, requester, exchange, has_body);
}

Exception user_exception;
Expand Down Expand Up @@ -615,7 +614,7 @@ final class HTTPClient {
return res;
}

private bool doRequestWithRetry(scope void delegate(HTTPClientRequest req) requester, bool confirmed_proxy_auth /* basic only */, out bool close_conn, out SysTime connected_time)
private bool doRequestWithRetry(scope void delegate(HTTPClientRequest req) requester, bool confirmed_proxy_auth /* basic only */, out HTTPClientExchange exchange, out SysTime connected_time)
{
if (m_conn && m_conn.connected && Clock.currTime(UTC()) > m_keepAliveLimit){
logDebug("Disconnected to avoid timeout");
Expand All @@ -630,16 +629,15 @@ final class HTTPClient {
foreach (i; 0 .. is_persistent_request ? 2 : 1) {
connected_time = Clock.currTime(UTC());

close_conn = false;
has_body = doRequest(requester, close_conn, false, connected_time);
has_body = doRequest(requester, exchange, false, connected_time);

logTrace("HTTP client waiting for response");
if (!m_stream.empty) break;
}
return has_body;
}

private bool doRequest(scope void delegate(HTTPClientRequest req) requester, ref bool close_conn, bool confirmed_proxy_auth = false /* basic only */, SysTime connected_time = Clock.currTime(UTC()))
private bool doRequest(scope void delegate(HTTPClientRequest req) requester, ref HTTPClientExchange exchange, bool confirmed_proxy_auth = false /* basic only */, SysTime connected_time = Clock.currTime(UTC()))
{
assert(!m_requesting, "Interleaved HTTP client requests detected!");
assert(!m_responding, "Interleaved HTTP client request/response detected!");
Expand Down Expand Up @@ -739,7 +737,8 @@ final class HTTPClient {
}

return () @trusted { // scoped
auto req = scoped!HTTPClientRequest(m_stream, m_conn);
auto exchange = new HTTP1ClientExchange(this);
auto req = scoped!HTTPClientRequest(exchange);
if (m_useTLS)
req.m_peerCertificate = m_tlsStream.peerCertificate;

Expand Down Expand Up @@ -784,23 +783,15 @@ 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 {
OutputStreamProxy m_bodyWriter;
FreeListRef!ChunkedOutputStream m_chunkedStream;
bool m_headerWritten = false;
FixedAppender!(string, 22) m_contentLengthBuffer;
TCPConnection m_rawConn;
TLSCertificateInformation m_peerCertificate;
}


/// private
this(StreamProxy conn, TCPConnection raw_conn)
this(HTTPClientExchange exchange)
{
super(conn);
m_rawConn = raw_conn;
m_exchange = exchange;
}

@property NetworkAddress localAddress() const { return m_rawConn.localAddress; }
Expand Down
9 changes: 0 additions & 9 deletions source/vibe/http/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ T enforceBadRequest(T)(T condition, lazy string message = null, string file = __
class HTTPRequest {
@safe:

protected {
InterfaceProxy!Stream m_conn;
}

public {
/// The HTTP protocol version used for the request
HTTPVersion httpVersion = HTTPVersion.HTTP_1_1;
Expand All @@ -203,11 +199,6 @@ class HTTPRequest {
InetHeaderMap headers;
}

protected this(InterfaceProxy!Stream conn)
{
m_conn = conn;
}

protected this()
{
}
Expand Down
13 changes: 11 additions & 2 deletions source/vibe/http/internal/http1/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import vibe.container.internal.utilallocator;
import vibe.core.connectionpool;
import vibe.core.log;
import vibe.http.client;
import vibe.internal.array : FixedAppender;
import vibe.internal.freelistref;
import vibe.stream.counting;
import vibe.stream.operations;
import vibe.stream.tls;
import vibe.stream.wrapper : createConnectionProxyStream;
import vibe.stream.zlib;

Expand All @@ -28,13 +30,20 @@ class HTTP1ClientExchange : HTTPClientExchange {
InputStreamProxy m_bodyReader;
bool m_closeConn;
int m_maxRequests;

// request
OutputStreamProxy m_bodyWriter;
FreeListRef!ChunkedOutputStream m_chunkedStream;
bool m_headerWritten = false;
FixedAppender!(string, 22) m_contentLengthBuffer;
TCPConnection m_rawConn;
TLSCertificateInformation m_peerCertificate;
}
@safe:

this(HTTPClient client, bool close_conn)
this(HTTPClient client)
{
m_client = client;
m_closeConn = close_conn;
}

@property int maxRequests() const { return m_maxRequests; }
Expand Down

0 comments on commit 56bc6fd

Please sign in to comment.