Skip to content

Commit

Permalink
ae.net.http.client: Allow reusing HttpClient objects
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberShadow committed Aug 27, 2015
1 parent b2f7692 commit 9026702
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions net/http/client.d
Expand Up @@ -142,20 +142,20 @@ protected:

void onDisconnect(string reason, DisconnectType type)
{
auto response = currentResponse;
if (type == DisconnectType.error)
currentResponse = null;
else
if (currentResponse)
currentResponse.data = inBuffer;

if (handleResponse)
handleResponse(currentResponse, reason);
response = null;
if (response)
response.data = inBuffer;

currentRequest = null;
currentResponse = null;
inBuffer.destroy();
expect = -1;
conn.handleReadData = null;

if (handleResponse)
handleResponse(response, reason);
}

IConnection adaptConnection(IConnection conn)
Expand Down Expand Up @@ -312,3 +312,32 @@ void httpPost(string url, UrlParameters vars, void delegate(string) resultHandle
},
errorHandler);
}

unittest
{
import ae.net.http.server;
import ae.net.http.responseex;

auto s = new HttpServer;
s.handleRequest = (HttpRequest request, HttpServerConnection conn) {
auto response = new HttpResponseEx;
conn.sendResponse(response.serveText("Hello!"));
};
auto port = s.listen(0, "localhost");

auto c = new HttpClient;
auto r = new HttpRequest("http://localhost:" ~ to!string(port));
int count;
c.handleResponse =
(HttpResponse response, string disconnectReason)
{
assert(cast(string)response.getContent.toHeap == "Hello!");
if (count++ == 3)
s.close();
else
c.request(r);
};
c.request(r);

socketManager.loop();
}

0 comments on commit 9026702

Please sign in to comment.